def refresh_status(self): """Refresh the status information of the container. """ containers = lxc.get_container_info(self.container_path, name=self.name) if not containers: raise error.ContainerError( 'No container found in directory %s with name of %s.' % (self.container_path, self.name)) attribute_values = containers[0] for attribute, value in attribute_values.iteritems(): setattr(self, attribute, value)
def get_all(self): """Get details of all containers. @return: A dictionary of all containers with detailed attributes, indexed by container name. """ info_collection = lxc.get_container_info(self.container_path) containers = {} for info in info_collection: container = Container.createFromExistingDir(self.container_path, **info) containers[container.name] = container return containers
def get_all(self, force_update=False): """Get details of all containers. Retrieves all containers owned by the bucket. Note that this doesn't include the base container, or any containers owned by the container pool. @param force_update: Boolean, ignore cached values if set. @return: A dictionary of all containers with detailed attributes, indexed by container name. """ info_collection = lxc.get_container_info(self.container_path) containers = {} if force_update else self.container_cache for info in info_collection: if info["name"] in containers: continue container = Container.create_from_existing_dir(self.container_path, **info) # Active containers have an ID. Zygotes and base containers, don't. if container.id is not None: containers[container.id] = container self.container_cache = containers return containers
def __init__(self, container_path, name, attribute_values, src=None, snapshot=False, host_path=None): """Initialize an object of LXC container with given attribute values. @param container_path: Directory that stores the container. @param name: Name of the container. @param attribute_values: A dictionary of attribute values for the container. @param src: An optional source container. If provided, the source continer is cloned, and the new container will point to the clone. @param snapshot: Whether or not to create a snapshot clone. By default, this is false. If a snapshot is requested and creating a snapshot clone fails, a full clone will be attempted. @param host_path: If set to None (the default), a host path will be generated based on constants.DEFAULT_SHARED_HOST_PATH. Otherwise, this can be used to override the host path of the new container, for testing purposes. """ # Check if this is a pre-existing LXC container. Do this before calling # the super ctor, because that triggers container creation. exists = lxc.get_container_info(container_path, name=name) super(Zygote, self).__init__(container_path, name, attribute_values, src, snapshot) logging.debug('Creating Zygote (lxcpath:%s name:%s)', container_path, name) # host_path is a directory within a shared bind-mount, which enables # bind-mounts from the host system to be shared with the LXC container. if host_path is not None: # Allow the host_path to be injected, for testing. self.host_path = host_path else: if exists: # Pre-existing Zygotes must have a host path. self.host_path = self._find_existing_host_dir() if self.host_path is None: raise error.ContainerError( 'Container %s has no host path.' % os.path.join(container_path, name)) else: # New Zygotes use a predefined template to generate a host path. self.host_path = os.path.join( os.path.realpath(constants.DEFAULT_SHARED_HOST_PATH), self.name) # host_path_ro is a directory for holding intermediate mount points, # which are necessary when creating read-only bind mounts. See the # mount_dir method for more details. # # Generate a host_path_ro based on host_path. ro_dir, ro_name = os.path.split(self.host_path.rstrip(os.path.sep)) self.host_path_ro = os.path.join(ro_dir, '%s.ro' % ro_name) # Remember mounts so they can be cleaned up in destroy. self.mounts = [] if exists: self._find_existing_bind_mounts() else: # Creating a new Zygote - initialize the host dirs. Don't use sudo, # so that the resulting directories can be accessed by autoserv (for # SSP installation, etc). if not lxc_utils.path_exists(self.host_path): os.makedirs(self.host_path) if not lxc_utils.path_exists(self.host_path_ro): os.makedirs(self.host_path_ro) # Create the mount point within the container's rootfs. # Changes within container's rootfs require sudo. utils.run('sudo mkdir %s' % os.path.join( self.rootfs, constants.CONTAINER_HOST_DIR.lstrip(os.path.sep))) self.mount_dir(self.host_path, constants.CONTAINER_HOST_DIR)