|
| 1 | +# (C) Copyright [2020] Hewlett Packard Enterprise Development LP |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +# copy of this software and associated documentation files (the "Software"), |
| 5 | +# to deal in the Software without restriction, including without limitation |
| 6 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | +# and/or sell copies of the Software, and to permit persons to whom the |
| 8 | +# Software is furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included |
| 11 | +# in all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 17 | +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 18 | +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 19 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +"""HPE Container Platform CLI.""" |
| 22 | + |
| 23 | +from __future__ import print_function |
| 24 | + |
| 25 | +import sys |
| 26 | +from hpecp.epic_worker import WorkerEpicStatus, WorkerEpic |
| 27 | +from hpecp.cli import base |
| 28 | + |
| 29 | + |
| 30 | +class EpicWorkerProxy(base.BaseProxy): |
| 31 | + """Proxy object to :py:attr:`<hpecp.client.epic_worker>`.""" |
| 32 | + |
| 33 | + def __dir__(self): |
| 34 | + """Return the CLI method names.""" |
| 35 | + return [ |
| 36 | + "create_with_ssh_key", |
| 37 | + "delete", |
| 38 | + "get", |
| 39 | + "list", |
| 40 | + "set_storage", |
| 41 | + "statuses", |
| 42 | + "wait_for_status", |
| 43 | + ] |
| 44 | + |
| 45 | + def __init__(self): |
| 46 | + """Create instance of proxy class with the client module name.""" |
| 47 | + super(EpicWorkerProxy, self).new_instance("epic_worker", WorkerEpic) |
| 48 | + |
| 49 | + @base.intercept_exception |
| 50 | + def create_with_ssh_key( |
| 51 | + self, |
| 52 | + ip=None, |
| 53 | + ssh_key=None, |
| 54 | + ssh_key_file=None, |
| 55 | + tags=[], |
| 56 | + ephemeral_disks=None, |
| 57 | + persistent_disks=None, |
| 58 | + wait_for_operation_secs=0, |
| 59 | + ): |
| 60 | + """Create a Epic Worker using SSH key authentication. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + ip : str, optional |
| 65 | + The IP address of the host, this is used for internal |
| 66 | + communication, by default None. |
| 67 | + ssh_key : str, optional |
| 68 | + The SSH key data as a string, instead of this location to a key |
| 69 | + file may also be provided, by default None. |
| 70 | + ssh_key_file : str, optional |
| 71 | + The SSH key file path, by default None |
| 72 | + tags : list, optional |
| 73 | + Tags to use, e.g. "{ "tag1": "foo", "tag2": "bar"}", by default [] |
| 74 | + ephemeral_disks : str |
| 75 | + Comma separated string containing ephemeral disks. |
| 76 | + e.g: "/dev/nvme2n1,/dev/nvme2n2" |
| 77 | + persistent_disks : str, optional |
| 78 | + Comma separated string containing persistent disks, by default |
| 79 | + None. |
| 80 | + e.g: "/dev/nvme1n1,/dev/nvme1n2" |
| 81 | + wait_for_operation_secs: int |
| 82 | + wait for operations to complete. 0 = don't wait |
| 83 | + """ |
| 84 | + if ssh_key is None and ssh_key_file is None: |
| 85 | + print( |
| 86 | + "Either ssh_key or ssh_key_file must be provided", |
| 87 | + file=sys.stderr, |
| 88 | + ) |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + if ssh_key is not None and ssh_key_file is not None: |
| 92 | + print( |
| 93 | + "Either ssh_key or ssh_key_file must be provided", |
| 94 | + file=sys.stderr, |
| 95 | + ) |
| 96 | + sys.exit(1) |
| 97 | + |
| 98 | + if ssh_key_file: |
| 99 | + try: |
| 100 | + with open(ssh_key_file) as f: |
| 101 | + ssh_key = f.read() |
| 102 | + except OSError: |
| 103 | + print( |
| 104 | + "Could not open/read ssh-key-file: {}".format( |
| 105 | + ssh_key_file |
| 106 | + ), |
| 107 | + file=sys.stderr, |
| 108 | + ) |
| 109 | + sys.exit(1) |
| 110 | + |
| 111 | + if ( |
| 112 | + ephemeral_disks is not None or persistent_disks is not None |
| 113 | + ) and wait_for_operation_secs == 0: |
| 114 | + print( |
| 115 | + ( |
| 116 | + "If setting disks, 'wait-for-operation-secs' parameter" |
| 117 | + " must be greater than zero (recommended 600 seconds)" |
| 118 | + ), |
| 119 | + file=sys.stderr, |
| 120 | + ) |
| 121 | + sys.exit(1) |
| 122 | + |
| 123 | + worker_id = base.get_client().epic_worker.create_with_ssh_key( |
| 124 | + ip=ip, |
| 125 | + ssh_key_data=ssh_key, |
| 126 | + tags=tags, |
| 127 | + ) |
| 128 | + |
| 129 | + if wait_for_operation_secs > 0: |
| 130 | + self.wait_for_status( |
| 131 | + id=worker_id, |
| 132 | + status=["storage_pending", "error"], |
| 133 | + timeout_secs=wait_for_operation_secs, |
| 134 | + ) |
| 135 | + |
| 136 | + if base.get_client().epic_worker.get(id=worker_id).status == "error": |
| 137 | + print( |
| 138 | + ( |
| 139 | + "Create request has errored. " |
| 140 | + "Check status message with `hpecp epicworker get {}".format( |
| 141 | + id |
| 142 | + ) |
| 143 | + ), |
| 144 | + file=sys.stderr, |
| 145 | + ) |
| 146 | + sys.exit(1) |
| 147 | + |
| 148 | + if ephemeral_disks is not None or persistent_disks is not None: |
| 149 | + self.set_storage( |
| 150 | + id=worker_id, |
| 151 | + ephemeral_disks=ephemeral_disks, |
| 152 | + persistent_disks=persistent_disks, |
| 153 | + ) |
| 154 | + |
| 155 | + if wait_for_operation_secs > 0: |
| 156 | + self.wait_for_status( |
| 157 | + id=worker_id, |
| 158 | + status=["ready"], |
| 159 | + timeout_secs=wait_for_operation_secs, |
| 160 | + ) |
| 161 | + |
| 162 | + print(worker_id) |
| 163 | + |
| 164 | + # TODO: verify with engineering if setup_log is a valid parameter |
| 165 | + # def get(self, id, setup_log=False): |
| 166 | + # """Get a K8SWorker.""" |
| 167 | + # if setup_log is True: |
| 168 | + # params = {"setup_log": "true"} |
| 169 | + # else: |
| 170 | + # params = {} |
| 171 | + # return super(EpicWorkerProxy, self).get(id=id, params=params) |
| 172 | + |
| 173 | + @base.intercept_exception |
| 174 | + def set_storage( |
| 175 | + self, |
| 176 | + id, |
| 177 | + ephemeral_disks, |
| 178 | + persistent_disks=None, |
| 179 | + ): |
| 180 | + """Set storage for a epic worker. |
| 181 | +
|
| 182 | + Parameters |
| 183 | + ---------- |
| 184 | + id : str |
| 185 | + The epic worker ID |
| 186 | + ephemeral_disks : str |
| 187 | + Comma separated string containing ephemeral disks. |
| 188 | + e.g: "/dev/nvme2n1,/dev/nvme2n2" |
| 189 | + persistent_disks : str, optional |
| 190 | + Comma separated string containing persistent disks, by default |
| 191 | + None. |
| 192 | + e.g: "/dev/nvme1n1,/dev/nvme1n2" |
| 193 | + """ |
| 194 | + if not ephemeral_disks: |
| 195 | + print("'ephemeral_disks' must be provided", file=sys.stderr) |
| 196 | + sys.exit(1) |
| 197 | + |
| 198 | + p_disks = ( |
| 199 | + persistent_disks.split(",") if persistent_disks is not None else [] |
| 200 | + ) |
| 201 | + e_disks = ephemeral_disks.split(",") |
| 202 | + |
| 203 | + base.get_client().epic_worker.set_storage( |
| 204 | + worker_id=id, |
| 205 | + persistent_disks=p_disks, |
| 206 | + ephemeral_disks=e_disks, |
| 207 | + ) |
| 208 | + |
| 209 | + def statuses( |
| 210 | + self, |
| 211 | + ): |
| 212 | + """Return a list of valid statuses.""" |
| 213 | + print([s.name for s in WorkerEpicStatus]) |
0 commit comments