def deploy_test(key_file_name="../ec2.pem"):
env.key_filename = key_file_name
changes = local("git status --porcelain", capture=True)
if len(changes):
print " {}".format(changes)
proceed = prompt(
"you have uncommited changes, do you want to proceed",
default=False,
validate=bool
)
if not proceed:
return
git_branch_name = local('git rev-parse --abbrev-ref HEAD', capture=True)
with prefix(". /usr/share/virtualenvwrapper/virtualenvwrapper.sh"):
with prefix("workon {}".format(virtual_env_name)):
run("git pull origin {}".format(git_branch_name))
run("pip install -r requirements.txt")
run("alembic upgrade head")
run("pkill twistd||true")
run("pkill gloss||true")
run("twistd multiple_mllp --receiver gloss.ohc_receiver.OhcReceiver")
run("gunicorn -w 1 -b 0.0.0:6767 -D gloss.api:app")
python类prefix()的实例源码
def update(version=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if version:
# try specified version first
to_version = version
elif not version and env.srvr in ['local', 'vagrant', 'dev']:
# if local, vagrant or dev deploy to develop branch
to_version = 'develop'
else:
# else deploy to master branch
to_version = 'master'
with cd(env.path), prefix(env.within_virtualenv):
run('git pull')
run('git checkout {}'.format(to_version))
def prebuild(build_dir='/tmp/build_spacy'):
if file_exists(build_dir):
shutil.rmtree(build_dir)
os.mkdir(build_dir)
spacy_dir = path.dirname(__file__)
wn_url = 'http://wordnetcode.princeton.edu/3.0/WordNet-3.0.tar.gz'
build_venv = path.join(build_dir, '.env')
with lcd(build_dir):
local('git clone %s .' % spacy_dir)
local('virtualenv ' + build_venv)
with prefix('cd %s && PYTHONPATH=`pwd` && . %s/bin/activate' % (build_dir, build_venv)):
local('pip install cython fabric fabtools pytest')
local('pip install --no-cache-dir -r requirements.txt')
local('fab clean make')
local('cp -r %s/corpora/en/wordnet corpora/en/' % spacy_dir)
local('PYTHONPATH=`pwd` python bin/init_model.py en lang_data corpora spacy/en/data')
local('PYTHONPATH=`pwd` fab test')
local('PYTHONPATH=`pwd` python -m spacy.en.download --force all')
local('PYTHONPATH=`pwd` py.test --models spacy/tests/')
def deploy():
with cd("/data/stregsystem"):
sudo("systemctl stop apache2.service")
with settings(sudo_user='stregsystem'):
sudo("git pull --ff-only")
with prefix("source /data/stregsystem/env/bin/activate"):
sudo("pip install -rrequirements.txt")
sudo("python manage.py collectstatic --noinput")
sudo("python manage.py migrate")
sudo("systemctl start apache2.service")
def install_modules():
requirements_path = os.path.join(PROJECT_FOLDER, 'requirements.txt')
venv_activate_path = os.path.join(VENV_BIN_DIRECTORY, 'activate')
with prefix('source %s' % venv_activate_path):
sudo('pip install wheel')
sudo('pip install -r %s' % requirements_path)
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 makemigrations(app=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['dev', 'stg', 'liv']:
print(yellow('Do not run makemigrations on the servers'))
return
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py makemigrations {}'.format(app if app else ''))
def migrate(app=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py migrate {}'.format(app if app else ''))
def update_index():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py build_solr_schema > schema.xml')
run('mv schema.xml ../../solr/collection1/conf/')
sudo('service tomcat7-{} restart'.format(env.srvr))
run('./manage.py update_index')
def clear_cache():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py clear_cache')
def install_requirements():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
reqs = 'requirements-{}.txt'.format(env.srvr)
try:
assert os.path.exists(reqs)
except AssertionError:
reqs = 'requirements.txt'
with cd(env.path), prefix(env.within_virtualenv):
run('pip install -U -r {}'.format(reqs))
def reinstall_requirement(which):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('pip uninstall {0} && pip install --no-deps {0}'.format(which))
def touch_wsgi():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(os.path.join(env.path, 'dprr')), prefix(env.within_virtualenv):
run('touch wsgi.py')
def runserver(port='8000'):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr not in ['local', 'vagrant']:
print(yellow('this server only runs for development purposes'))
return
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py runserver 0.0.0.0:{}'.format(port))
def install_requirements():
with prefix('source {}/venv/bin/activate'.format(PROJECT_DIR)):
run('pip install -r {}'.format(
os.path.join(PROJECT_DIR, 'requirements.txt')
))
def manage(command=""):
with prefix('source {0}venv/bin/activate && export DATABASE_URL={1}'.format(PROJECT_DIR, DATABASE_URL)):
with cd(DJANGO_DIR):
run('python manage.py {0}'.format(command))
def venv(name):
return prefix('source /venvs/%s/bin/activate' % name)
def docker_machine(machine):
"""
Sets the environment to use a given docker machine.
"""
_env = local('docker-machine env {}'.format(machine), capture=True)
# Reorganize into a string that could be used with prefix().
_env = re.sub(r'^#.*$', '', _env, flags=re.MULTILINE) # Remove comments
_env = re.sub(r'^export ', '', _env, flags=re.MULTILINE) # Remove `export `
_env = re.sub(r'\n', ' ', _env, flags=re.MULTILINE) # Merge to a single line
return _env
def docker_compose(command):
"""
Run a docker-compose command
:param command: Command you want to run
"""
if(env.machine):
with prefix(docker_machine(env.machine)):
return local(
"docker-compose -f {file} {command}".format(
file=env.compose_file, command=command)
)
return local(
"docker-compose -f {file} {command}".format(
file=env.compose_file, command=command)
)
def update_app():
with cd("/opt/mec_env/mec_app"):
run("git pull")
with cd("/opt/mec_env/mec_app/django_ecommerce"):
with prefix("source /opt/mec_env/bin/activate"):
run("pip install -r ../requirements.txt")
run("./manage.py migrate --noinput")
run("./manage.py collectstatic --noinput")
def kobo_workon(_virtualenv_name):
return prefix('kobo_workon %s' % _virtualenv_name)
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 config_environment():
sudo('apt-get -y install git screen')
sudo('adduser crestify --disabled-password --gecos GECOS')
sudo('locale-gen en_US.UTF-8')
with settings(sudo_user='crestify', shell='/bin/bash -c'):
with cd('/home/crestify'):
sudo('git clone https://github.com/crestify/crestify.git crestify')
sudo('virtualenv crestifyenv')
with prefix('source crestifyenv/bin/activate'):
sudo('pip install -r crestify/requirements.txt')
def run_migrations():
with settings(sudo_user='crestify', shell='/bin/bash -c'):
with cd('/home/crestify/crestify'):
with prefix('source ../crestifyenv/bin/activate'):
sudo('honcho run python main.py db upgrade') # Run initial migrations
def _git_update():
with settings(sudo_user="crestify", shell='/bin/bash -c'):
with cd('/home/crestify/crestify'):
with prefix('source ../crestifyenv/bin/activate'):
with settings(sudo_user='crestify', shell='/bin/bash -c'):
sudo('git pull')
sudo('pip install -r requirements.txt')
sudo('honcho run python main.py db upgrade')
def _upgrade_pip():
with prefix(env.virtualenv_activate):
run('pip install -U pip setuptools wheel')
def _install_requirements():
with prefix(env.virtualenv_activate):
run('pip install -r %(virtualenv_requirements)s' % env)
def _upgrade_pip():
with prefix(env.virtualenvwrapper_activate):
run('pip install -q -U pip setuptools wheel || pip install -U pip setuptools wheel')
def _install_requirements():
with prefix(env.virtualenvwrapper_activate):
run('pip install -r %(virtualenvwrapper_requirements)s' % env)