def run(self, deployment_id: int) -> None: """Run deployment. Args: deployment_id {int}: deployment id """ self._cursor.execute( f'SELECT type, model_uri, status ' f'FROM {DeployDbSchema.DEPLOYMENTS_TABLE} ' f'WHERE id = {deployment_id}' ) deployment_row = self._cursor.fetchone() if deployment_row is None: raise DeploymentNotFoundError(f'Deployment with ID {deployment_id} not found') deployment_type, model_uri, status = deployment_row if status == DeploymentStatus.RUNNING: return deployment = self._make_deployment(deployment_type) host, port, pid, instance_name = deployment.up(model_uri) self._cursor.execute( f'UPDATE {DeployDbSchema.DEPLOYMENTS_TABLE} ' f'SET host = %s, port = %s, status = %s, pid = %s, instance_name = %s, last_updated_at = %s ' f'WHERE id = {deployment_id}', (host, port, str(DeploymentStatus.RUNNING), pid, instance_name, get_rfc3339_time()) ) self._connection.commit()
def stop(self, deployment_id: int) -> None: """Stop deployment. Args: deployment_id {int}: deployment id """ self._cursor.execute( f'SELECT status, type, pid, instance_name ' f'FROM {DeployDbSchema.DEPLOYMENTS_TABLE} ' f'WHERE id = {deployment_id} AND ' f' status <> \'{str(DeploymentStatus.DELETED)}\'' ) deployment_row = self._cursor.fetchone() if deployment_row is None: raise DeploymentNotFoundError(f'Deployment with ID {deployment_id} not found') status, deployment_type, pid, instance_name = deployment_row if status == DeploymentStatus.STOPPED: return deployment = self._make_deployment(deployment_type) deployment.stop(pid, instance_name) self._cursor.execute( f'UPDATE {DeployDbSchema.DEPLOYMENTS_TABLE} ' f'SET status = %s, host = %s, port = %s, last_updated_at = %s ' f'WHERE id = {deployment_id}', (str(DeploymentStatus.STOPPED), None, None, get_rfc3339_time()) ) self._connection.commit()
def delete(self, deployment_id: int) -> None: """Delete deployment (mark as deleted. Args: deployment_id {int}: deployment id """ self.stop(deployment_id) self._cursor.execute( f'UPDATE {DeployDbSchema.DEPLOYMENTS_TABLE} ' f'SET status = %s, host = %s, port = %s, last_updated_at = %s ' f'WHERE id = {deployment_id}', (str(DeploymentStatus.DELETED), None, None, get_rfc3339_time()) ) self._connection.commit()
def create_project(self, name: Text, description: Text = '') -> int: """Create new project: create project folder in workspace and assign tracking server port for the project. Args: name {Text}: name of project description {Text}: project description Returns: int: project id """ if not isinstance(name, str) or len(name) == 0: raise BadProjectNameError(f'Bad project name: "{name}" of type {type(name)}') if self._project_name_exists(name): project = self.get_project_by_name(name) archived = project.get('archived') additional_error_msg = '' if archived is True: additional_error_msg = ' - archived' raise ProjectAlreadyExistsError( f'Project "{name}" already exists {additional_error_msg}') port = self._get_free_port() self._cursor.execute( f'INSERT INTO {ProjectsDBSchema.PROJECTS_TABLE} ' f'(name, description, port, archived, created_at, pid) ' f'VALUES (%s,%s,%s,%s,%s,%s) ' f'RETURNING id', (name, description, port, 0, get_rfc3339_time(), -1) ) project_id = self._cursor.fetchone()[0] print('project_id:', project_id) project_path = os.path.join(self._WORKSPACE, str(project_id)) os.makedirs(project_path, exist_ok=True) self._cursor.execute( f'UPDATE {ProjectsDBSchema.PROJECTS_TABLE} ' f'SET path = %s ' f'WHERE id = {project_id}', (project_path, ) ) self._connection.commit() return project_id
def _insert_new_deployment_in_db( self, project_id: int, model_id: Text, model_version: Text, model_uri: Text, host: Text, port: int, pid: int, instance_name: Text, deployment_type: Text ) -> int: """Insert new deployment record in database. Args: project_id {int}: project id model_id {Text}: model id (name) model_version {Text}: model version model_uri {Text}: path to model package host {Text}: host address port {int}: port number pid {int}: deployment process number instance_name {Text}: name of instance deployment_type {Text}: deployment type Returns: int: id of insert deployment record Notes: * pid: is some positive integer in case of local deployment and -1, if deployment type is remote; * instance_name: is empty string for local and non-empty string (name of remote virtual machine) for remote deployment. """ # pylint: disable=too-many-arguments creation_datetime = get_rfc3339_time() self._cursor.execute( f'INSERT INTO {DeployDbSchema.DEPLOYMENTS_TABLE} ' f'(project_id, model_id, version, model_uri, host, port, ' f'pid, instance_name, type, created_at, last_updated_at, status) ' f'VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ' f'RETURNING id', ( project_id, model_id, model_version, model_uri, host, port, pid, instance_name, deployment_type, creation_datetime, creation_datetime, str(DeploymentStatus.RUNNING) ) ) deployment_id = self._cursor.fetchone()[0] self._connection.commit() return deployment_id