forked from lucyparsons/OpenOversight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
83 lines (66 loc) · 3.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
from __future__ import with_statement
from fabric.api import env, run, cd, execute, get, lcd, local, put
import datetime
import os
# from dotenv import load_dotenv, find_dotenv
# load_dotenv(find_dotenv())
basedir = os.path.abspath(os.path.dirname(__file__))
env.use_ssh_config = False
# Hosts
# env.hosts list references aliases in ~/.ssh/config or IP address. When using .ssh/config,
# fab will use the ssh keyfile referenced by the host alias, otherwise need to do what is
# being done in dev to assign env a key_filename
def staging():
env.environment = 'staging'
env.user = 'root'
env.hosts = 'staging.openoversight.lucyparsonslabs.com'
env.unprivileged_user = 'nginx'
env.venv_dir = '/home/nginx/oovirtenv/venv'
env.code_dir = '/home/nginx/oovirtenv/venv/OpenOversight'
env.backup_dir = '/home/nginx/openoversight_backup'
env.s3bucket = 'openoversight-staging'
def production():
env.environment = 'production'
env.user = 'root'
env.hosts = 'openoversight.lucyparsonslabs.com'
env.unprivileged_user = 'nginx'
env.venv_dir = '/home/nginx/oovirtenv'
env.code_dir = '/home/nginx/oovirtenv/OpenOversight'
env.backup_dir = '/home/nginx/openoversight_backup'
env.s3bucket = 'openoversight-prod'
env.roledefs = {
'staging': staging(),
'prod': 'openoversight.com',
}
def deploy():
with lcd(os.path.dirname(os.path.realpath(__file__))):
with cd(env.code_dir):
run('su %s -c "git fetch && git status"' % env.unprivileged_user)
execute(buildassets)
run('su %s -c "git pull"' % env.unprivileged_user)
run('su %s -c "PATH=%s/bin:$PATH pip install -r requirements.txt"' % (env.unprivileged_user, env.venv_dir))
run('su %s -c "mkdir --parents %s/OpenOversight/app/static/dist"' % (env.unprivileged_user, env.code_dir))
put(local_path=os.path.join('OpenOversight', 'app', 'static', 'dist'),
remote_path=os.path.join(env.code_dir, 'OpenOversight', 'app', 'static')
)
run('sudo systemctl restart openoversight')
def migrate():
execute(deploy)
with cd(env.code_dir):
run('su %s -c "cd OpenOversight; FLASK_APP=OpenOversight.app %s/bin/flask db upgrade"' % (env.unprivileged_user, env.venv_dir))
run('sudo systemctl restart openoversight')
def backup():
with cd(env.backup_dir):
backup_datetime = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
run('%s/bin/python %s/db_backup.py' % (env.venv_dir, env.code_dir))
run('mv backup.sql backup.sql_%s' % backup_datetime)
run('su %s -c "aws s3 sync s3://%s /home/nginx/openoversight_backup/s3/%s"'
% (env.unprivileged_user, env.s3bucket, env.s3bucket))
run('tar czfv backup.tar.gz s3/ backup.sql_%s' % backup_datetime)
get(remote_path="backup.tar.gz",
local_path="./backup/backup-%s-%s.tar.gz"
% (env.environment, backup_datetime))
def buildassets():
with lcd(os.path.dirname(os.path.realpath(__file__))):
local('mkdir -p OpenOversight/app/static/dist ; chmod go+rw OpenOversight/app/static/dist')
local('make cleanassets assets')