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 project_name = get_leaf(slice_hrn)
289 aggregate = OSAggregate(self)
291 rspec = RSpec(rspec_string)
293 # ensure project and users exist in local db
294 aggregate.create_project(project_name, users, options=options)
296 # collect publick keys
300 pubkeys.extend(user['keys'])
301 # assume first user is the caller and use their context
302 # for the ec2/euca api connection. Also, use the first users
303 # key as the project key.
305 username = Xrn(user['urn']).get_leaf()
306 user_keys = self.shell.db.key_pair_get_all_by_user(username)
308 project_key = user_keys[0].name
309 self.euca_shell._init_ctx(username, project_name)
311 # use the first keypair we find
313 username = Xrn(user['urn']).get_leaf()
314 user_keys = self.shell.db.key_pair_get_all_by_user(username)
316 #project_key = user_keys[0].id
317 project_key = user_keys[0].name
320 # ensure person records exists
321 aggregate.run_instances(project_name, rspec_string, project_key, pubkeys)
323 return aggregate.get_rspec(slice_xrn=slice_urn, version=rspec.version)
325 def delete_sliver (self, slice_urn, slice_hrn, creds, options):
326 name = OSXrn(xrn=slice_urn).name
327 aggregate = OSAggregate(self)
328 return aggregate.delete_instances(name)
330 def update_sliver(self, slice_urn, slice_hrn, rspec, creds, options):
331 name = OSXrn(xrn=slice_urn).name
332 aggregate = OSAggregate(self)
333 return aggregate.update_instances(name)
335 def renew_sliver (self, slice_urn, slice_hrn, creds, expiration_time, options):
338 def start_slice (self, slice_urn, slice_hrn, creds):
341 def stop_slice (self, slice_urn, slice_hrn, creds):
342 name = OSXrn(xrn=slice_urn).name
343 aggregate = OSAggregate(self)
344 return aggregate.stop_instances(name)
346 def reset_slice (self, slice_urn, slice_hrn, creds):
347 raise SfaNotImplemented ("reset_slice not available at this interface")
349 # xxx this code is quite old and has not run for ages
350 # it is obviously totally broken and needs a rewrite
351 def get_ticket (self, slice_urn, slice_hrn, creds, rspec_string, options):
352 raise SfaNotImplemented,"OpenStackDriver.get_ticket needs a rewrite"
353 # please keep this code for future reference
354 # slices = PlSlices(self)
355 # peer = slices.get_peer(slice_hrn)
356 # sfa_peer = slices.get_sfa_peer(slice_hrn)
358 # # get the slice record
359 # credential = api.getCredential()
360 # interface = api.registries[api.hrn]
361 # registry = api.server_proxy(interface, credential)
362 # records = registry.Resolve(xrn, credential)
364 # # make sure we get a local slice record
366 # for tmp_record in records:
367 # if tmp_record['type'] == 'slice' and \
368 # not tmp_record['peer_authority']:
369 # #Error (E0602, GetTicket): Undefined variable 'SliceRecord'
370 # slice_record = SliceRecord(dict=tmp_record)
372 # raise RecordNotFound(slice_hrn)
374 # # similar to CreateSliver, we must verify that the required records exist
375 # # at this aggregate before we can issue a ticket
377 # rspec = RSpec(rspec_string)
378 # requested_attributes = rspec.version.get_slice_attributes()
380 # # ensure site record exists
381 # site = slices.verify_site(slice_hrn, slice_record, peer, sfa_peer)
382 # # ensure slice record exists
383 # slice = slices.verify_slice(slice_hrn, slice_record, peer, sfa_peer)
384 # # ensure person records exists
385 # # xxx users is undefined in this context
386 # persons = slices.verify_persons(slice_hrn, slice, users, peer, sfa_peer)
387 # # ensure slice attributes exists
388 # slices.verify_slice_attributes(slice, requested_attributes)
391 # slivers = slices.get_slivers(slice_hrn)
394 # raise SliverDoesNotExist(slice_hrn)
399 # 'timestamp': int(time.time()),
400 # 'initscripts': initscripts,
404 # # create the ticket
405 # object_gid = record.get_gid_object()
406 # new_ticket = SfaTicket(subject = object_gid.get_subject())
407 # new_ticket.set_gid_caller(api.auth.client_gid)
408 # new_ticket.set_gid_object(object_gid)
409 # new_ticket.set_issuer(key=api.key, subject=self.hrn)
410 # new_ticket.set_pubkey(object_gid.get_pubkey())
411 # new_ticket.set_attributes(data)
412 # new_ticket.set_rspec(rspec)
413 # #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
414 # new_ticket.encode()
417 # return new_ticket.save_to_string(save_parents=True)