(self, create=True)
Gets or creates the PyLXD container.
Gets or creates the PyLXD container.
Gets or creates the PyLXD container .
def _get_container(self, create=True):
        """ Gets or creates the PyLXD container. """
        try:
            container = self.client.containers.get(self.lxd_name)
        except NotFound:
            container = None
        else:
            return container

        if not create:
            return

        logger.warning('Unable to find container "{name}" for directory "{homedir}"'.format(
            name=self.name, homedir=self.homedir))

        logger.info(
            'Creating new container "{name}" '
            'from image {image}'.format(name=self.lxd_name, image=self.options['image']))
        privileged = self.options.get('privileged', False)
        mode = self.options.get('mode', 'pull')

        # Get user defined lxc configs
        lxc_config = self.options.get('lxc_config', {}).copy()

        # Overwrite any configuration settings with lxdock defaults
        lxc_config.update({
            'security.privileged': 'true' if privileged else 'false',
            'user.lxdock.made': '1',
            'user.lxdock.homedir': self.homedir,
        })

        container_config = {
            'name': self.lxd_name,
            'source': {
                'alias': self.options['image'],
                # The 'mode' defines how the container will be retrieved. In "local" mode the image
                # will be determined using a local alias. In "pull" mode the image will be fetched
                # from a remote server using a remote alias.
                'mode': mode,
                # The 'protocol' to use. LXD supports two protocol: 'lxd' (RESTful API that is used
                # between the clients and a LXD daemon) and 'simplestreams' (an image server
                # description format, using JSON to describe a list of images and allowing to get
                # image information and import images). We use "simplestreams" by default (as the
                # lxc command do).
                'protocol': self.options.get('protocol', 'simplestreams'),
                # The 'server' that should be used to fetch the images. We use the default
                # linuxcontainers server for LXC and LXD when no value is provided (and if we are
                # not in "local" mode).
                'server': (self.options.get('server', self._default_image_server) if mode == 'pull'
                           else ''),
                'type': 'image',
            },
            'config': lxc_config,
        }

        profiles = self.options.get('profiles')
        if profiles:
            container_config['profiles'] = profiles.copy()

        try:
            return self.client.containers.create(container_config, wait=True)
        except LXDAPIException as e:
            logger.error("Can't create container: {error}".format(error=e))
            raise ContainerOperationFailed()
def _get_container ( self , create = True ) : try : container = self . client . containers . get ( self . lxd_name ) except NotFound : container = None else : return container if not create : return logger . warning ( 'Unable to find container "{name}" for directory "{homedir}"' . format ( name = self . name , homedir = self . homedir ) ) logger . info ( 'Creating new container "{name}" ' 'from image {image}' . format ( name = self . lxd_name , image = self . options [ 'image' ] ) ) privileged = self . options . get ( 'privileged' , False ) mode = self . options . get ( 'mode' , 'pull' ) # Get user defined lxc configs lxc_config = self . options . get ( 'lxc_config' , { } ) . copy ( ) # Overwrite any configuration settings with lxdock defaults lxc_config . update ( { 'security.privileged' : 'true' if privileged else 'false' , 'user.lxdock.made' : '1' , 'user.lxdock.homedir' : self . homedir , } ) container_config = { 'name' : self . lxd_name , 'source' : { 'alias' : self . options [ 'image' ] , # The 'mode' defines how the container will be retrieved. In "local" mode the image # will be determined using a local alias. In "pull" mode the image will be fetched # from a remote server using a remote alias. 'mode' : mode , # The 'protocol' to use. LXD supports two protocol: 'lxd' (RESTful API that is used # between the clients and a LXD daemon) and 'simplestreams' (an image server # description format, using JSON to describe a list of images and allowing to get # image information and import images). We use "simplestreams" by default (as the # lxc command do). 'protocol' : self . options . get ( 'protocol' , 'simplestreams' ) , # The 'server' that should be used to fetch the images. We use the default # linuxcontainers server for LXC and LXD when no value is provided (and if we are # not in "local" mode). 'server' : ( self . options . get ( 'server' , self . _default_image_server ) if mode == 'pull' else '' ) , 'type' : 'image' , } , 'config' : lxc_config , } profiles = self . options . get ( 'profiles' ) if profiles : container_config [ 'profiles' ] = profiles . copy ( ) try : return self . client . containers . create ( container_config , wait = True ) except LXDAPIException as e : logger . error ( "Can't create container: {error}" . format ( error = e ) ) raise ContainerOperationFailed ( )