def list_zun_service(self, filters=None, limit=None, marker=None, sort_key=None, sort_dir=None): try: res = getattr(self.client.read('/zun_services'), 'children', None) except etcd.EtcdKeyNotFound: LOG.error( _LE("Path '/zun_services' does not exist, seems etcd server " "was not been initialized appropriately for Zun.")) raise except Exception as e: LOG.error(_LE("Error occurred while reading from etcd server: %s"), six.text_type(e)) raise services = [] for c in res: if c.value is not None: services.append(translate_etcd_result(c, 'zun_service')) if filters: services = self._filter_resources(services, filters) return self._process_list_result(services, limit=limit, sort_key=sort_key)
def load_image_driver(image_driver=None): """Load an image driver module. Load the container image driver module specified by the image_driver configuration option or, if supplied, the driver name supplied as an argument. :param image_driver: container image driver name to override config opt :returns: a ContainerImageDriver instance """ if not image_driver: LOG.error( _LE("Container image driver option required, " "but not specified")) sys.exit(1) LOG.info(_LI("Loading container image driver '%s'"), image_driver) try: driver = importutils.import_object('zun.image.%s' % image_driver) if not isinstance(driver, ContainerImageDriver): raise Exception( _('Expected driver of type: %s') % str(ContainerImageDriver)) return driver except ImportError: LOG.exception(_LE("Unable to load the container image driver")) sys.exit(1)
def load_container_driver(container_driver=None): """Load a container driver module. Load the container driver module specified by the container_driver configuration option or, if supplied, the driver name supplied as an argument. :param container_driver: a container driver name to override the config opt :returns: a ContainerDriver instance """ if not container_driver: container_driver = CONF.container_driver if not container_driver: LOG.error( _LE("Container driver option required, " "but not specified")) sys.exit(1) LOG.info(_LI("Loading container driver '%s'"), container_driver) try: if not container_driver.startswith('zun.'): container_driver = 'zun.container.%s' % container_driver driver = importutils.import_object(container_driver) if not isinstance(driver, ContainerDriver): raise Exception( _('Expected driver of type: %s') % str(ContainerDriver)) return driver except ImportError: LOG.exception(_LE("Unable to load the container driver")) sys.exit(1)
def container_list(self, context): LOG.debug('Listing container...') try: return self.driver.list() except exception.DockerError as e: LOG.error(_LE("Error occurred while calling Docker list API: %s"), six.text_type(e)) raise except Exception as e: LOG.exception(_LE("Unexpected exception: %s"), six.text_type(e)) raise
def _get_container_addresses(self, context, container): LOG.debug('Showing container: %s IP addresses', container.uuid) try: return self.driver.get_addresses(context, container) except exception.DockerError as e: LOG.error(_LE("Error occurred while calling Docker API: %s"), six.text_type(e)) raise except Exception as e: LOG.exception(_LE("Unexpected exception: %s"), six.text_type(e)) raise
def container_logs(self, context, container): LOG.debug('Showing container logs: %s', container.uuid) try: return self.driver.show_logs(container) except exception.DockerError as e: LOG.error(_LE("Error occurred while calling Docker logs API: %s"), six.text_type(e)) raise except Exception as e: LOG.exception(_LE("Unexpected exception: %s"), six.text_type(e)) raise
def container_exec(self, context, container, command): # TODO(hongbin): support exec command interactively LOG.debug('Executing command in container: %s', container.uuid) try: return self.driver.execute(container, command) except exception.DockerError as e: LOG.error(_LE("Error occurred while calling Docker exec API: %s"), six.text_type(e)) raise except Exception as e: LOG.exception(_LE("Unexpected exception: %s"), six.text_type(e)) raise
def container_show(self, context, container): LOG.debug('Showing container: %s', container.uuid) try: container = self.driver.show(container) container.save(context) return container except exception.DockerError as e: LOG.error(_LE("Error occurred while calling Docker show API: %s"), six.text_type(e)) raise except Exception as e: LOG.exception(_LE("Unexpected exception: %s"), six.text_type(e)) raise
def _do_container_unpause(self, context, container, reraise=False): LOG.debug('Unpausing container: %s', container.uuid) try: container = self.driver.unpause(container) container.save(context) return container except exception.DockerError as e: with excutils.save_and_reraise_exception(reraise=reraise): LOG.error( _LE("Error occurred while calling Docker unpause API: %s"), six.text_type(e)) except Exception as e: with excutils.save_and_reraise_exception(reraise=reraise): LOG.exception(_LE("Unexpected exception: %s"), six.text_type(e))
def update_container(self, context, container_uuid, values): # NOTE(yuywz): Update would fail if any other client # write '/containers/$CONTAINER_UUID' in the meanwhile if 'uuid' in values: msg = _("Cannot overwrite UUID for an existing Container.") raise exception.InvalidParameterValue(err=msg) if 'name' in values: self._validate_unique_container_name(context, values['name']) try: target_uuid = self.get_container_by_uuid(context, container_uuid).uuid target = self.client.read('/containers/' + target_uuid) target_value = json.loads(target.value) target_value.update(values) target.value = json.dumps(target_value) self.client.update(target) except etcd.EtcdKeyNotFound: raise exception.ContainerNotFound(container=container_uuid) except Exception as e: LOG.error(_LE('Error occurred while updating container: %s'), six.text_type(e)) raise return translate_etcd_result(target, 'container')
def list_container(self, context, filters=None, limit=None, marker=None, sort_key=None, sort_dir=None): try: res = getattr(self.client.read('/containers'), 'children', None) except etcd.EtcdKeyNotFound: # Before the first container been created, path '/containers' # does not exist. return [] except Exception as e: LOG.error(_LE("Error occurred while reading from etcd server: %s"), six.text_type(e)) raise containers = [] for c in res: if c.value is not None: containers.append(translate_etcd_result(c, 'container')) filters = self._add_tenant_filters(context, filters) filtered_containers = self._filter_resources(containers, filters) return self._process_list_result(filtered_containers, limit=limit, sort_key=sort_key)
def _get_images_collection(self, **kwargs): context = pecan.request.context limit = api_utils.validate_limit(kwargs.get('limit')) sort_dir = api_utils.validate_sort_dir(kwargs.get('sort_dir', 'asc')) sort_key = kwargs.get('sort_key', 'id') resource_url = kwargs.get('resource_url') expand = kwargs.get('expand') filters = None marker_obj = None marker = kwargs.get('marker') if marker: marker_obj = objects.Image.get_by_uuid(context, marker) images = objects.Image.list(context, limit, marker_obj, sort_key, sort_dir, filters=filters) for i, c in enumerate(images): try: images[i] = pecan.request.compute_api.image_show(context, c) except Exception as e: LOG.exception( _LE("Error while list image %(uuid)s: " "%(e)s."), { 'uuid': c.uuid, 'e': e }) return ImageCollection.convert_with_links(images, limit, url=resource_url, expand=expand, sort_key=sort_key, sort_dir=sort_dir)
def poll_until(retriever, condition=lambda value: value, sleep_time=1, time_out=None, success_msg=None, timeout_msg=None): """Retrieves object until it passes condition, then returns it. If time_out_limit is passed in, PollTimeOut will be raised once that amount of time is elapsed. """ start_time = time.time() def poll_and_check(): obj = retriever() if condition(obj): raise loopingcall.LoopingCallDone(retvalue=obj) if time_out is not None and time.time() - start_time > time_out: raise exception.PollTimeOut try: poller = loopingcall.FixedIntervalLoopingCall(f=poll_and_check).start( sleep_time, initial_delay=False) poller.wait() LOG.info(success_msg) except exception.PollTimeOut: LOG.error(timeout_msg) raise except Exception as e: LOG.exception(_LE("Unexpected exception occurred: %s"), six.text_type(e)) raise
def __init__(self, message=None, **kwargs): self.kwargs = kwargs if 'code' not in self.kwargs and hasattr(self, 'code'): self.kwargs['code'] = self.code if message: self.message = message try: self.message = self.message % kwargs except KeyError: # kwargs doesn't match a variable in the message # log the issue and the kwargs LOG.exception( _LE('Exception in string format operation, ' 'kwargs: %s') % kwargs) try: ferr = CONF.fatal_exception_format_errors except cfg.NoSuchOptError: ferr = CONF.oslo_versionedobjects.fatal_exception_format_errors if ferr: raise super(ZunException, self).__init__(self.message)
def list_image(self, context, filters=None, limit=None, marker=None, sort_key=None, sort_dir=None): try: res = getattr(self.client.read('/images'), 'children', None) except etcd.EtcdKeyNotFound: # Before the first image been pulled, path '/image' does # not exist. return [] except Exception as e: LOG.error(_LE("Error occurred while reading from etcd server: %s"), six.text_type(e)) raise images = [] for i in res: if i.value is not None: images.append(translate_etcd_result(i, 'image')) filters = self._add_tenant_filters(context, filters) filtered_images = self._filter_resources(images, filters) return self._process_list_result(filtered_images, limit=limit, sort_key=sort_key)
def image_show(self, context, image): LOG.debug('Listing image...') try: self.image.list() return image except Exception as e: LOG.exception(_LE("Unexpected exception: %s"), six.text_type(e)) raise
def clean_all_zun_data(self): try: for d in self.client.read('/').children: if d.key in ('/containers', ): self.client.delete(d.key, recursive=True) except etcd.EtcdKeyNotFound as e: LOG.error(_LE('Error occurred while cleaning zun data: %s'), six.text_type(e)) raise
def destroy_zun_service(self, host, binary): try: self.client.delete('/zun_services/' + host + '_' + binary) except etcd.EtcdKeyNotFound: raise exception.ZunServiceNotFound(host=host, binary=binary) except Exception as e: LOG.error(_LE('Error occurred while destroying zun service: %s'), six.text_type(e)) raise
def _get_container(container_id): container = api_utils.get_resource('Container', container_id) if not container: pecan.abort( 404, _LE('Not found; the container you requested ' 'does not exist.')) return container
def image_search(self, context, image, exact_match): LOG.debug('Searching image...', image=image) try: return image_driver.search_image(context, image, exact_match) except Exception as e: LOG.exception( _LE("Unexpected exception while searching " "image: %s"), six.text_type(e)) raise
def _do_container_kill(self, context, container, signal, reraise=False): LOG.debug('kill signal to container: %s', container.uuid) try: container = self.driver.kill(container, signal) container.save(context) return container except exception.DockerError as e: with excutils.save_and_reraise_exception(reraise=reraise): LOG.error( _LE("Error occurred while calling Docker kill API: %s"), six.text_type(e))
def find_image(context, image_ident): matches = find_images(context, image_ident, exact_match=True) if len(matches) == 0: raise exception.ImageNotFound(image=image_ident) if len(matches) > 1: msg = _LE("Multiple images exist with same name " "%(image_ident)s. Please use the image id " "instead.") % { 'image_ident': image_ident } raise exception.Conflict(msg) return matches[0]
def validate(cls, value): if value is None: return None if not isinstance(value, float): try: value = float(value) except Exception: LOG.exception(_LE('Failed to convert value to float')) raise exception.InvalidValue(value=value, type=cls.type_name) return value
def _do_image_pull(self, context, image): LOG.debug('Creating image...') repo_tag = image.repo + ":" + image.tag try: pulled_image = image_driver.pull_image(context, image.repo, image.tag) image_dict = self.driver.inspect_image(repo_tag, pulled_image['path']) image.image_id = image_dict['Id'] image.size = image_dict['Size'] image.save() except exception.ImageNotFound as e: LOG.error(six.text_type(e)) return except exception.DockerError as e: LOG.error(_LE("Error occurred while calling Docker image API: %s"), six.text_type(e)) raise except Exception as e: LOG.exception(_LE("Unexpected exception: %s"), six.text_type(e)) raise
def validate(cls, value, default=None): if value is None: value = default if not isinstance(value, bool): try: value = strutils.bool_from_string(value, strict=True) except Exception: LOG.exception(_LE('Failed to convert value to bool')) raise exception.InvalidValue(value=value, type=cls.type_name) return value
def validate(self, value): if value is None: return None if not isinstance(value, self.user_class): try: value = self.user_class(**value) except Exception: LOG.exception(_LE('Failed to validate received value')) raise exception.InvalidValue(value=value, type=self.type_name) return value
def validate(self, value): if value is None: return None if not isinstance(value, list): raise exception.InvalidValue(value=value, type=self.type_name) try: return [self.type.validate(v) for v in value] except Exception: LOG.exception(_LE('Failed to validate received value')) raise exception.InvalidValue(value=value, type=self.type_name)
def validate(self, value): if value is None: return None if not isinstance(value, dict): raise exception.InvalidValue(value=value, type=self.type_name) try: return {self.key_type.validate(k): self.value_type.validate(v) for k, v in value.items()} except Exception: LOG.exception(_LE('Failed to validate received value')) raise exception.InvalidValue(value=value, type=self.type_name)
def _ensure_active(self, novaclient, server, timeout=300): '''Wait until the Nova instance to become active.''' def _check_active(): return novaclient.check_active(server) success_msg = _LI("Created server %s successfully.") % server.id timeout_msg = _LE("Failed to create server %s. Timeout waiting for " "server to become active.") % server.id utils.poll_until(_check_active, sleep_time=CONF.default_sleep_time, time_out=timeout or CONF.default_timeout, success_msg=success_msg, timeout_msg=timeout_msg)
def _ensure_deleted(self, novaclient, server_id, timeout=300): '''Wait until the Nova instance to be deleted.''' def _check_delete_complete(): return novaclient.check_delete_server_complete(server_id) success_msg = _LI("Delete server %s successfully.") % server_id timeout_msg = _LE("Failed to create server %s. Timeout waiting for " "server to be deleted.") % server_id utils.poll_until(_check_delete_complete, sleep_time=CONF.default_sleep_time, time_out=timeout or CONF.default_timeout, success_msg=success_msg, timeout_msg=timeout_msg)