This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
98 lines (77 loc) · 2.84 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
from __future__ import with_statement
import os
from os.path import realpath, dirname, basename
from fabric.api import *
from fabric.contrib.files import upload_template
BASE = "/srv"
REPO = "https://github.com/sabf/typo.git"
# env injector. See:
# https://github.com/jacobian/django-dotenv/blob/master/dotenv.py
def _load_env():
dotenv = os.path.join(os.path.dirname(__file__), '.env')
for line in open(dotenv):
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
k, v = line.split('=', 1)
v = v.strip("'").strip('"')
os.environ.setdefault(k, v)
try:
_load_env()
except IOError:
print "Can't load .env file."
default_target = basename(dirname(realpath(__file__)))
TARGET = os.environ.get("FAB_DEPLOY_TARGET", default_target)
## Fab commands
def flask_deploy(rev=None, user="www-data", reset=False):
sudo("rm uwsgi.ini || true")
if reset:
sudo("git reset --hard", user=user)
sudo("git pull", user=user)
branch = 'master' if not rev else rev
if branch:
sudo("git checkout {branch}".format(branch=rev))
sudo("git pull origin {branch}".format(branch=rev))
with prefix(". env/bin/activate"):
sudo("pip install -r requirements.txt", user=user)
sudo("chown -R {user}:{user} .".format(user=user))
sudo("ln -s uwsgi.app uwsgi.ini", user=user)
def restart_service(target):
sudo("service {target} restart".format(target=target))
def update(branch=None, target=None, user='www-data'):
path = os.path.join(BASE, target or TARGET)
with cd(path):
flask_deploy(branch, user)
# let's play it safe...
# restart_service("uwsgi")
# restart_service("nginx")
def setup_venv(user, target=None):
directory = os.path.join(BASE, target or TARGET)
with cd(directory):
sudo("virtualenv env", user=user)
sudo("chown -R {user}:{user} env".format(user=user))
def setup(branch='master', user='www-data', target=None):
target = target or TARGET
directory = os.path.join(BASE, target)
with cd(BASE):
# pull from git repo
destroy(target=target)
sudo("git clone {url} {target}".format(url=REPO, target=target))
sudo("chown -R {user}:{user} .".format(user=user))
setup_venv(user, target)
update(branch, target, user)
def destroy(target=None):
target = target or TARGET
with cd(BASE):
sudo("rm -rf {dst} || true".format(dst=target))
def deploy(branch="master", target=None):
update(branch, target)
def devel(branch="development", target=None):
deploy(branch, target)
def run(port=8080):
with prefix(". env/bin/activate"):
local("python runserver.py 0.0.0.0:%s" % (port,))
def prepare():
local("virtualenv env")
with prefix(". env/bin/activate"):
local("pip install -r requirements.txt")