forked from EHRI/ehri-search-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
138 lines (113 loc) · 4.06 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
"""
Fabric deployment script for EHRI index helper and Solr config.
"""
from __future__ import with_statement
import os
import datetime
import subprocess
from datetime import datetime
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.project import upload_project
from contextlib import contextmanager as _contextmanager
# globals
env.prod = False
env.use_ssh_config = True
env.tool_name = 'index-data-converter'
env.service_name = 'tomcat6'
env.tool_jar_path = '/opt/webapps/docview/bin/indexer.jar'
env.config_path = '/opt/webapps/solr4/ehri/portal/conf'
env.remote_dir = '/opt/webapps/solr4'
env.lib_path = '/opt/webapps/solr4/ehri/lib'
env.data_path = '/opt/webapps/solr4/ehri/portal/data'
env.user = os.getenv("USER")
env.config_files = ["schema.xml", "solrconfig.xml", "*.txt", "lang/*"]
env.solr_admin_url = "http://localhost:8080/ehri/admin"
env.solr_core_name = "portal"
TIMESTAMP_FORMAT = "%Y%m%d%H%M%S"
# environments
def test():
"Use the remote testing server"
env.hosts = ['ehritest']
def stage():
"Use the remote staging server"
env.hosts = ['ehristage']
def prod():
"Use the remote virtual server"
env.hosts = ['ehriprod']
env.prod = True
def deploy():
"""
Deploy the indexer tool, copy the Solr config, set the permissions
correctly, and reload the Solr core.
"""
copy_to_server()
copy_solr_core()
_set_permissions()
reload()
def reload():
"""
Reload Solr config files by restarting the portal core.
"""
run("curl \"%(solr_admin_url)s/cores?action=RELOAD&core=%(solr_core_name)s\"" % env)
def clean_deploy():
"""Do a clean build, deploy the indexer tool, copy the Solr config, set the permissions
correctly, and reload the Solr core."""
local('mvn clean package -DskipTests')
deploy()
def copy_to_server():
"Upload the indexer tool to its target directory"
# Ensure the deployment directory is there...
local_file = _get_tool_jar_file()
if not os.path.exists(local_file):
abort("Jar not found: " + local_file)
put(local_file, env.tool_jar_path)
def copy_solr_core():
"""Copy the Solr core (lib and conf) to the server"""
version = _get_artifact_version()
core_tgz = "solr-config/target/solr-config-%s-solr-core.tar.gz" % version
temp_name = _get_temp_name(suffix = ".tar.gz")
remote_name = os.path.join("/tmp", os.path.basename(temp_name))
put(core_tgz, remote_name)
run("tar zxvf %s -C %s" % (remote_name, env.remote_dir))
run("rm %s" % remote_name)
def copy_config():
"""Copy the Solr config files to the server"""
with lcd("solr-config/solr/portal/conf"):
for f in env.config_files:
put(f, os.path.join(env.config_path, os.path.dirname(f)))
def start():
"Start Tomcat"
_run_service_cmd("start")
def stop():
"Stop Tomcat"
_run_service_cmd("stop")
def restart():
"Restart Tomcat"
_run_service_cmd("restart")
def _set_permissions():
"""Set the currect permissions on the config files."""
run("chown -RH %s.webadm %s" % (env.user, env.config_path))
run("chmod -R g+w %s" % env.config_path)
def _get_tool_jar_file():
version = _get_artifact_version()
tool_file = "%s-%s-jar-with-dependencies.jar" % (env.tool_name, version)
return os.path.join(env.tool_name, "target", tool_file)
def _run_service_cmd(name):
# NB: This doesn't use sudo() directly because it insists on asking
# for a password, even though we should have NOPASSWD in visudo.
with settings(service_task=name):
run('sudo service %(service_name)s %(service_task)s' % env, pty=False, shell=False)
def _get_artifact_version():
"""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+:)'",
capture=True).strip()
def _get_temp_name(suffix):
"""Get a temporary file name"""
import tempfile
tf = tempfile.NamedTemporaryFile(suffix=suffix)
name = tf.name
tf.close()
return name