Skip to content

Commit 5f81002

Browse files
[Onprem] Script for Automatic setup/launching for Onprem servers (skypilot-org#1115)
* Add example script * Fix * Addressed comments * Positional arg * Fix * ok
1 parent e536f7e commit 5f81002

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

examples/local/launch_cloud_onprem.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""Automatically launches and sets up a Sky onprem cluster on AWS.
2+
3+
The script allocates a cloud cluster and setups up the cloud cluster to run an
4+
'onprem' cluster on the cloud. It performs the following steps:
5+
1) Launches a cluster on the cloud with `sky launch -c
6+
on-prem-cluster-[ID]`. The Sky yaml sets up another user named `test`.
7+
2) Setups Sky dependencies for the admin user via `sky admin deploy ..`.
8+
3) Creates the local cluster config for the regular user, stored in
9+
`~/.sky/local/...`.
10+
To clean up the local cluster and the launched cloud cluster, run `sky down
11+
[LOCAL_CLUSTER] onprem-cluster-[ID]` and remove the corresponding local
12+
cluster config file.
13+
14+
Usage:
15+
# Creates a Sky on-premise cluster named `my-local-cluster`
16+
python examples/local/launch_cloud_onprem.py my-local-cluster
17+
"""
18+
19+
import argparse
20+
import os
21+
import subprocess
22+
import tempfile
23+
import textwrap
24+
import uuid
25+
import yaml
26+
27+
from click import testing as cli_testing
28+
29+
from sky import cli
30+
from sky import global_user_state
31+
from sky.backends import onprem_utils
32+
from sky.utils import common_utils
33+
34+
parser = argparse.ArgumentParser()
35+
parser.add_argument('name',
36+
type=str,
37+
default='my-local-cluster',
38+
nargs='?',
39+
help='Name of the local cluster.')
40+
args = parser.parse_args()
41+
42+
# Public and private keys (autogenerated by Sky)
43+
public_key = '~/.ssh/sky-key.pub'
44+
private_key = '~/.ssh/sky-key'
45+
46+
local_cluster_name = args.name
47+
48+
# Sky Task YAML to setup the user 'test'
49+
sky_yaml_config = {
50+
'resources': {
51+
'cloud': 'aws'
52+
},
53+
'file_mounts': {
54+
'/user-key': public_key
55+
},
56+
'setup': textwrap.dedent("""\
57+
sudo adduser --disabled-password --gecos '' test
58+
sudo -H su test -c 'mkdir ~/.ssh'
59+
sudo -H su test -c 'chmod 700 ~/.ssh'
60+
sudo -H su test -c 'touch ~/.ssh/authorized_keys'
61+
sudo -H su test -c 'chmod 600 ~/.ssh/authorized_keys'
62+
sudo -H su test -c 'cat /user-key >> ~/.ssh/authorized_keys'
63+
""")
64+
}
65+
66+
cli_runner = cli_testing.CliRunner()
67+
onprem_name = f'onprem-cluster-{uuid.uuid4().hex[:6]}'
68+
with tempfile.NamedTemporaryFile(suffix='.yaml', mode='w') as f:
69+
yaml.dump(sky_yaml_config, f)
70+
file_path = f.name
71+
cli_runner.invoke(cli.launch, ['-c', onprem_name, file_path])
72+
73+
handle = global_user_state.get_handle_from_cluster_name(onprem_name)
74+
head_ip = handle.head_ip
75+
76+
# Cluster config YAML to setup Sky Onprem on the admin account 'ubuntu'
77+
cluster_yaml_config = {
78+
'cluster': {
79+
'ips': [head_ip],
80+
'name': local_cluster_name
81+
},
82+
'auth': {
83+
'ssh_user': 'ubuntu',
84+
'ssh_private_key': private_key
85+
}
86+
}
87+
88+
with tempfile.NamedTemporaryFile(suffix='.yaml', mode='w') as f:
89+
yaml.dump(cluster_yaml_config, f)
90+
file_path = f.name
91+
subprocess.run(f'sky admin deploy {file_path}', shell=True, check=True)
92+
93+
# Fill out the local config file in ~/.sky/local/...
94+
local_config = onprem_utils.get_local_cluster_config_or_error(
95+
local_cluster_name)
96+
local_config['auth']['ssh_user'] = 'test'
97+
local_config['auth']['ssh_private_key'] = private_key
98+
local_config_path = onprem_utils.SKY_USER_LOCAL_CONFIG_PATH.format(
99+
local_cluster_name)
100+
common_utils.dump_yaml(os.path.expanduser(local_config_path), local_config)
101+
102+
print((
103+
f'Sky onprem cluster {local_cluster_name} is now ready for use! '
104+
f'You can launch jobs with `sky launch -c {local_cluster_name} -- [CMD].` '
105+
f'After you are done, shut down the cluster by running `sky down {local_cluster_name} {onprem_name}` '
106+
f'and removing the local cluster config in {local_config_path}.'))

0 commit comments

Comments
 (0)