Skip to content

Commit 233f1ef

Browse files
committed
refactor cli k8sworker
1 parent 6835b41 commit 233f1ef

File tree

2 files changed

+205
-178
lines changed

2 files changed

+205
-178
lines changed

bin/cli.py

Lines changed: 1 addition & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
ContainerPlatformClient,
4646
ContainerPlatformClientException,
4747
)
48-
from hpecp.k8s_worker import WorkerK8sStatus, WorkerK8s
4948
from hpecp.logger import Logger
5049
from hpecp.k8s_cluster import (
5150
K8sCluster,
@@ -66,6 +65,7 @@
6665
from hpecp.cli import base
6766
from hpecp.cli.catalog import CatalogProxy
6867
from hpecp.cli.gateway import GatewayProxy
68+
from hpecp.cli.k8sworker import K8sWorkerProxy
6969

7070
if sys.version_info[0] >= 3:
7171
unicode = str
@@ -137,183 +137,6 @@ def get_client(start_session=True):
137137
return client
138138

139139

140-
class K8sWorkerProxy(base.BaseProxy):
141-
"""Proxy object to :py:attr:`<hpecp.client.k8s_worker>`."""
142-
143-
def __dir__(self):
144-
"""Return the CLI method names."""
145-
return [
146-
"create_with_ssh_key",
147-
"delete",
148-
"get",
149-
"list",
150-
"set_storage",
151-
"statuses",
152-
"wait_for_status",
153-
]
154-
155-
def __init__(self):
156-
"""Create instance of proxy class with the client module name."""
157-
super(K8sWorkerProxy, self).new_instance("k8s_worker", WorkerK8s)
158-
159-
@intercept_exception
160-
def create_with_ssh_key(
161-
self,
162-
ip=None,
163-
ssh_key=None,
164-
ssh_key_file=None,
165-
tags=[],
166-
ephemeral_disks=None,
167-
persistent_disks=None,
168-
wait_for_operation_secs=0,
169-
):
170-
"""Create a K8s Worker using SSH key authentication.
171-
172-
Parameters
173-
----------
174-
ip : str, optional
175-
The IP address of the host, this is used for internal
176-
communication, by default None.
177-
ssh_key : str, optional
178-
The SSH key data as a string, instead of this location to a key
179-
file may also be provided, by default None.
180-
ssh_key_file : str, optional
181-
The SSH key file path, by default None
182-
tags : list, optional
183-
Tags to use, e.g. "{ "tag1": "foo", "tag2": "bar"}", by default []
184-
ephemeral_disks : str
185-
Comma separated string containing ephemeral disks.
186-
e.g: "/dev/nvme2n1,/dev/nvme2n2"
187-
persistent_disks : str, optional
188-
Comma separated string containing persistent disks, by default
189-
None.
190-
e.g: "/dev/nvme1n1,/dev/nvme1n2"
191-
wait_for_operation_secs: int
192-
wait for operations to complete. 0 = don't wait
193-
"""
194-
if ssh_key is None and ssh_key_file is None:
195-
print(
196-
"Either ssh_key or ssh_key_file must be provided",
197-
file=sys.stderr,
198-
)
199-
sys.exit(1)
200-
201-
if ssh_key is not None and ssh_key_file is not None:
202-
print(
203-
"Either ssh_key or ssh_key_file must be provided",
204-
file=sys.stderr,
205-
)
206-
sys.exit(1)
207-
208-
if ssh_key_file:
209-
try:
210-
with open(ssh_key_file) as f:
211-
ssh_key = f.read()
212-
except OSError:
213-
print(
214-
"Could not open/read ssh-key-file: {}".format(
215-
ssh_key_file
216-
),
217-
file=sys.stderr,
218-
)
219-
sys.exit(1)
220-
221-
if (
222-
ephemeral_disks is not None or persistent_disks is not None
223-
) and wait_for_operation_secs == 0:
224-
print(
225-
(
226-
"If setting disks, 'wait-for-operation-secs' parameter"
227-
" must be greater than zero (recommended 600 seconds)"
228-
),
229-
file=sys.stderr,
230-
)
231-
sys.exit(1)
232-
233-
worker_id = get_client().k8s_worker.create_with_ssh_key(
234-
ip=ip, ssh_key_data=ssh_key, tags=tags,
235-
)
236-
237-
if wait_for_operation_secs > 0:
238-
self.wait_for_status(
239-
id=worker_id,
240-
status=["storage_pending", "error"],
241-
timeout_secs=wait_for_operation_secs,
242-
)
243-
244-
if get_client().k8s_worker.get(id=worker_id).status == "error":
245-
print(
246-
(
247-
"Create request has errored. "
248-
"Check status message with `hpecp k8sworker get {}".format(
249-
id
250-
)
251-
),
252-
file=sys.stderr,
253-
)
254-
sys.exit(1)
255-
256-
if ephemeral_disks is not None or persistent_disks is not None:
257-
self.set_storage(
258-
id=worker_id,
259-
ephemeral_disks=ephemeral_disks,
260-
persistent_disks=persistent_disks,
261-
)
262-
263-
if wait_for_operation_secs > 0:
264-
self.wait_for_status(
265-
id=worker_id,
266-
status=["ready"],
267-
timeout_secs=wait_for_operation_secs,
268-
)
269-
270-
print(worker_id)
271-
272-
# TODO: verify with engineering if setup_log is a valid parameter
273-
# def get(self, id, setup_log=False):
274-
# """Get a K8SWorker."""
275-
# if setup_log is True:
276-
# params = {"setup_log": "true"}
277-
# else:
278-
# params = {}
279-
# return super(K8sWorkerProxy, self).get(id=id, params=params)
280-
281-
@intercept_exception
282-
def set_storage(
283-
self, id, ephemeral_disks, persistent_disks=None,
284-
):
285-
"""Set storage for a k8s worker.
286-
287-
Parameters
288-
----------
289-
id : str
290-
The k8s worker ID
291-
ephemeral_disks : str
292-
Comma separated string containing ephemeral disks.
293-
e.g: "/dev/nvme2n1,/dev/nvme2n2"
294-
persistent_disks : str, optional
295-
Comma separated string containing persistent disks, by default
296-
None.
297-
e.g: "/dev/nvme1n1,/dev/nvme1n2"
298-
"""
299-
if not ephemeral_disks:
300-
print("'ephemeral_disks' must be provided", file=sys.stderr)
301-
sys.exit(1)
302-
303-
p_disks = (
304-
persistent_disks.split(",") if persistent_disks is not None else []
305-
)
306-
e_disks = ephemeral_disks.split(",")
307-
308-
get_client().k8s_worker.set_storage(
309-
worker_id=id, persistent_disks=p_disks, ephemeral_disks=e_disks,
310-
)
311-
312-
def statuses(self,):
313-
"""Return a list of valid statuses."""
314-
print([s.name for s in WorkerK8sStatus])
315-
316-
317140
class K8sClusterProxy(base.BaseProxy):
318141
"""Proxy object to :py:attr:`<hpecp.client.k8s_cluster>`."""
319142

0 commit comments

Comments
 (0)