Skip to content

Commit 678821e

Browse files
[Provisioner] Generate valid cluster names when username has invalid characters (skypilot-org#1526)
* clean username for cluster name generation * lint * fix interactive nodes, allow hyphens * lint * comments Co-authored-by: Zongheng Yang <[email protected]> * lint Co-authored-by: Zongheng Yang <[email protected]>
1 parent b5e0dab commit 678821e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

sky/backends/backend_utils.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,29 @@ def check_local_gpus() -> bool:
11271127
def generate_cluster_name():
11281128
# TODO: change this ID formatting to something more pleasant.
11291129
# User name is helpful in non-isolated accounts, e.g., GCP, Azure.
1130-
return f'sky-{uuid.uuid4().hex[:4]}-{getpass.getuser()}'
1130+
return f'sky-{uuid.uuid4().hex[:4]}-{get_cleaned_username()}'
1131+
1132+
1133+
def get_cleaned_username() -> str:
1134+
"""Cleans the current username to be used as part of a cluster name.
1135+
1136+
Clean up includes:
1137+
1. Making all characters lowercase
1138+
2. Removing any non-alphanumeric characters (excluding hyphens)
1139+
3. Removing any numbers and/or hyphens at the start of the username.
1140+
4. Removing any hyphens at the end of the username
1141+
1142+
e.g. 1SkY-PiLot2- becomes sky-pilot2.
1143+
1144+
Returns:
1145+
A cleaned username that will pass the regex in check_cluster_name_is_valid().
1146+
"""
1147+
username = getpass.getuser()
1148+
username = username.lower()
1149+
username = re.sub(r'[^a-z0-9-]', '', username)
1150+
username = re.sub(r'^[0-9-]+', '', username)
1151+
username = re.sub(r'-$', '', username)
1152+
return username
11311153

11321154

11331155
def query_head_ip_with_retries(cluster_yaml: str, max_attempts: int = 1) -> str:

sky/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import copy
3131
import datetime
3232
import functools
33-
import getpass
3433
import os
3534
import shlex
3635
import subprocess
@@ -606,7 +605,7 @@ def _default_interactive_node_name(node_type: str):
606605
# same-username user. E.g., sky-gpunode-ubuntu. Not a problem on AWS
607606
# which is the current cloud for interactive nodes.
608607
assert node_type in _INTERACTIVE_NODE_TYPES, node_type
609-
return f'sky-{node_type}-{getpass.getuser()}'
608+
return f'sky-{node_type}-{backend_utils.get_cleaned_username()}'
610609

611610

612611
def _infer_interactive_node_type(resources: sky.Resources):

0 commit comments

Comments
 (0)