forked from EHRI/ehri-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
114 lines (88 loc) · 3.44 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
"""
Fabric deployment script for EHRI backend webapp.
"""
import os
import sys
from datetime import datetime
from fabric import task
from invoke import run as local
from patchwork import files
deploys_dir = "/opt/neo4j/ehri-rest/deploys"
target_link = "/opt/neo4j/plugins/ehri-data.jar"
neo4j_install = "/opt/neo4j/current"
@task
def deploy(ctx, clean=False):
"""Build (optionally with clean) and deploy the distribution"""
local("mvn package -DskipTests" if not clean else "mvn clean package -DskipTests")
artifact_version = get_artifact_version(ctx)
version = get_version_stamp(ctx)
src = f"build/target/ehri-data-{artifact_version}.jar"
dst = f"{deploys_dir}/ehri-data-{version}.jar"
ctx.put(src, remote=dst)
symlink_target(ctx, dst, target_link)
restart(ctx)
@task
def rollback(ctx):
"""Set the current version to the previous version directory"""
output = ctx.run(f"ls -1rt {deploys_dir} | tail -n 2 | head -n 1").stdout.strip()
if output == "":
raise Exception("Unable to get previous version for rollback!")
symlink_target(ctx, f"{deploys_dir}/{output}", target_link)
restart(ctx)
@task
def latest(ctx):
"""Set the current version to the latest version directory"""
output = ctx.run(f"ls -1rt {deploys_dir} | tail -n 1").stdout.strip()
if output == "":
raise Exception("Unable to get previous version for rollback!")
symlink_target(ctx, f"{deploys_dir}/{output}", target_link)
restart(ctx)
@task
def get_artifact_version(ctx):
"""Get the current artifact version from Maven"""
return local(
"mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate" +
" -Dexpression=project.version|grep -Ev '(^\[|Download\w+:)'").stdout.strip()
@task
def symlink_target(ctx, source, target):
"""Symlink a version directory"""
ctx.run(f"ln --force --no-dereference --symbolic {source} {target}")
ctx.run(f"chgrp -R webadm {target_link}")
@task
def restart(ctx):
"""Restart the Neo4j process"""
if input("Restart Neo4j (y/n)\n").lower() == "y":
ctx.run("sudo service neo4j restart")
@task
def get_version_stamp(ctx):
"""Get the tag for a version, consisting of the current time and git revision"""
res = local("git rev-parse --short HEAD").stdout.strip()
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
return f"{timestamp}_{res}"
@task
def online_backup(ctx, dir, tar = True):
"""Create an online backup to directory `dir`"""
if files.exists(ctx, dir):
print(f"Remote directory '{dir}' already exists!")
sys.exit(1)
tar_name = dir + ".tgz"
dirname = os.path.dirname(dir)
basename = os.path.basename(dir)
ctx.run(f"{neo4j_install}/bin/neo4j-admin backup --from localhost:6362 --name={basename} --backup-dir {dirname}")
if tar:
ctx.run(f"tar --create --gzip --file {tar_name} -C {dir} .")
ctx.run(f"rm -rf {dir}")
@task
def online_clone_db(ctx, dir):
"""Create an online backup of the database and download to local dir `dir`"""
timestamp = get_timestamp()
tmpdst = "/tmp/" + timestamp
online_backup(ctx, tmpdst, tar = False)
ctx.run(f"tar --create --gzip --file {tmpdst}.tgz -C {tmpdst} .")
ctx.get(f"{tmpdst}.tgz", f"{tmpdst}.tgz")
ctx.run(f"rm -rf {tmpdst} {tmpdst}.tgz")
local(f"mkdir -p {dir}")
local(f"tar xf {tmpdst}.tgz -C {dir}")
local(f"rm {tmpdst}.tgz")
def get_timestamp():
return datetime.now().strftime("%Y%m%d%H%M%S")