def _process(func, zone): """Worker stub for green thread pool. Give the worker an authenticated nova client and zone info.""" nova = novaclient.OpenStack(zone.username, zone.password, None, zone.api_url) nova.authenticate() return func(nova, zone)
def call_zone_method(context, method_name, errors_to_ignore=None, novaclient_collection_name='zones', zones=None, *args, **kwargs): """Returns a list of (zone, call_result) objects.""" if not isinstance(errors_to_ignore, (list, tuple)): # This will also handle the default None errors_to_ignore = [errors_to_ignore] pool = greenpool.GreenPool() results = [] if zones is None: zones = db.zone_get_all(context) for zone in zones: try: nova = novaclient.OpenStack(zone.username, zone.password, None, zone.api_url) nova.authenticate() except novaclient.exceptions.BadRequest, e: url = zone.api_url LOG.warn( _("Failed request to zone; URL=%(url)s: %(e)s") % locals()) #TODO (dabo) - add logic for failure counts per zone, # with escalation after a given number of failures. continue novaclient_collection = getattr(nova, novaclient_collection_name) collection_method = getattr(novaclient_collection, method_name) def _error_trap(*args, **kwargs): try: return collection_method(*args, **kwargs) except Exception as e: if type(e) in errors_to_ignore: return None raise res = pool.spawn(_error_trap, *args, **kwargs) results.append((zone, res))
def _ask_child_zone_to_create_instance(self, context, zone_info, request_spec, kwargs): """Once we have determined that the request should go to one of our children, we need to fabricate a new POST /servers/ call with the same parameters that were passed into us. Note that we have to reverse engineer from our args to get back the image, flavor, ipgroup, etc. since the original call could have come in from EC2 (which doesn't use these things).""" instance_type = request_spec['instance_type'] instance_properties = request_spec['instance_properties'] name = instance_properties['display_name'] image_ref = instance_properties['image_ref'] meta = instance_properties['metadata'] flavor_id = instance_type['flavorid'] reservation_id = instance_properties['reservation_id'] files = kwargs['injected_files'] ipgroup = None # Not supported in OS API ... yet child_zone = zone_info['child_zone'] child_blob = zone_info['child_blob'] zone = db.zone_get(context, child_zone) url = zone.api_url LOG.debug( _("Forwarding instance create call to child zone %(url)s" ". ReservationID=%(reservation_id)s") % locals()) nova = None try: nova = novaclient.OpenStack(zone.username, zone.password, None, url) nova.authenticate() except novaclient.exceptions.BadRequest, e: raise exception.NotAuthorized( _("Bad credentials attempting " "to talk to zone at %(url)s.") % locals())
#!/usr/bin/python import novaclient import ConfigParser # get our config from the config file config = ConfigParser.ConfigParser() config.read("./cf.ini") USERNAME = config.get("auth", "username") AUTH_URL = config.get("auth", "url") API_KEY = config.get("auth", "key") # Server ID to resize #SERVER_ID = 10010692 cnx = novaclient.OpenStack(USERNAME, API_KEY, AUTH_URL) print cnx cnx.servers.list() cnx.flavors.list()
def _call_novaclient(zone): """Call novaclient. Broken out for testing purposes.""" client = novaclient.OpenStack(zone.username, zone.password, zone.api_url) return client.zones.info()._info