This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
opstools-server-installation.py
executable file
·204 lines (189 loc) · 8.49 KB
/
opstools-server-installation.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
#!/usr/bin/env python
import argparse
import os
import shutil
import subprocess
import sys
class ConfigInstaller(object):
def __init__(self):
self.options = []
self.home = os.environ['HOME']
self.command = ''
def parse_arguments(self):
formatter = argparse.RawDescriptionHelpFormatter
uses = '''Examples:
*Install availability monitoring and centralized logging on localhost:
opstools-server-installation --am-hosts localhost --log-hosts localhost
*Install performance monitoring on a server IP:
opstools-server-installation --pm-hosts IP
*Install performance and availability monitoring on a server:
opstools-server-installation --pm-hosts IP --am-hosts IP
*Just create the inventory and overwritten if it exits:
opstools-server-installation -f --no-x '''
parser = argparse.ArgumentParser(prog='opstools-server-installation',
formatter_class=formatter,
description=(
'Script to set up the'
' operational servers.'
),
epilog=uses)
parser.add_argument('-i', '--inventory',
dest='opstoolinvpath',
help=(
'Opstool inventory path. '
'default= ~/.opstools.inventory'
),
default=self.home + '/.opstools.inventory')
parser.add_argument('--pm-hosts',
dest='pm_hosts',
action='append',
help=(
'Performance monitoring server IP. '
'default = []'
),
default=[])
parser.add_argument('--am-hosts',
dest='am_hosts',
action='append',
help=(
'Available monitoring server IP. '
'default = []'
),
default=[])
parser.add_argument('--log-hosts',
dest='logging_hosts',
action='append',
help=(
'Logging monitoring server IP. '
'default = localhost'
),
default=[])
parser.add_argument('-qs',
dest='ooo_ssh_ansible',
help=(
'Quickstart config path. '
'default=~/.quickstart/ssh.config.ansible'
),
default='{}/.quickstart/ssh.config.ansible'.format(
self.home))
parser.add_argument('--playbook',
dest='playbook',
type=str,
help='Playbook to run',
choices=('playbook.yml',
'playbook-post-install.yml'),
default='playbook.yml')
parser.add_argument('-f',
dest='force',
help='Overwrite the config file',
action='store_true',
default=False)
parser.add_argument('-e',
dest='parameters',
type=str,
help='Extra parameters file.yml')
parser.add_argument('--no-x',
dest='no_exec',
default=False,
action='store_true',
help='Dont run ansible-playbook')
parser.add_argument('--data-path',
dest='data_path',
default='/usr/share/opstools-ansible',
help=(
'Path where the playbooks, roles, inventory '
' and so on are located. default location is '
'/usr/share/opstools-ansible'
))
self.options = parser.parse_args()
self.parser = parser
def _parse_hosts(self):
hosts = {'all': [],
'split': {
'pm_hosts': [],
'am_hosts': [],
'logging_hosts': []
}}
for host_type in ('pm_hosts', 'am_hosts', 'logging_hosts'):
for host in getattr(self.options, host_type):
hosts['split'][host_type].append(host)
hosts['all'].append(host)
return hosts
def _create_hosts_file(self):
_path = os.path.join(self.options.opstoolinvpath, 'hosts')
hosts = self._parse_hosts()
if ((not os.path.exists(_path) or self.options.force) and
len(hosts['all']) > 0):
with open(_path, 'w') as hosts_file:
for host_type in hosts['split'].keys():
hosts_file.write('[{}]\n'.format(host_type))
for host in hosts['split'][host_type]:
hosts_file.write('{}\n'.format(host))
hosts_file.write('\n')
hosts_file.write("[targets]\n")
for host in set(hosts['all']):
if host in ('localhost', 'localhost6', '127.0.0.1', '::1'):
hosts_file.write('{} ansible_connection=local'
'\n'.format(host))
else:
hosts_file.write('{} ansible_connection=ssh '
'ansible_user=root\n'.format(
host
)
)
hosts_file.write('undercloud_host ansible_user=stack '
'ansible_host=undercloud '
'ansible_ssh_extra_args=\'-F {}\'\n'.format(
self.options.ooo_ssh_ansible
)
)
else:
print('\x1b[34m INFO: the inventory file was not'
' created/updated\x1b[0m')
def create_configuration(self):
try:
# destination file
_dest_f = os.path.join(self.options.opstoolinvpath,
'structure')
# origin file
_source_f = os.path.join(self.options.data_path,
'inventory',
'structure')
if not os.path.isdir(self.options.opstoolinvpath):
os.makedirs(self.options.opstoolinvpath)
if not os.path.exists(_dest_f) or self.options.force:
shutil.copy(_source_f, _dest_f)
self._create_hosts_file()
except Exception:
print('Error: There was a problem creating the inventory')
self.parser.print_help()
sys.exit(-1)
def create_command(self):
self.command = (
'ansible-playbook -i {inventory} {path}/{playbook}'.format(
inventory=self.options.opstoolinvpath,
path=self.options.data_path,
playbook=self.options.playbook
)
)
if self.options.parameters:
if os.path.exists(self.options.parameters):
self.command = '{command} -e {parameters}'.format(
command=self.command,
parameters=self.options.parameters)
else:
print('Error: The file {parameters} does not exits'
.format(parameters=self.options.parameters))
self.parser.print_usage()
sys.exit(-1)
def main():
config = ConfigInstaller()
config.parse_arguments()
config.create_configuration()
config.create_command()
if not config.options.no_exec:
subprocess.call(config.command, shell=True)
else:
print('To continue:\n\t{}'.format(config.command))
if __name__ == "__main__":
main()