-
Notifications
You must be signed in to change notification settings - Fork 2
/
make-portal-config
executable file
·206 lines (176 loc) · 6.55 KB
/
make-portal-config
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
#!/usr/bin/env python
# Generate a config file for the jacks-context generator.
import argparse
import datetime
import json
import sys
import xmlrpclib
from urlparse import urlparse
exogeni_vm_types = [
"XOLarge",
"XOMedium",
"XOSmall",
"XOXlarge",
]
exogeni_raw_types = [
"ExoGENI-M4"
]
DEFAULT_SR_URL = 'https://ch.geni.net:8444/SR'
DEFAULT_IR_URL = 'http://geni.renci.org:15080/registry/'
DEFAULT_IR_SINCE = '2014-01-01'
SR_TYPE_AGGREGATE = 0
SR_ATTRIBUTES = '_GENI_SERVICE_ATTRIBUTES'
class ChapiResponse(object):
def __init__(self, response):
self.code = response['code']
self.value = response['value']
self.output = response['output']
class Aggregate(object):
def __init__(self, sr_record):
self.url = sr_record['SERVICE_URL']
self.amtype = sr_record[SR_ATTRIBUTES]['UI_AM_TYPE']
self.categories = sr_record[SR_ATTRIBUTES]['UI_AM_CAT'].split()
self.name = sr_record['SERVICE_NAME']
self.urn = sr_record['SERVICE_URN']
def isExoGENI(self):
return self.amtype == 'ui_exogeni_am'
def isInstaGENI(self):
if self.amtype == 'ui_instageni_am':
return True
elif self.amtype == 'ui_other_am':
url = urlparse(self.url)
return url.port == 12369
else:
return False
def isOpenGENI(self):
if self.amtype == 'ui_other_am':
url = urlparse(self.url)
return url.port == 5002
else:
return False
def isStitchable(self):
return 'ui_stitchable_cat' in self.categories
def parse_ir_date(string):
format = '%Y-%m-%d'
try:
return datetime.datetime.strptime(string, format)
except:
msg = 'Invalid date, use format YYYY-MM-DD'
raise argparse.ArgumentTypeError(msg)
def parseArgs(argv):
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input',
help='jacks config file template (json)')
parser.add_argument('-o', '--output',
help='location for resulting jacks json config file')
parser.add_argument('--sr-url',
default=DEFAULT_SR_URL,
help='Service Registry URL')
parser.add_argument('--ir-url',
default=DEFAULT_IR_URL,
help='ExoGENI Image Registry URL')
parser.add_argument('--ir-since',
type=parse_ir_date,
default=DEFAULT_IR_SINCE,
help='Use ExoGENI Image newer than YYYY-MM-DD')
args = parser.parse_args(argv)
return args
def get_aggregates(sr_url):
"""Return a list of Aggregate instances retrieved from the service
registry.
"""
# There's a bug that will be fixed on 04-Mar-2015 that will allow us
# to use get_services_of_type
# aggs = sr_proxy.get_services_of_type(SR_TYPE_AGGREGATE)
sr_proxy = xmlrpclib.ServerProxy(sr_url)
result = ChapiResponse(sr_proxy.get_services())
if result.code != 0:
raise Exception('SR Error: %s' % (result.output))
aggs = [Aggregate(s) for s in result.value if s['SERVICE_TYPE'] == 0]
return aggs
def insert_agg_data(config, aggs):
"""Loop once through the aggregates putting each into its appropriate
buckets by type and stitchable and compute.
"""
eg_aggs = list()
eg_stitchable = list()
ig_aggs = list()
ig_stitchable = list()
og_aggs = list()
og_stitchable = list()
compute_aggs = list()
for a in aggs:
# print a.amtype, ' => ', a.url
if a.isExoGENI():
compute_aggs.append(a)
eg_aggs.append(a)
eg_stitchable.append(a)
if (a.isStitchable()):
ig_stitchable.append(a)
elif a.isInstaGENI():
compute_aggs.append(a)
ig_aggs.append(a)
if (a.isStitchable()):
ig_stitchable.append(a)
elif a.isOpenGENI():
compute_aggs.append(a)
og_aggs.append(a)
if (a.isStitchable()):
ig_stitchable.append(a)
# Would prefer dictionary comprehensions but we need 2.6 compatibility
config['aggregate_names'] = dict((a.urn, a.name) for a in compute_aggs)
agg_types = dict((a.urn, 'eg') for a in eg_aggs)
agg_types.update(dict((a.urn, 'ig') for a in ig_aggs))
agg_types.update(dict((a.urn, 'og') for a in og_aggs))
config['aggregate_types'] = agg_types
config['stitchable_eg'] = [a.urn for a in eg_stitchable]
config['stitchable_ig'] = [a.urn for a in ig_stitchable]
#----------------------------------------------------------------------
# Advertise images in the ExoGENI image registry
#----------------------------------------------------------------------
def parse_image_date(s):
format = '%Y-%m-%d %H:%M:%S.%f'
return datetime.datetime.strptime(s, format)
def parse_image_default(s):
return s.lower() == 'true'
def add_exogeni_images(config, ir_url, ir_since):
proxy=xmlrpclib.ServerProxy(ir_url)
#proxy.registryService.getDefaultImage()
all_images = proxy.registryService.getAllImages()
exo_images = []
for image in all_images:
name = image['ImageName']
url = image['ImageURL']
csum = image['ImageHash']
date = parse_image_date(image['ImageDate'])
default = parse_image_default(image['ImageDefault'])
if default or date >= ir_since:
exo_images.append(dict(name=name, id=url, version=csum))
# Write the ExoGENI images to the config.
#
# NOTE: this will overwrite existing images. I presume this is ok
# since ExoGENI is the only AM type that does not advertise
# images.
config['extra']['images'] = exo_images
# Add a constraint associating all ExoGENI images with the
# appropriate VM types.
image_urls = [i['id'] for i in exo_images]
extra_constraints = config['extra']['constraints']
extra_constraints.append(dict(node=dict(aggregates='*',
images=image_urls,
types=exogeni_vm_types)))
config['extra']['constraints'] = extra_constraints
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
args = parseArgs(argv)
with open(args.input, 'r') as f:
jacksConfig = json.load(f)
all_aggs = get_aggregates(args.sr_url)
insert_agg_data(jacksConfig, all_aggs)
add_exogeni_images(jacksConfig, args.ir_url, args.ir_since)
with open(args.output, 'w') as f:
json.dump(jacksConfig, f, indent=2, sort_keys=True)
return 0
if __name__ == '__main__':
exit(main())