-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
161 lines (133 loc) · 5.15 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from StringIO import StringIO
from fabric.api import cd, local, env, run
from fabric.colors import green
from fabric.context_managers import lcd, settings, shell_env, prefix
from fabric.contrib import files
from fabric.decorators import task
from fabric.operations import prompt, os, sudo
import fabtools
from fabtools.python import virtualenv
import sys
from fabtools.shorewall import Ping, SSH, HTTP, HTTPS, SMTP
@task
def devweek():
env.server_name = 'devweek_emo'
env.hosts = ['54.200.226.3', ]
env.user = 'ubuntu'
env.git_branch = 'master'
# env.walkychat_config = {"ssh_keyfile_name": env.server_name}
env.project_user = 'ubuntu'
# env.db_user = 'walkychat_prototype_user'
env.key_filename = "/Users/MJCampos/git/devweek-backend/{}".format("emotolize_key.pem")
def install_packages():
sudo('apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927')
run('echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list')
sudo('apt-get update')
fabtools.require.deb.packages([
"mongodb-org",
"shorewall", "git"
])
www = "/home/{user}/www/".format(user=env.project_user)
run('mkdir -p ' + www)
git_dir = www+'devweek_emo/'
git_repo = "https://github.com/SsureyMoon/devweek-backend.git"
if not files.exists(git_dir):
fabtools.git.clone(git_repo, path=git_dir)
else:
with cd(git_dir):
run('ls')
fabtools.git.checkout('.')
run('git fetch origin')
if not fabtools.nodejs.version():
fabtools.nodejs.install_from_source()
fabtools.require.nginx.server()
with cd(git_dir):
fabtools.nodejs.install_dependencies()
def update_project():
www = "/home/{user}/www/".format(user=env.project_user)
run('mkdir -p ' + www)
git_dir = www+'devweek_emo/'
git_repo = "https://github.com/SsureyMoon/devweek-backend.git"
with settings(user=env.project_user):
if not files.exists(git_dir):
fabtools.git.clone(git_repo, path=git_dir, use_sudo=True)
else:
with cd(git_dir):
print "djgsljolkj"
sudo('git checkout -- .')
fabtools.git.checkout('.', use_sudo=True)
sudo('git fetch origin')
sudo('git pull')
sudo ('chown {}:{} -R .'.format(env.project_user, env.project_user))
with cd(git_dir):
fabtools.nodejs.install_dependencies()
sudo
def setup_nginx():
www = "/home/{user}/www/".format(user=env.project_user)
git_dir = www+'devweek_emo/'
config_file = git_dir+'nginx/devweek.conf'
sudo('cp {original} /etc/nginx/sites-available/{target}'\
.format(original=config_file, target='54.200.226.3'))
if files.exists('/etc/nginx/sites-enabled/{target}'\
.format(target='54.200.226.3'), use_sudo=True):
sudo('unlink /etc/nginx/sites-enabled/{target}'\
.format(target='54.200.226.3'))
sudo('ln -s /etc/nginx/sites-available/{target} /etc/nginx/sites-enabled/{target}'\
.format(target='54.200.226.3'))
if files.exists('/etc/nginx/sites-enabled/default', use_sudo=True):
sudo('unlink /etc/nginx/sites-enabled/default')
log_dir = '/etc/nginx/log/'
if not files.exists(log_dir):
sudo('mkdir -p ' + log_dir)
sudo('touch {}local-wc.access.log'.format(log_dir))
sudo('touch {}local-wc.error.log'.format(log_dir))
if fabtools.service.is_running('nginx'):
fabtools.service.restart('nginx')
else:
fabtools.service.start('nginx')
def run_supervisor(**kwargs):
www = "/home/{user}/www/".format(user=env.project_user)
git_dir = www+'devweek_emo/'
log_dir = git_dir + 'logs/'
with settings(user=env.project_user):
if not files.exists(log_dir):
run('mkdir -p ' + log_dir)
concat = ",".join([key+"=\""+kwargs[key]+"\"" for key in kwargs])
#with prefix(concat):
with shell_env(**kwargs):
fabtools.require.supervisor.process('node',
environment=concat,#"NODE_ENV=%(ENV_NODE_ENV)s",
command='node app.js',
directory=git_dir,
user=env.project_user,
stdout_logfile=log_dir + 'node_stdout.log',
stderr_logfile=log_dir + 'node_stderr.log',
autorestart=True,
redirect_stderr=True,
)
def stop_supervisor(**kwargs):
www = "/home/{user}/www/".format(user=env.project_user)
git_dir = www+'devweek_emo/'
log_dir = git_dir + 'logs/'
with cd(git_dir):
with shell_env(**kwargs):
fabtools.supervisor.stop_process('node')
@task
def setup(**kwargs):
# install_packages()
update_project()
setup_nginx()
stop_supervisor()
run_supervisor(**kwargs)
@task
def rerun(**kwargs):
update_project()
stop_supervisor()
run_supervisor(**kwargs)
@task
def view_debug():
www = "/home/{user}/www/".format(user=env.project_user)
git_dir = www+'devweek_emo/'
log_dir = git_dir + 'logs/'
stdout_debugfile=log_dir + 'node_stdout.log'
run('cat '+stdout_debugfile)