Skip to content

Commit 20d9306

Browse files
committed
Store sim pubkey after generating it
1 parent 29dd0ee commit 20d9306

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

hpc_provisioner/src/hpc_provisioner/dynamodb_actions.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import logging.config
3-
from typing import Optional
3+
from typing import Optional, Union
44

55
import boto3
66
from boto3.dynamodb.conditions import Key
@@ -133,25 +133,35 @@ def delete_cluster(dynamodb_resource, cluster: Cluster) -> None:
133133
table.delete_item(Key={"name": cluster.name})
134134

135135

136-
def _update_cluster_claim(dynamodb_resource, cluster: Cluster, new_value: int) -> None:
136+
def _update_cluster_field(
137+
dynamodb_resource, cluster: Cluster, update_field: str, new_value: Union[int, str]
138+
) -> None:
137139
stored_cluster = get_cluster_by_name(dynamodb_resource, cluster.name)
138140
if not stored_cluster:
139-
raise ClusterNotRegistered(f"Cluster {cluster} is not registered - cannot claim it")
141+
raise ClusterNotRegistered(f"Cluster {cluster} is not registered - cannot update it")
140142

141-
if stored_cluster.get("provisioning_launched") == new_value:
142-
raise ClusterAlreadyInClaimState(f"Cluster {cluster} already has claim state {new_value}")
143+
if update_field == "provisioning_launched" and stored_cluster.get(update_field) == new_value:
144+
raise ClusterAlreadyInClaimState(
145+
f"Cluster {cluster} already has {update_field} set to {new_value}"
146+
)
143147

144148
table = dynamodb_resource.Table(CLUSTER_TABLE_NAME)
145149
table.update_item(
146150
Key={"name": cluster.name},
147-
UpdateExpression="SET provisioning_launched = :claim",
148-
ExpressionAttributeValues={":claim": new_value},
151+
UpdateExpression=f"SET {update_field} = :new_value",
152+
ExpressionAttributeValues={":new_value": new_value},
149153
)
150154

151155

152156
def claim_cluster(dynamodb_resource, cluster: Cluster) -> None:
153-
_update_cluster_claim(dynamodb_resource, cluster, 1)
157+
_update_cluster_field(dynamodb_resource, cluster, "provisioning_launched", 1)
154158

155159

156160
def release_cluster(dynamodb_resource, cluster: Cluster) -> None:
157-
_update_cluster_claim(dynamodb_resource, cluster, 0)
161+
_update_cluster_field(dynamodb_resource, cluster, "provisioning_launched", 0)
162+
163+
164+
def set_cluster_sim_user_ssh_key(
165+
dynamodb_resource, cluster: Cluster, sim_user_ssh_key: str
166+
) -> None:
167+
_update_cluster_field(dynamodb_resource, cluster, "sim_pubkey", sim_user_ssh_key)

hpc_provisioner/src/hpc_provisioner/handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
get_cluster_by_name,
3131
get_unclaimed_clusters,
3232
register_cluster,
33+
set_cluster_sim_user_ssh_key,
3334
)
3435
from hpc_provisioner.utils import (
3536
generate_public_key,
@@ -192,6 +193,7 @@ def pcluster_create_request_handler(event, _context=None):
192193

193194
if key_material := sm_client.get_secret_value(SecretId=sim_user_secret["ARN"]):
194195
cluster.sim_pubkey = generate_public_key(key_material["SecretString"])
196+
set_cluster_sim_user_ssh_key(dynamo, cluster, cluster.sim_pubkey)
195197
else:
196198
raise RuntimeError(
197199
f"Something went wrong retrieving the sim user private key: {sim_user_secret['ARN']}"

0 commit comments

Comments
 (0)