-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathimage-gcp
More file actions
executable file
·336 lines (291 loc) · 10.8 KB
/
Copy pathimage-gcp
File metadata and controls
executable file
·336 lines (291 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python3
# region copyright
# Copyright 2023-2026 NVIDIA Corporation
#
# 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.
# endregion
import os
import shlex
import sys
from datetime import date
import click
from src.python.config import c as config
from src.python.debug import debug_break, debug_start # noqa
from src.python.deploy_command import DeployCommand
from src.python.utils import (
colorize_error,
colorize_info,
colorize_prompt,
gcp_login,
shell_command,
)
def _normalize_gcp_image_name(name):
"""
GCE image names must be lowercase and may contain only letters, numbers,
and dashes. Convert dots/underscores to dashes and lowercase the result.
"""
return name.lower().replace(".", "-").replace("_", "-")
class ImageGCPCommand(DeployCommand):
"""
Defines options specific for "image-gcp" command.
"""
# gcp instance types capable of running isaac (same as deploy-gcp)
GCP_OVKIT_INSTANCE_TYPES = (
[f"g2-standard-{i}" for i in [4, 8, 12, 16, 24, 32, 48, 96]]
+ [f"n1-standard-{i}" for i in [4, 8, 16, 32, 64, 96]]
+ [f"n1-highmem-{i}" for i in [4, 8, 16, 32, 64, 96]]
+ [f"n1-highcpu-{i}" for i in [16, 32, 64, 96]]
+ [f"g4-standard-{i}" for i in [48, 96, 192, 384]]
)
@staticmethod
def project_callback(ctx, param, value):
if value == "":
raise click.BadParameter(colorize_error("Project ID cannot be empty"))
return value
@staticmethod
def instance_type_callback(ctx, param, value):
if value not in ImageGCPCommand.GCP_OVKIT_INSTANCE_TYPES:
raise click.BadParameter(
colorize_error(
f"Invalid instance type: {value}. Valid values: "
+ ", ".join(ImageGCPCommand.GCP_OVKIT_INSTANCE_TYPES)
)
)
return value
def make_context(self, info_name, args, parent=None, **extra):
"""Allow image name as an unnamed first argument."""
args = list(args)
if args and not args[0].startswith("-") and "--image-name" not in args:
args = ["--image-name", args[0]] + args[1:]
return click.core.Command.make_context(
self, info_name, args, parent=parent, **extra
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# remove params not applicable to image building
self.params = [
p
for p in self.params
if p.name
not in (
"in_china",
"from_image",
"ingress_cidrs",
"vnc_password",
"upload",
"deployment_name",
)
]
# --image-name
self.params.insert(
0,
click.core.Option(
("--image-name",),
show_default="<current date>",
default=lambda: date.today().strftime("%Y-%m-%d"),
help="Image name. Will be prefixed with"
" 'isaac-automator-isaacworkstation-' and normalized to lowercase.",
prompt=colorize_prompt(
"* Image name (will be prefixed with"
" 'isaac-automator-isaacworkstation-')"
),
),
)
# replace --existing with overwrite/fail
self.params = [p for p in self.params if p.name != "existing"]
self.params.insert(
len(self.params),
click.core.Option(
("--existing",),
type=click.Choice(["overwrite", "fail"]),
default="fail",
show_default=True,
help="What to do if an image with the same name already exists:"
" 'overwrite' will delete the old image first,"
" 'fail' will abort the build.",
),
)
# --project
self.params.insert(
len(self.params),
click.core.Option(
("--project",),
prompt=colorize_prompt(
"* GCP Project ID (see"
" https://console.cloud.google.com/cloud-resource-manager)"
),
callback=ImageGCPCommand.project_callback,
default=shell_command(
"gcloud config list --format 'value(core.project)'",
verbose=False,
exit_on_error=False,
capture_output=True,
)
.stdout.decode("utf-8")
.strip(),
help="GCP Project ID. Defaults to the active gcloud project.",
),
)
# --zone
self.params.insert(
len(self.params),
click.core.Option(
("--zone",),
prompt=colorize_prompt(
"* GCP zone (e.g. us-central1-a, see"
" https://cloud.google.com/compute/docs/gpus/gpu-regions-zones)"
),
callback=lambda ctx, param, value: value.lower(),
default="us-central1-a",
show_default=True,
help="GCP zone for the Packer build instance.",
),
)
# --instance-type
self.params.insert(
len(self.params),
click.core.Option(
(
"--instance-type",
"--isaac-workstation-instance-type",
"isaac_workstation_instance_type",
),
prompt=colorize_prompt(
"* Instance Type for Packer build"
" (see https://cloud.google.com/compute/docs/general-purpose-machines)"
),
show_default=True,
default=config["gcp_default_isaac_workstation_instance_type"],
callback=ImageGCPCommand.instance_type_callback,
help="Instance type for the Packer build VM. Supported: "
+ ", ".join(self.GCP_OVKIT_INSTANCE_TYPES)
+ ".",
),
)
# --gpu-type
self.params.insert(
len(self.params),
click.core.Option(
("--gpu-type",),
show_default=True,
default="nvidia-l4",
help="GPU accelerator type to attach to the Packer build VM"
" (e.g. nvidia-l4 for g2, nvidia-tesla-t4 for n1,"
" nvidia-rtx-pro-6000 for g4).",
),
)
# --gpu-count
self.params.insert(
len(self.params),
click.core.Option(
("--gpu-count",),
show_default=True,
default=1,
type=int,
help="Number of GPUs to attach to the Packer build VM.",
),
)
def delete_gcp_image(project, image_name, verbose=False):
"""
Delete a GCP image by name. No-op if the image does not exist.
"""
res = shell_command(
f"gcloud compute images describe {image_name} --project={project}",
verbose=verbose,
exit_on_error=False,
capture_output=True,
)
if res.returncode != 0:
return
click.echo(
colorize_info(f"* Deleting existing image '{image_name}' from '{project}'...")
)
shell_command(
f"gcloud compute images delete {image_name} --project={project} --quiet",
verbose=verbose,
exit_on_error=True,
capture_output=not verbose,
)
def build_gcp_image(params, config):
"""
Build GCP image using Packer.
"""
debug = params["debug"]
project = params["project"]
zone = params["zone"]
instance_type = params["isaac_workstation_instance_type"]
gpu_type = params["gpu_type"]
gpu_count = params["gpu_count"]
isaacsim = params["isaacsim"]
isaaclab = params["isaaclab"]
isaaclab_arena = params["isaaclab_arena"]
system_user_password = params["system_user_password"]
image_name = params.get("image_name", "")
existing = params["existing"]
version = os.environ.get("VERSION", "")
if not version:
click.echo(colorize_error("Error: VERSION environment variable is not set."))
sys.exit(1)
# log into GCP (writes application default credentials to /root/.config/gcloud)
gcp_login(verbose=debug)
# packer reads the project from var.gcp_project (also expose as env var)
os.environ["GCP_PROJECT"] = project
# handle pre-existing image
full_image_name = (
_normalize_gcp_image_name(f"isaac-automator-isaacworkstation-{image_name}")
if image_name
else ""
)
if existing == "overwrite" and full_image_name:
delete_gcp_image(project, full_image_name, verbose=debug)
packer_dir = f"{config['app_dir']}/src/packer/gcp"
click.echo(colorize_info("* Initializing Packer..."))
shell_command(
f"packer init {packer_dir}/isaac-workstation.pkr.hcl",
verbose=debug,
)
# build packer command (shlex.quote protects against shell metacharacters
# in user-supplied values such as passwords, git refs, and image names)
def _v(name, value):
return f"-var={name}={shlex.quote(str(value))}"
packer_vars = [
_v("gcp_project", project),
_v("gcp_zone", zone),
_v("instance_type", instance_type),
_v("gpu_type", gpu_type),
f"-var=gpu_count={int(gpu_count)}",
_v("version", version),
_v("system_user_password", system_user_password),
_v("isaacsim", isaacsim),
_v("isaaclab", isaaclab),
_v("isaaclab_arena", isaaclab_arena),
"-var=in_china=false",
]
if image_name:
packer_vars.append(_v("image_name", full_image_name))
packer_cmd = (
f"packer build " + " ".join(packer_vars) + f" {packer_dir}/isaac-workstation.pkr.hcl"
)
click.echo(colorize_info("* Building GCP image with Packer..."))
if debug:
click.echo(colorize_info(f"* Command: {packer_cmd}"))
shell_command(packer_cmd, verbose=debug)
click.echo(colorize_info("* Image build complete!"))
@click.command(cls=ImageGCPCommand)
def main(**params):
build_gcp_image(params, config)
if __name__ == "__main__":
if os.path.exists("/.dockerenv"):
main()
else:
shell_command(f"./run '{' '.join(sys.argv)}'", verbose=True)