Ejemplo n.º 1
0
def teardown_tmp_repo():
    """
    Remove the test repo
    """
    log.info(__name__ + ':: Tearing down test repo.')
    chdir(config['deploy.test_repo'])
    rmtree(config['deploy.test_repo'])
Ejemplo n.º 2
0
def teardown_tmp_repo():
    """
    Remove the test repo
    """
    log.info(__name__ + ':: Tearing down test repo.')
    chdir(config['deploy.test_repo'])
    rmtree(config['deploy.test_repo'])
Ejemplo n.º 3
0
def init_tmp_repo():
    """
    Create a test repo, change to directory
    """

    log.info(__name__ + ':: Creating test repo.')

    if exists(config['deploy.test_repo']):
        rmtree(config['deploy.test_repo'])

    mkdir(config['deploy.test_repo'])
    Repo.init(config['deploy.test_repo'])
    chdir(config['deploy.test_repo'])
Ejemplo n.º 4
0
def init_tmp_repo():
    """
    Create a test repo, change to directory
    """

    log.info(__name__ + ':: Creating test repo.')

    if exists(config['deploy.test_repo']):
        rmtree(config['deploy.test_repo'])

    mkdir(config['deploy.test_repo'])
    Repo.init(config['deploy.test_repo'])
    chdir(config['deploy.test_repo'])
Ejemplo n.º 5
0
    def check_lock(self):
        """ Returns boolean flag on lock file existence """

        cmd = "ls {0}/{1}".format(self.deploy_path, self.get_lock_name())

        # log.debug('{0} :: Executing - "{1}"'.format(__name__, cmd))
        log.info('{0} :: Checking for lock file at {1}.'.format(
            __name__, self.target))

        try:
            ret = ssh_command_target(cmd, self.target, self.user,
                                     self.key_path)
        except Exception as e:
            log.error(__name__ + ' :: ' + e.message)
            raise DeployLockerError(message=exit_codes[16], exit_code=16)

        # Pull the lock file handle from
        try:
            file_handle = ret['stdout'][0].split('/')[-1].strip()
        except (IndexError, ValueError):
            log.info('{0} :: No lock file exists.'.format(
                __name__, self.target))
            return False

        if file_handle == self.get_lock_name():
            log.info('{0} :: {1} has lock.'.format(__name__, self.user))
            return True
        else:
            log.info('{0} :: Another user has lock.'.format(__name__))
            return False
Ejemplo n.º 6
0
    def add_lock(self):
        """ Write the lock file """

        cmd = "touch {0}/{1}".format(self.deploy_path, self.get_lock_name())
        try:
            ssh_command_target(cmd, self.target, self.user, self.key_path)
        except Exception as e:
            log.error(__name__ + ' :: Failed to create lock -> ' + e.message)
            raise DeployLockerError(message=exit_codes[16], exit_code=16)

        # Logging
        log.info('{0} :: Created lock file at {1}:{2}/{3}.'.format(
            __name__, self.target, self.deploy_path, self.get_lock_name()))
        deploy_log.log('Created lock.')
Ejemplo n.º 7
0
    def check_lock(self):
        """ Returns boolean flag on lock file existence """

        cmd = "ls {0}/{1}".format(self.deploy_path, self.get_lock_name())

        # log.debug('{0} :: Executing - "{1}"'.format(__name__, cmd))
        log.info('{0} :: Checking for lock file at {1}.'.format(
            __name__, self.target))

        try:
            ret = ssh_command_target(cmd, self.target, self.user,
                                     self.key_path)
        except Exception as e:
            log.error(__name__ + ' :: ' + e.message)
            raise DeployLockerError(message=exit_codes[16], exit_code=16)

        # Pull the lock file handle from
        try:
            file_handle = ret['stdout'][0].split('/')[-1].strip()
        except (IndexError, ValueError):
            log.info('{0} :: No lock file exists.'.format(__name__,
                                                          self.target))
            return False

        if file_handle == self.get_lock_name():
            log.info('{0} :: {1} has lock.'.format(__name__,
                                                   self.user))
            return True
        else:
            log.info('{0} :: Another user has lock.'.format(__name__))
            return False
Ejemplo n.º 8
0
    def remove_lock(self):
        """ Remove the lock file """
        log.info('{0} :: SSH Lock destroy.'.format(__name__))

        cmd = "rm {0}/{1}".format(self.deploy_path, self.get_lock_name())

        try:
            ssh_command_target(cmd, self.target, self.user, self.key_path)
        except Exception as e:
            log.error(__name__ + ' :: Failed ot remove lock -> ' + e.message)
            raise DeployLockerError(message=exit_codes[16], exit_code=16)

        # Logging
        log.info('{0} :: Removed lock file at {1}:{2}/{3}.'.format(
            __name__, self.target, self.deploy_path, self.get_lock_name()))
        deploy_log.log('Removed lock.')
Ejemplo n.º 9
0
    def add_lock(self):
        """ Write the lock file """

        cmd = "touch {0}/{1}".format(self.deploy_path,
                                     self.get_lock_name())
        try:
            ssh_command_target(cmd, self.target, self.user,
                                     self.key_path)
        except Exception as e:
            log.error(__name__ + ' :: Failed to create lock -> ' + e.message)
            raise DeployLockerError(message=exit_codes[16], exit_code=16)

        # Logging
        log.info('{0} :: Created lock file at {1}:{2}/{3}.'.format(
            __name__, self.target, self.deploy_path, self.get_lock_name()))
        deploy_log.log('Created lock.')
Ejemplo n.º 10
0
    def remove_lock(self):
        """ Remove the lock file """
        log.info('{0} :: SSH Lock destroy.'.format(__name__))

        cmd = "rm {0}/{1}".format(self.deploy_path, self.get_lock_name())

        try:
            ssh_command_target(cmd, self.target, self.user, self.key_path)
        except Exception as e:
            log.error(__name__ + ' :: Failed ot remove lock -> ' + e.message)
            raise DeployLockerError(message=exit_codes[16], exit_code=16)

        # Logging
        log.info('{0} :: Removed lock file at {1}:{2}/{3}.'.format(
            __name__, self.target, self.deploy_path, self.get_lock_name()))
        deploy_log.log('Removed lock.')
Ejemplo n.º 11
0
    def sync(self, args):
        """Call custom syncing behaviour.

        1. Call common pre-sync hooks
        2. Call app specific pre-sync hooks
        3. Call app sync
        4. Call app specific post-sync hooks
        5. Call common post-sync hooks

        """

        app_path = '{0}/{1}'.format(args['deploy_apps'], args['env'])

        # 1. CALL deploy/apps/common
        log.info('{0} :: Calling pre-sync common: "{1}" ...'.format(
            __name__, args['deploy_apps_common']))
        _call_hooks(args['deploy_apps_common'], 'pre-sync', args['dryrun'])

        # 2. CALL deploy/apps/$env
        if not args['default']:
            log.info('{0} :: Calling pre-sync app: "{1}" ...'.format(
                __name__, app_path))
            _call_hooks(app_path, 'pre-sync', args['dryrun'])

        # 3. Apply optional release tag here
        if args['release'] and not args['dryrun']:
            _make_release_tag(args['tag'], args['author'])

        # 4. CALL sync, deploy/apps/sync/$env.sync
        log.info('{0} :: Calling pre-sync app: "{1}" ...'.format(
            __name__, args['deploy_sync']))
        if args['default'] or not args['env']:
            _call_hooks(args['deploy_sync'], 'default', args['dryrun'])
        else:
            _call_hooks(args['deploy_sync'], args['env'], args['dryrun'])

        # 4. CALL app post sync, deploy/apps/$env
        if not args['default']:
            log.info('{0} :: Calling post-sync app: "{1}" ...'.format(
                __name__, app_path))
            _call_hooks(app_path, 'post-sync', args['dryrun'])

        # 4. CALL common post sync, deploy/apps/common
        log.info('{0} :: Calling post-sync app: "{1}" ...'.format(
            __name__, args['deploy_apps_common']))
        _call_hooks(args['deploy_apps_common'], 'post-sync', args['dryrun'])
Ejemplo n.º 12
0
    def sync(self, args):
        """Call custom syncing behaviour.

        1. Call common pre-sync hooks
        2. Call app specific pre-sync hooks
        3. Call app sync
        4. Call app specific post-sync hooks
        5. Call common post-sync hooks

        """

        app_path = '{0}/{1}'.format(args['deploy_apps'], args['env'])

        # 1. CALL deploy/apps/common
        log.info('{0} :: Calling pre-sync common: "{1}" ...'.
            format(__name__, args['deploy_apps_common']))
        _call_hooks(args['deploy_apps_common'], 'pre-sync', args['dryrun'])

        # 2. CALL deploy/apps/$env
        if not args['default']:
            log.info('{0} :: Calling pre-sync app: "{1}" ...'.
                format(__name__, app_path))
            _call_hooks(app_path, 'pre-sync', args['dryrun'])

        # 3. Apply optional release tag here
        if args['release'] and not args['dryrun']:
            _make_release_tag(args['tag'], args['author'])

        # 4. CALL sync, deploy/apps/sync/$env.sync
        log.info('{0} :: Calling pre-sync app: "{1}" ...'.
            format(__name__, args['deploy_sync']))
        if args['default'] or not args['env']:
            _call_hooks(args['deploy_sync'], 'default', args['dryrun'])
        else:
            _call_hooks(args['deploy_sync'], args['env'], args['dryrun'])

        # 4. CALL app post sync, deploy/apps/$env
        if not args['default']:
            log.info('{0} :: Calling post-sync app: "{1}" ...'.
                format(__name__, app_path))
            _call_hooks(app_path, 'post-sync', args['dryrun'])

        # 4. CALL common post sync, deploy/apps/common
        log.info('{0} :: Calling post-sync app: "{1}" ...'.
            format(__name__, args['deploy_apps_common']))
        _call_hooks(args['deploy_apps_common'], 'post-sync', args['dryrun'])
Ejemplo n.º 13
0
def _call_hooks(path, phase, dryrun=False):
    """Performs calls on path/phase dependent hooks

    :param path: hooks path
    :param phase: deploy phase

    """
    if os.path.exists(path):
        sorted_path = sorted(os.listdir(path))
        for item in sorted_path:
            item_phase = item.split('.')[0]

            # CALL hook and log
            if phase == item_phase:
                cmd = path + '/' + item
                log_msg = 'CALLING \'{0}\' ON PHASE \'{1}\''.format(
                  cmd, phase
                )
                log.info(__name__ + ' :: {0}'.format(log_msg))
                if dryrun:
                    print ''
                    with open(cmd) as f:
                        for line in f.readlines():
                            if not line[-1] == '\n':
                                print '\t' + line[:-1]
                            else:
                                print '\t' + line[:-1]
                    print ''

                else:
                    proc = subprocess.Popen(path + '/' + item,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE)
                    log.info(cmd + ' OUT -> ' + '; '.join(
                        filter(lambda x: x, proc.communicate())))

                    # Flag a failed hook
                    if proc.returncode:
                        raise DeployDriverError(exit_code=17,
                                                message=exit_codes[17])

    else:
        log.error(__name__ + ' :: CANNOT FIND HOOK PATH \'{0}\''.format(
            path))
        raise DeployDriverError(exit_code=17, message=exit_codes[17])
Ejemplo n.º 14
0
def _call_hooks(path, phase, dryrun=False):
    """Performs calls on path/phase dependent hooks

    :param path: hooks path
    :param phase: deploy phase

    """
    if os.path.exists(path):
        sorted_path = sorted(os.listdir(path))
        for item in sorted_path:
            item_phase = item.split('.')[0]

            # CALL hook and log
            if phase == item_phase:
                cmd = path + '/' + item
                log_msg = 'CALLING \'{0}\' ON PHASE \'{1}\''.format(cmd, phase)
                log.info(__name__ + ' :: {0}'.format(log_msg))
                if dryrun:
                    print ''
                    with open(cmd) as f:
                        for line in f.readlines():
                            if not line[-1] == '\n':
                                print '\t' + line[:-1]
                            else:
                                print '\t' + line[:-1]
                    print ''

                else:
                    proc = subprocess.Popen(path + '/' + item,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE)
                    log.info(
                        cmd + ' OUT -> ' +
                        '; '.join(filter(lambda x: x, proc.communicate())))

                    # Flag a failed hook
                    if proc.returncode:
                        raise DeployDriverError(exit_code=17,
                                                message=exit_codes[17])

    else:
        log.error(__name__ + ' :: CANNOT FIND HOOK PATH \'{0}\''.format(path))
        raise DeployDriverError(exit_code=17, message=exit_codes[17])
Ejemplo n.º 15
0
    def sync(self, args):

        log.info('{0} :: DRYRUN SYNC'.format(__name__))

        log.info('--> TAG \'{0}\''.format(args['tag']))
        log.info('--> AUTHOR \'{0}\''.format(args['author']))
        log.info('--> REMOTE \'{0}\''.format(args['remote']))
        log.info('--> BRANCH \'{0}\''.format(args['branch']))

        log.info('DUMPING DEPLOY SCRIPTS IN ORDER OF EXECUTION.')
        DeployDriverDefault().sync(args)

        log.info('{0} :: DRYRUN SYNC COMPLETE'.format(__name__))
Ejemplo n.º 16
0
    def sync(self, args):

        log.info('{0} :: DRYRUN SYNC'.format(__name__))

        log.info('--> TAG \'{0}\''.format(args['tag']))
        log.info('--> AUTHOR \'{0}\''.format(args['author']))
        log.info('--> REMOTE \'{0}\''.format(args['remote']))
        log.info('--> BRANCH \'{0}\''.format(args['branch']))

        log.info('DUMPING DEPLOY SCRIPTS IN ORDER OF EXECUTION.')
        DeployDriverDefault().sync(args)

        log.info('{0} :: DRYRUN SYNC COMPLETE'.format(__name__))