-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathstart
More file actions
executable file
·190 lines (169 loc) · 5.4 KB
/
start
File metadata and controls
executable file
·190 lines (169 loc) · 5.4 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
#!/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 sys
import time
from pathlib import Path
import click
from src.python.alicloud import (
alicloud_configure_cli,
alicloud_get_instance_status,
alicloud_start_instance,
)
from src.python.aws import (
aws_validate_credentials,
aws_get_instance_status,
aws_start_instance,
)
from src.python.azure import azure_login, azure_start_instance
from src.python.config import c as config
from src.python.utils import (
colorize_error,
colorize_info,
colorize_result,
deployments,
format_instance_role,
format_cloud_name,
read_meta,
read_tf_output,
shell_command,
)
@click.command()
@click.option(
"--debug/--no-debug",
default=False,
show_default=True,
)
@click.argument(
"deployment_name",
required=True,
type=click.Choice(deployments()),
)
@click.option(
"--quick/--no-quick",
"-q/-Q",
"quick",
default=False,
show_default=True,
help="Quick start: skip full Ansible, only run autorun script.",
)
@click.option(
"--ansible/--no-ansible",
"-a/-A",
"ansible",
default=True,
show_default=True,
hidden=True,
help="Run Ansible? (--no-ansible is equivalent to --quick.)",
)
def main(
debug: bool,
deployment_name: str,
quick: bool,
ansible: bool,
):
# --no-ansible is a synonym for --quick
if not ansible:
quick = True
# check what cloud is deployment for
cloud = read_tf_output(deployment_name, "cloud", verbose=debug)
# validate credentials before any cloud operations
if "aws" == cloud:
meta = read_meta(deployment_name, debug)
region = meta.get("input_params", {}).get("region", None)
aws_validate_credentials(deployment_name, region=region, verbose=debug)
elif "azure" == cloud:
azure_login(verbose=debug)
# go through instance roles
for instance_role in ["isaac_workstation"]:
vm_id = read_tf_output(deployment_name, f"{instance_role}_vm_id", verbose=debug)
vm_ip = read_tf_output(deployment_name, f"{instance_role}_ip", verbose=debug)
click.echo(
colorize_info(
f"* Starting {format_instance_role(instance_role)} VM @ {format_cloud_name(cloud)} ({vm_ip})..."
),
nl=False,
)
# start VM
if "azure" == cloud:
click.echo() # extra new line
# start
azure_start_instance(vm_id, verbose=debug)
elif "alicloud" == cloud:
# configure aliyun cli
alicloud_configure_cli(deployment_name, verbose=debug)
# start
alicloud_start_instance(vm_id, verbose=debug)
# wait for VM to stop
while True:
if "Running" == alicloud_get_instance_status(vm_id, verbose=debug):
click.echo()
break
click.echo(colorize_info("."), nl=False)
time.sleep(2)
elif "aws" == cloud:
# start
aws_start_instance(vm_id, verbose=debug)
# wait for VM to start
while True:
status = aws_get_instance_status(vm_id, verbose=debug)
if "running" == status:
click.echo()
break
click.echo(colorize_info("."), nl=False)
time.sleep(2)
else:
click.echo(
colorize_error(
f"* Stop/start is not yet supported for {format_cloud_name(cloud)}."
)
)
exit(1)
click.echo(
colorize_result(f"* {format_instance_role(instance_role)} VM started.")
)
# run ansible
ansible_tags = "__autorun" if quick else "on_stop_start"
if quick:
click.echo(
colorize_info(
f"* Quick start: running autorun only for {format_instance_role(instance_role)} VM..."
)
)
else:
click.echo(
colorize_info(
f"* Configuring {format_instance_role(instance_role)} VM..."
)
)
shell_command(
f"ansible-playbook -i {config['state_dir']}/{deployment_name}/.inventory"
+ " isaac-workstation.yaml"
+ f" -t {ansible_tags}"
+ f" {'-vv' if debug else ''}",
cwd=config["ansible_dir"],
verbose=debug,
)
text = Path(f"{config['state_dir']}/{deployment_name}/info.txt").read_text()
click.echo(colorize_result(text))
if __name__ == "__main__":
if os.path.exists("/.dockerenv"):
# we're in docker, run command
main()
else:
# we're outside, start docker container first
shell_command(f"./run '{' '.join(sys.argv)}'", verbose=True)