forked from shinken-solutions/shinken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_hooks.py
183 lines (170 loc) · 8 KB
/
install_hooks.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
import os
import sys
import re
import fileinput
import getpass
def get_init_scripts(config):
""" Add init scripts in data_files for install """
data_files = config['files']['data_files']
if 'win' in sys.platform:
pass
elif 'linux' in sys.platform or 'sunos5' in sys.platform:
data_files = data_files + "\netc/init.d = bin/init.d/*"
data_files = data_files + "\netc/default = bin/default/shinken.in"
elif 'bsd' in sys.platform or 'dragonfly' in sys.platform:
data_files = data_files + "\nusr/local/etc/rc.d = bin/rc.d/*"
else:
raise "Unsupported platform, sorry"
data_files = []
config['files']['data_files'] = data_files
def fix_shinken_cfg(config):
""" Fix paths, user and group in shinken.cfg and daemons/*.ini """
default_paths = {
'lock_file': '/var/run/shinken/arbiterd.pid',
'local_log': '/var/log/shinken/arbiterd.log',
'pidfile': '/var/run/shinken/arbiterd.pid',
'workdir': '/var/run/shinken',
'pack_distribution_file': '/var/lib/shinken/pack_distribution.dat',
'modules_dir': '/var/lib/shinken/modules',
'ca_cert': '/etc/shinken/certs/ca.pem',
'server_cert': '/etc/shinken/certs/server.cert',
'server_key': '/etc/shinken/certs/server.key',
'logdir': '/var/log/shinken',
}
# Changing default user/group if root
default_users = {}
if getpass.getuser() == 'root':
default_users['shinken_user'] = 'shinken'
default_users['shinken_group'] = 'shinken'
default_users['user'] = 'shinken'
default_users['group'] = 'shinken'
default_users['SHINKENUSER'] = 'shinken'
default_users['SHINKENGROUP'] = 'shinken'
default_users['HOME'] = '`grep ^$SHINKENUSER: /etc/passwd | cut -d: -f 6`'
# Prepare pattern for shinken.cfg
pattern = "|".join(default_paths.keys())
changing_path = re.compile("^(%s) *= *" % pattern)
pattern = "|".join(default_users.keys())
changing_user = re.compile("^#(%s) *= *" % pattern)
# Fix shinken.cfg
shinken_cfg_path = os.path.join(config.install_dir,
"etc",
"shinken",
"shinken.cfg")
for line in fileinput.input(shinken_cfg_path, inplace=True):
line = line.strip()
path_attr_name = changing_path.match(line)
user_attr_name = changing_user.match(line)
if path_attr_name:
new_path = os.path.join(config.install_dir,
default_paths[path_attr_name.group(1)].strip("/"))
print("%s=%s" % (path_attr_name.group(1),
new_path))
elif user_attr_name:
print("%s=%s" % (user_attr_name.group(1),
default_users[user_attr_name.group(1)]))
else:
print(line)
# Handle daemons ini files
for ini_file in ["brokerd.ini", "schedulerd.ini", "pollerd.ini", "reactionnerd.ini", "receiverd.ini"]:
# Prepare pattern for ini files
daemon_name = ini_file.strip(".ini")
default_paths['lock_file'] = '/var/run/shinken/%s.pid' % daemon_name
default_paths['local_log'] = '/var/log/shinken/%s.log' % daemon_name
default_paths['pidfile'] = '/var/run/shinken/%s.pid' % daemon_name
pattern = "|".join(default_paths.keys())
changing_path = re.compile("^(%s) *= *" % pattern)
# Fix ini file
shinken_cfg_path = os.path.join(config.install_dir,
"etc",
"shinken",
"daemons",
ini_file)
for line in fileinput.input(shinken_cfg_path, inplace=True):
line = line.strip()
path_attr_name = changing_path.match(line)
user_attr_name = changing_user.match(line)
if path_attr_name:
new_path = os.path.join(config.install_dir,
default_paths[path_attr_name.group(1)].strip("/"))
print("%s=%s" % (path_attr_name.group(1),
new_path))
elif user_attr_name:
print("%s=%s" % (user_attr_name.group(1),
default_users[user_attr_name.group(1)]))
else:
print(line)
# Add shinken_cli.ini
shinken_cli_ini_path = os.path.join(config.install_dir,
"etc",
"shinken",
"shinken_cli.ini")
from shinken.bin.shinken_cli import main
try:
main(["--init", "-c", shinken_cli_ini_path])
except SystemExit:
pass
# Fix shinken_cli.ini
default_clipaths = {'etc': '/etc/shinken',
'lib': '/var/lib/shinken',
}
pattern = "|".join(default_clipaths.keys())
changing_clipath = re.compile("^(%s) *= *" % pattern)
for line in fileinput.input(shinken_cli_ini_path, inplace=True):
line = line.strip()
clipath_attr_name = changing_clipath.match(line)
if clipath_attr_name:
new_path = os.path.join(config.install_dir,
default_clipaths[clipath_attr_name.group(1)].strip("/"))
print("%s=%s" % (clipath_attr_name.group(1),
new_path))
else:
print(line)
# Handle default/shinken
if 'linux' in sys.platform or 'sunos5' in sys.platform:
old_name = os.path.join(config.install_dir, "etc", "default", "shinken.in")
new_name = os.path.join(config.install_dir, "etc", "default", "shinken")
os.rename(old_name, new_name)
default_paths = {
'ETC': '/etc/shinken',
'VAR': '/var/lib/shinken',
'BIN': '/bin',
'RUN': '/var/run/shinken',
'LOG': '/var/log/shinken',
}
pattern = "|".join(default_paths.keys())
changing_path = re.compile("^(%s) *= *" % pattern)
for line in fileinput.input(new_name, inplace=True):
line = line.strip()
path_attr_name = changing_path.match(line)
user_attr_name = changing_user.match(line)
if path_attr_name:
new_path = os.path.join(config.install_dir,
default_paths[path_attr_name.group(1)].strip("/"))
print("%s=%s" % (path_attr_name.group(1),
new_path))
elif user_attr_name:
print("%s=%s" % (user_attr_name.group(1),
default_users[user_attr_name.group(1)]))
else:
print(line)
# Add ENV vars only if we are in virtualenv
# in order to get init scripts working
if 'VIRTUAL_ENV' in os.environ:
activate_file = os.path.join(os.environ.get("VIRTUAL_ENV"), 'bin', 'activate')
try:
afd = open(activate_file, 'r+')
except Exception as exp:
print(exp)
env_config = ("""export PYTHON_EGG_CACHE=.\n"""
"""export SHINKEN_DEFAULT_FILE=%s/etc/default/shinken\n"""
% os.environ.get("VIRTUAL_ENV"))
if afd.read().find(env_config) == -1:
afd.write(env_config)
print("\n"
"=======================================================================================================\n"
"== ==\n"
"== You need to REsource env/bin/activate in order to set appropriate variables to use init scripts ==\n"
"== ==\n"
"=======================================================================================================\n"
)