|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8; -*- |
| 3 | + |
| 4 | +# Copyright (c) 2022, 2023 Oracle and/or its affiliates. |
| 5 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 6 | + |
| 7 | +from typing import Dict |
| 8 | +import ads |
| 9 | +from ads.common.auth import create_signer, AuthContext |
| 10 | +from ads.common.oci_client import OCIClientFactory |
| 11 | +from ads.opctl.backend.base import Backend |
| 12 | +from ads.pipeline import Pipeline, PipelineRun |
| 13 | + |
| 14 | + |
| 15 | +class PipelineBackend(Backend): |
| 16 | + def __init__(self, config: Dict) -> None: |
| 17 | + """ |
| 18 | + Initialize a MLPipeline object given config dictionary. |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + config: dict |
| 23 | + dictionary of configurations |
| 24 | + """ |
| 25 | + self.config = config |
| 26 | + self.oci_auth = create_signer( |
| 27 | + config["execution"].get("auth"), |
| 28 | + config["execution"].get("oci_config", None), |
| 29 | + config["execution"].get("oci_profile", None), |
| 30 | + ) |
| 31 | + self.auth_type = config["execution"].get("auth") |
| 32 | + self.profile = config["execution"].get("oci_profile", None) |
| 33 | + self.client = OCIClientFactory(**self.oci_auth).data_science |
| 34 | + |
| 35 | + def apply(self) -> None: |
| 36 | + """ |
| 37 | + Create Pipeline and Pipeline Run from YAML. |
| 38 | + """ |
| 39 | + with AuthContext(): |
| 40 | + ads.set_auth(auth=self.auth_type, profile=self.profile) |
| 41 | + pipeline = Pipeline.from_dict(self.config) |
| 42 | + pipeline.create() |
| 43 | + pipeline_run = pipeline.run() |
| 44 | + print("PIPELINE OCID:", pipeline.id) |
| 45 | + print("PIPELINE RUN OCID:", pipeline_run.id) |
| 46 | + |
| 47 | + def run(self) -> None: |
| 48 | + """ |
| 49 | + Create Pipeline and Pipeline Run from OCID. |
| 50 | + """ |
| 51 | + pipeline_id = self.config["execution"]["ocid"] |
| 52 | + with AuthContext(): |
| 53 | + ads.set_auth(auth=self.auth_type, profile=self.profile) |
| 54 | + pipeline = Pipeline.from_ocid(ocid=pipeline_id) |
| 55 | + pipeline_run = pipeline.run() |
| 56 | + print("PIPELINE RUN OCID:", pipeline_run.id) |
| 57 | + |
| 58 | + def delete(self) -> None: |
| 59 | + """ |
| 60 | + Delete Pipeline or Pipeline Run from OCID. |
| 61 | + """ |
| 62 | + if self.config["execution"].get("id"): |
| 63 | + pipeline_id = self.config["execution"]["id"] |
| 64 | + with AuthContext(): |
| 65 | + ads.set_auth(auth=self.auth_type, profile=self.profile) |
| 66 | + Pipeline.from_ocid(pipeline_id).delete() |
| 67 | + print(f"Pipeline {pipeline_id} has been deleted.") |
| 68 | + elif self.config["execution"].get("run_id"): |
| 69 | + run_id = self.config["execution"]["run_id"] |
| 70 | + with AuthContext(): |
| 71 | + ads.set_auth(auth=self.auth_type, profile=self.profile) |
| 72 | + PipelineRun.from_ocid(run_id).delete() |
| 73 | + print(f"Pipeline run {run_id} has been deleted.") |
| 74 | + |
| 75 | + def cancel(self) -> None: |
| 76 | + """ |
| 77 | + Cancel Pipeline Run from OCID. |
| 78 | + """ |
| 79 | + run_id = self.config["execution"]["run_id"] |
| 80 | + with AuthContext(): |
| 81 | + ads.set_auth(auth=self.auth_type, profile=self.profile) |
| 82 | + PipelineRun.from_ocid(run_id).cancel() |
| 83 | + print(f"Pipeline run {run_id} has been cancelled.") |
| 84 | + |
| 85 | + def watch(self) -> None: |
| 86 | + """ |
| 87 | + Watch Pipeline Run from OCID. |
| 88 | + """ |
| 89 | + run_id = self.config["execution"]["run_id"] |
| 90 | + log_type = self.config["execution"]["log_type"] |
| 91 | + with AuthContext(): |
| 92 | + ads.set_auth(auth=self.auth_type, profile=self.profile) |
| 93 | + PipelineRun.from_ocid(run_id).watch(log_type=log_type) |
0 commit comments