forked from inasafe/inasafe-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
250 lines (213 loc) · 8.39 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# ~/fabfile.py
# A Fabric file for carrying out various administrative tasks with InaSAFE.
# Tim Sutton, Jan 2013
import os
from fabric.api import *
from datetime import datetime
from fabric.contrib.files import contains, exists, append, sed
import fabtools
from fabtools import require
from fabgis import fabgis
# Don't remove even though its unused
from fabtools.vagrant import vagrant
# Usage for localhost commands:
# fab localhost [command]
# e.g. fab localhost update
# To run remotely do
# fab remote [command]
# e.g.
# fab -H 188.40.123.80:8697 show_environment
# To run on a vagrant vhost do
# fab vagrant show_environment
# e.g. fab vagrant show_environment
#
# Note: Vagrant tasks will only run if they @task decorator is used on
# the function. See show_environment function below.
# Usage fab localhost [command]
# or fab remote [command]
# e.g. fab localhost initialise_qgis_plugin_repo
# Global options
env.env_set = False
def _setup_env():
"""Things to do regardless of whether command is local or remote."""
if env.env_set:
fastprint('Environment already set!\n')
return
fastprint('Setting environment!\n')
# Key is hostname as it resolves by running hostname directly on the server
# value is desired web site url to publish the repo as.
doc_site_names = {
'cunonia': 'docs.localhost',
'waterfall': 'docs.localhost',
'spur': '-docs.localhost',
'maps.linfiniti.com': 'inasafe.linfiniti.com',
'linfiniti': 'docs.linfiniti.com',
#vagrant instance
'vagrant-inasafe-doc': 'docs.vagrant.localhost',
'shiva': 'docs.inasafe.org'}
with hide('output'):
env.user = run('whoami')
env.hostname = run('hostname')
if env.hostname not in doc_site_names:
print 'Error: %s not in: \n%s' % (env.hostname, doc_site_names)
exit()
else:
env.doc_site_name = doc_site_names[env.hostname]
env.inasafe_release_web_path = (
'\/var\/www\/inasafe-release-documentation')
env.inasafe_master_web_path = (
'\/var\/www\/inasafe-master-documentation')
env.home = os.path.join('/home/', env.user)
env.repo_path = os.path.join(env.home, 'dev', 'python')
env.git_url = 'git://github.com/AIFDR/inasafe-doc.git'
env.repo_alias = 'inasafe-doc'
env.code_path = os.path.join(env.repo_path, env.repo_alias)
env.env_set = True
fastprint('env.env_set = %s' % env.env_set)
###############################################################################
# Next section contains helper methods
###############################################################################
def get_webdir(branch):
if 'master' == branch:
webdir = env.inasafe_master_web_path
else:
webdir = env.inasafe_release_web_path
return webdir
@task
def build_docs(branch='master'):
"""Create a pdf and html doc tree and publish them online.
Args:
branch: str - a string representing the name of the branch to build
from. Defaults to 'master'.
To run e.g.::
fab -H 188.40.123.80:8697 build_docs
or to package up a specific branch (in this case minimum_needs)
fab -H 88.198.36.154:8697 build_docs:version-1_1
.. note:: Using the branch option will not work for branches older than 1.1
"""
_setup_env()
fabgis.setup_inasafe()
# Needed for when running on headless servers
fabtools.require.deb.package('xvfb')
fabgis.install_qgis1_8()
sudo('pip install Sphinx')
fabgis.update_git_checkout(
code_path=env.repo_path,
url=env.git_url,
repo_alias=env.repo_alias,
branch=branch)
fabgis.setup_latex()
dir_name = os.path.join(env.repo_path, env.repo_alias)
with cd(dir_name):
# build the Documentation
run('chmod +x scripts/post_translate.sh')
run('xvfb-run scripts/post_translate.sh')
@task
def setup_docs_webpage(branch='master'):
"""Initialise an InaSAFE docs site where we host docs and pdf."""
_setup_env()
build_docs()
fabtools.require.deb.package('apache2')
code_path = env.code_path
webdir = get_webdir(branch)
# if hostname = jenkins or vagrant or livesite then
# webdir = /var/www/inasafe-documentation
# for now we set dir just for jenkins anyway
# probably we should just set a symlink
jenkins_webdir = '/var/www/inasafe-documentation'
if not exists(jenkins_webdir):
sudo('mkdir -p %s/pdf' % jenkins_webdir)
sudo('chown -R %s.%s %s' % (env.user, env.user, jenkins_webdir))
sudo('chmod -R 777 %s' % jenkins_webdir)
inasafe_docs_apache_conf = '%s.inasafe-docs.conf' % branch
inasafe_docs_apache_conf_template = 'inasafe-docs.conf.templ'
if not exists(webdir):
sudo('mkdir -p %s/pdf' % webdir)
sudo('chown -R %s.%s %s' % (env.user, env.user, webdir))
apache_path = '/etc/apache2/sites-available/'
with cd(apache_path):
if not exists(inasafe_docs_apache_conf):
local_dir = os.path.dirname(__file__)
local_file = os.path.abspath(os.path.join(
local_dir,
'scripts',
inasafe_docs_apache_conf_template))
put(local_file,
"/etc/apache2/sites-available/%s" %
inasafe_docs_apache_conf_template,
use_sudo=True)
my_tokens = {
'SERVERNAME': env.doc_site_name, # Web Url e.g. foo.com
'WEBMASTER': '[email protected]', # email of web master
'DOCUMENTROOT': webdir, # Content root .e.g. /var/www
'SITENAME': 'inasafe-docs', # Choosen name of jenkins 'root'
}
fabgis.replace_tokens(inasafe_docs_apache_conf_template, my_tokens)
sudo('rm *.templ')
with cd(code_path):
# Copy built Documentation to the Webserver path
run('cp -r docs/output/html/* %s' % webdir)
run('cp -r docs/output/pdf %s' % webdir)
run('cp scripts/.htaccess %s' % webdir)
run('cp scripts/directory*.html %s/en/_static/' % webdir)
# Add a hosts entry for local testing - only really useful for localhost
hosts = '/etc/hosts'
if not contains(hosts, 'inasafe-docs'):
append(hosts, '127.0.0.1 %s' % env.doc_site_name, use_sudo=True)
sudo('a2ensite inasafe-docs.conf')
sudo('a2enmod rewrite')
sudo('service apache2 reload')
@task
def setup_jenkins(use_upstream_repo=True, branch='master'):
"""
:param use_upstream_repo:
:param branch:
:return:
"""
_setup_env()
# We need some additional tools to run jenkins checks for documentation
fabtools.require.deb.package('xvfb')
fabtools.require.deb.package('pep8')
fabtools.require.deb.package('pylint')
fabtools.require.deb.package('pyflakes')
fabtools.require.deb.package('sloccount')
fabgis.jenkins_deploy_website(use_upstream_repo=use_upstream_repo)
fabgis.install_jenkins(use_upstream_repo)
setup_jenkins_jobs(branch=branch)
@task
def setup_jenkins_jobs(branch='master'):
"""
uploads the prepared jobs into jenkins for testing
:param branch: which branch of the documentations
:return:
"""
_setup_env()
xvfb_config = "org.jenkinsci.plugins.xvfb.XvfbBuildWrapper.xml"
job_dir = ['InaSAFE-Documentation']
with cd('/var/lib/jenkins/'):
if not exists(xvfb_config):
local_dir = os.path.dirname(__file__)
local_file = os.path.abspath(os.path.join(
local_dir,
'scripts',
'jenkins_jobs',
xvfb_config))
put(local_file,
"/var/lib/jenkins/", use_sudo=True)
with cd('/var/lib/jenkins/jobs/'):
for job in job_dir:
if not exists(job):
local_dir = os.path.dirname(__file__)
local_job_file = os.path.abspath(os.path.join(
local_dir,
'scripts',
'jenkins_jobs',
'%s.xml') % job)
sudo('mkdir /var/lib/jenkins/jobs/%s' % job)
put(local_job_file,
"/var/lib/jenkins/jobs/%s/config.xml" % job,
use_sudo=True)
sudo('chown -R jenkins:nogroup InaSAFE*')
sudo('service jenkins restart')
webdir = get_webdir(branch)
fabtools.require.directory(webdir, use_sudo=True, owner='jenkins')