Skip to content

Adding AppWrapper from File System Management #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/codeflare_sdk/cluster/awload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2022 IBM, Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
The awload sub-module contains the definition of the AWManager object, which handles
submission and deletion of existing AppWrappers from a user's file system.
"""

from os.path import isfile
import errno
import os
import openshift as oc
import yaml


class AWManager:
def __init__(self, filename: str) -> None:
if not isfile(filename):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
self.filename = filename
try:
with open(self.filename) as f:
awyaml = yaml.load(f, Loader=yaml.FullLoader)
assert awyaml["kind"] == "AppWrapper"
self.name = awyaml["metadata"]["name"]
self.namespace = awyaml["metadata"]["namespace"]
except:
raise ValueError(
f"{filename } is not a correctly formatted AppWrapper yaml"
)
self.submitted = False

def submit(self) -> None:
"""
Attempts to create the AppWrapper custom resource using the yaml file
"""
try:
with oc.project(self.namespace):
oc.invoke("create", ["-f", self.filename])
except oc.OpenShiftPythonException as osp: # pragma: no cover
# WHATS THE EXCEPTION FOR ALREADY EXISTS?
error_msg = osp.result.err()
if "Unauthorized" in error_msg or "Forbidden" in error_msg:
raise PermissionError(
"Action not permitted, have you put in correct/up-to-date auth credentials?"
)
elif "AlreadyExists" in error_msg:
raise FileExistsError(
f"An AppWrapper of the name {self.name} already exists in namespace {self.namespace}"
)
raise osp

self.submitted = True
print(f"AppWrapper {self.filename} submitted!")

def remove(self) -> None:
"""
Attempts to delete the AppWrapper custom resource matching the name in the yaml,
if submitted by this manager.
"""
if not self.submitted:
print("AppWrapper not submitted by this manager yet, nothing to remove")
return

try:
with oc.project(self.namespace):
oc.invoke("delete", ["AppWrapper", self.name])
except oc.OpenShiftPythonException as osp: # pragma: no cover
error_msg = osp.result.err()
if (
'the server doesn\'t have a resource type "AppWrapper"' in error_msg
or "forbidden" in error_msg
or "Unauthorized" in error_msg
or "Missing or incomplete configuration" in error_msg
):
raise PermissionError(
"Action not permitted, have you put in correct/up-to-date auth credentials?"
)
elif "not found" in error_msg:
self.submitted = False
print("AppWrapper not found, was deleted in another manner")
return
else:
raise osp

self.submitted = False
print(f"AppWrapper {self.name} removed!")
2 changes: 1 addition & 1 deletion src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def down(self):
or "Missing or incomplete configuration" in error_msg
):
raise PermissionError(
"Action not permitted, have you run cluster.up() yet?"
"Action not permitted, have you run auth.login()/cluster.up() yet?"
)
elif "not found" in error_msg:
print("Cluster not found, have you run cluster.up() yet?")
Expand Down