Skip to content

Commit ef2be78

Browse files
committed
Adding AppWrapper from FS Management
1 parent 3527061 commit ef2be78

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/codeflare_sdk/cluster/awload.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
def __init__(self, filename: str) -> None:
29+
if not isfile(filename):
30+
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
31+
self.filename = filename
32+
try:
33+
with open(self.filename) as f:
34+
awyaml = yaml.load(f, Loader=yaml.FullLoader)
35+
assert awyaml["kind"] == "AppWrapper"
36+
self.name = awyaml["metadata"]["name"]
37+
self.namespace = awyaml["metadata"]["namespace"]
38+
except:
39+
raise ValueError(
40+
f"{filename } is not a correctly formatted AppWrapper yaml"
41+
)
42+
self.submitted = False
43+
44+
def submit(self) -> None:
45+
"""
46+
Attempts to create the AppWrapper custom resource using the yaml file
47+
"""
48+
try:
49+
with oc.project(self.namespace):
50+
oc.invoke("create", ["-f", self.filename])
51+
except oc.OpenShiftPythonException as osp: # pragma: no cover
52+
# WHATS THE EXCEPTION FOR ALREADY EXISTS?
53+
error_msg = osp.result.err()
54+
if "Unauthorized" in error_msg or "Forbidden" in error_msg:
55+
raise PermissionError(
56+
"Action not permitted, have you put in correct/up-to-date auth credentials?"
57+
)
58+
elif "AlreadyExists" in error_msg:
59+
raise FileExistsError(
60+
f"An AppWrapper of the name {self.name} already exists in namespace {self.namespace}"
61+
)
62+
raise osp
63+
64+
self.submitted = True
65+
print(f"AppWrapper {self.filename} submitted!")
66+
67+
def remove(self) -> None:
68+
"""
69+
Attempts to delete the AppWrapper custom resource matching the name in the yaml,
70+
if submitted by this manager.
71+
"""
72+
if not self.submitted:
73+
print("AppWrapper not submitted by this manager yet, nothing to remove")
74+
return
75+
76+
try:
77+
with oc.project(self.namespace):
78+
oc.invoke("delete", ["AppWrapper", self.name])
79+
except oc.OpenShiftPythonException as osp: # pragma: no cover
80+
error_msg = osp.result.err()
81+
if (
82+
'the server doesn\'t have a resource type "AppWrapper"' in error_msg
83+
or "forbidden" in error_msg
84+
or "Unauthorized" in error_msg
85+
or "Missing or incomplete configuration" in error_msg
86+
):
87+
raise PermissionError(
88+
"Action not permitted, have you put in correct/up-to-date auth credentials?"
89+
)
90+
elif "not found" in error_msg:
91+
self.submitted = False
92+
print("AppWrapper not found, was deleted in another manner")
93+
return
94+
else:
95+
raise osp
96+
97+
self.submitted = False
98+
print(f"AppWrapper {self.name} removed!")

src/codeflare_sdk/cluster/cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def down(self):
136136
or "Missing or incomplete configuration" in error_msg
137137
):
138138
raise PermissionError(
139-
"Action not permitted, have you run cluster.up() yet?"
139+
"Action not permitted, have you run auth.login()/cluster.up() yet?"
140140
)
141141
elif "not found" in error_msg:
142142
print("Cluster not found, have you run cluster.up() yet?")

0 commit comments

Comments
 (0)