Commit 4b2544b2ea03bfa1b4a245734633ba2d84620dc2
1 parent
bd81e677
Exists in
master
and in
39 other branches
New fabfile. Still have to configure it.
Showing
1 changed file
with
170 additions
and
73 deletions
Show diff stats
fabfile.py
1 | +# coding: utf-8 | |
1 | 2 | |
2 | -from fabric.operations import put | |
3 | -from fabric.api import run, sudo, env | |
3 | +import os | |
4 | + | |
5 | +from fabric import colors | |
6 | +from fabric.decorators import task | |
7 | +from fabric.api import env, run, sudo, local | |
4 | 8 | from fabric.contrib.files import exists |
5 | -from fabric.decorators import with_settings | |
6 | -from fabric.context_managers import prefix, cd, settings | |
9 | +from fabric.context_managers import prefix, cd, settings, shell_env | |
10 | + | |
11 | + | |
12 | +### Start of config | |
13 | + | |
14 | +APP_USER = APP_NAME = VENV_NAME = 'colab' | |
15 | +REPO_URL = 'To be defined' | |
7 | 16 | |
8 | -env.user = 'colab' # key depends on env | |
9 | -env.use_shell = False | |
10 | 17 | |
11 | 18 | environments = { |
12 | 19 | 'dev': { |
13 | 20 | 'hosts': ['127.0.0.1'], |
14 | 21 | 'key_filename': '~/.vagrant.d/insecure_private_key', |
15 | 22 | 'port': 2222, |
23 | + 'is_vagrant': True, | |
24 | + 'superuser': 'vagrant', | |
16 | 25 | }, |
17 | - 'live': { | |
18 | - 'hosts': ['10.1.2.153'], | |
19 | - 'key_filename': '~/.ssh/id_rsa', | |
20 | - 'port': 22, | |
26 | + 'qa': { | |
27 | + 'hosts': [], | |
28 | + 'port': 22, | |
29 | + 'is_vagrant': False, | |
30 | + 'superuser': 'root', | |
21 | 31 | }, |
22 | - 'demo': { | |
23 | - 'hosts': ['colab-demo.tracy.com.br'], | |
24 | - 'key_filename': '~/.ssh/id_rsa', | |
32 | + 'prod': { | |
33 | + 'hosts': [], | |
25 | 34 | 'port': 22, |
35 | + 'is_vagrant': False, | |
36 | + 'superuser': 'root', | |
26 | 37 | }, |
27 | 38 | } |
39 | +DEFAULT_ENVIRONMENT = 'dev' | |
28 | 40 | |
41 | +### End of config | |
29 | 42 | |
43 | +env.user = APP_USER | |
44 | +env.use_shell = False | |
45 | + | |
46 | +PROJECT_PATH = os.path.join(os.path.dirname(__file__)) | |
47 | +REPO_PATH = '/home/{}/{}'.format(APP_USER, APP_NAME) | |
30 | 48 | SOURCE_VENV = 'source /usr/local/bin/virtualenvwrapper.sh' |
31 | -WORKON_COLAB = '{} && workon colab'.format(SOURCE_VENV) | |
49 | +WORKON_ENV = '{} && workon {}'.format(SOURCE_VENV, VENV_NAME) | |
50 | +MANAGE_PATH = os.path.join(REPO_PATH, 'src') | |
51 | +SETTINGS_PATH = os.path.join(MANAGE_PATH, APP_NAME) | |
32 | 52 | |
33 | 53 | |
54 | +@task | |
34 | 55 | def environment(name): |
56 | + """Set the environment where the tasks will be executed""" | |
35 | 57 | env.update(environments[name]) |
36 | 58 | env.environment = name |
37 | -environment('dev') | |
59 | +environment(DEFAULT_ENVIRONMENT) | |
60 | + | |
61 | + | |
62 | +def aptget_install(pkg): | |
63 | + sudo('DEBIAN_FRONTEND=noninteractive apt-get install -y -q {}'.format(pkg)) | |
64 | + | |
65 | + | |
66 | +def install_requirements(): | |
67 | + with cd(REPO_PATH), prefix(WORKON_ENV): | |
68 | + run('pip install -U distribute') | |
69 | + if not env.is_vagrant: | |
70 | + run('pip install -r requirements.txt') | |
71 | + return | |
72 | + | |
73 | + if exists('requirements-{}.txt'.format(env.environment)): | |
74 | + run('pip install -r requirements-{}.txt'.format(env.environment)) | |
75 | + else: | |
76 | + run('pip install -r requirements.txt') | |
38 | 77 | |
39 | 78 | |
40 | 79 | def mkvirtualenv(): |
41 | - if not exists('~/.virtualenvs/colab'): | |
80 | + if not exists('~/.virtualenvs/' + VENV_NAME): | |
42 | 81 | with prefix(SOURCE_VENV): |
43 | - run('mkvirtualenv colab') | |
82 | + run('mkvirtualenv ' + VENV_NAME) | |
44 | 83 | return True |
45 | 84 | |
46 | 85 | |
47 | -def install(local_settings=None): | |
48 | - env_created = mkvirtualenv() | |
86 | +def manage(command): | |
87 | + django_settings = env.get('django_settings') | |
88 | + env_vars = {} | |
89 | + if django_settings: | |
90 | + env_vars.update({'DJANGO_SETTINGS_MODULE': django_settings}) | |
91 | + | |
92 | + with shell_env(**env_vars): | |
93 | + with cd(MANAGE_PATH), prefix(WORKON_ENV): | |
94 | + run('python manage.py {}'.format(command)) | |
95 | + | |
96 | + | |
97 | +def syncdb(): | |
98 | + manage('syncdb') | |
99 | + | |
100 | + | |
101 | +def migrate(): | |
102 | + manage('migrate') | |
103 | + | |
104 | + | |
105 | +def collectstatic(): | |
106 | + sudo('mkdir -p /usr/share/nginx/{}'.format(APP_NAME)) | |
107 | + sudo('chown {} /usr/share/nginx/{}'.format(env.user, APP_NAME)) | |
108 | + manage('collectstatic --noinput') | |
109 | + | |
110 | + | |
111 | +def create_local_settings(): | |
112 | + with cd(SETTINGS_PATH), settings(user=env.superuser): | |
113 | + env_local_settings = 'local_settings-{}.py'.format(env.environment) | |
114 | + | |
115 | + if not exists('local_settings.py') and exists(env_local_settings): | |
116 | + run('ln -s {} {}'.format(env_local_settings, 'local_settings.py')) | |
117 | + run('chown {} local_settings.py'.format(env.user)) | |
118 | + | |
49 | 119 | |
50 | - if not exists('~/colab'): | |
51 | - run('git clone https://github.com/TracyWebTech/colab ~/colab') | |
120 | +def update_code(): | |
121 | + if env.is_vagrant: | |
122 | + if not exists(REPO_PATH): | |
123 | + run('ln -s /vagrant/ {}'.format(REPO_PATH)) | |
124 | + return | |
52 | 125 | |
53 | - if local_settings: | |
54 | - put(local_settings, '~/colab/src/colab/local_settings.py') | |
126 | + if not exists(REPO_PATH): | |
127 | + run('git clone {} {}'.format(REPO_URL, REPO_PATH)) | |
128 | + else: | |
129 | + with cd(REPO_PATH): | |
130 | + run('git pull') | |
55 | 131 | |
56 | - if not exists('~/apache-solr-3.6.2/'): | |
57 | - run('wget http://archive.apache.org/dist/lucene/solr/3.6.2/apache-solr-3.6.2.tgz') | |
58 | - run('tar xzf apache-solr-3.6.2.tgz') | |
59 | - run('rm apache-solr-3.6.2.tgz') | |
60 | 132 | |
61 | - with cd('~/apache-solr-3.6.2/example/solr/conf/'): | |
62 | - if not exists('stopwords_en.txt'): | |
63 | - run('cp stopwords.txt stopwords_en.txt') | |
133 | +@task | |
134 | +def bootstrap(): | |
135 | + """Bootstrap machine to run fabric tasks""" | |
64 | 136 | |
65 | - if env_created: | |
66 | - update_requirements() | |
137 | + with settings(user=env.superuser): | |
67 | 138 | |
68 | - sudo('supervisorctl reload', shell=False) | |
139 | + if not exists('/usr/bin/git'): | |
140 | + aptget_install('git') | |
69 | 141 | |
142 | + if env.is_vagrant: | |
143 | + groups = 'sudo,vagrant' | |
144 | + local('chmod -fR g+w {}'.format(PROJECT_PATH)) | |
145 | + else: | |
146 | + groups = 'sudo' | |
70 | 147 | |
71 | -def update_requirements(): | |
72 | - with cd('~/colab'), prefix(WORKON_COLAB): | |
73 | - run('pip install -U -r requirements.txt') | |
148 | + sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, groups), | |
149 | + quiet=True) | |
150 | + ssh_dir = '/home/{0}/.ssh/'.format(APP_USER) | |
151 | + if not exists(ssh_dir): | |
152 | + sudo('mkdir -p {0}'.format(ssh_dir)) | |
153 | + sudo('chmod 700 {0}'.format(ssh_dir)) | |
154 | + sudo('cp ~{}/.ssh/authorized_keys /home/{}/.ssh/'.format( | |
155 | + env.superuser, | |
156 | + APP_USER | |
157 | + )) | |
158 | + sudo('chown -fR {0}:{0} {1}'.format(APP_USER, ssh_dir)) | |
74 | 159 | |
160 | + sudoers_file = os.path.join('/etc/sudoers.d/', APP_USER) | |
161 | + tmp_file = os.path.join('/tmp', APP_USER) | |
162 | + if not exists(sudoers_file): | |
163 | + sudo('echo "{} ALL=NOPASSWD: ALL" > {}'.format(APP_USER, tmp_file)) | |
164 | + sudo('chown root:root {}'.format(tmp_file)) | |
165 | + sudo('chmod 440 {}'.format(tmp_file)) | |
166 | + sudo('mv {} {}'.format(tmp_file, sudoers_file)) | |
75 | 167 | |
76 | -def deploy(update=False): | |
77 | 168 | |
78 | - with cd('~/colab/src/'), prefix(WORKON_COLAB): | |
79 | - run('git pull') | |
169 | +@task | |
170 | +def provision(): | |
171 | + """Run puppet""" | |
80 | 172 | |
81 | - if update: | |
82 | - update_requirements() | |
173 | + update_code() | |
83 | 174 | |
84 | - with cd('~/colab/src/'), prefix(WORKON_COLAB): | |
85 | - run('python manage.py syncdb') | |
86 | - run('python manage.py migrate') | |
87 | - run('python manage.py collectstatic --noinput') | |
88 | - run('python manage.py build_solr_schema -f ~/apache-solr-3.6.2/example/solr/conf/schema.xml') | |
175 | + puppet_path = os.path.join(REPO_PATH, 'puppet/') | |
176 | + modules_path = os.path.join(puppet_path, 'modules') | |
177 | + puppet_modules = '{}:/etc/puppet/modules'.format(modules_path) | |
89 | 178 | |
90 | - sudo('supervisorctl restart all') | |
179 | + with cd(puppet_path): | |
180 | + run('sudo python bootstrap.py') | |
91 | 181 | |
182 | + if env.is_vagrant: | |
183 | + cmd = os.path.join(puppet_path, 'manifests', 'site.pp') | |
184 | + else: | |
185 | + cmd = '-e "include {}"'.format(APP_NAME) | |
92 | 186 | |
93 | -def rebuild_index(age=None, batch=None): | |
94 | - with cd('~/colab/src/'), prefix(WORKON_COLAB): | |
95 | - age_arg = '' | |
96 | - if age: | |
97 | - age_arg = '--age={}'.format(age) | |
187 | + if not exists('/usr/bin/puppet'): | |
188 | + print(colors.red('Please install `puppet` before continue.')) | |
189 | + return | |
98 | 190 | |
99 | - batch_arg = '' | |
100 | - if batch: | |
101 | - batch_arg = '--batch-size={}'.format(batch) | |
191 | + sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd)) | |
102 | 192 | |
103 | 193 | |
104 | - run('python manage.py rebuild_index {} {}'.format(age_arg, batch_arg)) | |
194 | +@task | |
195 | +def ssh_keygen(): | |
196 | + """Create SSH credentials""" | |
105 | 197 | |
198 | + if not exists('~/.ssh/id_rsa'): | |
199 | + run("ssh-keygen -f ~/.ssh/id_rsa -N '' -b 1024 -q") | |
200 | + key = run('cat ~/.ssh/id_rsa.pub') | |
106 | 201 | |
107 | -@with_settings(user='vagrant') | |
108 | -def build_solr_schema(): | |
109 | - with cd('/vagrant/src/'), prefix(WORKON_COLAB): | |
110 | - run('python manage.py build_solr_schema -f /tmp/schema.xml') | |
202 | + print('Public key:') | |
203 | + print(colors.yellow(key)) | |
204 | + print('') | |
205 | + print('Add the key above to your github repository deploy keys') | |
111 | 206 | |
112 | - with settings(user='colab'): | |
113 | - run('cp /tmp/schema.xml ~/apache-solr-3.6.2/example/solr/conf/schema.xml') | |
114 | 207 | |
115 | - sudo('supervisorctl restart solr') | |
208 | +@task | |
209 | +def deploy(noprovision=False): | |
210 | + """Deploy and run the new code (master branch)""" | |
116 | 211 | |
212 | + if noprovision is False: | |
213 | + provision() | |
214 | + else: | |
215 | + update_code() | |
117 | 216 | |
118 | -@with_settings(user='vagrant') | |
119 | -def runserver(update_requirements=False): | |
120 | - env_created = mkvirtualenv() | |
217 | + mkvirtualenv() | |
121 | 218 | |
122 | - with cd('/vagrant/src/'), prefix(WORKON_COLAB): | |
219 | + sudo('supervisorctl stop all') | |
123 | 220 | |
124 | - # If explicitly called or if it's a new environment | |
125 | - if update_requirements or env_created: | |
126 | - run('pip install -r /vagrant/requirements.txt') | |
221 | + install_requirements() | |
222 | + create_local_settings() | |
223 | + collectstatic() | |
224 | + syncdb() | |
225 | + migrate() | |
127 | 226 | |
128 | - run('python manage.py syncdb') | |
129 | - run('python manage.py migrate') | |
130 | - run('python manage.py runserver 0.0.0.0:7000') | |
227 | + sudo('supervisorctl start all') | ... | ... |