-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.py
707 lines (561 loc) · 18.4 KB
/
install.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Cherokee easy-install script
#
# Authors:
# Alvaro Lopez Ortega <[email protected]>
#
# Copyright (C) 2001-2011 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import re
import os
import sys
import glob
import time
import stat
import subprocess
BUILD_DIR = "/var/tmp/cherokee-build"
URL_LATEST_RELEASE = "http://www.cherokee-project.com/cherokee-latest-tarball"
URL_SNAPSHOT_RELEASE = "http://www.cherokee-project.com/download/trunk/cherokee-latest-svn.tar.gz"
PREFIX_STANDARD = "/opt/cherokee"
PREFIX_DEVEL = "/opt/cherokee-dev"
PHASE_DOWNLOAD = 1
PHASE_UNPACK = 2
PHASE_COMPILE = 3
PHASE_INSTALL = 4
PHASE_INITD = 5
PHASE_REPORT = 6
# Texts
#
LAUNCHD_PLIST = """\
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>org.cherokee.webserver</string>
<key>RunAtLoad</key><true/>
<key>ProgramArguments</key><array>
<string>%(prefix)s/sbin/cherokee</string>
</array>
<key>UserName</key>
<string>root</string>
</dict>
</plist>
"""
SOLARIS_SVC = """\
<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<service_bundle type='manifest' name='cherokee'>
<service name='network/http' type='service' version='1'>
<instance name='cherokee' enabled='true'>
<dependency name='loopback' grouping='require_all' restart_on='error' type='service'>
<service_fmri value='svc:/network/loopback:default'/>
</dependency>
<dependency name='physical' grouping='optional_all' restart_on='error' type='service'>
<service_fmri value='svc:/network/physical:default'/>
</dependency>
<exec_method type='method' name='start' exec='%(prefix)s/sbin/cherokee -d' timeout_seconds='60'>
<method_context><method_credential user='root' group='root' /></method_context>
</exec_method>
<exec_method type='method' name='stop' exec='kill `cat %(prefix)s/var/run/cherokee.pid`' timeout_seconds='60'>
<method_context><method_credential user='root' group='root' /></method_context>
</exec_method>
<exec_method type='method' name='refresh' exec='kill -HUP `cat %(prefix)s/var/run/cherokee.pid`' timeout_seconds='60'>
<method_context><method_credential user='root' group='root' /></method_context>
</exec_method>
<property_group name='startd' type='framework'>
<propval name='duration' type='astring' value='contract'/>
<propval name='ignore_error' type='astring' value='core,signal' />
</property_group>
</instance>
<template>
<common_name><loctext xml:lang='C'>Advanced and Fast Web Server</loctext></common_name>
<documentation>
<doc_link name='www.cherokee-project.com' uri='http://www.cherokee-project.com/doc/' />
</documentation>
</template>
</service>
</service_bundle>
"""
INITD_SH = """\
#!/bin/sh -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin:%(prefix)s/sbin:%(prefix)s/bin
DAEMON=%(prefix)s/sbin/cherokee
NAME=cherokee
PIDFILE=%(prefix)s/var/run/cherokee.pid
set -e
test -x $DAEMON || exit 0
case "$1" in
start)
%(prefix)s/sbin/cherokee -d
;;
stop)
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
kill $PID
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload|force-reload)
printf "Reloading web server: %%s\t" "$NAME"
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if ps p $PID | grep $NAME >/dev/null 2>&1; then
kill -HUP $PID
else
echo "PID present, but $NAME not found at PID $PID - Cannot reload"
exit 1
fi
else
echo "No PID file present for $NAME - Cannot reload"
exit 1
fi
;;
status)
printf "%%s web server status:\t" "$NAME"
if [ -e $PIDFILE ] ; then
PROCNAME=$(ps -p $(cat $PIDFILE) -o comm=)
if [ "x$PROCNAME" = "x" ]; then
printf "Not running, but PID file present \t"
else
if [ "$PROCNAME" = "$NAME" ]; then
printf "Running\t"
else
printf "PID file points to process '%%s', not '%%s'\t" "$PROCNAME" "$NAME"
fi
fi
else
if PID=$(pidofproc cherokee); then
printf "Running (PID %%s), but PIDFILE not present\t" "$PID"
else
printf "Not running\t"
fi
fi
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
exit 1
;;
esac
if [ $? = 0 ]; then
echo .
exit 0
else
echo failed
exit 1
fi
exit 0
"""
BSD_INIT = """\
#!/bin/sh
. /etc/rc.subr
name="cherokee"
rcvar="`set_rcvar`"
command="%(prefix)s/sbin/cherokee"
load_rc_config $name
command_args="-d"
run_rc_command "$1"
"""
# Globals
#
prefix = PREFIX_STANDARD
start_at = PHASE_DOWNLOAD
download_snapshot = False
devel_build = False
# ANSI Colors
#
ESC = chr(27) + '['
RESET = '%s0m' %(ESC)
def green (s): return ESC + '0;32m' + s + RESET
def red (s): return ESC + '0;31m' + s + RESET
def yellow (s): return ESC + '1;33m' + s + RESET
def blue (s): return ESC + '0;34m' + s + RESET
# Utilities
#
def FATAL_error (error, retcode=1):
print (red(error))
sys.exit (retcode)
def exe (cmd, colorer=lambda x: x, cd=None, stdin=None, return_fatal=True):
print (yellow(cmd))
stdout = ''
kwargs = {'shell': True, 'stdout': subprocess.PIPE, 'cwd': cd}
if stdin:
kwargs['stdin'] = subprocess.PIPE
p = subprocess.Popen (cmd, **kwargs)
if stdin:
try:
p.stdin.write (stdin)
p.stdin.close()
except IOError:
pass
while True:
line = p.stdout.readline()
if not line:
break
line = line.decode('utf-8')
stdout += line
print (colorer (line.rstrip('\n\r')))
p.wait()
# Return
if p.returncode != 0 and return_fatal:
print ('\n%s: Could not execute: %s' %(red('ERROR'), cmd))
return {'stdout': stdout,
'retcode': p.returncode}
_root_password = None
def get_root_password():
global _root_password
while not _root_password:
_root_password = read_input ("root's password: ")
if _root_password:
_root_password += '\n'
return _root_password
def exe_sudo (cmd, **kwargs):
if os.getuid() != 0:
root_password = get_root_password()
kwargs['stdin'] = root_password
cmd = "sudo -S " + cmd
return exe (cmd, **kwargs)
def which (program):
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def rm (path):
return exe ("rm -rf '%s'" %(path), red)
def mkdir (path):
return exe ("mkdir -p '%s'" %(path), blue)
def download (url, target_file):
# Wget
wget_bin = which ('wget')
if wget_bin:
ret = exe ("wget '%(url)s' --output-document='%(target_file)s'" %(locals()))
if ret['retcode'] == 0:
return
# Curl
curl_bin = which ('curl')
if curl_bin:
ret = exe ("curl '%(url)s' --output '%(target_file)s'" %(locals()))
if ret['retcode'] == 0:
return
# Python
import urllib2
print ("Downloading %s" %(url))
i = urllib2.urlopen (url)
o = open (target_file, 'w+')
o.write (i.read())
def read_input (prompt):
try:
# Python 2.x
return raw_input (prompt)
except NameError:
# Python 3.x
return input (prompt)
def read_yes_no (prompt, empty_is=None):
while True:
ret = read_input (prompt).lower()
if ret in ('y','yes'):
return True
if ret in ('n','no'):
return False
if not ret and empty_is != None:
return empty_is
def figure_initd_app_level (directory, app, not_found=99):
files = [x.lower() for x in os.listdir(directory)]
for filename in files:
tmp = re.findall (r's(\d+)(.+)', filename)
if not tmp: continue
if tmp[0][1] == app:
return tmp[0][0]
return not_found
def make_path():
return which("gmake") or which("make")
# Cherokee
#
def cherokee_download (tar_file):
# Clean up
rm (BUILD_DIR)
rm (tar_file)
mkdir (BUILD_DIR)
# Download
if download_snapshot:
url = URL_SNAPSHOT_RELEASE
else:
url = URL_LATEST_RELEASE
download (url, tar_file)
def cherokee_find_unpacked():
for f in os.listdir (BUILD_DIR):
fp = os.path.join (BUILD_DIR, f)
tmp = re.findall (r'cherokee-(\d+\.\d+\.\d+)', f)
if tmp and os.path.isdir (fp):
return fp
def cherokee_unpack (latest_local):
# Unpack
ret = exe ("gzip -dc '%s' | tar xfv -" %(latest_local), cd=BUILD_DIR)
if ret['retcode'] != 0:
return None
# Look for the src directory
path = cherokee_find_unpacked()
if not path:
return
# Works around clock issues
touch = False
for f in [os.path.join (path, x) for x in os.listdir (path)]:
if os.stat(f)[stat.ST_MTIME] > time.time():
touch = True
break
if touch:
exe ("find '%s' -exec touch '{}' \;" %(path))
return path
def cherokee_compile (src_dir):
params="--prefix='%s'" %(prefix)
# Look for gettext
if not which ("msgfmt"):
params += " --enable-nls=no"
# Snaphost
if download_snapshot:
params += " --enable-beta"
# Trace
if devel_build:
params += " --enable-trace"
params += " CFLAGS='-ggdb3 -O0'"
# Configure
ret = exe ("./configure " + params, cd=src_dir)
if ret['retcode'] != 0:
return True
# Build
ret = exe (make_path(), cd=src_dir)
if ret['retcode'] != 0:
return True
def cherokee_install (src_dir):
if os.access (prefix, os.W_OK):
ret = exe ("%s install" %(make_path()), cd=src_dir)
else:
ret = exe_sudo ("%s install" %(make_path()), cd=src_dir)
if ret['retcode'] != 0:
return True
def cherokee_set_initd():
print('')
proceed = read_yes_no ("Do you want Cherokee to be started at boot time? [Y/n] ", True)
if not proceed:
return
variables = globals()
variables.update (locals())
# MacOS X
if sys.platform == 'darwin':
tmp_fp = os.path.join (BUILD_DIR, "launchd-cherokee.plist")
plist_fp = os.path.join (prefix, "launchd-cherokee.plist")
# Write the plist file
txt = LAUNCHD_PLIST %(variables)
f = open (tmp_fp, 'w+')
f.write (txt)
f.close()
# Permissions
exe_sudo ("cp '%s' '%s'" %(tmp_fp, plist_fp))
exe_sudo ("chown root '%s'" %(plist_fp))
exe_sudo ("chgrp admin '%s'" %(plist_fp))
# Let launchd know about it
exe_sudo ("launchctl unload -w '%s'" %(plist_fp))
exe_sudo ("launchctl load -w '%s'" %(plist_fp))
exe_sudo ("launchctl start org.cherokee.webserver")
return
# Solaris
if sys.platform.startswith('sunos'):
def smf_present():
return (os.access ("/etc/svc/volatile/repository_door", os.R_OK) and
not os.path.isfile ("/etc/svc/volatile/repository_door"))
variables['prefix_var'] = os.path.join (prefix, "var")
tmp_fp = os.path.join (BUILD_DIR, "http-cherokee.xml")
xml_fp = "/var/svc/manifest/network/http-cherokee.xml"
# Write the plist file
txt = SOLARIS_SVC %(variables)
f = open (tmp_fp, 'w+')
f.write (txt)
f.close()
# Permissions
exe_sudo ("cp '%s' '%s'" %(tmp_fp, xml_fp))
exe_sudo ("chown root '%s'" %(xml_fp))
exe_sudo ("chgrp sys '%s'" %(xml_fp))
# Let launchd know about it
if smf_present():
exe_sudo ("/usr/sbin/svccfg import '%s'" %(xml_fp))
exe_sudo ("/usr/sbin/svcadm enable svc:/network/http:cherokee")
else:
print ("INFO: Skipping SVC, SMF not present")
return
# BSD
if 'bsd' in sys.platform.lower():
rcd_fp = '/etc/rc.d/cherokee'
# Preliminary clean up
exe_sudo ("rm -f '%s'"%(rcd_fp))
# Write the init.d file
txt = BSD_INIT %(variables)
f = open (rcd_fp, 'w+')
f.write (txt)
f.close()
# Permissions
exe_sudo ("chown root '%s'" %(rcd_fp))
exe_sudo ("chmod 555 '%s'" %(rcd_fp))
return
# Init.d
if os.path.isdir ("/etc/init.d"):
# Figure runlevel
ret = exe ("runlevel")
tmp = re.findall (r'(\d+)', ret['stdout'])
if not tmp:
print (red ("Could not figure the current runlevel. Skiping step."))
return
runlevel = tmp[0]
# Figure rc<X>.d directory:
# /etc/rc2.d
# /etc/init.d/rc2.d
rc_paths = ('/etc/rc%s.d'%(runlevel), '/etc/init.d/rc%s.d'%(runlevel))
rc_dir = None
for d in rc_paths:
if os.path.isdir (d):
rc_dir = d
break
assert rc_dir, "Unknow init.d layout"
# Build paths
tmp_fp = os.path.join (BUILD_DIR, "cherokee.initd")
sh_fp = os.path.join (prefix, "cherokee.initd")
initd_fp = "/etc/init.d/cherokee-opt"
# Figure rc2.d file level
level = 99
for k in ('apache', 'apache2', 'httpd', 'lighttpd', 'nginx'):
level = min (level, figure_initd_app_level (rc_dir, k))
rcS_fp = os.path.join (rc_dir, "S%02dcherkee-opt"%(level-1))
rcK_fp = os.path.join (rc_dir, "K%02dcherkee-opt"%(level-1))
# Preliminary clean up
exe_sudo ("rm -f '%s' '%s' '%s' '%s' '%s'" %(tmp_fp, sh_fp, initd_fp, rcS_fp, rcK_fp))
# Write the init.d file
txt = INITD_SH %(variables)
f = open (tmp_fp, 'w+')
f.write (txt)
f.close()
# Permissions
exe_sudo ("cp '%s' '%s'" %(tmp_fp, sh_fp))
exe_sudo ("chown root '%s'" %(sh_fp))
exe_sudo ("chmod 755 '%s'" %(sh_fp))
# Add it
exe_sudo ("ln -s '%s' '%s'" %(sh_fp, initd_fp)) # /etc/init.d/cherokee -> /opt/..
exe_sudo ("ln -s '%s' '%s'" %(initd_fp, rcS_fp)) # /etc/rc2.d/S99cherokee -> /etc/init.d/..
exe_sudo ("ln -s '%s' '%s'" %(initd_fp, rcK_fp)) # /etc/rc2.d/K99cherokee -> /etc/init.d/..
def cherokee_report():
cherokee_fp = os.path.join (prefix, "sbin", "cherokee")
print (blue ("Technical details:"))
exe ("%s -i" %(cherokee_fp))
print (blue ("How to:"))
print (" - Launch manually the server: %s/sbin/cherokee -d" %(prefix))
print (" - Launch the administration GUI: %s/bin/cherokee-admin-launcher" %(prefix))
# Main
#
def main():
tar_file = os.path.join (BUILD_DIR, "cherokee-latest.tar.gz")
if start_at <= PHASE_DOWNLOAD:
cherokee_download (tar_file)
if start_at <= PHASE_UNPACK:
src_dir = cherokee_unpack (tar_file)
if not src_dir: return
else:
src_dir = cherokee_find_unpacked()
if not src_dir: return
if start_at <= PHASE_COMPILE:
error = cherokee_compile (src_dir)
if error: return
if start_at <= PHASE_INSTALL:
error = cherokee_install (src_dir)
if error: return
if start_at <= PHASE_INITD:
cherokee_set_initd()
if start_at <= PHASE_REPORT:
cherokee_report ()
def check_prerequisites():
# Check for a C compiler
if not which("gcc") and not which("cc"):
if sys.platform == 'sunos5':
cont = read_yes_no ("SUNWgcc must be installed. Proceed? [Y/n] ", True)
if not cont:
raise SystemExit
exe ("pkg install SUNWgcc")
else:
FATAL_error ("A C compiler is required")
# Check for Python
if not which("env"):
FATAL_error ("'env' is required")
ret = exe ("env python -V")
if ret['retcode'] != 0:
FATAL_error ("Python is not in the path")
# Check for make
if not make_path():
FATAL_error ("'make' or 'gmake' is required for the compilation")
def process_parameters():
global start_at
global download_snapshot
global devel_build
global prefix
if '--help' in sys.argv:
print ("Cherokee's assisted deployment script:")
print (" USAGE: python install.py [params]")
print ("")
print (" --snapshot Compile latest development snapshot")
print (" --devel snapshot w/ debug under cherokee-dev")
print ("")
print (" Development:")
print (" --from-unpack Start at the 'unpack' phase")
print (" --from-compile Start at the 'compilation' phase")
print (" --from-install Start at the 'install' phase")
print (" --from-initd Start at the 'initd' phase")
print (" --from-report Start at the 'report' phase")
print ("")
print ("Report bugs to: http://bugs.cherokee-project.com/")
raise SystemExit
# Development
if '--snapshot' in sys.argv:
download_snapshot = True
if '--devel' in sys.argv:
devel_build = True
download_snapshot = True
prefix = PREFIX_DEVEL
# Script development
if '--from-unpack' in sys.argv:
start_at = PHASE_UNPACK
if '--from-compile' in sys.argv:
start_at = PHASE_COMPILE
if '--from-install' in sys.argv:
start_at = PHASE_INSTALL
if '--from-initd' in sys.argv:
start_at = PHASE_INITD
if '--from-report' in sys.argv:
start_at = PHASE_REPORT
if __name__ == '__main__':
process_parameters()
check_prerequisites()
main()