4 from sfa.util.faults import MissingSfaInfo, UnknownSfaType, \
5 RecordNotFound, SfaNotImplemented, SliverDoesNotExist
7 from sfa.util.sfalogging import logger
8 from sfa.util.defaultdict import defaultdict
9 from sfa.util.sfatime import utcparse, datetime_to_string, datetime_to_epoch
10 from sfa.util.xrn import Xrn, hrn_to_urn, get_leaf, urn_to_sliver_id
11 from sfa.util.cache import Cache
12 # used to be used in get_ticket
13 #from sfa.trust.sfaticket import SfaTicket
15 from sfa.rspecs.version_manager import VersionManager
16 from sfa.rspecs.rspec import RSpec
18 # the driver interface, mostly provides default behaviours
19 from sfa.managers.driver import Driver
20 from sfa.openstack.nova_shell import NovaShell
21 from sfa.openstack.euca_shell import EucaShell
22 from sfa.openstack.osaggregate import OSAggregate
23 from sfa.plc.plslices import PlSlices
24 from sfa.util.osxrn import OSXrn
27 def list_to_dict(recs, key):
29 convert a list of dictionaries into a dictionary keyed on the
30 specified dictionary key
32 return dict ( [ (rec[key],rec) for rec in recs ] )
35 # PlShell is just an xmlrpc serverproxy where methods
36 # can be sent as-is; it takes care of authentication
37 # from the global config
39 class NovaDriver (Driver):
41 # the cache instance is a class member so it survives across incoming requests
44 def __init__ (self, config):
45 Driver.__init__ (self, config)
46 self.shell = NovaShell (config)
47 self.euca_shell = EucaShell(config)
49 if config.SFA_AGGREGATE_CACHING:
50 if NovaDriver.cache is None:
51 NovaDriver.cache = Cache()
52 self.cache = NovaDriver.cache
54 ########################################
55 ########## registry oriented
56 ########################################
58 ########## disabled users
59 def is_enabled (self, record):
60 # all records are enabled
63 def augment_records_with_testbed_info (self, sfa_records):
64 return self.fill_record_info (sfa_records)
67 def register (self, sfa_record, hrn, pub_key):
68 type = sfa_record['type']
69 pl_record = self.sfa_fields_to_pl_fields(type, hrn, sfa_record)
72 acceptable_fields=['url', 'instantiation', 'name', 'description']
73 # add slice description, name, researchers, PI
77 # add person roles, projects and keys
82 # xxx actually old_sfa_record comes filled with plc stuff as well in the original code
83 def update (self, old_sfa_record, new_sfa_record, hrn, new_key):
84 pointer = old_sfa_record['pointer']
85 type = old_sfa_record['type']
87 # new_key implemented for users only
88 if new_key and type not in [ 'user' ]:
89 raise UnknownSfaType(type)
92 # can update description, researchers and PI
95 # can update slices, keys and roles
101 def remove (self, sfa_record):
102 type=sfa_record['type']
103 name = Xrn(sfa_record['hrn']).get_leaf()
105 if self.shell.auth_manager.get_user(name):
106 self.shell.auth_manager.delete_user(name)
107 elif type == 'slice':
108 if self.shell.auth_manager.get_project(name):
109 self.shell.auth_manager.delete_project(name)
114 def fill_record_info(self, records):
116 Given a (list of) SFA record, fill in the PLC specific
117 and SFA specific fields in the record.
119 if not isinstance(records, list):
122 for record in records:
123 name = Xrn(record['hrn']).get_leaf()
125 if record['type'] == 'user':
126 os_record = self.shell.auth_manager.get_user(name)
127 projects = self.shell.db.project_get_by_user(name)
128 record['slices'] = [self.hrn + "." + proj.name for \
130 record['roles'] = self.shell.db.user_get_roles(name)
131 keys = self.shell.db.key_pair_get_all_by_user(name)
132 record['keys'] = [key.public_key for key in keys]
133 elif record['type'] == 'slice':
134 os_record = self.shell.auth_manager.get_project(name)
135 record['description'] = os_record.description
136 record['PI'] = [self.hrn + "." + os_record.project_manager.name]
137 record['geni_creator'] = record['PI']
138 record['researcher'] = [self.hrn + "." + user for \
139 user in os_record.member_ids]
142 record['geni_urn'] = hrn_to_urn(record['hrn'], record['type'])
143 record['geni_certificate'] = record['gid']
144 record['name'] = os_record.name
145 #if os_record.created_at is not None:
146 # record['date_created'] = datetime_to_string(utcparse(os_record.created_at))
147 #if os_record.updated_at is not None:
148 # record['last_updated'] = datetime_to_string(utcparse(os_record.updated_at))
154 # plcapi works by changes, compute what needs to be added/deleted
155 def update_relation (self, subject_type, target_type, subject_id, target_ids):
156 # hard-wire the code for slice/user for now, could be smarter if needed
157 if subject_type =='slice' and target_type == 'user':
158 subject=self.shell.project_get(subject_id)[0]
159 current_target_ids = [user.name for user in subject.members]
160 add_target_ids = list ( set (target_ids).difference(current_target_ids))
161 del_target_ids = list ( set (current_target_ids).difference(target_ids))
162 logger.debug ("subject_id = %s (type=%s)"%(subject_id,type(subject_id)))
163 for target_id in add_target_ids:
164 self.shell.project_add_member(target_id,subject_id)
165 logger.debug ("add_target_id = %s (type=%s)"%(target_id,type(target_id)))
166 for target_id in del_target_ids:
167 logger.debug ("del_target_id = %s (type=%s)"%(target_id,type(target_id)))
168 self.shell.project_remove_member(target_id, subject_id)
170 logger.info('unexpected relation to maintain, %s -> %s'%(subject_type,target_type))
173 ########################################
174 ########## aggregate oriented
175 ########################################
177 def testbed_name (self): return "openstack"
179 # 'geni_request_rspec_versions' and 'geni_ad_rspec_versions' are mandatory
180 def aggregate_version (self):
181 version_manager = VersionManager()
182 ad_rspec_versions = []
183 request_rspec_versions = []
184 for rspec_version in version_manager.versions:
185 if rspec_version.content_type in ['*', 'ad']:
186 ad_rspec_versions.append(rspec_version.to_dict())
187 if rspec_version.content_type in ['*', 'request']:
188 request_rspec_versions.append(rspec_version.to_dict())
190 'testbed':self.testbed_name(),
191 'geni_request_rspec_versions': request_rspec_versions,
192 'geni_ad_rspec_versions': ad_rspec_versions,
195 def list_slices (self, creds, options):
196 # look in cache first
198 slices = self.cache.get('slices')
200 logger.debug("OpenStackDriver.list_slices returns from cache")
204 projs = self.shell.auth_manager.get_projects()
205 slice_urns = [OSXrn(proj.name, 'slice').urn for proj in projs]
209 logger.debug ("OpenStackDriver.list_slices stores value in cache")
210 self.cache.add('slices', slice_urns)
214 # first 2 args are None in case of resource discovery
215 def list_resources (self, slice_urn, slice_hrn, creds, options):
216 cached_requested = options.get('cached', True)
218 version_manager = VersionManager()
219 # get the rspec's return format from options
220 rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
221 version_string = "rspec_%s" % (rspec_version)
223 #panos adding the info option to the caching key (can be improved)
224 if options.get('info'):
225 version_string = version_string + "_"+options.get('info', 'default')
227 # look in cache first
228 if cached_requested and self.cache and not slice_hrn:
229 rspec = self.cache.get(version_string)
231 logger.debug("OpenStackDriver.ListResources: returning cached advertisement")
234 #panos: passing user-defined options
235 #print "manager options = ",options
236 aggregate = OSAggregate(self)
237 rspec = aggregate.get_rspec(slice_xrn=slice_urn, version=rspec_version,
241 if self.cache and not slice_hrn:
242 logger.debug("OpenStackDriver.ListResources: stores advertisement in cache")
243 self.cache.add(version_string, rspec)
247 def sliver_status (self, slice_urn, slice_hrn):
248 # find out where this slice is currently running
249 project_name = Xrn(slice_urn).get_leaf()
250 project = self.shell.auth_manager.get_project(project_name)
251 instances = self.shell.db.instance_get_all_by_project(project_name)
252 if len(instances) == 0:
253 raise SliverDoesNotExist("You have not allocated any slivers here")
256 top_level_status = 'unknown'
258 top_level_status = 'ready'
259 result['geni_urn'] = slice_urn
260 result['plos_login'] = 'root'
261 result['plos_expires'] = None
264 for instance in instances:
266 # instances are accessed by ip, not hostname. We need to report the ip
267 # somewhere so users know where to ssh to.
268 res['plos_hostname'] = instance.hostname
269 res['plos_created_at'] = datetime_to_string(utcparse(instance.created_at))
270 res['plos_boot_state'] = instance.vm_state
271 res['plos_sliver_type'] = instance.instance_type.name
272 sliver_id = Xrn(slice_urn).get_sliver_id(instance.project_id, \
273 instance.hostname, instance.id)
274 res['geni_urn'] = sliver_id
276 if instance.vm_state == 'running':
277 res['boot_state'] = 'ready';
279 res['boot_state'] = 'unknown'
280 resources.append(res)
282 result['geni_status'] = top_level_status
283 result['geni_resources'] = resources
286 def create_sliver (self, slice_urn, slice_hrn, creds, rspec_string, users, options):
288 aggregate = OSAggregate(self)
289 slicename = get_leaf(slice_hrn)
292 rspec = RSpec(rspec_string)
293 requested_attributes = rspec.version.get_slice_attributes()
296 pubkeys.extend(user['keys'])
297 # assume that there is a key whos nane matches the caller's username.
298 project_key = Xrn(users[0]['urn']).get_leaf()
301 # ensure slice record exists
302 aggregate.create_project(slicename, users, options=options)
303 # ensure person records exists
304 aggregate.create_project_users(slicename, users, options=options)
305 # add/remove slice from nodes
306 aggregate.run_instances(slicename, rspec, project_key, pubkeys)
308 return aggregate.get_rspec(slice_xrn=slice_urn, version=rspec.version)
310 def delete_sliver (self, slice_urn, slice_hrn, creds, options):
311 name = OSXrn(xrn=slice_urn).name
312 slice = self.shell.project_get(name)
315 instances = self.shell.db.instance_get_all_by_project(name)
316 for instance in instances:
317 self.shell.db.instance_destroy(instance.instance_id)
320 def renew_sliver (self, slice_urn, slice_hrn, creds, expiration_time, options):
323 def start_slice (self, slice_urn, slice_hrn, creds):
326 def stop_slice (self, slice_urn, slice_hrn, creds):
327 name = OSXrn(xrn=slice_urn).name
328 slice = self.shell.get_project(name)
329 instances = self.shell.db.instance_get_all_by_project(name)
330 for instance in instances:
331 self.shell.db.instance_stop(instance.instance_id)
334 def reset_slice (self, slice_urn, slice_hrn, creds):
335 raise SfaNotImplemented ("reset_slice not available at this interface")
337 # xxx this code is quite old and has not run for ages
338 # it is obviously totally broken and needs a rewrite
339 def get_ticket (self, slice_urn, slice_hrn, creds, rspec_string, options):
340 raise SfaNotImplemented,"OpenStackDriver.get_ticket needs a rewrite"
341 # please keep this code for future reference
342 # slices = PlSlices(self)
343 # peer = slices.get_peer(slice_hrn)
344 # sfa_peer = slices.get_sfa_peer(slice_hrn)
346 # # get the slice record
347 # credential = api.getCredential()
348 # interface = api.registries[api.hrn]
349 # registry = api.server_proxy(interface, credential)
350 # records = registry.Resolve(xrn, credential)
352 # # make sure we get a local slice record
354 # for tmp_record in records:
355 # if tmp_record['type'] == 'slice' and \
356 # not tmp_record['peer_authority']:
357 # #Error (E0602, GetTicket): Undefined variable 'SliceRecord'
358 # slice_record = SliceRecord(dict=tmp_record)
360 # raise RecordNotFound(slice_hrn)
362 # # similar to CreateSliver, we must verify that the required records exist
363 # # at this aggregate before we can issue a ticket
365 # rspec = RSpec(rspec_string)
366 # requested_attributes = rspec.version.get_slice_attributes()
368 # # ensure site record exists
369 # site = slices.verify_site(slice_hrn, slice_record, peer, sfa_peer)
370 # # ensure slice record exists
371 # slice = slices.verify_slice(slice_hrn, slice_record, peer, sfa_peer)
372 # # ensure person records exists
373 # # xxx users is undefined in this context
374 # persons = slices.verify_persons(slice_hrn, slice, users, peer, sfa_peer)
375 # # ensure slice attributes exists
376 # slices.verify_slice_attributes(slice, requested_attributes)
379 # slivers = slices.get_slivers(slice_hrn)
382 # raise SliverDoesNotExist(slice_hrn)
387 # 'timestamp': int(time.time()),
388 # 'initscripts': initscripts,
392 # # create the ticket
393 # object_gid = record.get_gid_object()
394 # new_ticket = SfaTicket(subject = object_gid.get_subject())
395 # new_ticket.set_gid_caller(api.auth.client_gid)
396 # new_ticket.set_gid_object(object_gid)
397 # new_ticket.set_issuer(key=api.key, subject=self.hrn)
398 # new_ticket.set_pubkey(object_gid.get_pubkey())
399 # new_ticket.set_attributes(data)
400 # new_ticket.set_rspec(rspec)
401 # #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
402 # new_ticket.encode()
405 # return new_ticket.save_to_string(save_parents=True)