-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_stuff.py
More file actions
executable file
·65 lines (49 loc) · 1.83 KB
/
setup_stuff.py
File metadata and controls
executable file
·65 lines (49 loc) · 1.83 KB
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
#! /usr/bin/python
"""
1. Configures nginx sites
2. Configures uwsgi upstart scripts
Allows you to do:
sudo service
[ nginx | uwsgi-site | uwsgi-deploy ]
[ start | stop | restart | reload ]
"""
import os
import os.path
from os.path import join as j
import sys
import subprocess
import shlex
this_dir = os.path.abspath(os.path.dirname(__file__))
nginx_dir = '/etc/nginx/sites-enabled'
upstart_dir = "/etc/init"
try:
staging = os.environ['APPCUBATOR_MODE']
assert staging in ['prod', 'staging']
except (KeyError, AssertionError):
print >> sys.stderr, "export APPCUBATOR_MODE to staging or prod"
sys.exit(1)
def run_command(command):
shell_splitted_command = shlex.split(command)
p = subprocess.Popen(shell_splitted_command, stdout=sys.stdout, stderr=sys.stderr)
p.wait()
return p.returncode
def symlink(src, dest, *args, **kwargs):
if os.path.exists(dest):
print >> sys.stderr, "About to link %s to %s, overwriting %s, proceed? [y/n]:" % (dest, src, dest)
ch = raw_input()
if ch.lower().startswith('y'):
os.remove(dest)
else:
return
os.symlink(src, dest, *args, **kwargs)
def deploy_upstart(ini_path, desc, command_name):
site_upstart_template = open(j(this_dir, 'upstart.conf.template'), "r").read()
site_upstart = site_upstart_template.format(UWSGI_INI_LOCATION=ini_path,
UWSGI_FOR=desc)
with open(j(upstart_dir, '%s.conf' % command_name), "w") as upstart_dest:
upstart_dest.write(site_upstart)
# site
print "Setting up site nginx and uwsgi"
symlink(j(this_dir, 'site', 'nginx-%s.conf' % staging), j(nginx_dir, 'site-nginx-%s.conf' % staging))
deploy_upstart(j(this_dir, 'site', 'uwsgi-%s.ini' % staging), 'appcubator-site', 'uwsgi-site')
run_command('initctl reload-configuration')