def deploy(): """Checks whether everything is ready for deployment""" step('Checking whether we are on the expected branch...') with settings(warn_only=True), hide('everything'): branch = run_local('git symbolic-ref -q --short HEAD', capture=True) if not branch: abort(red('No branch checked out, cannot continue.', bold=True)) if branch != env.box_branch: puts(red( 'Warning: The currently checked out branch is \'%s\', but' ' the environment \'%s\' runs on \'%s\'.' % ( branch, env.box_environment, env.box_branch))) if not confirm('Continue deployment?', default=False): abort('Aborting.') execute('check.check') execute('check.test') # XXX Maybe even execute('check.primetime') if deploying to production? with cd('%(box_domain)s'): step('\nChecking for uncommitted changes on the server...') result = run('git status --porcelain') if result: abort(red('Uncommitted changes detected, aborting deployment.'))
def load_db(filename=None): """Loads a dump into the database""" env.box_dump_filename = filename if not filename: abort(red('Dump missing. "fab server.load_db:filename"', bold=True)) if not os.path.exists(filename): abort(red('"%(box_dump_filename)s" does not exist.' % env, bold=True)) if not confirm( 'Completely replace the remote database' ' "%(box_database)s" (if it exists)?', default=False): return run( 'psql -c "DROP DATABASE IF EXISTS %(box_database)s"') run( 'createdb %(box_database)s --encoding=UTF8 --template=template0' ' --owner=%(box_database)s') run_local( 'cat %(box_dump_filename)s |' 'ssh %(host_string)s "source .profile && psql %(box_database)s"') run( 'psql %(box_database)s -c "REASSIGN OWNED BY admin ' ' TO %(box_database)s"')
def deploy(): """Checks whether everything is ready for deployment""" step('Checking whether we are on the expected branch...') with settings(warn_only=True), hide('everything'): branch = run_local('git symbolic-ref -q --short HEAD', capture=True) if not branch: abort(red('No branch checked out, cannot continue.', bold=True)) if branch != env.box_branch: puts( red('Warning: The currently checked out branch is \'%s\', but' ' the environment \'%s\' runs on \'%s\'.' % (branch, env.box_environment, env.box_branch))) if not confirm('Continue deployment?', default=False): abort('Aborting.') execute('check.check') execute('check.test') # XXX Maybe even execute('check.primetime') if deploying to production? with cd('%(box_domain)s'): step('\nChecking for uncommitted changes on the server...') result = run('git status --porcelain') if result: abort(red('Uncommitted changes detected, aborting deployment.'))
def create_and_migrate_database(): """Creates and migrates a Postgres database""" if not confirm("Completely replace the local database" ' "%(box_database_local)s" (if it exists)?'): return run_local("dropdb --if-exists %(box_database_local)s") run_local("createdb %(box_database_local)s" " --encoding=UTF8 --template=template0") run_local("venv/bin/python manage.py migrate")
def pull_mediafiles(): """Pulls all mediafiles from the server. Beware, it is possible that this command pulls down several GBs!""" if not confirm('Completely replace local mediafiles?'): return rsync_project( local_dir='media/', remote_dir='%(box_domain)s/media/' % env, delete=False, # Devs can take care of their media folders. upload=False, )
def create_and_migrate_database(): """Creates and migrates a Postgres database""" if not confirm('Completely replace the local database' ' "%(box_database_local)s" (if it exists)?'): return run_local('dropdb --if-exists %(box_database_local)s') run_local('createdb %(box_database_local)s' ' --encoding=UTF8 --template=template0') run_local('venv/bin/python manage.py migrate')
def pull_database(): """Pulls the database contents from the server, dropping the local database first (if it exists)""" if not confirm('Completely replace the local database' ' "%(box_database_local)s" (if it exists)?'): return run_local('dropdb --if-exists %(box_database_local)s') run_local('createdb %(box_database_local)s' ' --encoding=UTF8 --template=template0') run_local('ssh %(host_string)s "source .profile &&' ' pg_dump %(box_database)s' ' --no-privileges --no-owner --no-reconnect"' ' | psql %(box_database_local)s')
def pull_database(): """Pulls the database contents from the server, dropping the local database first (if it exists)""" if not confirm("Completely replace the local database" ' "%(box_database_local)s" (if it exists)?'): return run_local("dropdb --if-exists %(box_database_local)s") run_local("createdb %(box_database_local)s" " --encoding=UTF8 --template=template0") run_local( 'ssh %(host_string)s "source .profile &&' " pg_dump %(box_database)s" ' --no-privileges --no-owner --no-reconnect"' " | psql %(box_database_local)s" )
def copy_data_from(environment=None): """ Copy the database from one environment to another. Usually from production to stage. Usage: ``fab s server.copy_data_from:production``. :param environment: the source environment """ if env.get('box_hardwired_environment'): abort(red('Cannot continue with a hardwired environment.')) if environment not in env.box_environments: abort(red('Invalid environment %s.' % environment)) source = env.box_environments[environment] target = env.box_environments[env.get('box_environment')] if source == target: abort(red( 'Source environment %s must not equal target environment %s.' % (environment, env.get('box_environment')))) if source['servers'][0] != target['servers'][0]: abort(red('The environments have to be on the same server, sorry!')) puts('Copying data from {0} to {1}'.format( source['remote'], target['remote'])) if not confirm( 'Completely replace the remote database' ' "%(box_database)s" (if it exists)?', default=False): return for key, value in source.items(): env['source_%s' % key] = value with settings(warn_only=True): run('dropdb %(box_database)s') run( 'createdb %(box_database)s --encoding=UTF8 --template=template0' ' --owner=%(box_database)s') run( 'pg_dump %(source_database)s' ' --no-privileges --no-owner --no-reconnect' ' | psql %(box_database)s') run( 'psql %(box_database)s -c "REASSIGN OWNED BY admin ' ' TO %(box_database)s"') with cd(env.box_domain): run('cp -al ~/%(source_domain)s/media/* media/') run('sctl restart %(box_domain)s:*')
def create_and_migrate_database(): """Creates and migrates a Postgres database""" if not confirm( 'Completely replace the local database' ' "%(box_database_local)s" (if it exists)?'): return run_local( 'dropdb --if-exists %(box_database_local)s') run_local( 'createdb %(box_database_local)s' ' --encoding=UTF8 --template=template0') with settings(warn_only=True): run_local('venv/bin/python manage.py makemigrations elephantblog') run_local('venv/bin/python manage.py makemigrations page') run_local('venv/bin/python manage.py migrate')
def remove_host(): if not confirm( 'Really remove the host "%(box_domain)s" and all associated data?', default=False): return run('sudo nine-manage-vhosts virtual-host remove %(box_domain)s') run('rm supervisor/conf.d/%(box_domain)s.conf') run('sctl reload') with cd(env.box_domain): env.box_datetime = datetime.now().strftime('%Y-%m-%d-%s') run('pg_dump %(box_database)s' ' --no-privileges --no-owner --no-reconnect' ' > %(box_database)s-%(box_environment)s-%(box_datetime)s.sql') run('dropdb %(box_database)s') run('dropuser %(box_database)s') puts( red('The folder ~/%(box_domain)s on the server has not been removed. The' ' "tmp" subfolder also contains a fresh database dump.' % env))
def remove_host(): if not confirm( 'Really remove the host "%(box_domain)s" and all associated data?', default=False): return run('sudo nine-manage-vhosts virtual-host remove %(box_domain)s') run('rm supervisor/conf.d/%(box_domain)s.conf') run('sctl reload') with cd(env.box_domain): env.box_datetime = datetime.now().strftime('%Y-%m-%d-%s') run( 'pg_dump %(box_database)s' ' --no-privileges --no-owner --no-reconnect' ' > %(box_database)s-%(box_environment)s-%(box_datetime)s.sql') run('dropdb %(box_database)s') run('dropuser %(box_database)s') puts(red( 'The folder ~/%(box_domain)s on the server has not been removed. The' ' "tmp" subfolder also contains a fresh database dump.' % env))