This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fabfile.py
86 lines (63 loc) · 2.5 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
from fabric.api import run, cd, env, sudo, local, shell_env
from fabric.contrib.files import sed
env.hosts = ['ubuntu@ec2-54-251-215-76.ap-southeast-1.compute.amazonaws.com']
env.key_filename = ['/home/noufal/.ssh/id_rsa_pratham']
app_repository = "https://github.com/PrathamBooks/frp.git"
app_base = "/opt/frp"
deployments_dir = "{0}/deployments".format(app_base)
currently_deployed_dir = "{0}/deployed".format(app_base)
version_file = "{0}/frp/frp/_version.py".format(currently_deployed_dir)
production_config_file = "/opt/config/deployed/frp/production.py"
app_production_config_file = "{0}/frp/frp/settings/production.py".format(
currently_deployed_dir)
virtualenv_home = "/opt/frp/environments"
app_virtualenv = "frp-app"
# Remote commands
def obtain_code(tag):
with cd(deployments_dir):
run("rm -Rf {0}".format(tag))
run("git clone -q {0} {1}".format(app_repository, tag))
run("cd {0}/{1} && git checkout {1}".format(deployments_dir, tag))
def set_deployed_version(tag):
with cd(app_base):
run("rm -f {0}".format(currently_deployed_dir))
run("ln -s {0}/{1} {2}".format(deployments_dir, tag,
currently_deployed_dir))
# Write version file which will get loaded by the application.
sed(version_file, "development", tag)
def update_virtualenv(tag):
with cd(app_base):
run(". {0}/{1}/bin/activate && "
"pip install -M -r {2}/requirements/production.txt".format(
virtualenv_home, app_virtualenv, currently_deployed_dir))
def monit_restart():
sudo("monit restart all")
def reset_database():
with cd(currently_deployed_dir), shell_env(FRP_CONFIG="settings/production.py"):
run(". {0}/{1}/bin/activate && python frp/manage.py resetdb".format(
virtualenv_home, app_virtualenv))
def fix_config():
with cd(currently_deployed_dir):
run("ln -s {0} {1}".format(production_config_file,
app_production_config_file))
# Local commands
def push_code():
local("git push --force")
local("git push --tags")
# Public commands
def deploy(tag, venv=False, resetdb=False):
"Deploy the application tagged by :tag:"
push_code()
obtain_code(tag)
set_deployed_version(tag)
fix_config()
if venv:
update_virtualenv(tag)
if resetdb:
reset_database()
monit_restart()
def rollback(tag):
"Roll back to given tag"
set_deployed_version(tag)
# update_monit_config()
monit_restart()