-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
103 lines (73 loc) · 2.3 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
"""
To get a list of commands use:
fab --list
Prerequisites:
- remote machine requires python and virtualenv, use `fab install_virtualenv` to install.
- local machine requires `fabric`.
"""
import getpass
from fabric.api import cd, sudo, run, env, warn_only, settings, put
from fabric.context_managers import prefix
INSTALL_DIR = "/opt/getthat"
MAIN_FILE = "getthat.py"
FILES = [
"webapp",
"getthat.py",
"fabfile.py",
"requirements.txt",
"README.md",
]
# put your IP here
env.hosts = []
# set user and password here if you don't want to enter it every time
env.user = None
env.password = None
env.user = env.user or getpass.getpass('Enter SSH username: ')
env.password = env.password or getpass.getpass('Enter SSH password: ')
# set SSH key here
env.key_filename = None # needed for SSH
def deploy():
""" Deploy server."""
kill()
sudo("mkdir " + INSTALL_DIR, warn_only=True)
with cd(INSTALL_DIR):
# upload all needed and only needed files
for f in FILES:
put(f, ".", use_sudo=True)
# prepare virtual environment
sudo("virtualenv .")
with prefix('source %s/bin/activate' % INSTALL_DIR):
sudo("pip install -r requirements.txt")
start()
def clean():
""" Kill server and delete related files."""
kill()
# remove files
with warn_only():
sudo("rm -rf %s/*" % INSTALL_DIR)
sudo("rm -rf %s/.*" % INSTALL_DIR)
sudo("rmdir %s" % INSTALL_DIR)
def kill():
""" Kill server."""
with settings(warn_only=True):
sudo("kill $(ps aux | grep %s | grep -v grep | awk '{print $2}')" % MAIN_FILE)
def start():
""" Start server."""
with cd(INSTALL_DIR):
with prefix('source %s/bin/activate' % INSTALL_DIR):
sudo("nohup python %s & sleep 2; exit 0" % MAIN_FILE)
def restart():
""" Restart server."""
kill()
start()
def install_virtualenv():
""" Install virtualenv on remote machine."""
# install pip if needed
with cd("/tmp"):
res = run("pip", warn_only=True)
if res.failed:
# install pip first
run("curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py >get-pip.py")
sudo("python get-pip.py")
run("rm get-pip.py")
sudo("pip install virtualenv")