-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
129 lines (102 loc) · 4.34 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from __future__ import absolute_import
import os
import re
import json
from datetime import datetime, timedelta
from fabric.api import env, hide, settings, cd, lcd
from fabric.operations import local as lrun, run, sudo, put
from fabric.contrib.console import confirm
from fabric.tasks import execute
from fabric.context_managers import shell_env, prefix
from fabric.contrib.files import exists
from ilogue.fexpect import expect, expecting, run as erun
import pexpect
from fabric.api import task as _task
remote_requirements = os.path.abspath('requirements/base.txt')
def console(message):
print '\n[+] %s\n' % message
def task(function):
name = function.__name__
def wrapped(*args,**kwargs):
console('executing %s...' % name)
return function(*args,**kwargs)
wrapped.__name__ = name
return _task(wrapped)
def parse_config(config_file):
with open(config_file,'rb') as f:
config = json.load(f)
return config
def write_config(config_file, config_dict):
config_str = json.dumps(config_dict, indent=4, sort_keys=True)
with open(config_file, 'wb') as f:
f.write(config_str)
return parse_config(config_file)
config = parse_config('haxclub.conf')
@task
def local():
env.run = erun
env.hosts = ['localhost']
@task
def remote_setup():
env.run = run
env.key_filename = config.get('credentials').get('ssh_key_path')
env.user = config.get('user')
# make SURE python is 2.6; otherwise yum is dead.
sudo('rm /usr/bin/python; ln -s /usr/bin/python2.6 /usr/bin/python;')
# get updates
sudo('yum update -y')
# build tools
sudo('yum install make automake gcc gcc-c++ kernel-devel git-core mysql mysql-server mysql-devel postgresql-devel patch ncurses ncurses-devel nginx -y')
# rabbitmq
sudo('yum install rabbitmq-server --enablerepo=epel -y')
# make sure python 2.7 is installed
sudo('yum install python27-devel -y')
python_27_dir = sudo('python27 -c "from distutils.sysconfig import get_python_lib;'\
'print(get_python_lib())"')
with cd(python_27_dir):
sudo('wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | sudo python')
sudo('easy_install pip')
sudo('pip install virtualenv')
sudo('pip install virtualenvwrapper')
# install pip remote requirements (obtained from requirements/remote.txt)
with cd('/tmp'):
with prefix('source $(which virtualenvwrapper.sh) && workon remote'):
put(remote_requirements,'requirements.txt')
run('pip install -r requirements.txt')
sudo('/etc/init.d/mysqld stop; /etc/init.d/mysqld start')
sudo('rabbitmqctl stop; rabbitmq-server -detached',user='rabbitmq')
@task
def deploy():
with prefix('source $(which virtualenvwrapper.sh) && workon remote'):
settings_file = '--settings=haxclub.settings.base'
env_vars = config.get('env_vars')
if not exists('~/haxclub'):
with cd('~/'):
run('git clone https://github.com/jsalva/haxclub')
with cd('~/haxclub/haxclub'):
if not exists('logs'):
run('mkdir logs')
run('git pull origin master')
with shell_env(**env_vars):
prompts = []
prompts += expect("Type 'yes' to continue","yes")
with expecting(prompts):
erun('python manage.py collectstatic %s' % settings_file)
erun('python manage.py migrate %s' % settings_file)
erun('python manage.py syncdb %s' % settings_file)
if exists('supervisord.pid'):
erun('python manage.py supervisor reload %s' % settings_file)
else:
erun('python manage.py supervisor --daemonize %s' % settings_file)
if not exists('/tmp/nginx'):
run('mkdir /tmp/nginx')
put('nginx.conf','/etc/nginx/nginx.conf',use_sudo=True)
put('nginx_haxclub.conf','/etc/nginx/conf.d/nginx_haxclub.conf',use_sudo=True)
put('ssl/haxclub.key.nopass','/etc/ssl/certs/haxclub.key.nopass',use_sudo=True)
put('ssl/haxclub.crt','/etc/ssl/certs/haxclub.crt',use_sudo=True)
put('nginx_haxclub.conf','/etc/nginx/conf.d/nginx_haxclub.conf',use_sudo=True)
sudo('service nginx stop; service nginx start;')
@task
def aws():
env.hosts = config.get('aws_nodes')
execute(remote_setup)