Skip to content
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

Add suspend/resume engine to the api #150

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.6.20

* Add suspend/resume engine to the api
* Add examples for suspend/resume

## v0.6.19

* Fix setting of created_on attribute on AccessToken
Expand Down
39 changes: 39 additions & 0 deletions examples/resume_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2021 RelationalAI, Inc.
#
# 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

"""Resume an engine."""

from argparse import ArgumentParser
import json
import time
from urllib.request import HTTPError
from railib import api, config, show


def run(engine: str, profile: str):
cfg = config.read(profile=profile)
ctx = api.Context(**cfg)
api.resume_engine_wait(ctx, engine)
print(json.dumps(api.get_engine(ctx, engine), indent=2))


if __name__ == "__main__":
p = ArgumentParser()
p.add_argument("engine", type=str, help="engine name")
p.add_argument("-p", "--profile", type=str, help="profile name", default="default")
args = p.parse_args()
try:
run(args.engine, args.profile)
except HTTPError as e:
show.http_error(e)
49 changes: 49 additions & 0 deletions examples/suspend_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2021 RelationalAI, Inc.
#
# 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

"""Suspend an engine."""

from argparse import ArgumentParser
import json
import time
from urllib.request import HTTPError
from railib import api, config, show


# Answers if the given state is a terminal state.
def is_term_state(state: str) -> bool:
return state == "SUSPENDED" or ("FAILED" in state)


def run(engine: str, profile: str):
cfg = config.read(profile=profile)
ctx = api.Context(**cfg)
rsp = api.suspend_engine(ctx, engine)
Akrion marked this conversation as resolved.
Show resolved Hide resolved
while True: # wait for request to reach terminal state
time.sleep(3)
rsp = api.get_engine(ctx, engine)
if not rsp or is_term_state(rsp["state"]):
break
print(json.dumps(rsp, indent=2))


if __name__ == "__main__":
p = ArgumentParser()
p.add_argument("engine", type=str, help="engine name")
p.add_argument("-p", "--profile", type=str, help="profile name", default="default")
args = p.parse_args()
try:
run(args.engine, args.profile)
except HTTPError as e:
show.http_error(e)
2 changes: 1 addition & 1 deletion railib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version_info__ = (0, 7, 3)
__version_info__ = (0, 7, 4)
__version__ = ".".join(map(str, __version_info__))
26 changes: 26 additions & 0 deletions railib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class Permission(str, Enum):
"list_oauth_clients",
"load_csv",
"update_user",
"suspend_engine",
"resume_engine",
]


Expand Down Expand Up @@ -373,6 +375,30 @@ def create_engine_wait(ctx: Context, engine: str, size: str = "XS", **kwargs):
return get_engine(ctx, engine)


def suspend_engine(ctx: Context, engine: str, **kwargs):
data = { "suspend": True }
Akrion marked this conversation as resolved.
Show resolved Hide resolved
url = _mkurl(ctx, f"{PATH_ENGINE}/{engine}")
rsp = rest.patch(ctx, url, data, **kwargs)
return json.loads(rsp.read())


def resume_engine(ctx: Context, engine: str, **kwargs):
data = { "suspend": False }
Akrion marked this conversation as resolved.
Show resolved Hide resolved
url = _mkurl(ctx, f"{PATH_ENGINE}/{engine}")
rsp = rest.patch(ctx, url, data, **kwargs)
return json.loads(rsp.read())


def resume_engine_wait(ctx: Context, engine: str, **kwargs):
resume_engine(ctx, engine, **kwargs)
poll_with_specified_overhead(
lambda: is_engine_term_state(get_engine(ctx, engine)["state"]),
overhead_rate=0.2,
timeout=30 * 60,
)
return get_engine(ctx, engine)


def create_user(ctx: Context, email: str, roles: List[Role] = None, **kwargs):
rs = roles or []
data = {"email": email, "roles": [r.value for r in rs]}
Expand Down
Loading