-
Notifications
You must be signed in to change notification settings - Fork 6
/
ossipee.py
329 lines (257 loc) · 10.6 KB
/
ossipee.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
#!/usr/bin/python
import argparse
import gi
import json
import logging
import os
import re
import shlex
import six
import sys
gi.require_version('GnomeKeyring', '1.0')
from glanceclient import client as glanceclient
from novaclient import client as novaclient
from neutronclient.neutron import client as neutronclient
import os_client_config
import depend
import planning
import work
def all_items_factory(resolver, name):
all = [(depend.WorkItemList, 'networks'),
work.SecurityGroup,
work.AllServers,
work.HostsEntries,
work.Inventory]
return depend.UnnamedComponentList(resolver, all)
def all_networks_factory(resolver, name=None):
plan = resolver.resolve(planning.Plan)
components = list()
if plan.public_network:
components.append((depend.WorkItemList, 'public-network'))
if plan.private_network:
components.append((depend.WorkItemList, 'private-network'))
return depend.UnnamedComponentList(resolver, components)
def all_servers_factory(resolver, name=None):
nova = resolver.resolve(novaclient.Client)
plan = resolver.resolve(planning.Plan)
domain_name = plan.domain_name
servers = depend.WorkItemList(
[resolver.resolve_named(work.Server, server_name)
for server_name in plan.hosts],
resolver, False)
float_ips = depend.WorkItemList(
[resolver.resolve_named(work.FloatIP, server_name)
for server_name in plan.hosts], resolver, False)
return work.AllServers(nova, domain_name, servers, float_ips)
def anisble_playbook_factory(resolver, name):
plan = resolver.resolve(planning.Plan)
return work.AnsiblePlaybook(plan.inventory_file, plan.ansible_playbook)
def args_factory(resolver):
parser = resolver.resolve(argparse.ArgumentParser)
args = parser.parse_args()
return args
def float_ip_factory(resolver, name):
plan = resolver.resolve(planning.Plan)
fqdn = plan.make_fqdn(name)
cloud_user = plan.profile['cloud_user']
nova = resolver.resolve(novaclient.Client)
neutron = resolver.resolve(neutronclient.Client)
return work.FloatIP(nova, neutron, fqdn, cloud_user)
def hosts_entries_factory(resolver, name=None):
nova = resolver.resolve(novaclient.Client)
plan = resolver.resolve(planning.Plan)
hosts = plan.hosts.values()
cloud_user = plan.profile['cloud_user']
domain_name = plan.domain_name
return work.HostsEntries(resolver, nova, hosts, cloud_user, domain_name)
def host_worker_factory(resolver, name):
work_items = [
work.Server,
work.FloatIP,
work.HostsEntries,
work.Inventory
]
return depend.NamedComponentList(resolver, work_items, name)
def inventory_factory(resolver, name=None):
plan = resolver.resolve(planning.Plan)
nova = resolver.resolve(novaclient.Client)
directory = plan.deployment_dir
hosts = plan.hosts
ipa_vars = plan.ipa_client_vars
inventory_file = plan.inventory_file
return work.Inventory(nova, inventory_file, directory, hosts, ipa_vars)
def network_factory(resolver, name):
neutron = resolver.resolve(neutronclient.Client)
plan = resolver.resolve(planning.Plan)
network_name = plan.build_network_name(name)
return work.Network(neutron, network_name)
def neutron_client_factory(resolver, name=None):
neutron = os_client_config.make_client('network')
neutron.format = 'json'
return neutron
def nova_client_factory(resolver, name=None):
nova_client = os_client_config.make_client('compute')
return nova_client
def glance_client_factory(resolver, name=None):
glance_client = os_client_config.make_client('image')
return glance_client
# TODO: this needs to be named. It is just a server, not *the* nova server
def nova_server_factory(resolver, name):
nova = resolver.resolve(novaclient.Client)
neutron = resolver.resolve(neutronclient.Client)
glance = resolver.resolve(glanceclient.Client)
plan = resolver.resolve(planning.Plan)
spec = plan.hosts[name]
return work.Server(nova, neutron, glance, spec)
def parser_factory(resolver, name=None):
parser = argparse.ArgumentParser(description='')
parser.add_argument('-s', '--section',
dest='section',
default='scope')
parser.add_argument('worker', nargs='?', default='all',
help='Worker to execute, defaults to "all"')
return parser
def plan_factory(resolver, name=None):
auth_url=os.environ.get('OS_AUTH_URL', '')
parser = resolver.resolve(argparse.ArgumentParser)
args = parser.parse_args()
plan = planning.Plan(args.section, auth_url)
return plan
def router_factory(resolver, name):
plan = resolver.resolve(planning.Plan)
neutron = resolver.resolve(neutronclient.Client)
router_name = plan.router_name(name)
return work.Router(neutron, name, router_name)
def router_interface_factory(resolver, name):
plan = resolver.resolve(planning.Plan)
name = name
neutron = resolver.resolve(neutronclient.Client)
router_name = plan.router_name(name)
subnet_name = plan.subnet_name(name)
return work.RouterInterface(neutron, name, router_name, subnet_name)
def security_group_factory(resolver, name=None):
nova = resolver.resolve(novaclient.Client)
neutron = resolver.resolve(neutronclient.Client)
plan = resolver.resolve(planning.Plan)
security_groups = []
security_ports = {}
for group, ports in plan.security_ports.items():
sec_group = "%s-%s" % (plan.name, group)
security_groups.append(sec_group)
security_ports[sec_group] = ports
return work.SecurityGroup(nova, neutron, security_groups, security_ports)
def worker_factory(resolver, name=None):
args = resolver.resolve("args")
return args.worker
def subnet_factory(resolver, name):
neutron = resolver.resolve(neutronclient.Client)
plan = resolver.resolve(planning.Plan)
network_name = plan.build_network_name(name)
cidr = plan.networks[name]['cidr']
subnet_name = plan.subnet_name(name)
return work.SubNet(neutron, name, network_name, cidr, subnet_name)
def enable_logging():
logging.basicConfig(level=logging.DEBUG)
class WorkerApplication(object):
def __init__(self):
resolver = depend.global_resolver
item = resolver.resolve("worker")
self.work_item_list = resolver.resolve(depend.WorkItemList, item)
def create(self, *args, **kwargs):
return self.work_item_list.create(*args, **kwargs)
def teardown(self, *args, **kwargs):
return self.work_item_list.teardown(*args, **kwargs)
def display(self, *args, **kwargs):
return self.work_item_list.display(*args, **kwargs)
def list(self, *args, **kwargs):
def list_registered(clazz):
worker_keys = []
for proxy_key in depend.GLOBAL_SCOPE.proxy_map:
try:
isinstance(depend.WorkItemList, proxy_key[0])
except TypeError:
continue
worker_keys.append(proxy_key[1])
print(sorted(worker_keys))
list_registered(depend.WorkItemList)
# So, we can lie. Since Python is not strictly typed,
# we do not need to return an actual instance of a class,
# but rather a component that conforms to the same
# interface as the class. In the case of WorkItemList,
# it must implement the methods create, tear_down, and display
depend.register_named(depend.WorkItemList, 'ipa', host_worker_factory)
depend.register_named(depend.WorkItemList, 'controller', host_worker_factory)
depend.register_named(depend.WorkItemList, 'openstack', host_worker_factory)
depend.register_named(depend.WorkItemList, 'keycloak', host_worker_factory)
depend.register_named(depend.WorkItemList, 'rippowam',
anisble_playbook_factory)
depend.register_named(depend.WorkItemList, 'ansible',
anisble_playbook_factory)
depend.register_named(depend.WorkItemList, 'all', all_items_factory)
depend.register_named(depend.WorkItemList, 'networks', all_networks_factory)
depend.register_named(depend.WorkItemList, 'security_group',
security_group_factory)
depend.register_named(depend.WorkItemList, 'inventory', inventory_factory)
depend.register_named(
depend.WorkItemList,
'servers',
lambda resolver, name: depend.NamedComponentList(
resolver, [work.AllServers, work.HostsEntries, work.Inventory], name))
depend.register_named(
depend.WorkItemList,
'public-network',
lambda resolver, name: depend.NamedComponentList(
resolver, [work.Router,
work.Network,
work.SubNet,
work.RouterInterface], 'public'))
depend.register_named(
depend.WorkItemList,
'private-network',
lambda resolver, name: depend.NamedComponentList(
resolver, [work.Network,
work.SubNet], 'private'))
depend.register(work.Network, network_factory)
depend.register(work.SubNet, subnet_factory)
depend.register(work.Router, router_factory)
depend.register(work.RouterInterface, router_interface_factory)
depend.register(work.SecurityGroup, security_group_factory)
depend.register(work.FloatIP, float_ip_factory)
depend.register(work.Server, nova_server_factory)
depend.register(work.AllServers, all_servers_factory)
depend.register(work.HostsEntries, hosts_entries_factory)
depend.register(work.Inventory, inventory_factory)
depend.register(argparse.ArgumentParser, parser_factory)
depend.register(planning.Plan, plan_factory)
depend.register("args", args_factory)
depend.register("worker", worker_factory)
depend.register(glanceclient.Client, glance_client_factory)
depend.register(novaclient.Client, nova_client_factory)
depend.register(neutronclient.Client, neutron_client_factory)
def main():
args_file = sys.argv[1]
args_data = file(args_file).read()
arguments = shlex.split(args_data)
worker = 'all'
action = WorkItemList.display
for arg in arguments:
# ignore any arguments without an equals in it
if '=' in arg:
(key, value) = arg.split('=')
if key == 'worker':
worker = workers[value]
if key == 'action':
if value == 'create':
action = WorkItemList.create
elif value == 'teardown':
action = WorkItemList.teardown
elif value == 'display':
action = WorkItemList.display
logging.basicConfig(level=logging.ERROR)
action(worker)
print (json.dumps({
'success': True,
'args': args_data
}))
if __name__ == '__main__':
main()