Skip to content

Commit c645dd4

Browse files
committed
fixed unlock passwd
1 parent 9a0ff50 commit c645dd4

File tree

2 files changed

+98
-95
lines changed

2 files changed

+98
-95
lines changed

simple_vm_client/openstack_connector/openstack_connector.py

Lines changed: 98 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def load_env_config(self) -> None:
138138
sys.exit(1)
139139

140140
self.USE_APPLICATION_CREDENTIALS = (
141-
os.environ.get("USE_APPLICATION_CREDENTIALS", "False").lower() == "true"
141+
os.environ.get("USE_APPLICATION_CREDENTIALS", "False").lower() == "true"
142142
)
143143

144144
if self.USE_APPLICATION_CREDENTIALS:
@@ -180,15 +180,15 @@ def load_env_config(self) -> None:
180180
self.PROJECT_DOMAIN_ID = os.environ["OS_PROJECT_DOMAIN_ID"]
181181

182182
def create_server(
183-
self,
184-
name: str,
185-
image_id: str,
186-
flavor_id: str,
187-
network_id: str,
188-
userdata: str,
189-
key_name: str,
190-
metadata: dict[str, str],
191-
security_groups: list[str],
183+
self,
184+
name: str,
185+
image_id: str,
186+
flavor_id: str,
187+
network_id: str,
188+
userdata: str,
189+
key_name: str,
190+
metadata: dict[str, str],
191+
security_groups: list[str],
192192
) -> Server:
193193
logger.info(
194194
f"Create Server:\n\tname: {name}\n\timage_id:{image_id}\n\tflavor_id:{flavor_id}\n\tmetadata:{metadata}"
@@ -229,7 +229,7 @@ def delete_volume(self, volume_id: str) -> None:
229229
raise DefaultException(message=e.message)
230230

231231
def create_volume_snapshot(
232-
self, volume_id: str, name: str, description: str
232+
self, volume_id: str, name: str, description: str
233233
) -> str:
234234
try:
235235
logger.info(f"Create Snapshot for Volume {volume_id}")
@@ -269,7 +269,7 @@ def delete_volume_snapshot(self, snapshot_id: str) -> None:
269269
raise DefaultException(message=e.message)
270270

271271
def create_volume_by_source_volume(
272-
self, volume_name: str, metadata: dict[str, str], source_volume_id: str
272+
self, volume_name: str, metadata: dict[str, str], source_volume_id: str
273273
) -> Volume:
274274
logger.info(f"Creating volume from source volume with id {source_volume_id}")
275275
try:
@@ -285,7 +285,7 @@ def create_volume_by_source_volume(
285285
raise ResourceNotAvailableException(message=e.message)
286286

287287
def create_volume_by_volume_snap(
288-
self, volume_name: str, metadata: dict[str, str], volume_snap_id: str
288+
self, volume_name: str, metadata: dict[str, str], volume_snap_id: str
289289
) -> Volume:
290290
logger.info(f"Creating volume from volume snapshot with id {volume_snap_id}")
291291
try:
@@ -319,7 +319,7 @@ def get_servers_by_ids(self, ids: list[str]) -> list[Server]:
319319
return servers
320320

321321
def attach_volume_to_server(
322-
self, openstack_id: str, volume_id: str
322+
self, openstack_id: str, volume_id: str
323323
) -> dict[str, str]:
324324
server = self.get_server(openstack_id=openstack_id)
325325
volume = self.get_volume(name_or_id=volume_id)
@@ -360,7 +360,7 @@ def resize_volume(self, volume_id: str, size: int) -> None:
360360
raise DefaultException(message=str(e))
361361

362362
def create_volume(
363-
self, volume_name: str, volume_storage: int, metadata: dict[str, str]
363+
self, volume_name: str, volume_storage: int, metadata: dict[str, str]
364364
) -> Volume:
365365
logger.info(f"Creating volume with {volume_storage} GB storage")
366366
try:
@@ -477,9 +477,9 @@ def get_active_image_by_os_version(self, os_version: str, os_distro: str) -> Ima
477477
image_os_distro = metadata.get("os_distro", None)
478478
base_image_ref = metadata.get("base_image_ref", None)
479479
if (
480-
os_version == image_os_version
481-
and image.status == "active"
482-
and base_image_ref is None
480+
os_version == image_os_version
481+
and image.status == "active"
482+
and base_image_ref is None
483483
):
484484
if os_distro and os_distro == image_os_distro:
485485
return image
@@ -491,11 +491,11 @@ def get_active_image_by_os_version(self, os_version: str, os_distro: str) -> Ima
491491
)
492492

493493
def get_image(
494-
self,
495-
name_or_id: str,
496-
replace_inactive: bool = False,
497-
ignore_not_active: bool = False,
498-
ignore_not_found: bool = False,
494+
self,
495+
name_or_id: str,
496+
replace_inactive: bool = False,
497+
ignore_not_active: bool = False,
498+
ignore_not_found: bool = False,
499499
) -> Image:
500500
logger.info(f"Get Image {name_or_id}")
501501

@@ -519,12 +519,12 @@ def get_image(
519519
return image
520520

521521
def create_snapshot(
522-
self,
523-
openstack_id: str,
524-
name: str,
525-
username: str,
526-
base_tags: list[str],
527-
description: str,
522+
self,
523+
openstack_id: str,
524+
name: str,
525+
username: str,
526+
base_tags: list[str],
527+
description: str,
528528
) -> str:
529529
logger.info(
530530
f"Create Snapshot from Instance {openstack_id} with name {name} for {username}"
@@ -636,9 +636,9 @@ def get_gateway_ip(self) -> dict[str, str]:
636636
return {"gateway_ip": self.GATEWAY_IP}
637637

638638
def create_mount_init_script(
639-
self,
640-
new_volumes: list[dict[str, str]] = None, # type: ignore
641-
attach_volumes: list[dict[str, str]] = None, # type: ignore
639+
self,
640+
new_volumes: list[dict[str, str]] = None, # type: ignore
641+
attach_volumes: list[dict[str, str]] = None, # type: ignore
642642
) -> str:
643643
logger.info(f"Create init script for volume ids:{new_volumes}")
644644
if not new_volumes and not attach_volumes:
@@ -719,7 +719,7 @@ def delete_security_group_rule(self, openstack_id):
719719
)
720720

721721
def open_port_range_for_vm_in_project(
722-
self, range_start, range_stop, openstack_id, ethertype="IPV4", protocol="TCP"
722+
self, range_start, range_stop, openstack_id, ethertype="IPV4", protocol="TCP"
723723
):
724724
server: Server = self.openstack_connection.get_server_by_id(id=openstack_id)
725725
if server is None:
@@ -768,13 +768,13 @@ def open_port_range_for_vm_in_project(
768768
raise OpenStackConflictException(message=e.message)
769769

770770
def create_security_group(
771-
self,
772-
name: str,
773-
udp_port: int = None, # type: ignore
774-
ssh: bool = True,
775-
udp: bool = False,
776-
description: str = "",
777-
research_environment_metadata: ResearchEnvironmentMetadata = None,
771+
self,
772+
name: str,
773+
udp_port: int = None, # type: ignore
774+
ssh: bool = True,
775+
udp: bool = False,
776+
description: str = "",
777+
research_environment_metadata: ResearchEnvironmentMetadata = None,
778778
) -> SecurityGroup:
779779
logger.info(f"Create new security group {name}")
780780
sec: SecurityGroup = self.openstack_connection.get_security_group(
@@ -881,7 +881,7 @@ def is_security_group_in_use(self, security_group_id):
881881
return False
882882

883883
def get_or_create_research_environment_security_group(
884-
self, resenv_metadata: ResearchEnvironmentMetadata
884+
self, resenv_metadata: ResearchEnvironmentMetadata
885885
):
886886
if not resenv_metadata.needs_forc_support:
887887
return None
@@ -1115,9 +1115,9 @@ def delete_server(self, openstack_id: str) -> None:
11151115
server=server, security_group=sec
11161116
)
11171117
if (
1118-
sg["name"] != self.DEFAULT_SECURITY_GROUP_NAME
1119-
and ("bibigrid" not in sec.name or "master" not in server.name)
1120-
and not self.is_security_group_in_use(security_group_id=sec.id)
1118+
sg["name"] != self.DEFAULT_SECURITY_GROUP_NAME
1119+
and ("bibigrid" not in sec.name or "master" not in server.name)
1120+
and not self.is_security_group_in_use(security_group_id=sec.id)
11211121
):
11221122
self.openstack_connection.delete_security_group(sg)
11231123
self.openstack_connection.compute.delete_server(server.id, force=True)
@@ -1154,47 +1154,51 @@ def get_vm_ports(self, openstack_id: str) -> dict[str, str]:
11541154
return {"port": str(ssh_port), "udp": str(udp_port)}
11551155

11561156
def create_userdata(
1157-
self,
1158-
volume_ids_path_new: list[dict[str, str]],
1159-
volume_ids_path_attach: list[dict[str, str]],
1160-
additional_keys: list[str],
1157+
self,
1158+
volume_ids_path_new: list[dict[str, str]],
1159+
volume_ids_path_attach: list[dict[str, str]],
1160+
additional_keys: list[str],
11611161
) -> str:
1162-
init_script = self.create_mount_init_script(
1163-
new_volumes=volume_ids_path_new,
1164-
attach_volumes=volume_ids_path_attach,
1162+
1163+
unlock_ubuntu_user_script = "\npasswd -u ubuntu\n"
1164+
unlock_ubuntu_user_script_encoded = encodeutils.safe_encode(
1165+
unlock_ubuntu_user_script.encode("utf-8")
11651166
)
1167+
init_script = unlock_ubuntu_user_script_encoded
11661168

11671169
if additional_keys:
1168-
if init_script:
1169-
add_key_script = self.create_add_keys_script(keys=additional_keys)
1170-
init_script = (
1170+
add_key_script = self.create_add_keys_script(keys=additional_keys)
1171+
init_script = (
11711172
add_key_script
11721173
+ encodeutils.safe_encode("\n".encode("utf-8"))
1173-
11741174
+ init_script
1175-
)
1175+
)
1176+
if volume_ids_path_new or volume_ids_path_attach:
1177+
mount_script = self.create_mount_init_script(
1178+
new_volumes=volume_ids_path_new,
1179+
attach_volumes=volume_ids_path_attach,
1180+
)
1181+
init_script = (
1182+
init_script
1183+
+ encodeutils.safe_encode("\n".encode("utf-8"))
1184+
+ mount_script
1185+
1186+
)
11761187

1177-
else:
1178-
init_script = self.create_add_keys_script(keys=additional_keys)
1179-
unlock_ubuntu_user_script = "\npasswd -u ubuntu\n"
1180-
unlock_ubuntu_user_script = encodeutils.safe_encode(
1181-
unlock_ubuntu_user_script.encode("utf-8")
1182-
)
1183-
init_script = init_script + unlock_ubuntu_user_script
11841188
return init_script
11851189

11861190
def start_server(
1187-
self,
1188-
flavor_name: str,
1189-
image_name: str,
1190-
servername: str,
1191-
metadata: dict[str, str],
1192-
public_key: str,
1193-
research_environment_metadata: Union[ResearchEnvironmentMetadata, None] = None,
1194-
volume_ids_path_new: Union[list[dict[str, str]], None] = None,
1195-
volume_ids_path_attach: Union[list[dict[str, str]], None] = None,
1196-
additional_keys: Union[list[str], None] = None,
1197-
additional_security_group_ids: Union[list[str], None] = None,
1191+
self,
1192+
flavor_name: str,
1193+
image_name: str,
1194+
servername: str,
1195+
metadata: dict[str, str],
1196+
public_key: str,
1197+
research_environment_metadata: Union[ResearchEnvironmentMetadata, None] = None,
1198+
volume_ids_path_new: Union[list[dict[str, str]], None] = None,
1199+
volume_ids_path_attach: Union[list[dict[str, str]], None] = None,
1200+
additional_keys: Union[list[str], None] = None,
1201+
additional_security_group_ids: Union[list[str], None] = None,
11981202
) -> str:
11991203
logger.info(f"Start Server {servername}")
12001204

@@ -1276,16 +1280,16 @@ def start_server(
12761280
raise DefaultException(message=str(e))
12771281

12781282
def start_server_with_playbook(
1279-
self,
1280-
flavor_name: str,
1281-
image_name: str,
1282-
servername: str,
1283-
metadata: dict[str, str],
1284-
research_environment_metadata: ResearchEnvironmentMetadata,
1285-
volume_ids_path_new: list[dict[str, str]] = None, # type: ignore
1286-
volume_ids_path_attach: list[dict[str, str]] = None, # type: ignore
1287-
additional_keys: list[str] = None, # type: ignore
1288-
additional_security_group_ids=None, # type: ignore
1283+
self,
1284+
flavor_name: str,
1285+
image_name: str,
1286+
servername: str,
1287+
metadata: dict[str, str],
1288+
research_environment_metadata: ResearchEnvironmentMetadata,
1289+
volume_ids_path_new: list[dict[str, str]] = None, # type: ignore
1290+
volume_ids_path_attach: list[dict[str, str]] = None, # type: ignore
1291+
additional_keys: list[str] = None, # type: ignore
1292+
additional_security_group_ids=None, # type: ignore
12891293
) -> tuple[str, str]:
12901294
logger.info(f"Start Server {servername}")
12911295

@@ -1421,16 +1425,16 @@ def add_udp_security_group(self, server_id):
14211425
return
14221426

14231427
def add_cluster_machine(
1424-
self,
1425-
cluster_id: str,
1426-
cluster_user: str,
1427-
cluster_group_id: list[str],
1428-
image_name: str,
1429-
flavor_name: str,
1430-
name: str,
1431-
key_name: str,
1432-
batch_idx: int,
1433-
worker_idx: int,
1428+
self,
1429+
cluster_id: str,
1430+
cluster_user: str,
1431+
cluster_group_id: list[str],
1432+
image_name: str,
1433+
flavor_name: str,
1434+
name: str,
1435+
key_name: str,
1436+
batch_idx: int,
1437+
worker_idx: int,
14341438
) -> str:
14351439
logger.info(f"Add machine to {cluster_id}")
14361440
image: Image = self.get_image(name_or_id=image_name, replace_inactive=True)

simple_vm_client/openstack_connector/scripts/bash/mount.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/bash
2-
sudo touch test
32
declare -a volumes_new=VOLUME_IDS_NEW
43
declare -a paths_new=VOLUME_PATHS_NEW
54
declare -a volumes_attach=VOLUME_IDS_ATTACH

0 commit comments

Comments
 (0)