def deploy(target='dev', sha1=None):
if sha1 is None:
# get current working git sha1
sha1 = local('git rev-parse HEAD', capture=True)
# server code reset to current working sha1
home_dir = '/home/pyconkr/{target}.pycon.kr/pyconkr-2016'.format(target=target)
if target == 'dev':
python_env = '/home/pyconkr/.pyenv/versions/pyconkr-2016-dev'
else:
python_env = '/home/pyconkr/.pyenv/versions/pyconkr-2016'
with settings(cd(home_dir), shell_env(DJANGO_SETTINGS_MODULE='pyconkr.settings_prod')):
sudo('git fetch --all -p', user='pyconkr')
sudo('git reset --hard ' + sha1, user='pyconkr')
sudo('bower install', user='pyconkr')
sudo('%s/bin/pip install -r requirements.txt' % python_env, user='pyconkr')
sudo('%s/bin/python manage.py compilemessages' % python_env, user='pyconkr')
sudo('%s/bin/python manage.py migrate' % python_env, user='pyconkr')
sudo('%s/bin/python manage.py collectstatic --noinput' % python_env, user='pyconkr')
# worker reload
run('echo r > /var/run/pyconkr-2016-%s.fifo' % target)
python类shell_env()的实例源码
def flatpages_mig(direction='www'):
dev_env = '/home/pyconkr/.pyenv/versions/pyconkr-2016-dev/bin/python'
www_env = '/home/pyconkr/.pyenv/versions/pyconkr-2016/bin/python'
from_env, to_env = (dev_env, www_env) if direction=='www' else (www_env, dev_env)
dev_dir = '/home/pyconkr/dev.pycon.kr/pyconkr-2016'
www_dir = '/home/pyconkr/www.pycon.kr/pyconkr-2016'
from_dir, to_dir = (dev_dir, www_dir) if direction=='www' else (www_dir, dev_dir)
with settings(cd(from_dir),
shell_env(DJANGO_SETTINGS_MODULE='pyconkr.settings_prod')
):
sudo('{python} manage.py dumpdata --indent 2 flatpages -o {fixture_to}'.format(
fixture_to=os.path.join(to_dir, 'pyconkr', 'fixtures', 'flatpages.json'),
python=from_env))
with settings(cd(to_dir),
shell_env(DJANGO_SETTINGS_MODULE='pyconkr.settings_prod')
):
sudo('{python} manage.py loaddata flatpages'.format(
python=to_env))
def create_db():
with settings(warn_only=True), hide('output', 'running'):
if env.get('settings'):
execute('servers.stop_service', 'uwsgi')
with shell_env(**app_config.database):
local('dropdb --if-exists %s' % app_config.database['PGDATABASE'])
if not env.get('settings'):
local('psql -c "DROP USER IF EXISTS %s;"' % app_config.database['PGUSER'])
local('psql -c "CREATE USER %s WITH SUPERUSER PASSWORD \'%s\';"' % (app_config.database['PGUSER'], app_config.database['PGPASSWORD']))
with shell_env(**app_config.database):
local('createdb %s' % app_config.database['PGDATABASE'])
if env.get('settings'):
execute('servers.start_service', 'uwsgi')
def deploy(target='dev', sha1=None):
if sha1 is None:
# get current working git sha1
sha1 = local('git rev-parse HEAD', capture=True)
# server code reset to current working sha1
home_dir = '/home/pyconkr/{target}.pycon.kr/pyconkr-2017'.format(target=target)
if target == 'dev':
python_env = '/home/pyconkr/.pyenv/versions/pyconkr-2017-dev'
else:
python_env = '/home/pyconkr/.pyenv/versions/pyconkr-2017'
with settings(cd(home_dir), shell_env(DJANGO_SETTINGS_MODULE='pyconkr.settings_prod')):
run('git fetch --all -p')
run('git reset --hard ' + sha1)
run('bower install')
run('%s/bin/pip install -r requirements.txt' % python_env)
run('%s/bin/python manage.py compilemessages' % python_env)
run('%s/bin/python manage.py migrate' % python_env)
run('%s/bin/python manage.py collectstatic --noinput' % python_env)
# worker reload
run('echo r > /var/run/pyconkr-2017-%s.fifo' % target)
def run_setup_script(script_name, context):
venv_activate_path = os.path.join(VENV_BIN_DIRECTORY, 'activate')
venv_activate_command = 'source %s' % venv_activate_path
with cd(PROJECT_FOLDER), shell_env(**context), prefix(venv_activate_command):
run('python3 %s' % os.path.join(PROJECT_FOLDER, script_name))
def create_db():
with settings(warn_only=True), hide('output', 'running'):
if env.get('settings'):
execute('servers.stop_service', 'uwsgi')
with shell_env(**app_config.database):
local('dropdb --if-exists %s' % app_config.database['PGDATABASE'])
if not env.get('settings'):
local('psql -c "DROP USER IF EXISTS %s;"' % app_config.database['PGUSER'])
local('psql -c "CREATE USER %s WITH SUPERUSER PASSWORD \'%s\';"' % (app_config.database['PGUSER'], app_config.database['PGPASSWORD']))
with shell_env(**app_config.database):
local('createdb %s' % app_config.database['PGDATABASE'])
if env.get('settings'):
execute('servers.start_service', 'uwsgi')
def _venv_local():
with shell_env(DJANGO_SETTINGS_MODULE=SETTINGS_MODULE):
with prefix('. %s/bin/activate' % VENV_PATH):
yield
def env_set():
with path('/opt/'):
run('echo $PATH')
run('echo $PATH')
#fixme ALL para you can use http://docs.fabfile.org/en/1.13/usage/env.html
with settings(warn_only=True):
run('echo $USER')
with prefix('free'):
with shell_env(JAVA_HOME='/opt/java'):
run('echo $JAVA_HOME')
def setup_environment():
"""Setup users, groups, supervisor, etc."""
# FIXME: When `fabtools v0.21.0` gets released, remove this...
with shell_env(SYSTEMD_PAGER=''):
require.users.user(
name=env.app_user,
group=env.app_user,
system=True,
shell='/bin/bash',
)
for path in (env.app_path, env.etc_path):
require.directory(
path=path,
owner=env.app_user,
group=env.app_user,
use_sudo=True,
)
require.python.virtualenv(
directory=env.venv_path,
venv_python='python3',
user=env.app_user,
use_sudo=True,
)
require.supervisor.process(
name=env.app_name,
command='{} stream --verbose'.format(env.hadroid_botctl),
user=env.app_user,
directory=env.app_path,
stdout_logfile='/var/log/hadroid.log',
stderr_logfile='/var/log/hadroid-err.log',
environment='HADROID_CONFIG={}'.format(env.hadroid_config),
)
#
# Deploy
#