cassinyspawner/swarmspawner.py
def start(self):
        """Start the single-user server in a docker service.
        You can specify the params for the service through jupyterhub_config.py
        or using the user_options
        """

        # https://github.com/jupyterhub/jupyterhub/blob/master/jupyterhub/user.py#L202
        # By default jupyterhub calls the spawner passing user_options
        if self.use_user_options:
            user_options = self.user_options
        else:
            user_options = {}

        self.log.warn("user_options: {}".format(user_options))

        service = yield self.get_service()

        if service is None:

            if 'name' in user_options:
                self.server_name = user_options['name']

            if hasattr(self, 'container_spec') and self.container_spec is not None:
                container_spec = dict(**self.container_spec)
            elif user_options == {}:
                raise("A container_spec is needed in to create a service")

            container_spec.update(user_options.get('container_spec', {}))

            # iterates over mounts to create
            # a new mounts list of docker.types.Mount
            container_spec['mounts'] = []
            for mount in self.container_spec['mounts']:
                m = dict(**mount)

                if 'source' in m:
                    m['source'] = m['source'].format(
                        username=self.service_owner)

                if 'driver_config' in m:
                    device = m['driver_config']['options']['device'].format(
                        username=self.service_owner
                    )
                    m['driver_config']['options']['device'] = device
                    m['driver_config'] = docker.types.DriverConfig(
                        **m['driver_config'])

                container_spec['mounts'].append(docker.types.Mount(**m))

            # some Envs are required by the single-user-image
            container_spec['env'] = self.get_env()

            if hasattr(self, 'resource_spec'):
                resource_spec = self.resource_spec
            resource_spec.update(user_options.get('resource_spec', {}))

            if hasattr(self, 'networks'):
                networks = self.networks
            if user_options.get('networks') is not None:
                networks = user_options.get('networks')

            if hasattr(self, 'placement'):
                placement = self.placement
            if user_options.get('placement') is not None:
                placement = user_options.get('placement')

            image = container_spec['Image']
            del container_spec['Image']

            # create the service
            container_spec = docker.types.ContainerSpec(
                image, **container_spec)
            resources = docker.types.Resources(**resource_spec)

            task_spec = {'container_spec': container_spec,
                         'resources': resources,
                         'placement': placement
                         }
            task_tmpl = docker.types.TaskTemplate(**task_spec)

            resp = yield self.docker('create_service',
                                     task_tmpl,
                                     name=self.service_name,
                                     networks=networks)

            self.service_id = resp['ID']

            self.log.info(
                "Created Docker service '%s' (id: %s) from image %s",
                self.service_name, self.service_id[:7], image)

        else:
            self.log.info(
                "Found existing Docker service '%s' (id: %s)",
                self.service_name, self.service_id[:7])
            # Handle re-using API token.
            # Get the API token from the environment variables
            # of the running service:
            envs = service['Spec']['TaskTemplate']['ContainerSpec']['Env']
            for line in envs:
                if line.startswith('JPY_API_TOKEN='):
                    self.api_token = line.split('=', 1)[1]
                    break

        ip = self.service_name
        port = self.service_port

        # we use service_name instead of ip
        # https://docs.docker.com/engine/swarm/networking/#use-swarm-mode-service-discovery
        # service_port is actually equal to 8888
        return (ip, port)
def start(self):
        """Start the single-user server in a docker service.
        You can specify the params for the service through jupyterhub_config.py
        or using the user_options
        """

        # https://github.com/jupyterhub/jupyterhub/blob/master/jupyterhub/user.py#L202
        # By default jupyterhub calls the spawner passing user_options
        if self.use_user_options:
            user_options = self.user_options
        else:
            user_options = {}

        self.log.warn("user_options: {}".format(user_options))

        service = yield self.get_service()

        if service is None:

            if 'name' in user_options:
                self.server_name = user_options['name']

            if hasattr(self, 'container_spec') and self.container_spec is not None:
                container_spec = dict(**self.container_spec)
            elif user_options == {}:
                raise("A container_spec is needed in to create a service")

            container_spec.update(user_options.get('container_spec', {}))

            # iterates over mounts to create
            # a new mounts list of docker.types.Mount
            container_spec['mounts'] = []
            for mount in self.container_spec['mounts']:
                m = dict(**mount)

                if 'source' in m:
                    m['source'] = m['source'].format(
                        username=self.service_owner)

                if 'driver_config' in m:
                    device = m['driver_config']['options']['device'].format(
                        username=self.service_owner
                    )
                    m['driver_config']['options']['device'] = device
                    m['driver_config'] = docker.types.DriverConfig(
                        **m['driver_config'])

                container_spec['mounts'].append(docker.types.Mount(**m))

            # some Envs are required by the single-user-image
            container_spec['env'] = self.get_env()

            if hasattr(self, 'resource_spec'):
                resource_spec = self.resource_spec
            resource_spec.update(user_options.get('resource_spec', {}))

            if hasattr(self, 'networks'):
                networks = self.networks
            if user_options.get('networks') is not None:
                networks = user_options.get('networks')

            if hasattr(self, 'placement'):
                placement = self.placement
            if user_options.get('placement') is not None:
                placement = user_options.get('placement')

            image = container_spec['Image']
            del container_spec['Image']

            # create the service
            container_spec = docker.types.ContainerSpec(
                image, **container_spec)
            resources = docker.types.Resources(**resource_spec)

            task_spec = {'container_spec': container_spec,
                         'resources': resources,
                         'placement': placement
                         }
            task_tmpl = docker.types.TaskTemplate(**task_spec)

            resp = yield self.docker('create_service',
                                     task_tmpl,
                                     name=self.service_name,
                                     networks=networks)

            self.service_id = resp['ID']

            self.log.info(
                "Created Docker service '%s' (id: %s) from image %s",
                self.service_name, self.service_id[:7], image)

        else:
            self.log.info(
                "Found existing Docker service '%s' (id: %s)",
                self.service_name, self.service_id[:7])
            # Handle re-using API token.
            # Get the API token from the environment variables
            # of the running service:
            envs = service['Spec']['TaskTemplate']['ContainerSpec']['Env']
            for line in envs:
                if line.startswith('JPY_API_TOKEN='):
                    self.api_token = line.split('=', 1)[1]
                    break

        ip = self.service_name
        port = self.service_port

        # we use service_name instead of ip
        # https://docs.docker.com/engine/swarm/networking/#use-swarm-mode-service-discovery
        # service_port is actually equal to 8888
        return (ip, port)
def start ( self ) : # https://github.com/jupyterhub/jupyterhub/blob/master/jupyterhub/user.py#L202 # By default jupyterhub calls the spawner passing user_options if self . use_user_options : user_options = self . user_options else : user_options = { } self . log . warn ( "user_options: {}" . format ( user_options ) ) service = yield self . get_service ( ) if service is None : if 'name' in user_options : self . server_name = user_options [ 'name' ] if hasattr ( self , 'container_spec' ) and self . container_spec is not None : container_spec = dict ( * * self . container_spec ) elif user_options == { } : raise ( "A container_spec is needed in to create a service" ) container_spec . update ( user_options . get ( 'container_spec' , { } ) ) # iterates over mounts to create # a new mounts list of docker.types.Mount container_spec [ 'mounts' ] = [ ] for mount in self . container_spec [ 'mounts' ] : m = dict ( * * mount ) if 'source' in m : m [ 'source' ] = m [ 'source' ] . format ( username = self . service_owner ) if 'driver_config' in m : device = m [ 'driver_config' ] [ 'options' ] [ 'device' ] . format ( username = self . service_owner ) m [ 'driver_config' ] [ 'options' ] [ 'device' ] = device m [ 'driver_config' ] = docker . types . DriverConfig ( * * m [ 'driver_config' ] ) container_spec [ 'mounts' ] . append ( docker . types . Mount ( * * m ) ) # some Envs are required by the single-user-image container_spec [ 'env' ] = self . get_env ( ) if hasattr ( self , 'resource_spec' ) : resource_spec = self . resource_spec resource_spec . update ( user_options . get ( 'resource_spec' , { } ) ) if hasattr ( self , 'networks' ) : networks = self . networks if user_options . get ( 'networks' ) is not None : networks = user_options . get ( 'networks' ) if hasattr ( self , 'placement' ) : placement = self . placement if user_options . get ( 'placement' ) is not None : placement = user_options . get ( 'placement' ) image = container_spec [ 'Image' ] del container_spec [ 'Image' ] # create the service container_spec = docker . types . ContainerSpec ( image , * * container_spec ) resources = docker . types . Resources ( * * resource_spec ) task_spec = { 'container_spec' : container_spec , 'resources' : resources , 'placement' : placement } task_tmpl = docker . types . TaskTemplate ( * * task_spec ) resp = yield self . docker ( 'create_service' , task_tmpl , name = self . service_name , networks = networks ) self . service_id = resp [ 'ID' ] self . log . info ( "Created Docker service '%s' (id: %s) from image %s" , self . service_name , self . service_id [ : 7 ] , image ) else : self . log . info ( "Found existing Docker service '%s' (id: %s)" , self . service_name , self . service_id [ : 7 ] ) # Handle re-using API token. # Get the API token from the environment variables # of the running service: envs = service [ 'Spec' ] [ 'TaskTemplate' ] [ 'ContainerSpec' ] [ 'Env' ] for line in envs : if line . startswith ( 'JPY_API_TOKEN=' ) : self . api_token = line . split ( '=' , 1 ) [ 1 ] break ip = self . service_name port = self . service_port # we use service_name instead of ip # https://docs.docker.com/engine/swarm/networking/#use-swarm-mode-service-discovery # service_port is actually equal to 8888 return ( ip , port )
Start the single-user server in a docker service.
        You can specify the params for the service through jupyterhub_config.py
        or using the user_options
Start the single - user server in a docker service . You can specify the params for the service through jupyterhub_config . py or using the user_options