|
| 1 | +# Copyright 2022 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +The awload sub-module contains the definition of the AWManager object, which handles |
| 17 | +submission and deletion of existing AppWrappers from a user's file system. |
| 18 | +""" |
| 19 | + |
| 20 | +from os.path import isfile |
| 21 | +import errno |
| 22 | +import os |
| 23 | +import openshift as oc |
| 24 | +import yaml |
| 25 | + |
| 26 | + |
| 27 | +class AWManager: |
| 28 | + """ |
| 29 | + An object for submitting and removing existing AppWrapper yamls |
| 30 | + to be added to the MCAD queue. |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, filename: str) -> None: |
| 34 | + """ |
| 35 | + Create the AppWrapper Manager object by passing in an |
| 36 | + AppWrapper yaml file |
| 37 | + """ |
| 38 | + if not isfile(filename): |
| 39 | + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename) |
| 40 | + self.filename = filename |
| 41 | + try: |
| 42 | + with open(self.filename) as f: |
| 43 | + awyaml = yaml.load(f, Loader=yaml.FullLoader) |
| 44 | + assert awyaml["kind"] == "AppWrapper" |
| 45 | + self.name = awyaml["metadata"]["name"] |
| 46 | + self.namespace = awyaml["metadata"]["namespace"] |
| 47 | + except: |
| 48 | + raise ValueError( |
| 49 | + f"{filename } is not a correctly formatted AppWrapper yaml" |
| 50 | + ) |
| 51 | + self.submitted = False |
| 52 | + |
| 53 | + def submit(self) -> None: |
| 54 | + """ |
| 55 | + Attempts to create the AppWrapper custom resource using the yaml file |
| 56 | + """ |
| 57 | + try: |
| 58 | + with oc.project(self.namespace): |
| 59 | + oc.invoke("create", ["-f", self.filename]) |
| 60 | + except oc.OpenShiftPythonException as osp: # pragma: no cover |
| 61 | + error_msg = osp.result.err() |
| 62 | + if "Unauthorized" in error_msg or "Forbidden" in error_msg: |
| 63 | + raise PermissionError( |
| 64 | + "Action not permitted, have you put in correct/up-to-date auth credentials?" |
| 65 | + ) |
| 66 | + elif "AlreadyExists" in error_msg: |
| 67 | + raise FileExistsError( |
| 68 | + f"An AppWrapper of the name {self.name} already exists in namespace {self.namespace}" |
| 69 | + ) |
| 70 | + raise osp |
| 71 | + |
| 72 | + self.submitted = True |
| 73 | + print(f"AppWrapper {self.filename} submitted!") |
| 74 | + |
| 75 | + def remove(self) -> None: |
| 76 | + """ |
| 77 | + Attempts to delete the AppWrapper custom resource matching the name in the yaml, |
| 78 | + if submitted by this manager. |
| 79 | + """ |
| 80 | + if not self.submitted: |
| 81 | + print("AppWrapper not submitted by this manager yet, nothing to remove") |
| 82 | + return |
| 83 | + |
| 84 | + try: |
| 85 | + with oc.project(self.namespace): |
| 86 | + oc.invoke("delete", ["AppWrapper", self.name]) |
| 87 | + except oc.OpenShiftPythonException as osp: # pragma: no cover |
| 88 | + error_msg = osp.result.err() |
| 89 | + if ( |
| 90 | + 'the server doesn\'t have a resource type "AppWrapper"' in error_msg |
| 91 | + or "forbidden" in error_msg |
| 92 | + or "Unauthorized" in error_msg |
| 93 | + or "Missing or incomplete configuration" in error_msg |
| 94 | + ): |
| 95 | + raise PermissionError( |
| 96 | + "Action not permitted, have you put in correct/up-to-date auth credentials?" |
| 97 | + ) |
| 98 | + elif "not found" in error_msg: |
| 99 | + self.submitted = False |
| 100 | + print("AppWrapper not found, was deleted in another manner") |
| 101 | + return |
| 102 | + else: |
| 103 | + raise osp |
| 104 | + |
| 105 | + self.submitted = False |
| 106 | + print(f"AppWrapper {self.name} removed!") |
0 commit comments