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") 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 env.box_remote_db = remote_env("DATABASE_URL") if not env.box_remote_db: abort(red("Unable to determine the remote DATABASE_URL", bold=True)) 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 psql %(box_remote_db)s" ) run('psql %(box_database)s -c "REASSIGN OWNED BY admin ' ' TO %(box_database)s"')
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") for line in env["box_disable_process"]: run(line) with cd(env.box_domain): env.box_datetime = datetime.now().strftime("%Y-%m-%d-%s") run( "pg_dump -Ox %(box_database)s" " > %(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" " project folder also contains a fresh database dump." % env ) )
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.') step('\nChecking whether we are up to date...') run_local('git push --dry-run origin %(box_branch)s') execute('check.check') execute('check.test') 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 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_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 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 -Ox %(source_database)s | 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 -aln ~/%(source_domain)s/media/* media/") execute("server.restart")
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 env.box_remote_db = remote_env('DATABASE_URL') if not env.box_remote_db: abort(red('Unable to determine the remote DATABASE_URL', bold=True)) 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 pg_dump -Ox "%(box_remote_db)s"' ' | 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 -Ox %(source_database)s | 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 -aln ~/%(source_domain)s/media/* media/') for line in env['box_restart']: run(line)