|
| 1 | +from fabric.api import env, run, cd, prefix, sudo |
| 2 | +from fabric.contrib.project import rsync_project |
| 3 | +from fabric.contrib.files import exists |
| 4 | +from contextlib import contextmanager as _contextmanager |
| 5 | + |
| 6 | + |
| 7 | +# the user to use for the remote commands |
| 8 | +env.user = 'root' |
| 9 | + |
| 10 | +# the servers where the commands are executed |
| 11 | +env.hosts = ['104.131.15.37'] |
| 12 | +env.root = '/root' |
| 13 | +env.project = '%s/BizTest' % env.root |
| 14 | +env.environment = '%s/BizTestEnv' % env.root |
| 15 | +env.activate = 'source %s/bin/activate' % env.environment |
| 16 | +env.forward_agent = True |
| 17 | +env.node_version = 'v0.10.36' |
| 18 | + |
| 19 | + |
| 20 | +@_contextmanager |
| 21 | +def virtualenv(): |
| 22 | + with cd(env.project): |
| 23 | + with prefix(env.activate): |
| 24 | + yield |
| 25 | + |
| 26 | + |
| 27 | +@_contextmanager |
| 28 | +def buildenv(): |
| 29 | + with cd(env.root): |
| 30 | + yield |
| 31 | + |
| 32 | + |
| 33 | +def deploy(): |
| 34 | + if not exists('%s/launch.sh' % env.root): |
| 35 | + # We haven't installed anything yet |
| 36 | + sudo('aptitude update') |
| 37 | + sudo('aptitude -y upgrade') |
| 38 | + sudo('aptitude install -y nginx supervisor ruby python-virtualenv gcc make python-dev git wget curl make g++ libevent-dev ' |
| 39 | + 'python-setuptools memcached libmemcached-dev libxml2-dev libxslt1-dev postgresql-9.3 postgresql-server-dev-9.3') |
| 40 | + |
| 41 | + if not exists('%s/node' % env.root): |
| 42 | + run('wget http://nodejs.org/dist/{0}/node-{0}.tar.gz'.format(env.node_version)) |
| 43 | + run('tar -xzf node-{}.tar.gz'.format(env.node_version)) |
| 44 | + with cd('node-{}'.format(env.node_version)): |
| 45 | + run('./configure --prefix=%s/node' % env.root) |
| 46 | + run('make && make install') |
| 47 | + run('%s/node/bin/npm install -g grunt-cli' % env.root) |
| 48 | + |
| 49 | + if not exists(env.environment): |
| 50 | + with cd(env.root): |
| 51 | + run('virtualenv %s' % env.environment) |
| 52 | + |
| 53 | + rsync_project( |
| 54 | + remote_dir=env.project, |
| 55 | + local_dir='./', |
| 56 | + exclude=('*.pyc', 'config.py', '.git', '.gitignore', '.tmp', 'node_modules', '.idea', 'dist', 'alembic.ini', 'dist/index.html', 'app/scripts/config.js', 'app/index.html', 'bower_components'), |
| 57 | + ) |
| 58 | + |
| 59 | + if not exists('/etc/supervisor/conf.d/biztest.conf'): |
| 60 | + sudo('cp %s/config/biztest-supervisor.conf /etc/supervisor/conf.d/biztest.conf' % env.project) |
| 61 | + sudo('supervisorctl reread') |
| 62 | + sudo('supervisorctl update') |
| 63 | + sudo('supervisorctl start biztest') |
| 64 | + |
| 65 | + if not exists('/etc/nginx/sites-enabled/biztest'): |
| 66 | + sudo('cp %s/config/biztest-nginx.conf /etc/nginx/sites-enabled/biztest' % env.project) |
| 67 | + sudo('service nginx reload') |
| 68 | + |
| 69 | + if not exists('%s/config.py' % env.project): |
| 70 | + sudo('cp %s/config-template %s/config.py' % (env.project, env.project)) |
| 71 | + |
| 72 | + with virtualenv(): |
| 73 | + run('pip install -r requirements.txt') |
| 74 | + |
| 75 | + if not exists('/root/bizness.db'): |
| 76 | + with virtualenv(): |
| 77 | + run('PYTHONPATH=%s DATABASE_URL=sqlite:////root/bizness.db python %s/tools/populate_db.py' % (env.project, env.project)) |
| 78 | + |
| 79 | + sudo('supervisorctl stop biztest') |
| 80 | + sudo('supervisorctl start biztest') |
| 81 | + |
| 82 | + #with cd(env.project): |
| 83 | + # run('%s/node/bin/npm install' % env.root) |
| 84 | + # run('%s/node/bin/bower install' % env.root) |
| 85 | + # run('grunt') |
0 commit comments