-
Notifications
You must be signed in to change notification settings - Fork 10
/
install.py
executable file
·519 lines (452 loc) · 17.6 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
if sys.version_info < (2, 6):
print "Sorry, requires Python 2.6 or 2.7."
sys.exit(1)
import os
import os.path
euid = os.geteuid()
if euid != 0:
print "Please restart this script as root!"
sys.exit(1)
try:
import domogikmq
except ImportError:
print "Please install Domogik MQ first! (https://github.com/domogik/domogik-mq)"
sys.exit(1)
import pwd
import shutil
import re
import site
sitepackages = site.getsitepackages()[0]
BLUE = '\033[94m'
OK = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def info(msg):
print "%s [ %s ] %s" % (BLUE,msg,ENDC)
def ok(msg):
print "%s ==> %s %s" % (OK,msg,ENDC)
def warning(msg):
print "%s ==> %s %s" % (WARNING,msg,ENDC)
def fail(msg):
print "%s ==> %s %s" % (FAIL,msg,ENDC)
def main():
"""Main function that is called at the install of Domoweb."""
from optparse import OptionParser
p = OptionParser(usage="usage: %prog [options]",
version="Install for Domoweb 0.4")
p.add_option('--uninstall',
dest='uninstall',
action="store_true",
help="Uninstall Domoweb")
p.add_option('--simul',
dest='simul',
action="store_true",
help="Simulation mode for Uninstall")
p.add_option('--nodeps',
dest='nodeps',
action="store_true",
help="Do not install dependencies")
p.add_option('-u', '--user',
dest='user',
default=None,
help="User that will run Domoweb (default: domoweb)")
p.add_option('--libdir',
dest='libdir',
default='/var/lib/domoweb',
help="Folder for domoweb lib files (default: /var/lib/domoweb)")
p.add_option('--logdir',
dest='logdir',
default='/var/log/domoweb',
help="Folder for domoweb log files (default: /var/log/domoweb)")
p.add_option('--piddir',
dest='piddir',
default='/var/run/domoweb',
help="Folder for domoweb pid files (default: /var/run/domoweb)")
p.add_option('--noconfig',
dest='noconfig',
action="store_true",
help="Do not install Init and /etc files")
p.add_option('--nodbupdate',
dest='nodbupdate',
action="store_true",
help="Do not update the Domoweb DB")
p.add_option('--notest',
dest='notest',
action="store_true",
help="Do not test Domoweb Installation")
p.add_option('--db',
dest='db',
default='/var/lib/domoweb/db.sqlite',
help="Force domoweb DB file (default: /var/lib/domoweb/db.sqlite)")
p.add_option('--noclean',
dest='noclean',
action="store_true",
help="Do not clean old Domoweb install")
p.add_option('--nousercheck',
dest='nousercheck',
action="store_true",
help="Do not check for user account")
p.add_option('--nofoldercreation',
dest='nofoldercreation',
action="store_true",
help="Do not create folders")
# parse command line for defined options
options, args = p.parse_args()
# Initial Clean
if not options.noclean:
clean()
# Uninstall
if options.uninstall:
uninstall(options.simul)
sys.exit(0)
# Dependencies
if options.nodeps:
warning('Not installing dependencies')
else:
info("Installing setuptools...")
import ez_setup
ez_setup.main('')
info("Installing dependencies...")
install_dependencies()
# Domoweb User
if options.nousercheck:
warning('Not checking user')
else:
info("Checking user")
if options.user:
user = options.user
else:
user = raw_input('Which user will run domogik (default : domoweb)? ')
if not user:
user = 'domoweb'
test_user(user)
# Domoweb folders creation
if options.nofoldercreation:
warning('Not creating folders')
else:
info("Checking %s folder" % options.libdir)
createFolder(options.libdir, user)
info("Checking %s folder" % options.logdir)
createFolder(options.logdir, user)
info("Checking %s folder" % options.piddir)
createFolder(options.piddir, user)
info("Copying default files")
installFiles(options.libdir, user)
# Config files
if options.noconfig:
warning('Not installing Init and /etc files')
else:
upgradeOld()
installConfig(user)
installDefault(user)
installInit()
installLogrotate()
# Update DB
if options.nodbupdate:
warning('Not updating Domoweb DB')
else:
info("Updating Domoweb DB...")
updateDb(user, options.db)
# Adding module path to PYTHONPATH
if sitepackages:
mypth = os.path.join(sitepackages, "domoweb.pth")
path_to_add = os.path.abspath(os.path.dirname(__file__))
ok("Adding %s to site-packages" % path_to_add)
with open(mypth, "a") as f:
f.write(path_to_add)
f.write("\n")
else:
fail('site-packages not found')
# Test installation
if options.notest:
warning('Not testing Domoweb Installation')
else:
ok("Everything seems to be good, DomoWeb should be installed correctly.")
ok("Testing installation")
#raw_input('Please press Enter when ready.')
try:
testConfigFiles()
testInit()
tornado_url = getTornadoUrl()
print "\n\n"
ok("================================================== <==")
ok(" Everything seems ok, you should be able to start <==")
ok(" DomoWeb with /etc/init.d/domoweb start <==")
ok(" or /etc/rc.d/domoweb start <==")
ok(" DomoWeb UI is available on <==")
ok(" %49s <==" % tornado_url)
ok("================================================== <==")
except:
fail(sys.exc_info()[1])
def upgradeOld():
# Move previous install from /etc/domoweb/domoweb.cfg to /etc/domoweb.cfg
if os.path.isdir('/etc/domoweb/'):
if os.path.isfile('/etc/domoweb/domoweb.cfg'):
info("Found old config path: moving to /etc/domoweb.cfg")
shutil.move('/etc/domoweb/domoweb.cfg', '/etc/domoweb.cfg')
os.rmdir('/etc/domoweb/')
def installConfig(user):
info("Installing /etc/domoweb.cfg")
uid = pwd.getpwnam(user).pw_uid
installpath = "%s/examples/config/domoweb.cfg" % os.path.dirname(os.path.abspath(__file__))
if os.path.isfile('/etc/domoweb.cfg'):
keep = raw_input('You already have Domoweb configuration files. Do you want to keep them ? [Y/n] ')
if keep == 'N' or keep == 'n':
shutil.copy(installpath, '/etc/')
os.chown('/etc/domoweb.cfg', uid, -1)
else:
shutil.copy(installpath, '/etc/')
os.chown('/etc/domoweb.cfg', uid, -1)
def installFiles(libdir, user):
# Copy default backgrounds
info("Create backgrounds folder")
backgroundspath = os.path.join(libdir, 'backgrounds')
createFolder(backgroundspath, user)
thumbnailspath = os.path.join(libdir, 'backgrounds', 'thumbnails')
createFolder(thumbnailspath, user)
def installDefault(user):
info("Installing /etc/default/domoweb")
installpath = "%s/examples/default/domoweb" % os.path.dirname(os.path.abspath(__file__))
if not os.path.isdir('/etc/default/'):
fail("Can't find the directory where I can copy system-wide config. Usually it is /etc/default/")
sys.exit(1)
else:
shutil.copy(installpath, '/etc/default/')
if not os.path.isfile('/etc/default/domoweb'):
fail("Can't find /etc/default/domoweb!")
sys.exit(1)
info("Configuring /etc/default/domoweb")
with open('/etc/default/domoweb') as f:
s = f.read()
s = re.sub(r'DOMOWEB_USER=.*', ('DOMOWEB_USER=%s' % user), s)
s = re.sub(r'DOMOWEB_PATH=.*', ('DOMOWEB_PATH=%s' % os.path.dirname(os.path.abspath(__file__))), s)
with open('/etc/default/domoweb', "w") as f:
f.write(s)
def installInit():
installpath = "%s/examples/init/domoweb" % os.path.dirname(os.path.abspath(__file__))
if os.path.isdir('/etc/init.d/'):
info("Installing /etc/init.d/domoweb")
shutil.copy(installpath, '/etc/init.d/')
os.chmod('/etc/init.d/domoweb', 0755)
elif os.path.isdir('/etc/rc.d/'):
info("Installing /etc/rc.d/domoweb")
shutil.copy(installpath, '/etc/rc.d/')
os.chmod('/etc/rc.d/domoweb', 0755)
else:
warning("Init directory does not exist (/etc/init.d or /etc/rc.d): require manual install")
def installLogrotate():
installpath = "%s/examples/logrotate/domoweb" % os.path.dirname(os.path.abspath(__file__))
if os.path.isdir('/etc/logrotate.d/'):
info("Installing /etc/logrotate/domoweb")
shutil.copy(installpath, '/etc/logrotate.d/')
os.chmod('/etc/logrotate.d/domoweb', 0644)
def createFolder(path, user):
if not os.path.isdir(path):
info("%s do not exist... creation" % path)
os.makedirs(path)
uid = pwd.getpwnam(user).pw_uid
info("Updating rights for user %s[%s]" % (user, uid))
os.chown(path, uid, -1)
os.chmod(path, 0755)
def test_user(user):
info("Looking for user %s" % user)
try:
pw = pwd.getpwnam(user)
except KeyError:
warning("Can't find informations about user %s" % user)
create_user = raw_input('Do you want to create it? [Y/n] ')
if not create_user or create_user == 'Y' or create_user == 'y':
info("Creating user %s" % user)
os.system("/usr/sbin/adduser %s" % user)
ok("User %s created" % user)
else:
fail("Please restart this script when the user %s will exist." % user)
sys.exit(1)
else:
ok("User %s found" % user)
def install_dependencies():
from setuptools.command import easy_install
import pkg_resources
easy_install.main( ['setuptools',
'sqlalchemy',
'alembic',
'tornado >= 3.1',
'simplejson >= 1.9.2',
'WTForms >= 2.0',
'WTForms-Components',
'pillow < 3.0.0',
'pyzmq'
])
# pkg_resources.get_distribution('django').activate()
def updateDb(user, db):
from domoweb.models import metadata, engine, Session, Section, SectionParam
from sqlalchemy import create_engine, func
from alembic.config import Config
from alembic import command
alembic_cfg = Config("alembic.ini")
if not os.path.isfile(db):
ok("Creating new database: %s" % db)
metadata.create_all(engine)
command.stamp(alembic_cfg, "head")
uid = pwd.getpwnam(user).pw_uid
os.chown(db, uid, -1)
ok("Adding initial data")
session = Session()
s = Section(name=unicode('Root'), description=unicode('Root dashboard'), left=1, right=2)
session.add(s)
p = SectionParam(section_id=1, key='GridWidgetSize', value='100')
session.add(p)
p = SectionParam(section_id=1, key='GridWidgetSpace', value='20')
session.add(p)
p = SectionParam(section_id=1, key='GridMode', value='3')
session.add(p)
session.commit()
else:
ok("Upgrading existing database")
session = Session()
sections = session.query(Section).all()
for s in sections:
p = session.query(SectionParam).filter_by(section_id=s.id).filter(SectionParam.key.in_(['GridWidgetSize', 'GridWidgetSpace', 'GridColumns', 'GridRows'])).count()
if p == 0:
ok("Updating section '%s'" % s.name)
p = SectionParam(section_id=s.id, key='GridWidgetSize', value='100')
session.add(p)
p = SectionParam(section_id=s.id, key='GridWidgetSpace', value='20')
session.add(p)
p = SectionParam(section_id=s.id, key='GridMode', value='3')
session.add(p)
session.commit()
command.upgrade(alembic_cfg, "head")
def testImports():
good = True
info("Test imports")
try:
import django
except ImportError:
warning("Can't import django, please install it by hand (== 1.4)")
good = False
import httplib
except ImportError:
warning("Can't import httplib, please install it by hand (>= 2)")
good = False
try:
import simplejson
except ImportError:
warning("Can't import simplejson, please install it by hand (>= 1.9.2)")
good = False
assert good, "One or more import have failed, please install required packages and restart this script."
ok("Imports are good")
def testConfigFiles():
global user
info("Checking global config file")
assert os.path.isfile("/etc/conf.d/domoweb") or os.path.isfile("/etc/default/domoweb"), \
"No global config file found"
assert not (os.path.isfile("/etc/conf.d/domoweb") and os.path.isfile("/etc/default/domoweb")), \
"Global config file found at 2 locations. Please put it only at /etc/default/domoweb or \
/etc/conf.d/domoweb"
if os.path.isfile("/etc/default/domoweb"):
file = "/etc/default/domoweb"
else:
file = "/etc/conf.d/domoweb"
f = open(file,"r")
r = f.readlines()
lines = filter(lambda x: not x.startswith('#') and x != '\n',r)
f.close()
for line in lines:
item,value = line.strip().split("=")
if item.strip() == "DOMOWEB_USER":
user = value
elif item.strip() == "DOMOWEB_PATH":
if not os.path.dirname(os.path.abspath(__file__)) == value:
fail("DOMOWEB_PATH do not match the current installation folder")
else:
warning("Unknown config value in the main config file : %s" % item)
ok("Global config file exists and contains right stuff")
info("Test user / config file")
#Check user config file
assert os.path.isfile("/etc/domoweb.cfg"), "The domogik config file /etc/domoweb.cfg does not exist. Please report this as a bug."
ok("Domogik's user exists and has a config file")
# info("Check user config file contents")
# import ConfigParser
# config = ConfigParser.ConfigParser()
# config.read("/etc/domoweb.cfg")
#check [global] section
# django = dict(config.items('global'))
# ok("Config file correctly loaded")
def testInit():
info("Checking init.d / rc.d")
assert os.access("/etc/init.d/domoweb", os.X_OK) or os.access("/etc/rc.d/domoweb", os.X_OK), "/etc/init.d/domoweb and /etc/rc.d/domoweb do not \
exist or can't be executed.\
Please copy src/examples/init/domoweb to /etc/init.d or /etc/rc.d depending on your system, and chmod +x /etc/init.d/domoweb"
ok("/etc/init.d/domoweb or /etc/rc.d/domoweb found with good permissions")
def testDB(db):
info("Checking Domoweb DB")
assert os.path.isfile(db), \
"%s not found" % db
ok("%s found" % db)
def getTornadoUrl():
import socket
from tornado.options import options
ip = socket.gethostbyname(socket.gethostname())
# Check config file
SERVER_CONFIG = '/etc/domoweb.cfg'
if not os.path.isfile(SERVER_CONFIG):
sys.stderr.write("Error: Can't find the file '%s'\n" % SERVER_CONFIG)
sys.exit(1)
options.define("port", default=40404, help="Launch on the given port", type=int)
options.parse_config_file(SERVER_CONFIG)
return "http://%s:%s/" % (ip, options.port)
def clean():
import commands
status,output = commands.getstatusoutput('which dmg_domoweb')
if status == 0:
info("Deleting %s shortcut" % output)
os.remove(output)
from distutils.sysconfig import get_python_lib
sitepackages = get_python_lib()
dirList=os.listdir(sitepackages)
for fname in dirList:
try:
fname.index('Domoweb')
except ValueError:
pass
else:
fullpath = os.path.join(sitepackages, fname)
if os.path.isdir(fullpath):
info("Deleting old dir %s" % fullpath)
shutil.rmtree(fullpath)
else:
info("Deleting old file %s" % fullpath)
os.remove(fullpath)
def uninstall(simul):
ok("This script will uninstall Domoweb")
if simul: warning('Simulation mode: not deleting')
sure = raw_input('Are you sure ? [y/N] ')
if sure == 'Y' or sure == 'y':
if os.path.isfile('/etc/default/domoweb'):
info('Deleting /etc/default/domoweb')
if not simul: os.remove('/etc/default/domoweb')
if os.path.isfile('/etc/init.d/domoweb'):
info('Deleting /etc/init.d/domoweb')
if not simul: os.remove('/etc/init.d/domoweb')
if os.path.isfile('/etc/rc.d/domoweb'):
info('Deleting /etc/rc.d/domoweb')
if not simul: os.remove('/etc/rc.d/domoweb')
if os.path.isfile('/etc/domoweb.cfg'):
info('Deleting config file /etc/domoweb.cfg')
if not simul: os.remove('/etc/domoweb.cfg')
if os.path.isdir('/var/lib/domoweb'):
info('Deleting /var/lib/domoweb')
if not simul: shutil.rmtree('/var/lib/domoweb')
if os.path.isdir('/var/log/domoweb'):
info('Deleting /var/log/domoweb')
if not simul: shutil.rmtree('/var/log/domoweb')
ok('Deleting complete')
else:
warning("Aborting...")
if __name__ == "__main__":
main()