forked from domogik/domogik
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·672 lines (593 loc) · 28.1 KB
/
install.py
File metadata and controls
executable file
·672 lines (593 loc) · 28.1 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pwd
import sys
import platform
try:
# from python3 onwards
import configparser
except ImportError:
# python 2
import ConfigParser as configparser
import argparse
import shutil
import logging
import pkg_resources
from subprocess import Popen, PIPE, STDOUT
from distutils import version
import uuid
BLUE = '\033[94m'
OK = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
### define display functions
def info(msg):
logging.info(msg)
print("{0} [ {1} ] {2}".format(BLUE, msg, ENDC))
def ok(msg):
logging.info(msg)
print("{0} ==> {1} {2}".format(OK, msg, ENDC))
def warning(msg):
logging.warning(msg)
print("{0} ==> {1} {2}".format(WARNING, msg, ENDC))
def fail(msg):
logging.error(msg)
print("{0} ==> {1} {2}".format(FAIL, msg, ENDC))
def debug(msg):
logging.debug(msg)
### test if script is launch as root
# CHECK run as root
info("Check this script is started as root")
assert os.getuid() == 0, "This script must be started as root"
ok("Correctly started with root privileges.")
logging.basicConfig(filename='install.log', level=logging.DEBUG)
### other functions
def get_mysql_or_mariadb_release():
cmd = 'mysqld --version 2>/dev/null | sed "s/^.* \([0-9\.]*\)[- ].*$/\\1/"'
subp = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
res = subp.communicate()
return res[0].strip()
def do_we_need_mariadb(release, command_line_mode = False):
""" Check if the mysql/mariadb release is compliant with Domogik
@release : the mysql or mariadb release string : 6.4.2 for example
@command_line_mode : args.command_line. If set, don't ask the user if not a valid release and continue install.
"""
if release == "":
# no mariadb or mysql installed, the user may used postgresql or anything else.
return
info(u"Your installed MySQL or MariaDB release is : {0}".format(release))
if version.StrictVersion(release) < version.StrictVersion("5.7.5"):
fail(u"Your MySQL release is older than 5.7.5. Depending on its configuration, you may encountered some issues. You should use MariaDB instead, which is fully compliant with MySQL.")
if not command_line_mode:
print("If you plan to use MySQL or MariaDB for Domogik, please stop and install MariaDB. If you plan to use another database engine (PostgreSQL for example), you can continue.\nContinue ? [y/N]: ")
new_value = sys.stdin.readline().rstrip('\n')
if new_value == "y" or new_value == "Y":
debug("The installation will continue.")
else:
debug("Installation aborted...")
sys.exit(1)
else:
ok(u"You are running a MySQL or MariaDB compliant with Domogik")
def get_c_hub():
hub = {
'x86_64' : 'src/domogik/xpl/tools/64bit/xPL_Hub',
'i686' : 'src/domogik/xpl/tools/32bit/xPL_Hub',
'arm' : 'src/domogik/xpl/tools/arm/xPL_Hub',
'armv5tel' : 'src/domogik/xpl/tools/arm/xPL_Hub',
'armv6l' : 'src/domogik/xpl/tools/arm/xPL_Hub'
}
arch = platform.machine()
if arch in hub.keys():
return hub[arch]
else:
return None
def build_file_list(user, noXpl):
d_files = [
('/etc/domogik', [user, 0755], \
['src/domogik/examples/config/domogik.cfg.sample']),
('/var/cache/domogik', [user, None], []),
('/var/cache/domogik/pkg-cache', [user, None], []),
('/var/cache/domogik/cache', [user, None], []),
('/var/lib/domogik', [user, None], []),
('/var/lib/domogik/domogik_packages', [user, None], \
['src/domogik/common/__init__.py']),
('/var/lib/domogik/resources', [user, None], []),
('/var/lib/domogik/resources/butler', [user, None], []),
('/var/lib/domogik/resources', [user, None], \
['src/domogik/common/datatypes.json']),
('/var/lib/domogik/resources/sphinx', [user, None], \
['docs/Makefile',
'docs/conf-packages.py']),
('/var/lock/domogik', [user, 0755], []),
('/var/log/domogik', [user, 0755], []),
]
if not noXpl:
d_files.append(('/var/log/xplhub', [user, None], []))
d_files.append(('/etc/domogik', [user, 0755], ['src/domogik/xpl/hub/examples/config/xplhub.cfg.sample']))
if os.path.exists('/etc/default'):
debug("Found directory to store the system wide config: /etc/default")
d_files.append(('/etc/default/', [user, None], \
['src/domogik/examples/default/domogik']))
else:
fail("Can't find directory where i can copy system wide config")
exit(1)
if os.path.exists('/etc/logrotate.d') and not noXpl:
debug("Found a directory for the logrotate script: /etc/logrotate.d")
d_files.append(('/etc/logrotate.d', ['root', None], \
['src/domogik/xpl/hub/examples/logrotate/xplhub']))
# /etc/systemd/system
# domogik-mq-broker.service domogik-mq-forwarder.service domogik.service domogik-xpl.service
# src/domogik/examples/systemd/system/
if os.path.exists('/etc/systemd/system'):
debug("SystemD found, copyinf giles")
for f in ["domogik-mq-broker.service", "domogik-mq-forwarder.service", "domogik.service", "domogik-xpl.service"]:
d_files.append(('/etc/systemd/system', [user, 0755], \
['src/domogik/examples/systemd/system/{0}'.format(f)]))
if os.path.exists('/etc/init.d'):
debug("Init script path is /etc/init.d")
d_files.append(('/etc/init.d/', [user, 0755], \
['src/domogik/examples/init/domogik']))
elif os.path.exists('/etc/rc.d'):
debug("Init script path is /etc/rc.d")
d_files.append(('/etc/rc.d/', [user, 0755], \
['src/domogik/examples/init/domogik']))
else:
warning("Can't find firectory for init script: Require manual install")
if os.path.exists('/etc/cron.d'):
debug("Found directory to store the cron config: /etc/cron.d")
d_files.append(('/etc/cron.d/', ['root', 0644], \
['src/domogik/examples/cron/domogik']))
else:
fail("Can't find directory where i can copy cron config")
exit(1)
print d_files
hub = get_c_hub()
if hub is not None:
debug("Adding c hub path: {0}".format(hub))
d_files.append(('/usr/sbin/', [user, None], [hub]))
return d_files
def copy_files(user, noXpl):
info("Copy files")
try:
for directory, perm, files in build_file_list(user, noXpl):
if not os.path.exists(directory):
if perm[1] != None:
res = os.makedirs(directory, int(perm[1]))
else:
res = os.makedirs(directory)
if not res:
ok("Creating dir {0}".format(directory))
else:
fail("Failed creating dir {0}".format(directory))
else:
ok("Directory {0} already exists".format(directory))
if perm[0] != '':
debug("chown directory {0} with {1}".format(directory, perm[0]))
os.system('chown {0} {1}'.format(perm[0], directory))
for fname in files:
# copy the file
shutil.copy(os.path.join(\
os.path.dirname(os.path.realpath(__file__)), \
fname), \
directory)
ok("Copyed file {0}".format(fname))
dfname = os.path.join(directory, os.path.basename(fname))
if perm[0] != '':
debug("chown dile {0} with {1}".format(dfname, perm[0]))
os.system('chown {0} {1}'.format(perm[0], dfname))
#if perm[1] != None:
# os.system('chmod {0} {1}'.format(perm[1], dfname))
# rename files
os.rename('/var/lib/domogik/resources/sphinx/conf-packages.py', '/var/lib/domogik/resources/sphinx/conf.py')
except:
raise
def ask_user_name():
info("Create domogik user")
print("As what user should domogik run? [domogik]: "),
new_value = sys.stdin.readline().rstrip('\n')
if new_value == "":
d_user = 'domogik'
else:
d_user = new_value
debug("Username will be {0}".format(d_user))
return d_user
def create_user(d_user, d_shell = "/bin/sh"):
info("Create domogik user")
if d_user not in [x[0] for x in pwd.getpwall()]:
print("Creating the {0} user and add it to dialout".format(d_user))
cmd_line = 'adduser --system {0} --shell {1} '.format(d_user, d_shell)
debug(cmd_line)
os.system(cmd_line)
cmd_line = 'adduser {0} dialout'.format(d_user)
debug(cmd_line)
os.system(cmd_line)
if d_user not in [x[0] for x in pwd.getpwall()]:
fail("Failed to create domogik user")
else:
ok("Correctly created domogik user")
# return the user to use
def is_domogik_advanced(advanced_mode, sect, key):
advanced_keys = {
'domogik': ['libraries_path', 'src_prefix', \
'log_dir_path', 'pid_dir_path', 'broadcast', 'log_level', \
'log_when', 'log_interval', 'log_backup_count'],
'database': ['prefix', 'pool_recycle', 'portcache'],
'admin': ['port', 'ws_port', 'use_ssl', 'ssl_certificate', 'ssl_key', 'clean_json', 'rest_auth', 'secret_key', 'http_workers_number'],
}
if advanced_mode:
return True
else:
if sect not in advanced_keys:
return True
else:
if key not in advanced_keys[sect]:
return True
else:
return False
def is_xplhub_advanced(advanced_mode, sect, key):
advanced_keys = {
'hub': ['log_dir_path', 'log_bandwidth', 'log_invalid_data', 'log_level'],
}
if advanced_mode:
return True
else:
if sect not in advanced_keys:
return True
else:
if key not in advanced_keys[sect]:
return True
else:
return False
def write_domogik_configfile(advanced_mode, intf):
# read the sample config file
newvalues = False
config = configparser.RawConfigParser()
config.read( ['/etc/domogik/domogik.cfg.sample'] )
itf = ['bind_interface', 'interfaces']
for sect in config.sections():
info("Starting on section {0}".format(sect))
if sect != "metrics":
for item in config.items(sect):
if sect == 'admin'and item[0] == 'secret_key':
config.set(sect, item[0], uuid.uuid4())
elif item[0] in itf and not advanced_mode:
config.set(sect, item[0], intf)
debug("Value {0} in domogik.cfg set to {1}".format(item[0], intf))
elif is_domogik_advanced(advanced_mode, sect, item[0]):
print("- {0} [{1}]: ".format(item[0], item[1])),
new_value = sys.stdin.readline().rstrip('\n')
if new_value != item[1] and new_value != '':
# need to write it to config file
config.set(sect, item[0], new_value)
newvalues = True
# manage metrics section
else:
config.set(sect, "id", uuid.getnode()) # set an unique id which is hardware dependent
print("Set [{0}] : {1} = {2}".format(sect, id, uuid.getnode()))
debug("Value {0} in domogik.cfg > [metrics] set to {1}".format(id, uuid.getnode()))
# write the config file
with open('/etc/domogik/domogik.cfg', 'wb') as configfile:
ok("Writing the config file")
config.write(configfile)
def write_xplhub_configfile(advanced_mode, intf):
# read the sample config file
newvalues = False
config = configparser.RawConfigParser()
config.read( ['/etc/domogik/xplhub.cfg.sample'] )
for sect in config.sections():
info("Starting on section {0}".format(sect))
for item in config.items(sect):
if item[0] == 'interfaces' and not advanced_mode:
config.set(sect, item[0], intf)
debug("Value {0} in xplhub.cfg set to {1}".format(item[0], intf))
elif is_xplhub_advanced(advanced_mode, sect, item[0]):
print("- {0} [{1}]: ".format(item[0], item[1])),
new_value = sys.stdin.readline().rstrip('\n')
if new_value != item[1] and new_value != '':
# need to write it to config file
config.set(sect, item[0], new_value)
newvalues = True
debug("Value {0} in xplhub.cfg set to {1}".format(item[0], new_value))
# write the config file
with open('/etc/domogik/xplhub.cfg', 'wb') as configfile:
ok("Writing the config file")
config.write(configfile)
def write_domogik_configfile_from_command_line(args, intf):
# read the sample config file
newvalues = False
config = configparser.RawConfigParser()
config.read( ['/etc/domogik/domogik.cfg.sample'] )
itf = ['bind_interface', 'interfaces']
for sect in config.sections():
info("Starting on section {0}".format(sect))
for item in config.items(sect):
# handle the intf parameter
new_value = eval("args.{0}_{1}".format(sect, item[0]))
# notice that finally because of network interfaces, the newvalues will always be True...
# TODO : improve
if item[0] not in itf:
if new_value != item[1] and new_value != '' and new_value != None:
# need to write it to config file
print("Set [{0}] : {1} = {2}".format(sect, item[0], new_value))
config.set(sect, item[0], new_value)
newvalues = True
else:
# user changed the default value
print("{0} vs {1}".format(new_value, item[1]))
if new_value != item[1] and new_value != '' and new_value != None:
# need to write it to config file
print("Set [{0}] : {1} = {2}".format(sect, item[0], new_value))
config.set(sect, item[0], new_value)
newvalues = True
# user didn't change the default value, we put the found network interfaces instead of the default value
else:
# need to write it to config file
print("Set [{0}] : {1} = {2}".format(sect, item[0], new_value))
config.set(sect, item[0], intf)
newvalues = True
debug("Value {0} in domogik.cfg set to {1}".format(item[0], new_value))
# write the config file
with open('/etc/domogik/domogik.cfg', 'wb') as configfile:
ok("Writing the config file")
config.write(configfile)
def write_xplhub_configfile_from_command_line(args, intf):
# read the sample config file
newvalues = False
config = configparser.RawConfigParser()
config.read( ['/etc/domogik/xplhub.cfg.sample'] )
for sect in config.sections():
info("Starting on section {0}".format(sect))
for item in config.items(sect):
if item[0] == 'interfaces':
# for the interface key, if the user set a value != of the default one, take it. Else take the builded value
new_value = eval("args.{0}_{1}".format(sect, item[0]))
# the user changed the value
if new_value != item[1] and new_value != '' and new_value != None:
# need to write it to config file
print("Set [{0}] : {1} = {2}".format(sect, item[0], new_value))
config.set(sect, item[0], new_value)
newvalues = True
# the user did not change the value
else:
print("Set [{0}] : {1} = {2}".format(sect, item[0], intf))
config.set(sect, item[0], intf)
newvalues = True
debug("Value {0} in domogik.cfg set to {1}".format(item[0], new_value))
else:
new_value = eval("args.{0}_{1}".format(sect, item[0]))
if new_value != item[1] and new_value != '' and new_value != None:
# need to write it to config file
print("Set [{0}] : {1} = {2}".format(sect, item[0], new_value))
config.set(sect, item[0], new_value)
newvalues = True
debug("Value {0} in domogik.cfg set to {1}".format(item[0], new_value))
# write the config file
with open('/etc/domogik/xplhub.cfg', 'wb') as configfile:
ok("Writing the config file")
config.write(configfile)
def needupdate():
# first check if there are already some config files
if os.path.isfile("/etc/domogik/domogik.cfg") or \
os.path.isfile("/etc/domogik/xplhub.cfg"):
info("Configuration files")
# DEL # print("Please notice that Domogik 0.3.x configuration files are no more compliant with Domogik 0.4 :")
# DEL # print("- backup your Domogik 0.3 configuration files")
# DEL # print("- say 'n' to the question to recreate them from scratch")
print("Do you want to keep your current config files ? [Y/n]: ")
new_value = sys.stdin.readline().rstrip('\n')
if new_value == "y" or new_value == "Y" or new_value == '':
debug("keeping curent config files")
return False
else:
debug("NOT keeping curent config files")
return True
return True
def update_default(user):
info("Update /etc/default/domogik")
os.system('sed -i "s;^DOMOGIK_USER.*$;DOMOGIK_USER={0};" /etc/default/domogik'.format(user))
def find_interface():
info("Trying to find an interface to listen on")
intf = ""
try:
import traceback
pkg_resources.get_distribution("domogik").activate()
from domogik.common.utils import get_interfaces, interface_has_ip
for intf in get_interfaces():
if intf == 'lo':
continue
if interface_has_ip(intf):
break
except:
fail("Trace: {0}".format(traceback.format_exc()))
else:
ok("Selected interface {0}".format(intf))
return intf
def install():
parser = argparse.ArgumentParser(description='Domogik installation.')
parser.add_argument('--dist-packages', dest='dist_packages', action="store_true",
default=False, help='Try to use distribution packages instead of pip packages')
parser.add_argument('--no-create-database', dest='no_create_database', action="store_true",
default=False, help='create and allow domogik to access to it, if it is not already created')
parser.add_argument('--no-setup', dest='setup', action="store_true",
default=False, help='Don\'t install the python packages')
parser.add_argument('--no-test', dest='test', action="store_true",
default=False, help='Don\'t run a config test')
parser.add_argument('--no-config', dest='config', action="store_true",
default=False, help='Don\'t run a config writer')
parser.add_argument('--no-create-user', dest='user_creation', \
action="store_false", \
default=True, help='Don\'t create a user')
parser.add_argument('--no-db-upgrade', dest='db', action="store_true",
default=False, help='Don\'t do a db upgrade')
parser.add_argument('--no-mq-check', dest='mq', action="store_true",
default=False, help='Don\'t check the mq package')
parser.add_argument('--no-db-backup', dest='skip_database_backup', action="store_true",
default=False, help='Don\'t do a db backup')
parser.add_argument('--no-xpl-hub', dest='skip_xpl_hub', action="store_true",
default=False, help='Don\'t install an xpl hub')
parser.add_argument("--user",
help="Set the domogik user")
parser.add_argument("--user-shell", dest="user_shell",
help="Set the domogik user shell")
parser.add_argument('--advanced', dest='advanced_mode', action="store_true",
default=False, help='Allow to configure all options in interactive mode')
# generate dynamically all arguments for the various config files
# notice that we MUST NOT have the same sections in the different files!
parser.add_argument('--command-line', dest='command_line', \
action="store_true", default=False, \
help='Configure the configuration files from the command line only')
add_arguments_for_config_file(parser, \
"src/domogik/examples/config/domogik.cfg.sample")
add_arguments_for_config_file(parser, \
"src/domogik/xpl/hub/examples/config/xplhub.cfg.sample")
args = parser.parse_args()
print(args)
try:
# CHECK python version
if sys.version_info < (2, 6):
fail("Python version is to low, at least python 2.6 is needed")
exit(1)
# CHECK sources not in / or /root
info("Check the sources location (not in /root/ or /")
print(os.getcwd())
assert os.getcwd().startswith("/root/") == False, "Domogik sources must not be located in the /root/ folder"
# CHECK mysql or mariaDB release
do_we_need_mariadb(get_mysql_or_mariadb_release(), args.command_line)
# CHECK mq installed
if not args.mq:
info("Check that domogik-mq is installed")
try:
import domogikmq
except ImportError:
fail("Please install Domogik MQ first! (https://github.com/domogik/domogik-mq)")
exit(1)
# Execute database fix for some 0.2/0.3 databases
#info("Process some database upgrade issues with previous releases")
#cmd = "sh ./src/domogik/install/db_fix_03.sh"
#p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
#lines_iterator = iter(p.stdout.readline, b"")
#for line in lines_iterator:
# print(line)
if args.dist_packages:
dist_packages_install_script = ''
#platform.dist() and platform.linux_distribution()
#doesn't works with ubuntu/debian, both say debian.
#So I not found pettiest test :(
if os.system(' bash -c \'[ "`lsb_release -si`" == "Debian" ]\'') == 0:
dist_packages_install_script = './debian_packages_install.sh'
elif os.system(' bash -c \'[ "`lsb_release -si`" == "Ubuntu" ]\'') == 0:
dist_packages_install_script = './debian_packages_install.sh'
elif os.system(' bash -c \'[ "`lsb_release -si`" == "Raspbian" ]\'') == 0:
dist_packages_install_script = './debian_packages_install.sh'
if dist_packages_install_script == '' :
raise OSError("The option --dist-packages is not implemented on this distribution. \nPlease install the packages manually.\n When packages have been installed, you can re reun the installation script without the --dist-packages option.")
if os.system(dist_packages_install_script) != 0:
raise OSError("Cannot install packages correctly script '%s'" % dist_packages_install_script)
# RUN setup.py
if not args.setup:
info("Run setup.py")
subp = Popen('python setup.py develop', shell=True)
res = subp.communicate()
rc = subp.returncode
if rc != 0:
raise OSError("setup.py doesn't finish correctly")
#if os.system('python setup.py develop') != 0:
# raise OSError("setup.py doesn't finish correctly")
# ask for the domogik user
if args.user == None or args.user == '':
user = ask_user_name()
else:
ok("User setted to '{0}' from the command line".format(args.user))
user = args.user
# create user
if args.user_creation:
if args.user_shell:
create_user(user, args.user_shell)
else:
create_user(user)
# Copy files
copy_files(user, args.skip_xpl_hub)
update_default(user)
# select the correct interface
intf = find_interface()
# if 'lo' not int intf, add it (because domoweb by default try to catch REST on 127.0.0.1)
if 'lo' not in intf:
intf = "lo,{0}".format(intf)
# write config file
if args.command_line:
info("Update the config file : /etc/domogik/domogik.cfg")
write_domogik_configfile_from_command_line(args, intf)
if not args.skip_xpl_hub:
info("Update the config file : /etc/domogik/xplhub.cfg")
write_xplhub_configfile_from_command_line(args, intf)
else:
if not args.config and needupdate():
info("Update the config file : /etc/domogik/domogik.cfg")
write_domogik_configfile(args.advanced_mode, intf)
if not args.skip_xpl_hub:
info("Update the config file : /etc/domogik/xplhub.cfg")
write_xplhub_configfile(args.advanced_mode, intf)
# upgrade db
if not args.db:
# check if alembic version is at least 0.7.4. Else raise an error
from alembic import __version__ as alembic_version
if version.StrictVersion(alembic_version) < version.StrictVersion("0.7.4"):
fail("The 'alembic' version installed on this system ({0}) is not recent enough. Please install at least alembic >= 0.7.4".format(alembic_version))
exit(1)
# do db upgrade
try:
user_entry = pwd.getpwnam(user)
except KeyError:
raise KeyError("The user %s does not exists, you MUST create it or change the DOMOGIK_USER parameter in %s. Please report this as a bug if you used install.sh." % (user, file))
# launch db_install as the domogik user
#uid = user_entry.pw_uid
#user_home = user_entry.pw_dir
#os.setreuid(0,uid)
#old_home = os.environ['HOME']
#os.environ['HOME'] = user_home
#os.system('python src/domogik/install/db_install.py')
#os.setreuid(0,0)
#os.environ['HOME'] = old_home
try:
import traceback
# we must activate the domogik module as setup.py is launched from install.py and just after we try
# to import something from the domogik package but the module is not known without doing anything...
pkg_resources.get_distribution("domogik").activate()
from domogik.install.db_install import DbInstall
except:
print("Trace: {0}".format(traceback.format_exc()))
dbi = DbInstall()
if not args.no_create_database:
dbi.create_db()
dbi.install_or_upgrade_db(args.skip_database_backup, args.command_line)
# change permissions to some files created as root during the installation to the domogik user
os.chown("/var/log/domogik/db_api.log", user_entry.pw_uid, -1)
os.chown("/var/lock/domogik/config.lock", user_entry.pw_uid, -1)
if not args.test:
print("Running test_config.py...")
os.system('python test_config.py')
print("\n\n")
except SystemExit:
# a sys.exit have been called, do not raise more errors
pass
sys.exit(1)
except:
import traceback
print("========= TRACEBACK =============")
print(traceback.format_exc())
print("=================================")
fail(sys.exc_info())
sys.exit(1)
def add_arguments_for_config_file(parser, fle):
# read the sample config file
config = configparser.RawConfigParser()
config.read( [fle] )
for sect in config.sections():
for item in config.items(sect):
key = "{0}_{1}".format(sect, item[0])
parser.add_argument("--{0}".format(key),
help="Update section {0}, key {1} value".format(sect, item[0]))
if __name__ == "__main__":
install()