/
preconfig.py
executable file
·447 lines (397 loc) · 19.6 KB
/
preconfig.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
#!/usr/bin/env python
import argparse
from ast import literal_eval
import json
import logging
import paramiko
import os
import re
import subprocess
import sys
# Testbed Converter Version
__version__ = '1.0'
log = logging.getLogger('preconfig')
log.setLevel(logging.DEBUG)
class Utils(object):
@staticmethod
def initialize_logger(log_file='preconfig.log', log_level=40):
log = logging.getLogger('preconfig')
file_h = logging.FileHandler(log_file)
file_h.setLevel(logging.DEBUG)
stream_h = logging.StreamHandler(sys.stdout)
stream_h.setLevel(log_level)
long_format = '[%(asctime)-15s: %(filename)s:%(lineno)s:%(funcName)s: %(levelname)s] %(message)s'
short_format = '[%(asctime)-15s: %(funcName)s] %(message)s'
file_formatter = logging.Formatter(long_format)
stream_formatter = logging.Formatter(short_format)
file_h.setFormatter(file_formatter)
stream_h.setFormatter(stream_formatter)
log.addHandler(file_h)
log.addHandler(stream_h)
@staticmethod
def is_file_exists(*filenames):
for filename in filenames:
filename = os.path.abspath(os.path.expanduser(filename))
if not os.path.isfile(filename):
raise RuntimeError('file (%s) does not exists' %filename)
return filenames
@staticmethod
def parse_args(args):
parser = argparse.ArgumentParser(description='Server Manager Lite Preconfig Utility',
add_help=True)
parser.add_argument('--version',
action='version',
version=__version__,
help='Print version and exit')
parser.add_argument('-v', action='count', default=0,
help='Increase verbosity. -vvv prints more logs')
parser.add_argument('--server-json',
required=True,
help='Absolute path to testbed file')
parser.add_argument('--server-manager-ip',
required=True,
help='IP Address of Server Manager Node')
parser.add_argument('--server-manager-repo-port',
default=9003,
help='Port Number of Server Manager Node which hosts repos')
parser.add_argument('--log-file',
default='preconfig.log',
help='Absolute path of a file for logging')
cliargs = parser.parse_args(args)
if len(args) == 0:
parser.print_help()
sys.exit(2)
Utils.is_file_exists(cliargs.server_json)
# update log level and log file
log_level = [logging.ERROR, logging.WARN, \
logging.INFO, logging.DEBUG]
cliargs.v = cliargs.v if cliargs.v <= 3 else 3
Utils.initialize_logger(log_file=cliargs.log_file,
log_level=log_level[cliargs.v])
return cliargs
@staticmethod
def preconfig(cliargs):
hosts = []
with open(cliargs.server_json, 'r') as fid:
contents = fid.read()
server_json = json.loads(contents)
for host_dict in server_json['server']:
hostobj = Server(host_dict, args.server_manager_ip,
args.server_manager_repo_port)
hostobj.connect()
hostobj.preconfig()
hosts.append(hostobj)
@staticmethod
def get_net_size(netmask):
netmask_parts = netmask.split('.')
binary_str = ''
for octet in netmask_parts:
binary_str += bin(int(octet))[2:].zfill(8)
return str(len(binary_str.rstrip('0')))
class Server(object):
def __init__(self, server_dict, server_manager_ip,
server_manager_repo_port=9003):
self.server_dict = server_dict
self.server_manager_ip = server_manager_ip
self.server_manager_repo_port = server_manager_repo_port
self.connection = paramiko.SSHClient()
self.connection_timeout = 5
self.username = 'root'
self.export_server_info()
self.os_version = ()
self.extra_packages_12_04 = ['puppet=3.7.3-1puppetlabs1', 'python-netaddr',
'ifenslave-2.6=1.1.0-19ubuntu5', 'sysstat',
'ethtool']
self.extra_packages_14_04 = ['puppet=3.7.3-1puppetlabs1', 'python-netaddr',
'ifenslave-2.6=2.4ubuntu1', 'sysstat',
'ethtool']
def __del__(self):
log.info('Disconnecting...')
self.disconnect()
def export_server_info(self):
for key, value in self.server_dict.items():
setattr(self, key, value)
def set_mgmt_interface(self):
self.mgmt_iface = self.network['management_interface']
def set_mgmt_ip_address(self, ):
self.set_mgmt_interface()
for iface_dict in self.network['interfaces']:
if iface_dict['name'] == self.mgmt_iface:
self.ip, self.cidr = iface_dict['ip_address'].split('/')
def connect(self):
self.set_mgmt_ip_address()
self.connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
self.connection.connect(self.ip, username=self.username, \
password=self.password, \
timeout=self.connection_timeout)
log.info('Connected to Host (%s)' % self.ip)
except Exception, err:
log.error('ERROR: %s' % err)
log.error('ERROR: Unable to connect Host (%s) with username(%s) ' \
'and password(%s)' % (self.ip, self.username, self.password))
raise RuntimeError('Connection to (%s) Failed' % self.ip)
def disconnect(self):
self.connection.close()
def local_exec_cmd(self, cmd, error_on_fail=False):
exit_status = 1
log.info('[localhost]: %s' % cmd)
proc = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
exit_status = 0
log.error(stdout)
log.error(stderr)
if error_on_fail:
raise RuntimeError('Command (%s) Failed' % cmd)
return exit_status, stdout, stderr
def exec_cmd(self, cmd, error_on_fail=False):
exit_status = 1
magic_pattern = r'001902803704605506407308209100'
original_cmd = cmd
cmd += ' && echo %s' % magic_pattern
log.info('[%s]: %s' % (self.ip, original_cmd))
log.debug('[%s]: %s' % (self.ip, cmd))
stdin, stdout, stderr = self.connection.exec_command(cmd)
# check stderr is pending
output = stdout.read()
log.debug(output)
if output.count(magic_pattern) > 0:
exit_status = 0
output = re.sub(r'%s\n$' % magic_pattern, '', output, 1)
if exit_status:
log.warn('Cmd (%s) Failed' % cmd)
log.warn('%s' % stderr.read())
if error_on_fail and exit_status:
log.error('[error_on_fail]: Cmd (%s) Failed' % original_cmd)
raise RuntimeError('[error_on_fail]: Cmd (%s) Failed' % original_cmd)
return exit_status, output
def get_os_version(self):
log.debug('Retrieve OS version')
cmd = r'python -c "import platform; print platform.linux_distribution()"'
status, output = self.exec_cmd(cmd)
version_info = literal_eval(output)
return version_info
def set_os_version(self):
self.os_version = self.get_os_version()
def preconfig(self):
self.set_os_version()
#self.preconfig_verify_domain()
self.preconfig_hosts_file()
self.preconfig_unauthenticated_packages()
self.preconfig_repos()
self.install_packages()
self.setup_interface()
# Setup static routes if defined
if getattr(self, 'static_routes', None):
self.setup_static_routes()
self.preconfig_ntp_config()
self.preconfig_puppet_config()
def verify_puppet_host(self):
ping_cmd = r'ping -q -c 1 puppet > /dev/null 2>@1'
puppet_cmd = r'grep puppet /etc/hosts | grep -v "^[ ]*#"'
status, old_entry = self.exec_cmd(puppet_cmd)
old_entry = old_entry.strip()
if status:
log.info('Seems puppet host is not configured')
log.info('Adding puppet alias to /etc/hosts file')
puppet_cmd = 'echo %s puppet >> /etc/hosts' % self.server_manager_ip
self.exec_cmd(puppet_cmd, error_on_fail=True)
else:
log.info('Seems puppet host is already configured. ' \
'Replacing with Server Manager (%s) entry' % self.server_manager_ip)
self.exec_cmd(r"sed -i 's/%s/%s puppet/g' /etc/hosts" % (
old_entry, self.server_manager_ip),
error_on_fail=True)
log.debug('Verify puppet host after configuration')
self.exec_cmd(ping_cmd, error_on_fail=True)
def verify_setup_hostname(self):
if not self.id:
log.error('Hostname is not configured')
raise RuntimeError('Hostname is not configured for (%s)' % self.ip)
def preconfig_hosts_file(self):
self.verify_puppet_host()
self.verify_setup_hostname()
def preconfig_verify_domain(self):
if self.domain == "":
log.error('Domain name is not configured. ' \
'All target nodes has to be setup with proper domain name')
raise RuntimeError('Domain name is not configured for (%s)' % self.ip)
def preconfig_unauthenticated_packages(self):
apt_auth = r'APT::Get::AllowUnauthenticated \"true\"\;'
status, output = self.exec_cmd('grep --quiet \"^%s\" /etc/apt/apt.conf' % apt_auth)
if status:
log.info('Configure Allow Unauthenticated true')
self.exec_cmd('echo %s >> /etc/apt/apt.conf' % apt_auth, error_on_fail=True)
def preconfig_repos(self):
repo_entry = r'deb http://%s:%s/thirdparty_packages/ ./' % ('puppet', self.server_manager_repo_port)
repo_entry_verify = r'%s.*\/thirdparty_packages' % 'puppet'
status, output = self.exec_cmd('apt-cache policy | grep "%s"' % repo_entry_verify)
if status:
log.info('/etc/apt/sources.list has no thirdparty_packages '
'repo entry')
log.debug('Backup existing sources.list')
self.exec_cmd(r'cp /etc/apt/sources.list '\
'/etc/apt/sources.list_$(date +%Y_%m_%d__%H_%M_%S).contrailbackup')
log.debug('Adding Repo Entry (%s) to /etc/apt/sources.list' % repo_entry)
self.exec_cmd('echo >> /etc/apt/sources.list', error_on_fail=True)
self.exec_cmd(r"sed -i '1 i\%s' /etc/apt/sources.list" % repo_entry)
self.exec_cmd('apt-get update')
self.exec_cmd('apt-cache policy | grep "%s"' % repo_entry_verify,
error_on_fail=True)
def install_packages(self):
os_type, version, misc = self.os_version
if os_type.lower() == 'ubuntu' and version == '12.04':
packages_list = self.extra_packages_12_04
elif os_type.lower() == 'ubuntu' and version == '14.04':
packages_list = self.extra_packages_14_04
else:
raise RuntimeError('UnSupported OS type (%s)' % self.os_version)
for package in packages_list:
self.exec_cmd('apt-get -y install %s' % package,
error_on_fail=True)
def verify_interface_ip(self, interface, ip):
return self.exec_cmd('ip addr show %s | grep %s' % (interface, ip))
def verify_static_route_ip(self, inet_prefix, device):
return self.exec_cmd('ip route show %s | grep %s' % (inet_prefix, device))
def exec_setup_interface(self, iface_info, error_on_fail=True):
iface_script_path = '/opt/contrail/bin/interface_setup.py'
cmd = r'%s ' % iface_script_path
cmd += r'--device %s --ip %s ' % (iface_info['name'],
iface_info['ip_address'])
if 'member_interfaces' in iface_info.keys():
cmd += r'--members %s ' % " ".join(iface_info['member_interfaces'])
if iface_info['ip_address'] == self.ip and 'gateway' in iface_info.keys():
cmd += r'--gw %s ' % iface_info['gateway']
if 'vlan' in iface_info.keys():
cmd += r'--vlan %s ' % iface_info['vlan']
if 'bond_options' in iface_info.keys():
cmd += r"--bond-opts '%s'" % json.dumps(iface_info['bond_options'])
status, output = self.exec_cmd(cmd)
if error_on_fail and status:
raise RuntimeError('Setup Interface failed for ' \
'Iface Info (%s)' % iface_info)
return status, output
def exec_setup_static_routes(self, static_route, error_on_fail=True):
iface_script_path = "/opt/contrail/bin/staticroute_setup.py"
cmd = r'%s ' % iface_script_path
cmd += r'--device %s ' % (static_route['intf'])
if 'ip' in static_route.keys():
cmd += r'--network %s ' % static_route['ip']
if 'gw' in static_route.keys():
cmd += r'--gw %s ' % static_route['gw']
if 'netmask' in static_route.keys():
cmd += r'--netmask %s ' % static_route['netmask']
if 'vlan' in static_route.keys():
cmd += r'--vlan %s ' % static_route['vlan']
status, output = self.exec_cmd(cmd)
if error_on_fail and status:
raise RuntimeError('Setup Interface failed for ' \
'Iface Info (%s)' % static_route)
return status, output
def setup_interface(self):
script_path = os.path.abspath(sys.argv[0])
iface_script_path = os.path.join(os.path.dirname(script_path), 'interface_setup.py')
sftp_connection = self.connection.open_sftp()
self.exec_cmd('mkdir -p /opt/contrail/bin/')
sftp_connection.put(iface_script_path,
'/opt/contrail/bin/interface_setup.py')
self.exec_cmd('chmod 755 /opt/contrail/bin/interface_setup.py')
for iface_info in self.network['interfaces']:
status, output = self.verify_interface_ip(iface_info['name'],
iface_info['ip_address'])
if not status:
log.warn('Interface (%s) already configured with ' \
'IP Address (%s)' % (iface_info['name'],
iface_info['ip_address']))
else:
self.exec_setup_interface(iface_info)
def setup_static_routes(self):
script_path = os.path.abspath(sys.argv[0])
staticroute_script_path = os.path.join(os.path.dirname(script_path), 'staticroute_setup.py')
sftp_connection = self.connection.open_sftp()
self.exec_cmd('mkdir -p /opt/contrail/bin/')
sftp_connection.put(staticroute_script_path, '/opt/contrail/bin/staticroute_setup.py')
self.exec_cmd('chmod 755 /opt/contrail/bin/staticroute_setup.py')
for static_route in self.static_routes:
inet_prefix = str(static_route['ip']) + "/" + str(Utils.get_net_size(static_route['netmask']))
status, output = self.verify_static_route_ip(inet_prefix, static_route['intf'])
if not status:
log.warn('Static Route for interface (%s) already configured with ' \
'IP Address (%s)' % (static_route['intf'],
static_route['ip']))
else:
self.exec_setup_static_routes(static_route)
def check_ntp_status(self):
status, output = self.exec_cmd(r'ntpq -pn | grep "%s" ' % self.server_manager_ip)
if status:
self.setup_ntp()
def setup_ntp(self):
log.debug('Install ntp package')
self.exec_cmd('apt-get -y install ntp', error_on_fail=True)
log.debug('Setup NTP configuration')
self.exec_cmd('ntpdate %s' % self.server_manager_ip)
log.debug('Backup existing ntp.conf')
self.exec_cmd(r'mv /etc/ntp.conf /etc/ntp.conf.$(date +%Y_%m_%d__%H_%M_%S).contrailbackup',
error_on_fail=True)
self.exec_cmd('touch /var/lib/ntp/drift', error_on_fail=True)
ntp_config = 'driftfile /var/lib/ntp/drift\n' \
'server %s\n' \
'restrict 127.0.0.1\n' \
'restrict -6 ::1\n' \
'includefile /etc/ntp/crypto/pw\n' \
'keys /etc/ntp/keys' % self.server_manager_ip
if self.ip == self.server_manager_ip:
ntp_config = ntp_config.replace('server %s' % self.server_manager_ip, 'server 127.127.1.0 iburst maxpoll 9')
self.exec_cmd(r'echo "%s" >> /etc/ntp.conf' % ntp_config,
error_on_fail=True)
def restart_ntp_service(self):
self.exec_cmd('service ntp restart', error_on_fail=True)
def preconfig_ntp_config(self):
self.check_ntp_status()
self.restart_ntp_service()
def setup_puppet_configs(self):
log.info('Setup puppet Configs')
puppet_config = '[agent]\n' \
'pluginsync = true\n' \
'ignorecache = true\n' \
'usecacheonfailure = false\n' \
'ordering = manifest\n' \
'report = true\n' \
'stringify_facts = false\n' \
'[main]\n' \
'runinterval = 10\n' \
'configtimeout = 500\n'
self.exec_cmd(r'cp /etc/puppet/puppet.conf /etc/puppet/puppet.conf.$(date +%Y_%m_%d__%H_%M_%S).contrailbackup',
error_on_fail=True)
self.exec_cmd(r'echo "%s" >> /etc/puppet/puppet.conf' % puppet_config,
error_on_fail=True)
def update_default_puppet(self):
log.info('Update default puppet config file for non-server-manager node')
self.exec_cmd(r'sed -i "s/initialize(name, path, source, ignore = nil, environment = nil, source_permissions = :ignore)/initialize(name, path, source, ignore = nil, environment = nil, source_permissions = :use)/g" /usr/lib/ruby/vendor_ruby/puppet/configurer/downloader.rb', error_on_fail=True)
self.exec_cmd(r"sed -i 's/START=.*$/START=yes/' /etc/default/puppet",
error_on_fail=True)
def remove_puppet_ssl(self):
log.info('Remove puppet ssl for non-server-manager node')
if self.ip != self.server_manager_ip:
if self.domain:
fqdn = '%s.%s' % (self.id, self.domain)
else:
fqdn = self.id
self.local_exec_cmd(r'puppet cert list %s && puppet cert clean %s' % (fqdn, fqdn))
#self.exec_cmd(r'find /var/lib/puppet/ssl -name %s*.pem -delete' % fqdn, error_on_fail=True)
self.exec_cmd(r'rm -rf /var/lib/puppet/ssl')
def restart_puppet_service(self):
self.exec_cmd(r'service puppet restart', error_on_fail=True)
def preconfig_puppet_config(self):
self.setup_puppet_configs()
self.remove_puppet_ssl()
self.update_default_puppet()
self.restart_puppet_service()
if __name__ == '__main__':
args = Utils.parse_args(sys.argv[1:])
log.info('Executing: %s' % " ".join(sys.argv))
Utils.preconfig(args)