Newer
Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Environment setup script for MediaServer
'''
import imp
import os
import sys
import traceback
import utils
from utils import log
class EnvSetup():
PY_SETUP_NAME = '0_setup.py'
BASH_SETUP_NAME = '0_setup.sh'
def __init__(self, *args):
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
args = list(args)
# Check current dir
root_dir = utils.get_dir(__file__)
if root_dir != '':
os.chdir(root_dir)
# Add to python path
if root_dir not in sys.path:
sys.path.append(root_dir)
# Get available actions
self.root_dir = root_dir
names = os.listdir(root_dir)
self.actions = list()
for name in names:
path = os.path.join(root_dir, name)
if os.path.isdir(path):
try:
index = int(name.split('.')[0])
except ValueError:
continue
label = name[len(str(index)) + 1:].strip()
if not label:
log('No label found for dir %s.' % name)
continue
if os.path.isfile(os.path.join(path, self.PY_SETUP_NAME)):
setup_module = imp.load_source('setup_%s' % name, os.path.join(path, self.PY_SETUP_NAME))
self.actions.append(dict(index=index, label=label, path=path, fct=setup_module.setup))
elif os.path.isfile(os.path.join(path, self.BASH_SETUP_NAME)):
self.actions.append(dict(index=index, label=label, path=path, fct=os.path.join(path, self.BASH_SETUP_NAME)))
else:
self.actions.append(dict(index=index, label=label, path=path, fct=None))
self.actions.sort(key=lambda a: a['index'])
if not self.actions:
log('No action available.')
sys.exit(1)
# Check that this script is run by root
debug = '-d' in args
if debug:
args.remove('-d')
code, out = utils.exec_cmd(['whoami'], get_out=True)
if out != 'root' and not debug:
log('This script should be run as root user.')
sys.exit(1)
# Load conf
utils.load_conf()
if args:
# Run command
for arg in args:
self.run(arg, interactive=False)
else:
# Open main menu
self.menu()
def display_header(self):
log('\033[96m-------------------------------------\033[0m')
log('\033[96m- Environment setup for MediaServer -\033[0m')
log('\033[96m-------------------------------------\033[0m')
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
def menu(self):
# Show main menu
log('Actions:')
for action in self.actions:
if action['fct']:
log(' %s: %s' % (action['index'], action['label']))
else:
log(' \033[1;94m%s\033[0m' % (action['label']))
log('')
log(' t: Run tests')
log(' c: Configuration import status')
log(' e: Exit\n')
log('Info:')
log('\033[0;36m To setup a system entirely for a determined purpose (Worker, MS, CM, ...), you can use envsetup-launcher:\033[0m')
log('\033[0;36m bash /root/envsetup/envsetup-launcher.sh\033[0m')
log('\nWhat action do you want to start ?')
try:
target = input('---> ').strip()
except (KeyboardInterrupt, EOFError):
log('')
target = 'e'
self.run(target)
def run(self, target, interactive=True):
if target == 'e':
log('Exit')
sys.exit(0)
exit_code = 0
if target == 't':
# Run tests
path = os.path.join(self.root_dir, 'tests')
if not os.path.isdir(path):
exit_code = 1
log('The tests dir is missing (%s).' % path)
else:
os.chdir(path)
names = os.listdir(path)
names.sort()
results = list()
for name in names:
log('\033[1;95m-- Test "%s" --\033[0;0m' % name)
test_path = os.path.join(path, name)
try:
code = utils.exec_cmd(test_path)
if code != 0:
raise Exception('Command exited with code %s.' % code)
except Exception as e:
exit_code = 1
log(e)
results.append((False, test_path))
else:
results.append((True, test_path))
log('\nTests results:')
for success, test_path in results:
if success:
log(' %s: \033[92msuccess\033[0m' % test_path)
else:
log(' %s: \033[91mfailure\033[0m' % test_path)
elif target == 'c':
# Display current configuration
log('Configuration import status:')
log(utils.get_conf('config_import_status', 'Configuration file has been overwritten.'))
else:
# Run an action
found = False
for action in self.actions:
if target == str(action['index']) and action['fct']:
found = True
log('Starting %s setup.' % action['label'])
log('Please wait, the setup is in progress... ')
try:
os.chdir(action['path'])
if isinstance(action['fct'], str):
utils.run_commands(['\'%s\'' % action['fct']])
else:
action['fct'](interactive)
except Exception as e:
exit_code = 1
if isinstance(action['fct'], str):
log(action['fct'])
else:
log(traceback.format_exc())
log('Unable to setup %s:\n%s\n' % (action['label'], e))
else:
log('%s setup complete.\n' % action['label'])
os.chdir(self.root_dir)
break
if not found:
exit_code = 1
log('Invalid action requested: "%s".' % target)
if interactive:
try:
input('Press enter to continue.')
except (KeyboardInterrupt, EOFError):
log('')
sys.exit(exit_code)
self.menu()
else:
sys.exit(exit_code)
if __name__ == '__main__':
EnvSetup(*sys.argv[1:])