-
Notifications
You must be signed in to change notification settings - Fork 155
/
install
executable file
·307 lines (270 loc) · 11.8 KB
/
install
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
#!/usr/bin/env python3
import argparse
import os
import pwd
import shutil
import socket
import subprocess
import sys
################################################################################
## command line parsing
################################################################################
parser = argparse.ArgumentParser(
description='Configure script for PalMA',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--prefix', default='/usr', help='set install prefix')
parser.add_argument('--sysconfdir', default='/etc', help='set sysconfdir')
parser.add_argument('--libdir', default=argparse.SUPPRESS,
help='set lib directory explicitly (default: PREFIX/lib/palma)')
parser.add_argument('--wwwdir', default=argparse.SUPPRESS,
help='set www root directory explicitly (default: PREFIX/share/palma)')
parser.add_argument('--browser', default='chromium',
choices=[ 'chromium', 'chromium-browser', 'midori' ],
help='set browser')
parser.add_argument('--dbdir', default='/var/lib/palma',
help='set database directory')
parser.add_argument('--no-deps', action='store_false', dest='deps',
help='install dependencies')
parser.add_argument('--init', choices=['sysvinit','systemd'], default='systemd',
help='set init system for start script')
parser.add_argument('--name', default=argparse.SUPPRESS,
help='set host name (default: %s)' % socket.getfqdn())
parser.add_argument('--server', choices=['apache2','nginx'], default='nginx',
help='set web server')
parser.add_argument('-v', '--verbose', action='store_true',
help='set verbose output')
parser.add_argument('mode', nargs='?', choices=['install','deb', 'deps'],
default='install', help='run mode')
args = parser.parse_args()
################################################################################
## lists of files and dependencies
################################################################################
deps_debian = [
'feh', 'libreoffice', 'ssvnc', 'vlc', 'x11vnc', 'zathura',
'wmctrl', 'xdotool', 'openbox', 'sqlite3', 'unclutter',
'php', 'php-cgi', 'php-cli', 'php-curl', 'php-fpm',
'php-gd', 'php-intl', 'php-sqlite3', 'php-mbstring',
'gettext', 'git', 'libavcodec-extra', 'make', 'wget', 'xorg',
]
files_wwwdir = [
# php
'auth.php', 'globals.php', 'control.php', 'DBConnector.class.php', 'db.php',
'download.php', 'FileHandler.class.php', 'i12n.php', 'index.php',
'login.php', 'logout.php', 'SSVNCDaemon.php', 'upload.php',
# css
'dropzone.min.css',
'palma.css',
'pure-min.css',
# js
'dropzone.min.js',
'jquery.min.js',
]
dirs_wwwdir = [
'font-awesome', 'images', 'locale', 'php-gettext', 'qrcode', 'settings',
'theme',
]
pkgs_server = {
'nginx': [ 'nginx-light' ],
'apache': [ 'apache2', 'libapache2-mod-php' ],
}
################################################################################
## helper functions for configuration file creation
################################################################################
def makedeb_version():
lastcommit = check_output(
['/usr/bin/git', 'log', '--pretty=format:%h', '-n', '1'])
curvers = check_output(
['/usr/bin/git', 'tag', '--contains', lastcommit])
if not len(curvers):
lastvers = check_output(
['/usr/bin/git', 'tag']).strip().split(b'\n').pop()
lastvers = lastvers.replace(b'v',b'')
curvers = "%s+%s" % (lastvers,lastcommit)
return curvers
################################################################################
## helper functions
################################################################################
def printv(*a):
if args.verbose:
print(" ".join(map(str,a)), file=sys.stderr)
def printe(*a):
print(" ".join(map(str,a)), file=sys.stderr)
def mkdir(d):
if not os.path.exists(d):
os.makedirs(d)
def snr(pattern,f):
subprocess.check_call(['/bin/sed', '-i', pattern, f])
################################################################################
## start of script
################################################################################
if 'libdir' not in vars(args):
vars(args)['libdir'] = os.path.join(args.prefix,'lib','palma')
if 'wwwdir' not in vars(args):
vars(args)['wwwdir'] = os.path.join(args.prefix,'share','palma')
if args.verbose:
printv("Settings summary:")
for k,v in sorted(args.__dict__.items()):
printv(" %10s: %s" % (k,repr(v)))
if args.mode == 'deps':
print(" ".join(deps_debian + [args.browser] + pkgs_server[args.server]))
sys.exit(0)
try:
destdir = ''
if args.mode == 'deb':
destdir = os.getenv('DESTDIR', 'build-deb')
printv("Creating debian package in '%s'" % destdir)
if args.mode == 'install' and args.deps:
# install dependencies
if os.path.exists('/usr/bin/apt-get'):
try:
subprocess.check_call(['/usr/bin/apt-get', 'install',
'-y', '--no-install-recommends']
+ deps_debian + [ args.browser ]
+ pkgs_server[args.server])
except Exception:
printe('Error installing dependencies, exiting.')
if args.verbose: raise
sys.exit(1)
if args.mode == 'install':
# stop palma if already installed
if args.init == 'systemd':
if os.path.exists('/bin/systemctl') and \
subprocess.call(
['/bin/systemctl', 'is-active', 'palma'],
subprocess.DEVNULL
) == 0:
printv("Stopping systemd service")
subprocess.call(['/bin/systemctl', 'stop', 'palma'])
# build localization files
subprocess.check_call(['/usr/bin/make'])
# copy files
userstruct = pwd.getpwnam('www-data')
wwwdir = destdir + args.wwwdir
mkdir(wwwdir)
for f in files_wwwdir:
printv("Copying '%s'" % f)
shutil.copy(f,wwwdir)
for d in dirs_wwwdir:
printv("Copying directory '%s'" % d)
trg = os.path.join(wwwdir,d)
if os.path.exists(trg): shutil.rmtree(trg)
shutil.copytree(d,trg,symlinks=True)
printv("Installing 'palma.ini'")
# palma.ini
snr("s,'palma.ini','%s',g" % os.path.join(args.sysconfdir,'palma.ini'),
os.path.join(wwwdir,'globals.php'))
sysconfdir = destdir + args.sysconfdir
mkdir(sysconfdir)
shutil.copy('examples/palma.ini', sysconfdir)
# palma.db
snr('s,palma.db,%s,g' % os.path.join(args.dbdir,'palma.db'),
os.path.join(wwwdir,'DBConnector.class.php'))
dbdir = destdir + args.dbdir
mkdir(dbdir)
if args.mode == 'install':
os.chown(dbdir,userstruct.pw_uid,userstruct.pw_gid)
# configure web server
phpversion = ''
if args.mode == 'deb' or args.server == 'nginx':
if args.mode == 'install':
printv("Creating configuration for nginx")
for root, dirs, files in os.walk('/etc/php'):
for d in dirs:
if not d == 'fpm': continue
phpversion = os.path.basename(root)
break
if not len(phpversion):
printe("Could not determine php version, is php-fpm installed? Exiting")
sys.exit(1)
path = os.path.join(args.sysconfdir,
'php/%s/mods-available' % phpversion)
if not os.path.exists(path):
os.makedirs(path)
fn = os.path.join(path,'palma.ini')
shutil.copyfile('examples/palma.php.ini', fn)
path = os.path.join(args.sysconfdir,
'nginx/sites-available')
if not os.path.exists(path):
os.makedirs(path)
fn = os.path.join(path,'palma')
shutil.copyfile('examples/palma.nginx.conf',fn)
snr('s,/var/www/html,%s,g' % args.wwwdir, fn)
snr('s,php7\.[0-9],php%s,g' % phpversion, fn)
subprocess.call(['/usr/sbin/phpenmod', 'palma'])
subprocess.call(
['/usr/sbin/invoke-rc.d', 'php%s-fpm' % phpversion, 'restart']
)
path = os.path.join(args.sysconfdir,'nginx/sites-enabled')
if not os.path.exists(path):
os.makedirs(path)
fn = os.path.join(path,'palma')
if not os.path.exists(fn):
os.symlink('../sites-available/palma', fn)
fn = os.path.join(path,'default')
if os.path.exists(fn):
os.remove(fn)
printv("Reloading nginx configuration")
subprocess.call(['/usr/sbin/invoke-rc.d', 'nginx', 'restart'])
if args.mode == 'deb' or args.server == 'apache':
printv("Creating configuration for apache2")
path = os.path.join(destdir + args.sysconfdir,'apache2/conf-available')
if not os.path.exists(path):
os.makedirs(path)
fn = os.path.join(path,'palma.conf')
shutil.copyfile('examples/palma.apache.conf', fn)
snr('s,/var/www/html,%s,g' % args.wwwdir, fn)
if args.mode == 'install':
for root, dirs, files in os.walk('/etc/php'):
for d in dirs:
if not d == 'apache2': continue
phpversion = os.path.basename(root)
break
if not len(phpversion):
printe("Could not determine php version, is libapache2-mode-php installed? Exiting")
sys.exit(1)
path = os.path.join(args.sysconfdir,
'php/%s/apache2/conf.d' % phpversion)
if not os.path.exists(path):
os.makedirs(path)
fn = os.path.join(path,'palma.ini')
shutil.copyfile('examples/palma.php.ini', fn)
printv("Reloading apache2 configuration")
subprocess.check_call(['/usr/sbin/a2enmod', 'rewrite'])
subprocess.check_call(['/usr/sbin/a2enconf', 'palma'])
subprocess.check_call(['/usr/sbin/apache2ctl', 'configtest'])
subprocess.call(['/usr/sbin/apache2ctl', 'restart'])
libdir = destdir + args.libdir
mkdir(libdir)
shutil.copy('scripts/palma-browser',libdir)
shutil.copy('scripts/startx',libdir)
# install syslog configuration
printv('Creating configuration for syslog')
path = os.path.join(destdir + args.sysconfdir,'rsyslog.d')
mkdir(path)
shutil.copyfile('examples/palma.syslog', os.path.join(path,'palma.conf'))
if args.mode == 'install':
if os.path.exists('/bin/systemctl'):
subprocess.call(['/bin/systemctl', 'restart', 'rsyslog'])
# install init script
if args.init == 'systemd':
printv('Installing systemd service')
path = os.path.join(destdir + args.sysconfdir,'systemd/system')
mkdir(path)
fn = os.path.join(path,'palma.service')
shutil.copyfile('examples/palma.service', fn)
snr('s,^\\(ExecStart\\)=.*,\\1=%s,g' % os.path.join(args.libdir,'startx'), fn)
snr('s,^\\(WorkingDirectory\\)=.*,\\1=%s,g' % args.wwwdir, fn)
snr('s,^\\(Group\\)=.*,\\1=www-data,g', fn)
if args.mode == 'install':
if os.path.exists('/bin/systemctl'):
subprocess.call(['/bin/systemctl', 'daemon-reload'])
subprocess.call(['/bin/systemctl', 'enable', 'palma'])
subprocess.call(['/usr/sbin/invoke-rc.d', 'palma', 'start'])
else:
pass
except Exception:
printe('Error installing PalMA, exiting.')
#raise
sys.exit(1)
except KeyboardInterrupt:
sys.exit(0)