Skip to content

Commit ef167f2

Browse files
Bugfix: making the hardware instance api call to defer (#61)
* worked on adding hardware instance mapping api to cli (#52) * fixing api call initialize issue by removing decorators
1 parent 3f90011 commit ef167f2

File tree

1 file changed

+44
-89
lines changed

1 file changed

+44
-89
lines changed

centml/cli/cluster.py

Lines changed: 44 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ def get_hw_to_id_map():
3737
return hw_to_id_map, id_to_hw_map
3838

3939

40+
# # Hardware pricing tier that loads choices dynamically
41+
class HardwarePricingTier(click.ParamType):
42+
def __init__(self):
43+
self.hw_to_id_map, self.id_to_hw_map = get_hw_to_id_map()
44+
self.choices = list(self.hw_to_id_map.keys())
45+
46+
def convert(self, value, param, ctx):
47+
if value not in self.choices:
48+
self.fail(f"{value} is not a valid choice. Available choices are: {', '.join(self.choices)}", param, ctx)
49+
return value
50+
51+
52+
hardware_pricing_tier_instance = HardwarePricingTier()
53+
4054
depl_type_map = {
4155
"inference": platform_api_client.DeploymentType.INFERENCE,
4256
"compute": platform_api_client.DeploymentType.COMPUTE,
@@ -113,16 +127,14 @@ def get(type, id):
113127

114128
click.echo(f"The current status of Deployment #{id} is: {ready_status}.")
115129

116-
_, id_to_hw_map = get_hw_to_id_map()
117-
118130
click.echo(
119131
tabulate(
120132
[
121133
("Name", deployment.name),
122134
("Image", deployment.image_url),
123135
("Endpoint", deployment.endpoint_url),
124136
("Created at", deployment.created_at.strftime("%Y-%m-%d %H:%M:%S")),
125-
("Hardware", id_to_hw_map[deployment.hardware_instance_id]),
137+
("Hardware", hardware_pricing_tier_instance.id_to_hw_map[deployment.hardware_instance_id]),
126138
],
127139
tablefmt="rounded_outline",
128140
disable_numparse=True,
@@ -158,88 +170,36 @@ def get(type, id):
158170
)
159171

160172

161-
# Define common deployment
162-
def common_options(func):
163-
hw_to_id_map, _ = get_hw_to_id_map()
164-
func = click.option("--name", "-n", prompt="Name", help="Name of the deployment")(func)
165-
func = click.option("--image", "-i", prompt="Image", help="Container image")(func)
166-
func = click.option(
167-
"--hardware",
168-
"-h",
169-
prompt="Hardware",
170-
type=click.Choice(list(hw_to_id_map.keys())),
171-
help="Hardware instance type",
172-
)(func)
173-
return func
174-
175-
176-
# Define inference specific options
177-
def inference_options(func):
178-
func = click.option("--port", "-p", prompt="Port", type=int, help="Port to expose")(func)
179-
func = click.option(
180-
"--env", type=InferenceEnvType(), help="Environment variables in the format KEY=VALUE", multiple=True
181-
)(func)
182-
func = click.option("--min_replicas", default="1", prompt="Min replicas", type=click.IntRange(1, 10))(func)
183-
func = click.option("--max_replicas", default="1", prompt="Max replicas", type=click.IntRange(1, 10))(func)
184-
func = click.option("--health", default="/", prompt="Health check", help="Health check endpoint")(func)
185-
func = click.option("--is_private", default=False, type=bool, prompt="Is private?", help="Is private endpoint?")(
186-
func
187-
)
188-
func = click.option("--timeout", prompt="Max concurrency", default=0, type=int)(func)
189-
func = click.option("--command", type=str, required=False, default=None, help="Define a command for a container")(
190-
func
191-
)
192-
func = click.option("--command_args", multiple=True, type=str, default=None, help="List of command arguments")(func)
193-
return func
194-
195-
196-
# Define compute specific options
197-
def compute_options(func):
198-
func = click.option("--username", prompt="Username", type=str, help="Username")(func)
199-
func = click.option("--password", prompt="Password", hide_input=True, type=str, help="password")(func)
200-
func = click.option(
201-
"--ssh_key", prompt="Add ssh key", default="", type=str, help="Would you like to add an SSH key?"
202-
)(func)
203-
return func
204-
205-
206-
# Main command group
207173
@click.group(help="Create a new deployment")
208-
@click.pass_context
209-
def create(ctx):
174+
def create():
210175
pass
211176

212177

213-
# Define the inference subcommand
214178
@create.command(name="inference", help="Create an inference deployment")
215-
@common_options
216-
@inference_options
217-
@click.pass_context
218-
def create_inference(ctx, **kwargs):
179+
@click.option("--name", "-n", prompt="Name", help="Name of the deployment")
180+
@click.option("--image", "-i", prompt="Image", help="Container image")
181+
@click.option("--hardware", "-h", prompt="Hardware", type=hardware_pricing_tier_instance, help="Hardware instance type")
182+
@click.option("--port", "-p", prompt="Port", type=int, help="Port to expose")
183+
@click.option("--env", type=InferenceEnvType(), help="Environment variables in the format KEY=VALUE", multiple=True)
184+
@click.option("--min_replicas", default="1", prompt="Min replicas", type=click.IntRange(1, 10))
185+
@click.option("--max_replicas", default="1", prompt="Max replicas", type=click.IntRange(1, 10))
186+
@click.option("--health", default="/", prompt="Health check", help="Health check endpoint")
187+
@click.option("--is_private", default=False, type=bool, prompt="Is private?", help="Is private endpoint?")
188+
@click.option("--timeout", prompt="Max concurrency", default=0, type=int)
189+
@click.option("--command", type=str, required=False, default=None, help="Define a command for a container")
190+
@click.option("--command_args", multiple=True, type=str, default=None, help="List of command arguments")
191+
def create_inference(
192+
name, image, hardware, port, env, min_replicas, max_replicas, health, is_private, timeout, command, command_args
193+
):
219194
click.echo("Creating inference deployment with the following options:")
220195

221-
name = kwargs.get("name")
222-
image = kwargs.get("image")
223-
port = kwargs.get("port")
224-
is_private = kwargs.get("is_private")
225-
hardware = kwargs.get("hardware")
226-
health = kwargs.get("health")
227-
min_replicas = kwargs.get("min_replicas")
228-
max_replicas = kwargs.get("max_replicas")
229-
env = kwargs.get("env")
230-
command = kwargs.get("command")
231-
command_args = kwargs.get("command_args")
232-
timeout = kwargs.get("timeout")
233-
234-
hw_to_id_map, _ = get_hw_to_id_map()
235-
236-
# Call the API function for creating infrence deployment
196+
# Call the API function for creating inference deployment
237197
resp = api.create_inference(
238198
name,
239199
image,
240200
port,
241201
is_private,
242-
hw_to_id_map[hardware],
202+
hardware_pricing_tier_instance.hw_to_id_map[hardware],
243203
health,
244204
min_replicas,
245205
max_replicas,
@@ -252,25 +212,20 @@ def create_inference(ctx, **kwargs):
252212
click.echo(f"Inference deployment #{resp.id} created at https://{resp.endpoint_url}/")
253213

254214

255-
# Define the compute subcommand
256215
@create.command(name="compute", help="Create a compute deployment")
257-
@common_options
258-
@compute_options
259-
@click.pass_context
260-
def create_compute(ctx, **kwargs):
261-
click.echo("Creating compute deployment with the following options:")
262-
263-
name = kwargs.get("name")
264-
image = kwargs.get("image")
265-
username = kwargs.get("username")
266-
password = kwargs.get("password")
267-
ssh_key = kwargs.get("ssh_key")
268-
hardware = kwargs.get("hardware")
269-
270-
hw_to_id_map, _ = get_hw_to_id_map()
216+
@click.option("--name", "-n", prompt="Name", help="Name of the deployment")
217+
@click.option("--image", "-i", prompt="Image", help="Container image")
218+
@click.option("--hardware", "-h", prompt="Hardware", type=hardware_pricing_tier_instance, help="Hardware instance type")
219+
@click.option("--username", prompt="Username", type=str, help="Username")
220+
@click.option("--password", prompt="Password", hide_input=True, type=str, help="password")
221+
@click.option("--ssh_key", prompt="Add ssh key", default="", type=str, help="Would you like to add an SSH key?")
222+
def create_compute(name, image, hardware, username, password, ssh_key):
223+
click.echo("Creating inference deployment with the following options:")
271224

272225
# Call the API function for creating infrence deployment
273-
resp = api.create_compute(name, image, username, password, ssh_key, hw_to_id_map[hardware])
226+
resp = api.create_compute(
227+
name, image, username, password, ssh_key, hardware_pricing_tier_instance.hw_to_id_map[hardware]
228+
)
274229

275230
click.echo(f"Compute deployment #{resp.id} created at https://{resp.endpoint_url}/")
276231

0 commit comments

Comments
 (0)