def discover_latest_release(): """ Connects to remote server and discovers next release number """ releases_path = fetch('releases_path') echo_subtask("Discovering latest release number") # Get latest release number with settings(hide('warnings'), warn_only=True): if run('test -d %s' % releases_path).failed: last_release_number = 0 create_entity(releases_path, entity_type='directory', protected=False) else: releases_respond = run('ls -1p %s | sed "s/\///g"' % releases_path) releases_dirs = list( filter(lambda x: len(x) != 0, releases_respond.split('\r\n'))) if len(releases_dirs) > 0: last_release_number = max(map(lambda x: int(x), releases_dirs)) or 0 else: last_release_number = 0 release_number = last_release_number + 1 # Set release info to config set('release_number', release_number) set('release_to', '/'.join([releases_path, str(release_number)]))
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_files(files): global shared global build_to for sfile in files: relative_path = '/'.join(['.', sfile]) directory, filename_ = os.path.split(relative_path) shared_path = '/'.join([shared, sfile]) with cd(build_to): create_entity(directory, entity_type='directory', protected=False) # create parent directory run('ln -sf %s %s' % (shared_path, relative_path)) # link shared to current folder
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 link_dirs(dirs): global shared global build_to for sdir in dirs: relative_path = '/'.join(['.', sdir]) directory, filename_ = os.path.split(relative_path) shared_path = '/'.join([shared, sdir]) with cd(build_to): create_entity(directory, entity_type='directory', protected=False) # create parent directory run('rm -rf %s' % relative_path ) # remove directory if it conficts with shared run('ln -s %s %s' % (shared_path, relative_path)) # link shared to current folder
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))