예제 #1
0
 def on_start(self):
     self._model_client = ModelClient('recognize_digits',
                                      '1.0',
                                      token=build_client().get_token(
                                          'recognize_digits', '1.0'),
                                      use_relative_url=True,
                                      http_client=self.client)
예제 #2
0
 def on_start(self):
     self._model_client = ModelClient('movie-lens',
                                      '1.0',
                                      use_relative_url=True,
                                      http_client=self.client,
                                      token=build_client().get_token(
                                          'movie-lens', '1.0'))
예제 #3
0
 def setUp(self):
     self._client = ModelClient(os.environ.get(*legion.config.MODEL_ID))
예제 #4
0
    def __enter__(self):
        """
        Enter into context

        :return: self
        """
        try:
            print('Building model file {} v {}'.format(self._model_id,
                                                       self._model_version))
            legion.model.model_id.init(self._model_id)
            self._model_builder(self._model_path, self._model_version)
            print('Model file has been built')

            print('Building model image {} v {}'.format(
                self._model_id, self._model_version))
            args = Namespace(model_file=self._model_path,
                             model_id=None,
                             base_docker_image=None,
                             docker_network=None,
                             python_package=self._wheel,
                             python_package_version=None,
                             python_repository=None,
                             docker_image_tag=None,
                             serving=deploy.VALID_SERVING_WORKERS[1],
                             push_to_registry=None)
            self.image = deploy.build_model(args)
            print('Model image has been built')

            print('Deploying model {} v {}'.format(self._model_id,
                                                   self._model_version))
            additional_environment = {
                legion.config.REGISTER_ON_GRAFANA[0]: 'false',
                legion.config.REGISTER_ON_CONSUL[0]: 'false',
            }
            with patch_environ(additional_environment):
                args = Namespace(model_id=self._model_id,
                                 docker_image=None,
                                 docker_network=None,
                                 grafana_server=None,
                                 grafana_user=None,
                                 grafana_password=None,
                                 expose_model_port=0)
                self.container = deploy.deploy_model(args)
                self.container_id = self.container.id
            print('Model image has been deployed')

            wait = 3
            print('Waiting {} sec'.format(wait))
            time.sleep(wait)
            self.container = self._docker_client.containers.get(
                self.container_id)
            if self.container.status != 'running':
                try:
                    logs = self.container.logs().decode('utf-8')

                    print('--- CONTAINER LOGS ---')
                    print(logs)
                except Exception:
                    print('Cannot get logs of container')

                raise Exception('Invalid container state: {}'.format(
                    self.container.status))
            print('OK')

            print('Detecting bound ports')
            ports_information = [
                item for sublist in self.container.attrs['NetworkSettings']
                ['Ports'].values() for item in sublist
            ]
            ports_information = [int(x['HostPort']) for x in ports_information]
            print('Detected ports: {}'.format(', '.join(
                str(port) for port in ports_information)))

            if len(ports_information) != 1:
                raise Exception('Should be only one bound port')
            self.model_port = ports_information[0]
            print('Model port: {}'.format(self.model_port))

            print('Building client')
            url = 'http://{}:{}'.format('localhost', self.model_port)
            print('Target URI is {}'.format(url))
            self.client = ModelClient(self._model_id, url)

            print('Getting model information')
            self.model_information = self.client.info()

            return self
        except Exception as build_exception:
            self.__exit__(build_exception)
예제 #5
0
 def on_start(self):
     self._model_client = ModelClient('income',
                                      use_relative_url=True,
                                      http_client=self.client)
예제 #6
0
 def on_start(self):
     self._model_client = ModelClient('test_summation', '1.0', use_relative_url=True,
                                      http_client=self.client,
                                      token=build_client().get_token('test_summation', '1.0'))
예제 #7
0
    def __enter__(self):
        """
        Enter into context

        :return: self
        """
        try:
            LOGGER.info('Deploying model {} v {} from image {}'.format(self._model_id,
                                                                       self._model_version,
                                                                       self._image_id))

            additional_environment = {
                **build_environ_for_test_environments()
            }

            container_labels = legion.containers.docker.generate_docker_labels_for_container(self._image)

            ports = {'{}/tcp'.format(os.getenv(*legion.config.LEGION_PORT)): 0}

            self.container = self._docker_client.containers.run(self._image_id,
                                                                stdout=True,
                                                                stderr=True,
                                                                detach=True,  # in the background
                                                                remove=True,  # remove automatically after kill
                                                                environment=additional_environment,
                                                                labels=container_labels,
                                                                ports=ports)
            self.container_id = self.container.id

            LOGGER.info('Model image has been deployed')

            wait = 3
            LOGGER.info('Waiting {} sec'.format(wait))
            time.sleep(wait)

            self.container = self._docker_client.containers.get(self.container_id)
            if self.container.status != 'running':
                print_docker_container_logs(self.container)
                raise Exception('Invalid container state: {}'.format(self.container.status))

            LOGGER.info('OK')

            LOGGER.info('Detecting bound ports')
            ports_information = [item for sublist in self.container.attrs['NetworkSettings']['Ports'].values()
                                 for item in sublist]
            ports_information = [int(x['HostPort']) for x in ports_information]
            LOGGER.info('Detected ports: {}'.format(', '.join(str(port) for port in ports_information)))

            if len(ports_information) != 1:
                raise Exception('Should be only one bound port')
            self.model_port = ports_information[0]
            LOGGER.info('Model port: {}'.format(self.model_port))

            LOGGER.info('Building client')
            url = 'http://{}:{}'.format('localhost', self.model_port)
            LOGGER.info('Target URI is {}'.format(url))
            self.client = ModelClient(self._model_id, self._model_version, host=url)

            LOGGER.info('Getting model information')
            self.model_information = self.client.info()

            return self
        except Exception as build_exception:
            self.__exit__(build_exception)
예제 #8
0
 def setUp(self):
     self._client = ModelClient(os.environ.get(*legion.config.MODEL_ID),
                                '1.0',
                                token=build_client().get_token(
                                    os.environ.get(*legion.config.MODEL_ID),
                                    '1.0'))
예제 #9
0
 def on_start(self):
     self._model_client = ModelClient('movie-lens',
                                      use_relative_url=True,
                                      http_client=self.client)
예제 #10
0
 def on_start(self):
     self._model_client = ModelClient('test_summation',
                                      '1.0',
                                      use_relative_url=True,
                                      http_client=self.client)
예제 #11
0
 def on_start(self):
     self._model_client = ModelClient('recognize_digits',
                                      use_relative_url=True,
                                      http_client=self.client)
예제 #12
0
def inspect(cluster_config, cluster_secrets, namespace=None):
    """
    Get model deployments information

    :param cluster_config: cluster configuration
    :type cluster_config: dict
    :param cluster_secrets: secrets with credentials
    :type cluster_secrets: dict[str, str]
    :param namespace:
    :return: list[:py:class:`legion.containers.k8s.ModelDeploymentDescription`]
    """
    deployments = find_all_models_deployments(namespace)
    models = []

    edge_url = 'http://%s:%d' % (cluster_config['edge']['domain'],
                                 cluster_config['edge']['port'])

    for deployment in deployments:
        ready_replicas = deployment.status.ready_replicas
        if not ready_replicas:
            ready_replicas = 0
        replicas = deployment.status.replicas
        if not replicas:
            replicas = 0
        status = 'ok'

        if ready_replicas == 0:
            status = 'fail'
        elif replicas > ready_replicas > 0:
            status = 'warning'

        container_image = deployment.spec.template.spec.containers[0].image

        model_name = deployment.metadata.labels.get(
            normalize_name(legion.containers.headers.DOMAIN_MODEL_ID), '?')
        model_version = deployment.metadata.labels.get(
            normalize_name(legion.containers.headers.DOMAIN_MODEL_VERSION),
            '?')

        model_api_info = {'host': edge_url}

        try:
            model_client = ModelClient(model_name, host=edge_url)
            model_api_info['result'] = model_client.info()
            model_api_ok = True
        except Exception as model_api_exception:
            model_api_info['exception'] = str(model_api_exception)
            model_api_ok = False

        model_information = ModelDeploymentDescription(
            status=status,
            model=model_name,
            version=model_version,
            image=container_image,
            scale=replicas,
            ready_replicas=ready_replicas,
            namespace=deployment.metadata.namespace,
            deployment='deployment',
            model_api_ok=model_api_ok,
            model_api_info=model_api_info)
        models.append(model_information)

    return models