def rollback_release(): """ Rollbacks latest release """ ensure('current_path') releases_path = fetch('releases_path') with (settings(show('debug'))): with cd(releases_path): echo_subtask('Finding previous release for rollback') rollback_release_number = run( 'ls -1A | sort -n | tail -n 2 | head -n 1') if int(rollback_release_number) > 0: echo_subtask('Linking previous release to current') run('ln -nfs %s/%s %s' % (releases_path, rollback_release_number, fetch('current_path'))) echo_subtask('Finding latest release') current_release = run('ls -1A | sort -n | tail -n 1') if int(current_release) > 0: echo_subtask('Removing latest release') run('rm -rf %s/%s' % (releases_path, current_release)) else: abort('Can\'t find latest release for remove.') else: abort('Can\'t find previous release for rollback.')
def move_build_to_releases(): """ Moves current build folder to releases path """ ensure('build_to') ensure('release_to') echo_subtask('Moving from build path to release path') run('mv %s %s' % (fetch('build_to'), fetch('release_to')))
def lock(): """ Locks deploy Parallel deploys are not allowed """ ensure('build_to') echo_subtask("Creating `deploy.lock` file") run('touch %s' % os.path.join(fetch('deploy_to'), 'deploy.lock'))
def add_host_to_known_hosts(): """ Adds current host to the known hosts `fabric3` library sets to `env.host_string` current host where task is executed """ ensure('hosts') echo_subtask('Adding current host to known hosts') add_to_known_hosts(env.host_string, env.port)
def lock(): """ Locks deploy Parallel deploys are not allowed """ ensure('build_to') echo_subtask("Creating `deploy.lock` file") create_entity('/'.join([fetch('deploy_to'), 'deploy.lock']), entity_type='file', protected=False)
def link_release_to_current(): """ Creates current release symlink in `deploy_to` path """ ensure('current_path') release_to = fetch('release_to') echo_subtask("Linking release to current") with cd(release_to): run('ln -nfs %s %s' % (release_to, fetch('current_path')))
def use_git_branch(): """ Clones repository to build dir """ ensure('scm') ensure('branch') build_to = fetch('build_to') echo_subtask("Copying code from repository to build folder") run('git clone {0} {1} --recursive --branch {2}'.format( fetch('scm'), build_to, fetch('branch'))) run('rm -rf %s' % os.path.join(build_to, '.git'))
def use_git_branch(): """ Clones repository to build dir """ ensure('scm') ensure('branch') build_to = fetch('build_to') echo_subtask("Copying code from repository to build folder") run('git clone {0} {1} --recursive --branch {2}'.format( fetch('scm'), build_to, fetch('branch'))) with settings(warn_only=True): run('rm -rf %s' % '/'.join([build_to, '.git']))
def create_build_path(): """ Creates tmp dir for building app. Folder will be: * deleted -> if build fails * moved to releases -> if build succeeds """ ensure('build_to') echo_subtask("Creating build path") with settings(hide('warnings'), warn_only=True): build_path = fetch('build_to') if run('test -d %s' % build_path).failed: create_entity(build_path, entity_type='directory', protected=False)
def cleanup_releases(): """ Cleans up old releases """ ensure('releases_path') releases_count = str(fetch('keep_releases')) echo_subtask("Cleaning up old realeses. Keeping latest %s" % releases_count) with cd(fetch('releases_path')), show('debug'): cmd = ''' count=$(ls -A1 | sort -rn | wc -l) remove=$((count > %s ? count - %s : 0)) ls -A1 | sort -rn | tail -n $remove | xargs rm -rf {}''' run(cmd % (releases_count, releases_count))
def maybe_clone_git_repository(): """ Clones bare git repository on first deploy """ ensure('repository') scm_path = fetch('scm') with settings(hide('warnings'), warn_only=True): echo_subtask('Ensuring git repository presence') if run('test -d %s' % scm_path).failed: create_entity(scm_path, entity_type='directory', protected=False) if run('test -f %s' % '/'.join([scm_path, 'HEAD'])).failed: echo_subtask("Cloning bare git repository") with settings(hide('output')): run('git clone {0} {1} --bare'.format(fetch('repository'), scm_path))
def cleanup_releases(): """ Cleans up old releases """ ensure('releases_path') ensure('keep_releases') releases_count = str(fetch('keep_releases')) if (releases_count == "-1"): return echo_subtask("Cleaning up old realeses. Keeping latest %s" % releases_count) with cd(fetch('releases_path')), show('debug'): use_sudo = 'sudo' if fetch('sudo_on_cleanup_releases') else '' cmd = ''' count=$(ls -A1 | sort -rn | wc -l) remove=$((count > %s ? count - %s : 0)) ls -A1 | sort -rn | tail -n $remove | xargs %s rm -rf {}''' run(cmd % (releases_count, releases_count, use_sudo))
def fetch_new_commits(): """ Fetches new git commits """ ensure('scm') ensure('repository') ensure('branch') echo_subtask("Fetching new commits") with cd(fetch('scm')): run('git fetch {0} "{1}:{1}" --force'.format(fetch('repository'), fetch('branch')))