Пример #1
0
    async def deploy(
        self,
        env: data.Environment,
        agent_trigger_method: const.AgentTriggerMethod = const.
        AgentTriggerMethod.push_full_deploy,
        agents: Optional[List[str]] = None,
    ) -> Apireturn:
        warnings = []

        # get latest version
        version_id = await data.ConfigurationModel.get_version_nr_latest_version(
            env.id)
        if version_id is None:
            return 404, {"message": "No version available"}

        # filter agents
        allagents = await data.ConfigurationModel.get_agents(
            env.id, version_id)
        if agents is not None:
            required = set(agents)
            present = set(allagents)
            allagents = list(required.intersection(present))
            notfound = required - present
            if notfound:
                warnings.append(
                    "Model version %d does not contain agents named [%s]" %
                    (version_id, ",".join(sorted(list(notfound)))))

        if not allagents:
            return attach_warnings(404,
                                   {"message": "No agent could be reached"},
                                   warnings)

        present = set()
        absent = set()

        await self.autostarted_agent_manager._ensure_agents(env, allagents)

        for agent in allagents:
            client = self.agentmanager_service.get_agent_client(env.id, agent)
            if client is not None:
                incremental_deploy = agent_trigger_method is const.AgentTriggerMethod.push_incremental_deploy
                self.add_background_task(
                    client.trigger(env.id, agent, incremental_deploy))
                present.add(agent)
            else:
                absent.add(agent)

        if absent:
            warnings.append("Could not reach agents named [%s]" %
                            ",".join(sorted(list(absent))))

        if not present:
            return attach_warnings(404,
                                   {"message": "No agent could be reached"},
                                   warnings)

        return attach_warnings(200, {"agents": sorted(list(present))},
                               warnings)
Пример #2
0
    async def delete_param(self, env: data.Environment, parameter_name: str,
                           resource_id: str) -> Apireturn:
        if resource_id is None:
            params = await data.Parameter.get_list(environment=env.id,
                                                   name=parameter_name)
        else:
            params = await data.Parameter.get_list(environment=env.id,
                                                   name=parameter_name,
                                                   resource_id=resource_id)

        if len(params) == 0:
            return 404

        param = params[0]
        await param.delete()
        metadata = {
            "message":
            "Recompile model because one or more parameters were deleted",
            "type": "param",
            "params": [(param.name, param.resource_id)],
        }
        warnings = await self.server_slice._async_recompile(env,
                                                            False,
                                                            metadata=metadata)

        return attach_warnings(200, None, warnings)
Пример #3
0
    async def set_parameters(self, env: data.Environment,
                             parameters: List[Dict[str, Any]]) -> Apireturn:
        recompile = False

        params: List[Tuple[str, ResourceIdStr]] = []
        for param in parameters:
            name: str = param["id"]
            source = param["source"]
            value = param["value"] if "value" in param else None
            resource_id: ResourceIdStr = param[
                "resource_id"] if "resource_id" in param else None
            metadata = param["metadata"] if "metadata" in param else None

            result = await self._update_param(env, name, value, source,
                                              resource_id, metadata)
            if result:
                recompile = True
                params.append((name, resource_id))

        compile_metadata = {
            "message":
            "Recompile model because one or more parameters were updated",
            "type": "param",
            "params": params,
        }

        warnings = None
        if recompile:
            warnings = await self.server_slice._async_recompile(
                env, False, metadata=compile_metadata)

        return attach_warnings(200, None, warnings)
Пример #4
0
    async def set_param(
        self,
        env: data.Environment,
        param_id: str,
        source: ParameterSource,
        value: str,
        resource_id: str,
        metadata: JsonType,
        recompile: bool,
    ) -> Apireturn:
        result = await self._update_param(env, param_id, value, source,
                                          resource_id, metadata, recompile)
        warnings = None
        if result:
            compile_metadata = {
                "message":
                "Recompile model because one or more parameters were updated",
                "type": "param",
                "params": [(param_id, resource_id)],
            }
            warnings = await self.server_slice._async_recompile(
                env, False, metadata=compile_metadata)

        if resource_id is None:
            resource_id = ""

        params = await data.Parameter.get_list(environment=env.id,
                                               name=param_id,
                                               resource_id=resource_id)

        return attach_warnings(200, {"parameter": params[0]}, warnings)
Пример #5
0
 async def delete_setting(self, env: data.Environment,
                          key: str) -> Apireturn:
     try:
         original_env = env.to_dto()
         await env.unset(key)
         warnings = await self._setting_change(env, key)
         await self.notify_listeners(EnvironmentAction.updated,
                                     env.to_dto(), original_env)
         return attach_warnings(200, None, warnings)
     except KeyError:
         raise NotFound()
Пример #6
0
    async def notify_change(self, env: data.Environment, update: bool,
                            metadata: JsonType) -> Apireturn:
        LOGGER.info("Received change notification for environment %s", env.id)
        if "type" not in metadata:
            metadata["type"] = "api"

        if "message" not in metadata:
            metadata["message"] = "Recompile trigger through API call"

        warnings = await self._async_recompile(env, update, metadata=metadata)

        return attach_warnings(200, None, warnings)
Пример #7
0
 async def set_setting(self, env: data.Environment, key: str,
                       value: model.EnvSettingType) -> Apireturn:
     try:
         original_env = env.to_dto()
         await env.set(key, value)
         warnings = await self._setting_change(env, key)
         await self.notify_listeners(EnvironmentAction.updated,
                                     env.to_dto(), original_env)
         return attach_warnings(200, None, warnings)
     except KeyError:
         raise NotFound()
     except ValueError as e:
         raise ServerError(f"Invalid value. {e}")