Skip to content

Commit

Permalink
Add SSH keys to GCP metadata (#881)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1d5964d)
  • Loading branch information
Egor-S committed Feb 7, 2024
1 parent 4bbb2f7 commit 3530693
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/dstack/_internal/core/backends/gcp/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def run_job(
project_id=self.config.project_id,
)
disk_size = round(instance_offer.instance.resources.disk.size_mib / 1024)
authorized_keys = [
run.run_spec.ssh_key_pub.strip(),
project_ssh_public_key.strip(),
]
for zone in _get_instance_zones(instance_offer):
request = compute_v1.InsertInstanceRequest()
request.zone = zone
Expand All @@ -112,12 +116,10 @@ def run_job(
user_data=get_user_data(
backend=BackendType.GCP,
image_name=job.job_spec.image_name,
authorized_keys=[
run.run_spec.ssh_key_pub.strip(),
project_ssh_public_key.strip(),
],
authorized_keys=authorized_keys,
registry_auth_required=job.job_spec.registry_auth is not None,
),
authorized_keys=authorized_keys,
labels={
"owner": "dstack",
"dstack_project": project_id,
Expand Down Expand Up @@ -179,6 +181,7 @@ def create_gateway(
accelerators=[],
spot=False,
user_data=get_gateway_user_data(ssh_key_pub),
authorized_keys=[ssh_key_pub],
labels={
"owner": "dstack",
"dstack_project": project_id,
Expand Down
4 changes: 4 additions & 0 deletions src/dstack/_internal/core/backends/gcp/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def create_instance_struct(
accelerators: List[compute_v1.AcceleratorConfig],
spot: bool,
user_data: str,
authorized_keys: List[str],
labels: Dict[str, str],
tags: List[str],
instance_name: str,
Expand Down Expand Up @@ -75,6 +76,9 @@ def create_instance_struct(

metadata_items = [
compute_v1.Items(key="user-data", value=user_data),
compute_v1.Items(
key="ssh-keys", value="\n".join(f"ubuntu:{key}" for key in authorized_keys)
),
]
instance.metadata = compute_v1.Metadata(items=metadata_items)

Expand Down

0 comments on commit 3530693

Please sign in to comment.