2 """ Karmaworld Fabric management script
3 Finals Club (c) 2013"""
8 from fabric.api import cd, env, lcd, prefix, run, sudo, task, local, settings
9 from fabric.contrib import files
14 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
15 env.repo_root = '~/karmaworld'
16 env.proj_root = '/var/www/karmaworld'
18 env.code_root = env.proj_root
19 env.env_root = env.proj_root
20 env.supervisor_conf = '{0}/confs/{1}/supervisord.conf'.format(env.code_root, env.branch)
22 env.use_ssh_config = True
24 ######## Define host(s)
27 Connection information for the local machine
29 def _custom_local(command):
30 prefixed_command = '/bin/bash -l -i -c "%s"' % command
31 return local(prefixed_command)
33 # This is required for the same reason as above
34 env.proj_root = '/var/www/karmaworld'
36 env.reqs = 'reqs/dev.txt'
37 env.confs = 'confs/beta'
42 ####### Define production host
46 Connection Information for production machine
50 env.hosts = ['karmanotes.org']
51 env.proj_root = '/var/www/karmaworld'
52 env.reqs = 'reqs/prod.txt'
53 env.confs = 'confs/prod/'
55 env.gunicorn_addr = '127.0.0.1:8000'
57 ####### Define beta host
61 Connection Information for beta machine
65 env.hosts = ['beta.karmanotes.org']
66 env.proj_root = '/var/www/karmaworld'
67 env.reqs = 'reqs/prod.txt'
68 env.confs = 'confs/prod/'
71 ######## Run Commands in Virutal Environment
74 Builds the virtualenv path.
76 # not much here now, but maybe useful later.
79 def virtenv_exec(command):
81 Execute command in Virtualenv
85 with prefix('source {0}/bin/activate'.format(path)):
88 ######## Sync database
94 virtenv_exec('{0}/manage.py syncdb --migrate'.format(env.code_root))
97 ####### Collect Static Files
101 Collect static files (if AWS config. present, push to S3)
104 virtenv_exec('%s/manage.py collectstatic --noinput' % env.code_root )
106 ####### Run Dev Server
110 Runs the built-in django webserver
113 virtenv_exec('%s/manage.py runserver' % env.code_root)
115 ####### Create Virtual Environment
120 Link the karmaworld repo into the appropriate production location
122 if not files.exists(env.code_root):
123 run('ln -s {0} {1}'.format(env.repo_root, env.code_root))
126 def make_virtualenv():
128 Create our Virtualenv
130 run('virtualenv {0}'.format(virtenv_path()))
133 def start_supervisord():
137 virtenv_exec('supervisord -c {0}'.format(env.supervisor_conf))
141 def stop_supervisord():
145 virtenv_exec('supervisorctl -c {0} shutdown'.format(env.supervisor_conf))
149 def restart_supervisord():
157 def supervisorctl(action, process):
159 Takes as arguments the name of the process as is
160 defined in supervisord.conf and the action that should
161 be performed on it: start|stop|restart.
163 virtenv_exec('supervisorctl -c {0} {1} {2}'.format(env.supervisor_conf, action, process))
169 Starts the celeryd process
171 supervisorctl('start', 'celeryd')
177 Stops the celeryd process
179 supervisorctl('stop', 'celeryd')
183 def restart_celery():
185 Restarts the celeryd process
187 supervisorctl('restart', 'celeryd')
191 def start_gunicorn():
193 Starts the gunicorn process
195 supervisorctl('start', 'gunicorn')
201 Stops the gunicorn process
203 supervisorctl('stop', 'gunicorn')
207 def restart_gunicorn():
209 Restarts the gunicorn process
211 supervisorctl('restart', 'gunicorn')
214 ####### Update Requirements
217 virtenv_exec('pip install -r {0}/reqs/{1}.txt'.format(env.repo_root, env.branch))
219 ####### Pull new code
222 virtenv_exec('cd %s; git pull' % env.proj_root )
226 Create backup using bup
233 Deploy expected files and directories from non-apt system services.
235 ini_parser = ConfigParser.SafeConfigParser()
236 if not ini_parser.read(env.supervisor_conf):
237 raise Exception("Could not parse INI file {0}".format(env.supervisor_conf))
238 for section, option in (('supervisord','logfile'),
239 ('supervisord','pidfile'),
240 ('unix_http_server','file'),
241 ('program:celeryd','stdout_logfile')):
242 filepath = ini_parser.get(section, option)
243 # generate file's directory structure if needed
244 run('mkdir -p {0}'.format(os.path.split(filepath)[0]))
245 # touch a file and change ownership if needed
246 if 'log' in option and not files.exists(filepath):
247 sudo('touch {0}'.format(filepath))
248 sudo('chown {0}:{1} {2}'.format(env.user, env.group, filepath))
253 Ensure secret files exist for syncdb to run.
256 secrets_path = env.code_root + '/karmaworld/secret'
257 secrets_files = ('filepicker.py', 'static_s3.py', 'db_settings.py', 'drive.py', 'client_secrets.json')
260 for sfile in secrets_files:
261 ffile = os.path.sep.join((secrets_path,sfile))
262 if not files.exists(ffile):
263 errors.append('{0} missing. Please add and try again.'.format(ffile))
265 raise Exception('\n'.join(errors))
270 Sets up and deploys the project for the first time.
285 Deploys the latest changes
291 restart_supervisord()
292 ########## END COMMANDS