def _do_deploy(args=()): with cd("%(box_domain)s"): run('find . -name "*.pyc" -delete') run("venv/bin/pip install -U pip wheel setuptools") run("venv/bin/pip install -r requirements.txt") run("venv/bin/python manage.py migrate --noinput") step("\nUploading static files...") rsync_project( local_dir="static/", remote_dir="%(box_domain)s/static/" % env, delete=("clear" in args), ) step("\nCollecting static files...") with cd("%(box_domain)s"): run("venv/bin/python manage.py collectstatic --noinput") step("\nRunning system checks on server...") with cd("%(box_domain)s"): run("venv/bin/python manage.py check --deploy") step("\nRestarting server process...") execute("server.restart") execute("git.fetch_remote")
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 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 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 nginx_vhost_and_supervisor(): run('sudo nine-manage-vhosts virtual-host create %(box_domain)s' ' --template=feinheit --webroot=/home/www-data/%(box_domain)s/htdocs') with cd('%(box_domain)s'): run('mkdir -p media tmp') for line in env['box_enable_process']: run(line)
def nginx_vhost_and_supervisor(): run( "sudo nine-manage-vhosts virtual-host create %(box_domain)s" " --template=%(box_vhost_template)s" " --webroot=/home/www-data/%(box_domain)s/htdocs" ) with cd("%(box_domain)s"): run("mkdir -p media tmp") for line in env["box_enable_process"]: run(line)
def deploy(*args): """Deploys frontend and backend code to the server if the checking step did not report any problems""" execute('check.deploy') step('\nCompiling static sources...') run_local('yarn run prod') step('\nPushing changes...') run_local('git push origin %(box_branch)s') step('\nDeploying new code on server...') with cd('%(box_domain)s'): run('git fetch') run('git reset --hard origin/%(box_branch)s') run('find . -name "*.pyc" -delete') run('venv/bin/pip install -r requirements.txt') run('venv/bin/python manage.py migrate --noinput') step('\nUploading static files...') rsync_project( local_dir='static/', remote_dir='%(box_domain)s/static/' % env, delete=('clear' in args), ) step('\nCollecting static files...') with cd('%(box_domain)s'): run('venv/bin/python manage.py collectstatic --noinput') step('\nRunning system checks on server...') with cd('%(box_domain)s'): run('venv/bin/python manage.py check --deploy') step('\nRestarting server process...') for line in env['box_restart']: run(line) execute('git.fetch_remote')
def create_database_and_dotenv(): env.box_sentry_dsn = prompt("Sentry DSN") env.box_oauth2_client_id = prompt("Google OAuth2 Client ID") env.box_oauth2_client_secret = prompt("Google OAuth2 Client Secret") env.box_database_pw = get_random_string( 20, chars="abcdefghijklmopqrstuvwx01234567890" ) env.box_secret_key = get_random_string(50) run( 'psql -c "CREATE ROLE %(box_database)s WITH' " ENCRYPTED PASSWORD '%(box_database_pw)s'" ' LOGIN NOCREATEDB NOCREATEROLE NOSUPERUSER"' ) run('psql -c "GRANT %(box_database)s TO admin"') run( 'psql -c "CREATE DATABASE %(box_database)s WITH' " OWNER %(box_database)s" " TEMPLATE template0" " ENCODING 'UTF8'\"" ) with cd("%(box_domain)s"): put( StringIO( """\ DEBUG=False DATABASE_URL=postgres://%(box_database)s:%(box_database_pw)s\ @localhost:5432/%(box_database)s CACHE_URL=hiredis://localhost:6379/1/?key_prefix=%(box_database)s SECRET_KEY=%(box_secret_key)s SENTRY_DSN=%(box_sentry_dsn)s ALLOWED_HOSTS=['.%(box_domain)s', '.%(host_string_host)s'] GOOGLE_CLIENT_ID=%(box_oauth2_client_id)s GOOGLE_CLIENT_SECRET=%(box_oauth2_client_secret)s # LIVE=True # CANONICAL_DOMAIN=%(box_domain)s # CANONICAL_DOMAIN_SECURE=True """ % dict(env, host_string_host=env.host_string.split("@")[-1]) ), ".env", ) run("venv/bin/python manage.py migrate --noinput")
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 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)
def direct(): """Deploys code directly, most useful when Bitbucket is down""" execute("check.deploy") step("\nCompiling static sources...") run_local("yarn run prod") step("\nPushing changes...") run_local("git push %(box_remote)s %(box_branch)s:refs/heads/DIRECTDEPLOY") step("\nDeploying new code on server...") with cd("%(box_domain)s"): run("git merge --ff-only DIRECTDEPLOY") _do_deploy() run_local("git push %(box_remote)s :refs/heads/DIRECTDEPLOY") step("\nPLEASE do not forget to push to the source repository anyway!")
def deploy(*args): """Deploys frontend and backend code to the server if the checking step did not report any problems""" step("\nChecking whether we are up to date...") run_local("git push --dry-run origin %(box_branch)s") execute("check.deploy") step("\nCompiling static sources...") run_local("yarn run prod") step("\nPushing changes...") run_local("git push --all origin %(box_branch)s") step("\nDeploying new code on server...") with cd("%(box_domain)s"): run("git fetch") run("git merge --ff-only origin/%(box_branch)s") _do_deploy(args)
def create_database_and_dotenv(): env.box_sentry_dsn = prompt('Sentry DSN') env.box_oauth2_client_id = prompt('Google OAuth2 Client ID') env.box_oauth2_client_secret = prompt('Google OAuth2 Client Secret') env.box_database_pw = get_random_string( 20, chars='abcdefghijklmopqrstuvwx01234567890') env.box_secret_key = get_random_string(50) run('psql -c "CREATE ROLE %(box_database)s WITH' ' ENCRYPTED PASSWORD \'%(box_database_pw)s\'' ' LOGIN NOCREATEDB NOCREATEROLE NOSUPERUSER"') run('psql -c "GRANT %(box_database)s TO admin"') run('psql -c "CREATE DATABASE %(box_database)s WITH' ' OWNER %(box_database)s' ' TEMPLATE template0' ' ENCODING \'UTF8\'"') with cd('%(box_domain)s'): put( StringIO('''\ DATABASE_URL=postgres://%(box_database)s:%(box_database_pw)s\ @localhost:5432/%(box_database)s CACHE_URL=hiredis://localhost:6379/1/?key_prefix=%(box_database)s SECRET_KEY=%(box_secret_key)s SENTRY_DSN=%(box_sentry_dsn)s ALLOWED_HOSTS=['.%(box_domain)s', '.%(host_string_host)s'] GOOGLE_CLIENT_ID=%(box_oauth2_client_id)s GOOGLE_CLIENT_SECRET=%(box_oauth2_client_secret)s # LIVE=True # CANONICAL_DOMAIN=%(box_domain)s # CANONICAL_DOMAIN_SECURE=True ''' % dict(env, host_string_host=env.host_string.split('@')[-1])), '.env') run('venv/bin/python manage.py migrate --noinput')
def create_virtualenv(): with cd("%(box_domain)s"): run("rm -rf venv") run("python3 -m venv venv") run("venv/bin/pip install -U pip wheel setuptools") run("venv/bin/pip install -r requirements.txt")
def create_virtualenv(): with cd('%(box_domain)s'): run('rm -rf venv') run('python3 -m venv venv') run('venv/bin/pip install -U pip wheel') run('venv/bin/pip install -r requirements.txt')