summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/gitautodeploy.py
blob: 6b238c5916e6aadefc33f5abb7299a9adc8c097e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
class LogInterface(object):
    """Interface that functions as a stdout and stderr handler and directs the
    output to the logging module, which in turn will output to either console,
    file or both."""

    def __init__(self, level=None):
        import logging
        self.level = (level if level else logging.getLogger().info)

    def write(self, msg):
        for line in msg.strip().split("\n"):
            self.level(line)


class GitAutoDeploy(object):
    _instance = None
    _server = None
    _config = None

    def __new__(cls, *args, **kwargs):
        """Overload constructor to enable Singleton access"""
        if not cls._instance:
            cls._instance = super(GitAutoDeploy, cls).__new__(
                cls, *args, **kwargs)
        return cls._instance

    @staticmethod
    def debug_diagnosis(port):
        import logging
        logger = logging.getLogger()

        pid = GitAutoDeploy.get_pid_on_port(port)
        if pid is False:
            logger.warning('I don\'t know the number of pid that is using my configured port')
            return

        logger.info('Process with pid number %s is using port %s' % (pid, port))
        with open("/proc/%s/cmdline" % pid) as f:
            cmdline = f.readlines()
            logger.info('cmdline ->', cmdline[0].replace('\x00', ' '))

    @staticmethod
    def get_pid_on_port(port):
        import os

        with open("/proc/net/tcp", 'r') as f:
            file_content = f.readlines()[1:]

        pids = [int(x) for x in os.listdir('/proc') if x.isdigit()]
        conf_port = str(port)
        mpid = False

        for line in file_content:
            if mpid is not False:
                break

            _, laddr, _, _, _, _, _, _, _, inode = line.split()[:10]
            decport = str(int(laddr.split(':')[1], 16))

            if decport != conf_port:
                continue

            for pid in pids:
                try:
                    path = "/proc/%s/fd" % pid
                    if os.access(path, os.R_OK) is False:
                        continue

                    for fd in os.listdir(path):
                        cinode = os.readlink("/proc/%s/fd/%s" % (pid, fd))
                        minode = cinode.split(":")

                        if len(minode) == 2 and minode[1][1:-1] == inode:
                            mpid = pid

                except Exception as e:
                    pass

        return mpid

    def find_config_file_path(self):
        """Attempt to find a config file in cwd and script path."""

        import os
        import re
        import logging
        logger = logging.getLogger()

        # Look for a custom config file if no path is provided as argument
        target_directories = [
            os.path.dirname(os.path.realpath(__file__)),  # Script path
        ]

        # Add current CWD if not identical to script path
        if not os.getcwd() in target_directories:
            target_directories.append(os.getcwd())
        
        target_directories.reverse()

        # Look for a *conf.json or *config.json
        for dir in target_directories:
            if not os.access(dir, os.R_OK):
                continue
            for item in os.listdir(dir):
                if re.match(r".*conf(ig)?\.json$", item):
                    path = os.path.realpath(os.path.join(dir, item))
                    logger.info("Using '%s' as config" % path)
                    return path

        return
    
    def read_json_file(self, file_path):
        import json
        import logging
        import re
        logger = logging.getLogger()
        
        try:
            json_string = open(file_path).read()

        except Exception as e:
            logger.critical("Could not load %s file\n" % file_path)
            raise e

        try:
            # Remove commens from JSON (makes sample config options easier)
            regex = r'\s*(#|\/{2}).*$'
            regex_inline = r'(:?(?:\s)*([A-Za-z\d\.{}]*)|((?<=\").*\"),?)(?:\s)*(((#|(\/{2})).*)|)$'
            lines = json_string.split('\n')

            for index, line in enumerate(lines):
                if re.search(regex, line):
                    if re.search(r'^' + regex, line, re.IGNORECASE):
                        lines[index] = ""
                    elif re.search(regex_inline, line):
                        lines[index] = re.sub(regex_inline, r'\1', line)
                        
            data = json.loads('\n'.join(lines))

        except Exception as e:
            logger.critical("%s file is not valid JSON\n" % file_path)
            raise e

        return data

    def read_repo_config_from_environment(self, config_data):
        """Look for repository config in any defined environment variables. If
        found, import to main config."""
        import logging
        import os

        if 'GAD_REPO_URL' not in os.environ:
            return config_data

        logger = logging.getLogger()

        repo_config = {
            'url': os.environ['GAD_REPO_URL']
        }
        
        logger.info("Added configuration for '%s' found in environment variables" % os.environ['GAD_REPO_URL'])

        if 'GAD_REPO_BRANCH' in os.environ:
            repo_config['branch'] = os.environ['GAD_REPO_BRANCH']

        if 'GAD_REPO_REMOTE' in os.environ:
            repo_config['remote'] = os.environ['GAD_REPO_REMOTE']

        if 'GAD_REPO_PATH' in os.environ:
            repo_config['path'] = os.environ['GAD_REPO_PATH']

        if 'GAD_REPO_DEPLOY' in os.environ:
            repo_config['deploy'] = os.environ['GAD_REPO_DEPLOY']

        if not 'repositories' in config_data:
            config_data['repositories'] = []

        config_data['repositories'].append(repo_config)

        return config_data

    def init_config(self, config_data):
        import os
        import re
        import logging
        logger = logging.getLogger()
        
        self._config = config_data

        # Translate any ~ in the path into /home/<user>
        if 'pidfilepath' in self._config:
            self._config['pidfilepath'] = os.path.expanduser(self._config['pidfilepath'])

        if 'repositories' not in self._config:
            self._config['repositories'] = []

        for repo_config in self._config['repositories']:

            # Setup branch if missing
            if 'branch' not in repo_config:
                repo_config['branch'] = "master"

            # Setup remote if missing
            if 'remote' not in repo_config:
                repo_config['remote'] = "origin"

            # Setup deploy commands list if not present
            if 'deploy_commands' not in repo_config:
                repo_config['deploy_commands'] = []

            # Check if any global pre deploy commands is specified
            if 'global_deploy' in self._config and len(self._config['global_deploy'][0]) is not 0:
                repo_config['deploy_commands'].insert(0, self._config['global_deploy'][0])

            # Check if any repo specific deploy command is specified
            if 'deploy' in repo_config:
                repo_config['deploy_commands'].append(repo_config['deploy'])

            # Check if any global post deploy command is specified
            if 'global_deploy' in self._config and len(self._config['global_deploy'][1]) is not 0:
                repo_config['deploy_commands'].append(self._config['global_deploy'][1])

            # If a Bitbucket repository is configured using the https:// URL, a username is usually
            # specified in the beginning of the URL. To be able to compare configured Bitbucket
            # repositories with incoming web hook events, this username needs to be stripped away in a
            # copy of the URL.
            if 'url' in repo_config and 'bitbucket_username' not in repo_config:
                regexp = re.search(r"^(https?://)([^@]+)@(bitbucket\.org/)(.+)$", repo_config['url'])
                if regexp:
                    repo_config['url_without_usernme'] = regexp.group(1) + regexp.group(3) + regexp.group(4)

            # Translate any ~ in the path into /home/<user>
            if 'path' in repo_config:
                repo_config['path'] = os.path.expanduser(repo_config['path'])

        return self._config

    def clone_all_repos(self):
        """Iterates over all configured repositories and clones them to their
        configured paths."""
        import os
        import re
        import logging
        from wrappers import GitWrapper
        logger = logging.getLogger()

        # Iterate over all configured repositories
        for repo_config in self._config['repositories']:
            
            # Only clone repositories with a configured path
            if 'path' not in repo_config:
                logger.info("Repository %s will not be cloned (no path configured)" % repo_config['url'])
                continue
            
            if os.path.isdir(repo_config['path']) and os.path.isdir(repo_config['path']+'/.git'):
                logger.info("Repository %s already present" % repo_config['url'])
                continue

            # Clone repository
            GitWrapper.clone(url=repo_config['url'], branch=repo_config['branch'], path=repo_config['path'])
            
            if os.path.isdir(repo_config['path']):
                logger.info("Repository %s successfully cloned" % repo_config['url'])
            else:
                logger.error("Unable to clone %s branch of repository %s" % (repo_config['branch'], repo_config['url']))

    def ssh_key_scan(self):
        import re
        import logging
        from wrappers import ProcessWrapper
        logger = logging.getLogger()

        for repository in self._config['repositories']:

            url = repository['url']
            logger.info("Scanning repository: %s" % url)
            m = re.match('.*@(.*?):', url)

            if m is not None:
                port = repository['port']
                port = '' if port is None else ('-p' + port)
                ProcessWrapper().call(['ssh-keyscan -t ecdsa,rsa ' +
                                       port + ' ' +
                                       m.group(1) +
                                       ' >> ' +
                                       '$HOME/.ssh/known_hosts'], shell=True)

            else:
                logger.error('Could not find regexp match in path: %s' % url)

    def kill_conflicting_processes(self):
        import os
        import logging
        logger = logging.getLogger()

        pid = GitAutoDeploy.get_pid_on_port(self._config['port'])

        if pid is False:
            logger.error('[KILLER MODE] I don\'t know the number of pid ' +
                         'that is using my configured port\n[KILLER MODE] ' +
                         'Maybe no one? Please, use --force option carefully')
            return False

        os.kill(pid, signal.SIGKILL)
        return True

    def create_pid_file(self):
        import os

        with open(self._config['pidfilepath'], 'w') as f:
            f.write(str(os.getpid()))

    def read_pid_file(self):
        with open(self._config['pidfilepath'], 'r') as f:
            return f.readlines()

    def remove_pid_file(self):
        import os
        os.remove(self._config['pidfilepath'])

    def exit(self):
        import sys
        import logging
        logger = logging.getLogger()
        logger.info('Goodbye')
        self.remove_pid_file()
        sys.exit(0)

    @staticmethod
    def create_daemon():
        import os

        try:
            # Spawn first child. Returns 0 in the child and pid in the parent.
            pid = os.fork()
        except OSError, e:
            raise Exception("%s [%d]" % (e.strerror, e.errno))

        # First child
        if pid == 0:
            os.setsid()

            try:
                # Spawn second child
                pid = os.fork()
                
            except OSError, e:
                raise Exception("%s [%d]" % (e.strerror, e.errno))

            if pid == 0:
                os.chdir('/')
                os.umask(0)
            else:
                # Kill first child
                os._exit(0)
        else:
            # Kill parent of first child
            os._exit(0)

        return 0

    def run(self):
        import sys
        from BaseHTTPServer import HTTPServer
        import socket
        import os
        import logging
        import argparse
        from lock import Lock
        from httpserver import WebhookRequestHandler

        # Attempt to retrieve default config values from environment variables
        default_quiet_value = 'GAD_QUIET' in os.environ or False
        default_daemon_mode_value = 'GAD_DAEMON_MODE' in os.environ or False
        default_config_value = 'GAD_CONFIG' in os.environ and os.environ['GAD_CONFIG'] or None
        default_ssh_keygen_value = 'GAD_SSH_KEYGEN' in os.environ or False
        default_force_value = 'GAD_FORCE' in os.environ or False
        default_use_ssl = 'GAD_SSL' in os.environ or False
        default_ssl_pem_file_path = 'GAD_SSL_PEM_FILE' in os.environ and os.environ['GAD_SSL_PEM_FILE'] or '~/.gitautodeploy.pem'
        default_pid_file_value = 'GAD_PID_FILE' in os.environ and os.environ['GAD_PID_FILE'] or '~/.gitautodeploy.pid'
        default_log_file_value = 'GAD_LOG_FILE' in os.environ and os.environ['GAD_LOG_FILE'] or None
        default_host_value = 'GAD_HOST' in os.environ and os.environ['GAD_HOST'] or '0.0.0.0'
        default_port_value = 'GAD_PORT' in os.environ and int(os.environ['GAD_PORT']) or 8001

        parser = argparse.ArgumentParser()

        parser.add_argument("-d", "--daemon-mode",
                            help="run in background (daemon mode)",
                            default=default_daemon_mode_value,
                            action="store_true")

        parser.add_argument("-q", "--quiet",
                            help="supress console output",
                            default=default_quiet_value,
                            action="store_true")

        parser.add_argument("-c", "--config",
                            help="custom configuration file",
                            default=default_config_value,
                            type=str)

        parser.add_argument("--ssh-keygen",
                            help="scan repository hosts for ssh keys",
                            default=default_ssh_keygen_value,
                            action="store_true")

        parser.add_argument("--force",
                            help="kill any process using the configured port",
                            default=default_force_value,
                            action="store_true")

        parser.add_argument("--pid-file",
                            help="specify a custom pid file",
                            default=default_pid_file_value,
                            type=str)

        parser.add_argument("--log-file",
                            help="specify a log file",
                            default=default_log_file_value,
                            type=str)

        parser.add_argument("--host",
                            help="address to bind to",
                            default=default_host_value,
                            type=str)

        parser.add_argument("--port",
                            help="port to bind to",
                            default=default_port_value,
                            type=int)

        parser.add_argument("--ssl",
                            help="use ssl",
                            default=default_use_ssl,
                            action="store_true")

        parser.add_argument("--ssl-pem",
                            help="path to ssl pem file",
                            default=default_ssl_pem_file_path,
                            type=str)

        args = parser.parse_args()

        # Set up logging
        logger = logging.getLogger()
        logFormatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s]  %(message)s")

        # Enable console output?
        if args.quiet:
            logger.addHandler(logging.NullHandler())
        else:
            consoleHandler = logging.StreamHandler()
            consoleHandler.setFormatter(logFormatter)
            logger.addHandler(consoleHandler)

        # All logs are recording
        logger.setLevel(logging.NOTSET)

        # Look for log file path provided in argument
        config_file_path = None
        if args.config:
            config_file_path = os.path.realpath(args.config)
            logger.info('Using custom configuration file \'%s\'' % config_file_path)

        # Try to find a config file on the file system
        if not config_file_path:
            config_file_path = self.find_config_file_path()

        # Read config data from json file
        if config_file_path:
            config_data = self.read_json_file(config_file_path)
        else:
            logger.info('No configuration file found or specified. Using default values.')
            config_data = {}

        # Configuration options coming from environment or command line will
        # override those coming from config file
        if args.pid_file:
            config_data['pidfilepath'] = args.pid_file

        if args.log_file:
            config_data['logfilepath'] = args.log_file

        if args.host:
            config_data['host'] = args.host

        if args.port:
            config_data['port'] = args.port

        # Extend config data with any repository defined by environment variables
        config_data = self.read_repo_config_from_environment(config_data)

        # Initialize config using config file data
        self.init_config(config_data)

        # Translate any ~ in the path into /home/<user>
        if 'logfilepath' in self._config:
            log_file_path = os.path.expanduser(self._config['logfilepath'])
            fileHandler = logging.FileHandler(log_file_path)
            fileHandler.setFormatter(logFormatter)
            logger.addHandler(fileHandler)

        if args.ssh_keygen:
            logger.info('Scanning repository hosts for ssh keys...')
            self.ssh_key_scan()

        if args.force:
            logger.info('Attempting to kill any other process currently occupying port %s' % self._config['port'])
            self.kill_conflicting_processes()

        # Clone all repos once initially
        self.clone_all_repos()

        # Set default stdout and stderr to our logging interface (that writes
        # to file and console depending on user preference)
        sys.stdout = LogInterface(logger.info)
        sys.stderr = LogInterface(logger.error)

        if args.daemon_mode:
            logger.info('Starting Git Auto Deploy in daemon mode')
            GitAutoDeploy.create_daemon()
        else:
            logger.info('Git Auto Deploy started')

        self.create_pid_file()

        # Clear any existing lock files, with no regard to possible ongoing processes
        for repo_config in self._config['repositories']:

            # Do we have a physical repository?
            if 'path' in repo_config:
                Lock(os.path.join(repo_config['path'], 'status_running')).clear()
                Lock(os.path.join(repo_config['path'], 'status_waiting')).clear()

        try:
            WebhookRequestHandler._config = self._config
            self._server = HTTPServer((self._config['host'],
                                       self._config['port']),
                                      WebhookRequestHandler)
            if args.ssl:
                import ssl
                logger.info("enabling ssl")
                self._server.socket = ssl.wrap_socket(self._server.socket,
                                                      certfile=os.path.expanduser(args.ssl_pem),
                                                      server_side=True)
            sa = self._server.socket.getsockname()
            logger.info("Listening on %s port %s", sa[0], sa[1])
            self._server.serve_forever()

        except socket.error, e:

            if not args.daemon_mode:
                logger.critical("Error on socket: %s" % e)
                GitAutoDeploy.debug_diagnosis(self._config['port'])

            sys.exit(1)

    def stop(self):
        if self._server is not None:
            self._server.socket.close()

    def signal_handler(self, signum, frame):
        import logging
        logger = logging.getLogger()
        self.stop()

        if signum == 1:
            self.run()
            return

        elif signum == 2:
            logger.info('Requested close by keyboard interrupt signal')

        elif signum == 6:
            logger.info('Requested close by SIGABRT (process abort signal). Code 6.')

        self.exit()


def main():
    import signal
    from gitautodeploy import GitAutoDeploy

    app = GitAutoDeploy()

    signal.signal(signal.SIGHUP, app.signal_handler)
    signal.signal(signal.SIGINT, app.signal_handler)
    signal.signal(signal.SIGABRT, app.signal_handler)
    signal.signal(signal.SIGPIPE, signal.SIG_IGN)

    app.run()