-
Notifications
You must be signed in to change notification settings - Fork 6
/
fabfile.py
140 lines (112 loc) · 3.85 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
from patchwork.transfers import rsync
from fabric import task
from fabric.connection import Connection
from dotenv import dotenv_values
from pathlib import Path
def get_active_branch_name():
head_dir = Path(".") / ".git" / "HEAD"
with head_dir.open("r") as f: content = f.read().splitlines()
for line in content:
if line[0:4] == "ref:":
return line.partition("refs/heads/")[2]
config = dotenv_values(".env")
project = "watchtower"
@task
def chipnet(ctx):
ctx.config.network = 'chipnet'
ctx.config.project_dir = f'/root/{project}'
ctx.config.run.env['conn'] = Connection(
config['CHIPNET_SERVER_HOST'],
user=config['CHIPNET_SERVER_USER']
)
@task
def mainnet(ctx):
branch = get_active_branch_name()
if branch == 'master':
ctx.config.network = 'mainnet'
ctx.config.project_dir = f'/root/{project}'
ctx.config.run.env['conn'] = Connection(
config['MAINNET_SERVER_HOST'],
user=config['MAINNET_SERVER_USER']
)
else:
print(f'\nAborted: {branch} branch is not allowed to access mainnet server!\n')
@task
def uname(ctx):
if 'network' not in ctx.config.keys(): return
conn = ctx.config.run.env['conn']
conn.run('uname -a')
@task
def sync(ctx):
if 'network' not in ctx.config.keys(): return
conn = ctx.config.run.env['conn']
rsync(
conn,
'.',
ctx.config.project_dir,
exclude=[
'.venv',
'.git',
'/static',
'.DS_Store',
'__pycache__',
'*.pyc',
'*.log',
'*.pid'
]
)
with conn.cd(ctx.config.project_dir):
conn.run(f'cat compose/.env_{ctx.config.network} >> .env')
@task
def build(ctx):
if 'network' not in ctx.config.keys(): return
conn = ctx.config.run.env['conn']
with conn.cd(ctx.config.project_dir):
conn.run(f'docker-compose -f compose/{ctx.config.network}.yml --env-file {ctx.config.project_dir}/.env build')
@task
def up(ctx):
if 'network' not in ctx.config.keys(): return
conn = ctx.config.run.env['conn']
with conn.cd(ctx.config.project_dir):
conn.run(f'docker-compose -f compose/{ctx.config.network}.yml --env-file {ctx.config.project_dir}/.env up -d')
@task
def down(ctx):
if 'network' not in ctx.config.keys(): return
conn = ctx.config.run.env['conn']
with conn.cd(ctx.config.project_dir):
conn.run(f'docker-compose -f compose/{ctx.config.network}.yml --env-file {ctx.config.project_dir}/.env down --remove-orphans')
@task
def deploy(ctx):
if 'network' not in ctx.config.keys(): return
sync(ctx)
build(ctx)
down(ctx)
up(ctx)
@task
def nginx(ctx):
if 'network' not in ctx.config.keys(): return
sync(ctx)
conn = ctx.config.run.env['conn']
with conn.cd(f'{ctx.config.project_dir}/compose'):
nginx_conf = f"/etc/nginx/sites-available/{project}"
nginx_slink = f"/etc/nginx/sites-enabled/{project}"
try:
conn.run(f'sudo rm {nginx_conf}')
conn.run(f'sudo rm {nginx_slink}')
except:
pass
conn.run(f'sudo cat nginx.conf > {nginx_conf}')
conn.run(f'sudo ln -s {nginx_conf} {nginx_slink}')
conn.run('sudo service nginx restart')
@task
def logs(ctx):
if 'network' not in ctx.config.keys(): return
conn = ctx.config.run.env['conn']
with conn.cd(ctx.config.project_dir):
conn.run(f'docker-compose -f compose/{ctx.config.network}.yml --env-file {ctx.config.project_dir}/.env logs -f web')
@task
def reports(ctx):
if 'network' not in ctx.config.keys(): return
conn = ctx.config.run.env['conn']
with conn.cd(ctx.config.project_dir):
conn.run(f'docker-compose -f compose/{ctx.config.network}.yml --env-file {ctx.config.project_dir}/.env exec -T web python manage.py reports -p paytaca')