def copy_database(host='', no_confirm=False): path = get_remote_dump(host=host) with settings(**get_host_settings(host)): msg = colors.red('Replace local database ' 'with dump?') if no_confirm or confirm(msg): restore_database(path=path, dbsettings=env.database, no_confirm=no_confirm)
def install(host=''): """Instala SMVE en servidor""" h = get_host_settings(host) with settings(**h): create_virtualenv() create_project_paths() update_git() create_app_dirs() install_system_packages() install_pip_requirements() install_supervisor() configure_gunicorn()
def get_remote_dump(host='', backupfile=SMVE_DEFAULT_BACKUP_PATH): h = get_host_settings(host) with settings(**h): print( colors.yellow( "Getting remote database: {database}".format(**env.database))) cmd = 'pg_dump -Fc {database} -f {backupfile}' cmd = cmd.format(backupfile=backupfile, **env.database) sudo('su postgres -c "{}"'.format(cmd)) with hide('warnings'): get(backupfile, backupfile) run('rm -f %s' % backupfile) return backupfile
def get_remote_dump(host='', backupfile=SMVE_DEFAULT_BACKUP_PATH): h = get_host_settings(host) with settings(**h): print( colors.yellow("Getting remote database: {database}".format( **env.database)) ) cmd = 'pg_dump -Fc {database} -f {backupfile}' cmd = cmd.format(backupfile=backupfile, **env.database) sudo('su postgres -c "{}"'.format(cmd)) with hide('warnings'): get(backupfile, backupfile) run('rm -f %s' % backupfile) return backupfile
def install_server(host=''): h = get_host_settings(host) with settings(**h): install_deb_packages() if not command_available('pip'): install_pip() enusre_pip_package('virtualenvwrapper', with_sudo=True) create_project_paths() update_git() create_virtualenv() install_pip_requirements() install_supervisor()
def freeze(host='', commit=False): """Conjela los requerimientos del virtualenv""" with settings(**get_host_settings(host)): print("Dumping", colors.yellow(DEV_REQUIEREMENTS_FILE)) pip_freeze_to_file(DEV_REQUIEREMENTS_FILE) print("Dumping", colors.green(REQUIEREMENTS_FILE)) pip_freeze_to_file(REQUIEREMENTS_FILE, filter_dev_only=True) if commit: local('git add {} {}'.format( DEV_REQUIEREMENTS_FILE, REQUIEREMENTS_FILE)) with settings(warn_only=True): result = local( 'git commit -m "Requirements update"', capture=True) if result.failed: abort("Git up to date.") else: local('git push origin master')
def freeze(host='', commit=False): """Conjela los requerimientos del virtualenv""" with settings(**get_host_settings(host)): print("Dumping", colors.yellow(DEV_REQUIEREMENTS_FILE)) pip_freeze_to_file(DEV_REQUIEREMENTS_FILE) print("Dumping", colors.green(REQUIEREMENTS_FILE)) pip_freeze_to_file(REQUIEREMENTS_FILE, filter_dev_only=True) if commit: local('git add {} {}'.format(DEV_REQUIEREMENTS_FILE, REQUIEREMENTS_FILE)) with settings(warn_only=True): result = local('git commit -m "Requirements update"', capture=True) if result.failed: abort("Git up to date.") else: local('git push origin master')
def get_remote_media(host='', no_confirm=False): """Gets remote media""" media_cmd = 'python manage.py diffsettings | grep MEDIA_ROOT | cut -d = -f 2' with settings(**get_host_settings(host)): if no_confirm or confirm(colors.red('Replace local media with dump?', True)): with cd(os.path.join(env.repo_path, 'src/pysmve/nguru')): with hide(): local_media_root = extract_path_from_stdout( local(media_cmd, True)) local_media_root = os.path.abspath( os.path.join(local_media_root, '..')) with prefix(env.venv_prefix): remote_meida_root = extract_path_from_stdout( run(media_cmd, True)) print(remote_meida_root, '=>', local_media_root) with hide('warnings'): get(remote_meida_root, local_media_root)
def get_remote_media(host='', no_confirm=False): """Gets remote media""" media_cmd = 'python manage.py diffsettings | grep MEDIA_ROOT | cut -d = -f 2' with settings(**get_host_settings(host)): if no_confirm or confirm( colors.red('Replace local media with dump?', True)): with cd(os.path.join(env.repo_path, 'src/pysmve/nguru')): with hide(): local_media_root = extract_path_from_stdout( local(media_cmd, True)) local_media_root = os.path.abspath( os.path.join(local_media_root, '..')) with prefix(env.venv_prefix): remote_meida_root = extract_path_from_stdout( run(media_cmd, True)) print(remote_meida_root, '=>', local_media_root) with hide('warnings'): get(remote_meida_root, local_media_root)
def update(host='', release=''): """Updates deployment to upstream git version or tag. Recommended use for release: $ git tag <version> $ fab update:<server_from_fabsettings_HOST>,<version> """ h = get_host_settings(host) available_tags = get_tags() if release: if release not in available_tags: abort(( "Release {} is not a valid tag (Latest tags were: {})." " Run fab tag first").format( colors.red(release, True), colors.green('; '.join(available_tags[-3:])) ) ) else: if confirm('Create tag from current code?'): release = tag() local('git push origin --tags') local('git push origin master') # update local repo (needed?) local('git fetch') with settings(**h): procs = ('poll_mara', 'send_sms') with hold(procs): update_git(tag=release) install_pip_requirements() syncdb() migrate() update_static_media() update_permissions() restart_webserver(host)
def update(host='', release=''): """Updates deployment to upstream git version or tag. Recommended use for release: $ git tag <version> $ fab update:<server_from_fabsettings_HOST>,<version> """ h = get_host_settings(host) available_tags = get_tags() if release: if release not in available_tags: abort(("Release {} is not a valid tag (Latest tags were: {})." " Run fab tag first").format( colors.red(release, True), colors.green('; '.join(available_tags[-3:])))) else: if confirm('Create tag from current code?'): release = tag() local('git push origin --tags') local('git push origin master') # update local repo (needed?) local('git fetch') with settings(**h): procs = ('poll_mara', 'send_sms') with hold(procs): update_git(tag=release) install_pip_requirements() syncdb() migrate() update_static_media() update_permissions() restart_webserver(host)
def copy_database(host='', no_confirm=False): path = get_remote_dump(host=host) with settings(**get_host_settings(host)): msg = colors.red('Replace local database ''with dump?') if no_confirm or confirm(msg): restore_database(path=path, dbsettings=env.database, no_confirm=no_confirm)
def restart_webserver(host=''): """Restart webserver""" h = get_host_settings(host) with settings(**h): sudo('supervisorctl restart gunicorn_production')
def shell(host=''): """Abre shell remota en host""" from fabric.operations import open_shell h = get_host_settings(host) with settings(**h): open_shell()
def pip_uninstall(host='', package=''): if not package: abort("Please provide a package to uninstall") with settings(**get_host_settings(host)): with prefix(env.venv_prefix): run('pip uninstall -y {}'.format(package))
def install_sentry(host=''): with settings(**get_host_settings(host)): run("ls")
def get_remote_all(host='', no_confirm=False): get_host_settings(host) # Check if no_confirm or confirm(colors.red('Replace local data with remote?')): copy_database(host=host, no_confirm=no_confirm) get_remote_media(host=host, no_confirm=no_confirm)
def pip_list(host=''): with settings(**get_host_settings(host)): with prefix(env.venv_prefix): run('pip freeze')