|
| 1 | +# Copyright 2021 RelationalAI, Inc. |
| 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 | +"""Suspend an engine.""" |
| 16 | + |
| 17 | +from argparse import ArgumentParser |
| 18 | +import json |
| 19 | +import time |
| 20 | +from urllib.request import HTTPError |
| 21 | +from railib import api, config, show |
| 22 | + |
| 23 | + |
| 24 | +# Answers if the given state is a terminal state. |
| 25 | +def is_term_state(state: str) -> bool: |
| 26 | + return state == "SUSPENDED" or ("FAILED" in state) |
| 27 | + |
| 28 | + |
| 29 | +def run(engine: str, profile: str): |
| 30 | + cfg = config.read(profile=profile) |
| 31 | + ctx = api.Context(**cfg) |
| 32 | + rsp = api.suspend_engine(ctx, engine) |
| 33 | + while True: # wait for request to reach terminal state |
| 34 | + time.sleep(3) |
| 35 | + rsp = api.get_engine(ctx, engine) |
| 36 | + if not rsp or is_term_state(rsp["state"]): |
| 37 | + break |
| 38 | + print(json.dumps(rsp, indent=2)) |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + p = ArgumentParser() |
| 43 | + p.add_argument("engine", type=str, help="engine name") |
| 44 | + p.add_argument("-p", "--profile", type=str, help="profile name", default="default") |
| 45 | + args = p.parse_args() |
| 46 | + try: |
| 47 | + run(args.engine, args.profile) |
| 48 | + except HTTPError as e: |
| 49 | + show.http_error(e) |
0 commit comments