def restore(db_owner=None, database=None, dump_path=None): if not db_owner: db_owner = raw_input('Username Owner Database: \n>') env.db_main_user = db_owner if not database: database = raw_input('Database: \n>') env.db_name = database if not dump_path: dump_path = raw_input('Dump path: \n>') env.path = dump_path db_info = dj_database_url.parse(env.database_url) db_info['NAME'] = database restore_command = ['pg_restore'] restore_command.append('-U %(db_main_user)s' % env) if db_info.get('NAME', ''): restore_command.append('-d %(NAME)s' % db_info) restore_command.append('-h %(HOST)s' % db_info) restore_command.append('-p %(PORT)s' % db_info) if hasattr(env, 'extra_restore'): restore_command.append(env.extra_restore) restore_command.append(' < ' + env.path) command = ' '.join(restore_command) msg = command msg = msg + '\nConfirm restore %s: \n y - continue; \n n - abort \n>' % database if not confirm_operation(msg, expected='y'): return run_local(command)
def delete_pyc(): print magenta("Delete *.pyc files") command = 'find . -name \*.pyc -print0 | xargs -0 rm' if env.is_local: run_local(command) else: with cd(env.project_root): run(command)
def compilemessages(): """compiles all translations""" print magenta("Compile messages") if env.is_local: run_local('./manage.py compilemessages') else: with cd(env.project_root), prefix('source env/bin/activate'): run('./manage.py compilemessages')
def create_database(): database_name = env.django_settings.UNIQUE_PREFIX print magenta("Create database") if env.is_local: run_local('mysql --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE DATABASE %s;"' % database_name) else: database = env.django_settings.UNIQUE_PREFIX run('mysql --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE DATABASE db_%s;"' % database)
def update_requirements(): """update external dependencies on remote host """ require('root', provided_by=('local', ) + env.deployments) print magenta("Update requirements") if env.is_local: run_local('pip install --requirement REQUIREMENTS_LOCAL') else: _update_requirements_remote()
def update_requirements(): """update external dependencies on remote host """ require('root', provided_by=('local',) + env.deployments) print magenta("Update requirements") if env.is_local: run_local('pip install --requirement REQUIREMENTS_LOCAL') else: _update_requirements_remote()
def dump_media(): """ Reset local media from remote host """ require('project_root', provided_by=env.deployments) if not console.confirm(red('Are you sure you want to replace the local media with the %s media data?' % env.environment, bold=True), default=False): utils.abort('Reset local media aborted.') remote_media = os.path.join(env.project_root, 'media',) local_media = os.path.join(os.getcwd(), 'media') run_local('rsync --delete --exclude=".gitignore" -rvaz %s@%s:%s/ %s' % (env.user, env.hosts[0], remote_media, local_media))
def update_js_requirements(): """ update external javascript dependencies on remote host """ require('root', provided_by=('local', ) + env.deployments) print magenta("Install javascript requirements") if env.is_local: run_local('npm install') else: with cd(env.project_root): run('npm install')
def dump_database(): """ resets the local database to the database on the server """ local_settings = import_module('%s.settings_development' % env.project_python) require('project_root', provided_by=env.deployments) if not console.confirm(red('Are you sure you want to replace the local database with the %s database data?' % env.environment, bold=True), default=False): utils.abort('Reset local database aborted.') run_local('mysql --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "DROP DATABASE %s; CREATE DATABASE %s;"' % (local_settings.UNIQUE_PREFIX, local_settings.UNIQUE_PREFIX)) run_local('ssh %s@%s "source ~/.profile; mysqldump -u \$MYSQL_USER -p\$MYSQL_PASSWORD db_%s | gzip" | gunzip | mysql -u $MYSQL_USER -p$MYSQL_PASSWORD %s' % (env.django_settings.DEPLOYMENT['user'], env.django_settings.DEPLOYMENT['hosts'][0], env.django_settings.UNIQUE_PREFIX, local_settings.UNIQUE_PREFIX))
def update_js_requirements(): """ update external javascript dependencies on remote host """ require('root', provided_by=('local',) + env.deployments) print magenta("Install javascript requirements") if env.is_local: run_local('npm install') else: with cd(env.project_root): run('npm install')
def update_requirements(): """ update external dependencies on remote host """ require('project_root', provided_by=('local',) + env.deployments) print magenta("Update requirements") if env.is_local: run_local('pip install --requirement REQUIREMENTS_LOCAL') else: with cd(env.project_root): with prefix('source env/bin/activate'): run('pip install --requirement REQUIREMENTS_SERVER')
def update_requirements(): """ update external dependencies on remote host """ require('project_root', provided_by=('local', ) + env.deployments) print magenta("Update requirements") if env.is_local: run_local('pip install --requirement REQUIREMENTS_LOCAL') else: with cd(env.project_root): with prefix('source env/bin/activate'): run('pip install --requirement REQUIREMENTS_SERVER')
def create_database(): database_name = env.django_settings.UNIQUE_PREFIX print magenta("Create database") if env.is_local: run_local( 'mysql --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE DATABASE %s;"' % database_name) else: database = env.django_settings.UNIQUE_PREFIX run('mysql --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE DATABASE db_%s;"' % database)
def reset_local_media(): """ Reset local media from remote host """ require("root", provided_by=("staging", "production")) if not console.confirm( "Are you sure you want to replace the local media with the %s media data?" % env.environment, default=False ): utils.abort("Reset local media aborted.") remote_media = os.path.join(env.code_root, "media") local_media = os.path.join(os.getcwd(), "media/") run_local( 'rsync --delete --exclude=".gitignore" -rvaz %s@%s:%s/ %s' % (env.user, env.hosts[0], remote_media, local_media) )
def makemessages(**kwargs): """pulls out all strings marked for translation""" require('root', provided_by=('local',)) if not env.is_local: utils.abort('runs on local env only. usage: fab local makemessages:lang=fr') if 'lang' in kwargs: utils.abort('missing language. usage: fab local makemessages:lang=fr') print magenta("Make messages") cmd = './manage.py makemessages --domain=%s --locale=%s --ignore=env/* --ignore=node_modules/*' run_local(cmd % ('django', kwargs['lang'])) utils.puts('If you have javascript translations, don\'t forget to run:') utils.puts(cmd % ('djangojs', kwargs['lang']))
def makemessages(**kwargs): """pulls out all strings marked for translation""" require('root', provided_by=('local', )) if not env.is_local: utils.abort( 'runs on local env only. usage: fab local makemessages:lang=fr') if 'lang' in kwargs: utils.abort('missing language. usage: fab local makemessages:lang=fr') print magenta("Make messages") cmd = './manage.py makemessages --domain=%s --locale=%s --ignore=env/* --ignore=node_modules/*' run_local(cmd % ('django', kwargs['lang'])) utils.puts('If you have javascript translations, don\'t forget to run:') utils.puts(cmd % ('djangojs', kwargs['lang']))
def create_database(): database_name = env.unique_identifier print magenta("Create database") if env.is_local: run_local('psql -U $PGUSER -d postgres -c "CREATE DATABASE %s;"' % database_name) else: user = env.unique_identifier database = env.unique_identifier allowed_chars = 'abcdefghijklmnopqrstuvwxyz0123456789' password = ''.join(random.choice(allowed_chars) for i in range(10)) run('sudo nine-manage-databases database create --user=nmd_%s --password=%s nmd_%s' % (user, password, database)) _add_to_dotenv('DATABASE_URL', 'postgres://nmd_%s:%s@localhost/nmd_%s' % (user, password, database)) _add_to_dotenv('PG_USER', 'nmd_%s' % (user)) _add_to_dotenv('PG_PASSWORD', '%s' % (password))
def rsync_mir(from_server,from_path): with settings( hide('warnings', 'running', 'stdout', 'stderr'), warn_only=True, capture=True ): workdir = run_local('pwd',capture=True) if not workdir == '/tmp': print 'BAD' with localcd('/tmp'): cwd = run_local('pwd',capture=True) print '%s == %s' % ('Changed path to:', cwd); results = run_local('/usr/bin/rsync '+from_server+from_path,capture=True) return results
def rsync_mir(from_server, from_path): with settings(hide('warnings', 'running', 'stdout', 'stderr'), warn_only=True, capture=True): workdir = run_local('pwd', capture=True) if not workdir == '/tmp': print 'BAD' with localcd('/tmp'): cwd = run_local('pwd', capture=True) print '%s == %s' % ('Changed path to:', cwd) results = run_local('/usr/bin/rsync ' + from_server + from_path, capture=True) return results
def create_database(): database_name = env.unique_identifier print magenta("Create database") if env.is_local: run_local('psql -U $PGUSER -d postgres -c "CREATE DATABASE %s;"' % database_name) else: user = env.unique_identifier database = env.unique_identifier allowed_chars = 'abcdefghijklmnopqrstuvwxyz0123456789' password = ''.join(random.choice(allowed_chars) for i in range(10)) run('psql -U $PGUSER -d postgres -c "CREATE USER %s WITH PASSWORD \'%s\';"' % (user, password)) run('psql -U $PGUSER -d postgres -c "CREATE DATABASE %s;"' % database) run('psql -U $PGUSER -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE %s to %s;"' % (database, user)) run('psql -U $PGUSER -d postgres -c "GRANT %s to $PGUSER;"' % user) _add_to_dotenv('DATABASE_URL', 'postgres://%s:%s@localhost/%s' % (user, password, database))
def reset_local_media(): """ Reset local media from remote host """ require('root', provided_by=('staging', 'production')) if not console.confirm( 'Are you sure you want to replace the local media with the %s media data?' % env.environment, default=False): utils.abort('Reset local media aborted.') remote_media = os.path.join( env.code_root, 'media', ) local_media = os.path.join(os.getcwd(), 'media/') run_local('rsync --delete --exclude=".gitignore" -rvaz %s@%s:%s/ %s' % (env.user, env.hosts[0], remote_media, local_media))
def dump_media(): """ Reset local media from remote host """ require('project_root', provided_by=env.deployments) if not console.confirm(red( 'Are you sure you want to replace the local media with the %s media data?' % env.environment, bold=True), default=False): utils.abort('Reset local media aborted.') remote_media = os.path.join( env.project_root, 'media', ) local_media = os.path.join(os.getcwd(), 'media') run_local('rsync --delete --exclude=".gitignore" -rvaz %s@%s:%s/ %s' % (env.user, env.hosts[0], remote_media, local_media))
def run_psql_direct(command): pqsl_command = ['psql'] pqsl_command.append(env.database_url) pqsl_command.append(command) output = run_local(' '.join(pqsl_command)) return output
def run_pgdump(command): pqsl_command = ['pgdump'] pqsl_command.append(env.database_url) pqsl_command.append(command) output = run_local(' '.join(pqsl_command)) return output
def dump_database(): """ resets the local database to the database on the server """ local_settings = import_module('%s.settings_development' % env.project_python) require('project_root', provided_by=env.deployments) if not console.confirm(red( 'Are you sure you want to replace the local database with the %s database data?' % env.environment, bold=True), default=False): utils.abort('Reset local database aborted.') run_local( 'mysql --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "DROP DATABASE %s; CREATE DATABASE %s;"' % (local_settings.UNIQUE_PREFIX, local_settings.UNIQUE_PREFIX)) run_local( 'ssh %s@%s "source ~/.profile; mysqldump -u \$MYSQL_USER -p\$MYSQL_PASSWORD db_%s | gzip" | gunzip | mysql -u $MYSQL_USER -p$MYSQL_PASSWORD %s' % (env.django_settings.DEPLOYMENT['user'], env.django_settings.DEPLOYMENT['hosts'][0], env.django_settings.UNIQUE_PREFIX, local_settings.UNIQUE_PREFIX))
def dump(database, dump_path=None): db_info = dj_database_url.parse(env.database_url) database = db_info.get('NAME', '') or database if db_info.get('NAME', ''): print(u'A database({0}) was defined in the database_url'.format( database)) msg = 'Confirm database. \ny - continue; \nn - abort \n>' confirm_operation(msg, expected='y') if not database: database = raw_input('Database: \n>') env.db_name = database if not dump_path: dump_path = raw_input('Dump path: \n>') env.path = dump_path cmd = 'pg_dump %(database_url)s/%(db_name)s %(extra_dump)s > %(path)s'\ % env run_local(cmd)
def run_psql(command, dry_run=True): tmp_command = command.replace(' ', '') pqsl_command = ['psql'] pqsl_command.append(env.database_url) pqsl_command.append('-c " %s "' % tmp_command) output = run_local(' '.join(pqsl_command)) return output
def create_database(): database_name = env.unique_identifier print magenta("Create database") if env.is_local: run_local('psql -U $PGUSER -d postgres -c "CREATE DATABASE %s;"' % database_name) else: user = env.unique_identifier database = env.unique_identifier allowed_chars = 'abcdefghijklmnopqrstuvwxyz0123456789' password = ''.join(random.choice(allowed_chars) for i in range(10)) run('psql -U $PGUSER -d postgres -c "CREATE USER %s WITH PASSWORD \'%s\';"' % (user, password)) run('psql -U $PGUSER -d postgres -c "CREATE DATABASE %s;"' % database) run('psql -U $PGUSER -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE %s to %s;"' % (database, user)) run('psql -U $PGUSER -d postgres -c "GRANT %s to $PGUSER;"' % user) _add_to_dotenv( 'DATABASE_URL', 'postgres://%s:%s@localhost/%s' % (user, password, database))
def reset_local_database(): """ resets the local database to the database on the server """ require('code_root', provided_by=('staging', 'production')) if not console.confirm( 'Are you sure you want to replace the local database with the %s database data?' % env.environment, default=False): utils.abort('Reset local database aborted.') filename = "tmp_dump%d_%d_%d.json" % datetime.datetime.now().isocalendar() require('code_root', provided_by=('staging', 'production')) server_data = os.path.join(env.project_root, filename) local_manage = os.path.join(os.getcwd(), 'manage.py') local_data = os.path.join(os.getcwd(), filename) with cd(env.code_root): with prefix('source ../env/bin/activate'): run('./manage.py dumpdata > %s' % (server_data, )) get(server_data, local_data) run('rm %s' % server_data) with lcd(os.path.dirname(__file__)): run_local('%s sqlflush | %s dbshell' % (local_manage, local_manage)) run_local('%s loaddata %s' % ( local_manage, local_data, )) run_local('rm %s' % local_data)
def dump_database(): require('virtualenv_root', provided_by=env.deployments) if not console.confirm(red('Are you sure you want to replace the local database with the %s database data?' % env.environment, bold=True), default=False): utils.abort('Reset local database aborted.') save_ok_ret_codes = env.ok_ret_codes env.ok_ret_codes.append(1) run_local('psql -U $PGUSER -d postgres -c "DROP DATABASE %s;"' % (env.project_python,)) env.ok_ret_codes = save_ok_ret_codes run_local('psql -U $PGUSER -d postgres -c "CREATE DATABASE %s;"' % (env.project_python,)) run_local('ssh %s@%s "source ~/.profile; pg_dump -U\$PGUSER --no-privileges --no-owner --no-reconnect %s | gzip" | gunzip |psql --quiet -U$PGUSER %s' % (env.user, env.hosts[0], env.unique_identifier, env.project_python))
def dump_database(): require('virtualenv_root', provided_by=env.deployments) if not console.confirm(red( 'Are you sure you want to replace the local database with the %s database data?' % env.environment, bold=True), default=False): utils.abort('Reset local database aborted.') save_ok_ret_codes = env.ok_ret_codes env.ok_ret_codes.append(1) run_local('psql -U $PGUSER -d postgres -c "DROP DATABASE %s;"' % (env.project_python, )) env.ok_ret_codes = save_ok_ret_codes run_local('psql -U $PGUSER -d postgres -c "CREATE DATABASE %s;"' % (env.project_python, )) run_local( 'ssh %s@%s "source ~/.profile; pg_dump -U\$PGUSER --no-privileges --no-owner --no-reconnect %s | gzip" | gunzip |psql --quiet -U$PGUSER %s' % (env.user, env.hosts[0], env.unique_identifier, env.project_python))
def reset_local_database(): """ resets the local database to the database on the server """ require("code_root", provided_by=("staging", "production")) if not console.confirm( "Are you sure you want to replace the local database with the %s database data?" % env.environment, default=False, ): utils.abort("Reset local database aborted.") filename = "tmp_dump%d_%d_%d.json" % datetime.datetime.now().isocalendar() require("code_root", provided_by=("staging", "production")) server_data = os.path.join(env.project_root, filename) local_manage = os.path.join(os.getcwd(), "manage.py") local_data = os.path.join(os.getcwd(), filename) with cd(env.code_root): with prefix("source ../env/bin/activate"): run("./manage.py dumpdata > %s" % (server_data,)) get(server_data, local_data) run("rm %s" % server_data) with lcd(os.path.dirname(__file__)): run_local("%s sqlflush | %s dbshell" % (local_manage, local_manage)) run_local("%s loaddata %s" % (local_manage, local_data)) run_local("rm %s" % local_data)
def __call__(self, cmd, local=None): docker_cmd = "docker-compose {}".format(cmd) if _check_local(local, env): return run_local(docker_cmd, capture=True) else: return run(docker_cmd)
def __call__(self, cmd, local=None): docker_cmd = "docker {}".format(cmd) if _check_local(local, env): return run_local(docker_cmd, capture=env.get("docker_local_capture", False)) else: return run(docker_cmd)
def dump_database(): require('virtualenv_root', provided_by=env.deployments) if not console.confirm(red('Are you sure you want to replace the local database with the %s database data?' % env.environment, bold=True), default=False): utils.abort('Reset local database aborted.') save_ok_ret_codes = env.ok_ret_codes env.ok_ret_codes.append(1) with hide('output'): run_local('psql -U $PGUSER -d postgres -c "DROP DATABASE %s;"' % (env.project_python,)) env.ok_ret_codes = save_ok_ret_codes run_local('psql -U $PGUSER -d postgres -c "CREATE DATABASE %s;"' % (env.project_python,)) import time timestamp = unicode(time.time()).replace('.', '') with cd(env.project_root): with prefix('source .env'): run("pg_dump --no-privileges --no-owner --no-reconnect --dbname=$DATABASE_URL | gzip > db_dump_%s.sql.gz" % timestamp) remote_dump = os.path.join(env.project_root, 'db_dump_%s.sql.gz' % timestamp) run_local('rsync -rvaz %s@%s:%s db_dump.sql.gz' % (env.user, env.hosts[0], remote_dump)) run_local('gunzip db_dump.sql.gz') run_local('psql --quiet -U$PGUSER %s < db_dump.sql' % env.project_python) run_local('rm db_dump.sql') with cd(env.project_root): with prefix('source env/bin/activate'): run('rm db_dump_%s.sql.gz' % timestamp)