-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(deployment): adds command for updating deployment
This commit introduces update deployment command that enables users to conveniently update existing deployments by providing deployment name or regex that can update multiple deployments
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright 2023 Rapyuta Robotics | ||
# | ||
# 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. | ||
|
||
import functools | ||
import re | ||
import sys | ||
|
||
import click | ||
from click_help_colors import HelpColorsCommand | ||
|
||
from rapyuta_io import Client, DeploymentPhaseConstants | ||
|
||
from riocli.config import new_client | ||
from riocli.constants import Symbols, Colors | ||
from riocli.utils import tabulate_data | ||
from riocli.utils.spinner import with_spinner | ||
|
||
@click.command( | ||
'update', | ||
cls=HelpColorsCommand, | ||
help_headers_color=Colors.YELLOW, | ||
help_options_color=Colors.GREEN, | ||
) | ||
@click.option('--force', '-f', '--silent', is_flag=True, default=False, | ||
help='Skip confirmation') | ||
@click.option('--update-all', '-a', is_flag=True, default=False, | ||
help='Updates all deployments') | ||
@click.argument('deployment-name-or-regex', type=str, default="") | ||
@with_spinner(text="Update deployment...") | ||
def update_deployment(force: bool, deployment_name_or_regex: str, update_all: bool, spinner=None) -> None: | ||
""" | ||
Delete the deployment from the Platform | ||
""" | ||
client = new_client() | ||
if not(deployment_name_or_regex or update_all): | ||
return | ||
|
||
deployments = fetch_deployments(client, deployment_name_or_regex, update_all) | ||
if not deployments: | ||
return | ||
|
||
headers = ["deployment name", "deployment id"] | ||
data = [] | ||
for deployment in deployments: | ||
data.append([deployment.name, deployment.deploymentId]) | ||
sys.stdout.write("\n") | ||
tabulate_data(data, headers) | ||
|
||
if not force: | ||
with spinner.hidden(): | ||
click.confirm("Do you want to update above deployment(s)?", default=True, abort=True) | ||
|
||
try: | ||
for deployment in deployments: | ||
client.update_deployment(deployment.deploymentId) | ||
|
||
spinner.text = click.style( | ||
'Deployment(s) updated successfully!', fg=Colors.GREEN) | ||
spinner.green.ok(Symbols.SUCCESS) | ||
except Exception as e: | ||
spinner.text = click.style( | ||
'Failed to update deployment(s): {}'.format(e), Colors.RED) | ||
spinner.red.fail(Symbols.ERROR) | ||
raise SystemExit(1) from e | ||
|
||
def fetch_deployments(client: Client, deployment_name_or_regex: str, update_all: bool): | ||
find_func = functools.partial(client.get_all_deployments, | ||
phases=[DeploymentPhaseConstants.SUCCEEDED, | ||
DeploymentPhaseConstants.PROVISIONING]) | ||
deployments = find_func() | ||
result = [] | ||
for deployment in deployments: | ||
if update_all or deployment.name == deployment_name_or_regex or \ | ||
(deployment_name_or_regex not in deployment.name and \ | ||
re.search(deployment_name_or_regex, deployment.name)): | ||
result.append(deployment) | ||
return result |