-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabfile.py
100 lines (80 loc) · 2.63 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
"""
Note: Requires fabric<2.0
"""
import logging
import os
import yaml
from fabric.api import lcd, task, local
from shutil import rmtree
USE_RSYNC_PROJECT = True
if USE_RSYNC_PROJECT:
from fabric.api import env
from fabric.contrib.project import rsync_project
from functools import partial
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger()
repo_root = local("git rev-parse --show-toplevel", capture=True)
workspace = local("mktemp -d", capture=True)
conf_file = "deploy.yaml"
ignore = [".git", "fabfile.py", "cache", "config", "*.log"]
try:
conf = yaml.load(
open(os.path.join(repo_root, conf_file), "rb").read(),
Loader=yaml.Loader,
)
except:
log.exception("error: unable to read {} config file:".format(conf_file))
raise
env.user = conf["user"]
env.hosts = ["{}@{}:22".format(env.user, host) for host in conf["hosts"]]
def export():
"""Exports repository's master branch to a temporary workspace."""
with lcd(repo_root):
local("git archive master | tar -x -C " + workspace)
def deploy_project(local_dir, remote_dir, exclude=[]):
"""Deploy the entire project at local_dir to remote_dir, excluding the given paths."""
export()
if USE_RSYNC_PROJECT:
sync = partial(
rsync_project,
remote_dir=remote_dir,
exclude=exclude,
delete=True,
extra_opts="-e 'ssh -l {}'".format(conf["user"]),
)
else:
exclude = [".git", "fabfile.py", "cache", "config", "*.log", "js", "image"]
cmd = "rsync -pthrvz --delete"
cmd = (
cmd
+ " {exclude} --rsh='ssh -p 22 ' -e 'ssh -l {user}' {local_dir} {host}:{remote_dir}"
)
cmd_params = {
"user": conf["user"],
"host": conf["host"],
"remote_dir": remote_dir,
"exclude": " ".join("--exclude '{}'".format(x) for x in exclude),
}
def sync(local_dir):
cmd_params["local_dir"] = local_dir
local(cmd.format(**cmd_params))
try:
with lcd(workspace):
with lcd(local_dir):
sync(local_dir=".")
sync(local_dir="resources")
except:
log.exception("deployment error:")
raise
finally:
rmtree(workspace)
@task
def publish():
"""Publishes web implementation and resources to remote server."""
deploy_project("web", conf["web_remote_dir"], exclude=ignore + ["template"])
@task
def deploy():
"""Deploys notification script to remote server."""
deploy_project(
"script", conf["script_remote_dir"], exclude=ignore + ["js", "image"]
)