@@ -138,7 +138,7 @@ def load_env_config(self) -> None:
138
138
sys .exit (1 )
139
139
140
140
self .USE_APPLICATION_CREDENTIALS = (
141
- os .environ .get ("USE_APPLICATION_CREDENTIALS" , "False" ).lower () == "true"
141
+ os .environ .get ("USE_APPLICATION_CREDENTIALS" , "False" ).lower () == "true"
142
142
)
143
143
144
144
if self .USE_APPLICATION_CREDENTIALS :
@@ -180,15 +180,15 @@ def load_env_config(self) -> None:
180
180
self .PROJECT_DOMAIN_ID = os .environ ["OS_PROJECT_DOMAIN_ID" ]
181
181
182
182
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 ],
192
192
) -> Server :
193
193
logger .info (
194
194
f"Create Server:\n \t name: { name } \n \t image_id:{ image_id } \n \t flavor_id:{ flavor_id } \n \t metadata:{ metadata } "
@@ -229,7 +229,7 @@ def delete_volume(self, volume_id: str) -> None:
229
229
raise DefaultException (message = e .message )
230
230
231
231
def create_volume_snapshot (
232
- self , volume_id : str , name : str , description : str
232
+ self , volume_id : str , name : str , description : str
233
233
) -> str :
234
234
try :
235
235
logger .info (f"Create Snapshot for Volume { volume_id } " )
@@ -269,7 +269,7 @@ def delete_volume_snapshot(self, snapshot_id: str) -> None:
269
269
raise DefaultException (message = e .message )
270
270
271
271
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
273
273
) -> Volume :
274
274
logger .info (f"Creating volume from source volume with id { source_volume_id } " )
275
275
try :
@@ -285,7 +285,7 @@ def create_volume_by_source_volume(
285
285
raise ResourceNotAvailableException (message = e .message )
286
286
287
287
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
289
289
) -> Volume :
290
290
logger .info (f"Creating volume from volume snapshot with id { volume_snap_id } " )
291
291
try :
@@ -319,7 +319,7 @@ def get_servers_by_ids(self, ids: list[str]) -> list[Server]:
319
319
return servers
320
320
321
321
def attach_volume_to_server (
322
- self , openstack_id : str , volume_id : str
322
+ self , openstack_id : str , volume_id : str
323
323
) -> dict [str , str ]:
324
324
server = self .get_server (openstack_id = openstack_id )
325
325
volume = self .get_volume (name_or_id = volume_id )
@@ -360,7 +360,7 @@ def resize_volume(self, volume_id: str, size: int) -> None:
360
360
raise DefaultException (message = str (e ))
361
361
362
362
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 ]
364
364
) -> Volume :
365
365
logger .info (f"Creating volume with { volume_storage } GB storage" )
366
366
try :
@@ -477,9 +477,9 @@ def get_active_image_by_os_version(self, os_version: str, os_distro: str) -> Ima
477
477
image_os_distro = metadata .get ("os_distro" , None )
478
478
base_image_ref = metadata .get ("base_image_ref" , None )
479
479
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
483
483
):
484
484
if os_distro and os_distro == image_os_distro :
485
485
return image
@@ -491,11 +491,11 @@ def get_active_image_by_os_version(self, os_version: str, os_distro: str) -> Ima
491
491
)
492
492
493
493
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 ,
499
499
) -> Image :
500
500
logger .info (f"Get Image { name_or_id } " )
501
501
@@ -519,12 +519,12 @@ def get_image(
519
519
return image
520
520
521
521
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 ,
528
528
) -> str :
529
529
logger .info (
530
530
f"Create Snapshot from Instance { openstack_id } with name { name } for { username } "
@@ -636,9 +636,9 @@ def get_gateway_ip(self) -> dict[str, str]:
636
636
return {"gateway_ip" : self .GATEWAY_IP }
637
637
638
638
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
642
642
) -> str :
643
643
logger .info (f"Create init script for volume ids:{ new_volumes } " )
644
644
if not new_volumes and not attach_volumes :
@@ -719,7 +719,7 @@ def delete_security_group_rule(self, openstack_id):
719
719
)
720
720
721
721
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"
723
723
):
724
724
server : Server = self .openstack_connection .get_server_by_id (id = openstack_id )
725
725
if server is None :
@@ -768,13 +768,13 @@ def open_port_range_for_vm_in_project(
768
768
raise OpenStackConflictException (message = e .message )
769
769
770
770
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 ,
778
778
) -> SecurityGroup :
779
779
logger .info (f"Create new security group { name } " )
780
780
sec : SecurityGroup = self .openstack_connection .get_security_group (
@@ -881,7 +881,7 @@ def is_security_group_in_use(self, security_group_id):
881
881
return False
882
882
883
883
def get_or_create_research_environment_security_group (
884
- self , resenv_metadata : ResearchEnvironmentMetadata
884
+ self , resenv_metadata : ResearchEnvironmentMetadata
885
885
):
886
886
if not resenv_metadata .needs_forc_support :
887
887
return None
@@ -1115,9 +1115,9 @@ def delete_server(self, openstack_id: str) -> None:
1115
1115
server = server , security_group = sec
1116
1116
)
1117
1117
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 )
1121
1121
):
1122
1122
self .openstack_connection .delete_security_group (sg )
1123
1123
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]:
1154
1154
return {"port" : str (ssh_port ), "udp" : str (udp_port )}
1155
1155
1156
1156
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 ],
1161
1161
) -> 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 = "\n passwd -u ubuntu\n "
1164
+ unlock_ubuntu_user_script_encoded = encodeutils .safe_encode (
1165
+ unlock_ubuntu_user_script .encode ("utf-8" )
1165
1166
)
1167
+ init_script = unlock_ubuntu_user_script_encoded
1166
1168
1167
1169
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 = (
1171
1172
add_key_script
1172
1173
+ encodeutils .safe_encode ("\n " .encode ("utf-8" ))
1173
-
1174
1174
+ 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
+ )
1176
1187
1177
- else :
1178
- init_script = self .create_add_keys_script (keys = additional_keys )
1179
- unlock_ubuntu_user_script = "\n passwd -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
1184
1188
return init_script
1185
1189
1186
1190
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 ,
1198
1202
) -> str :
1199
1203
logger .info (f"Start Server { servername } " )
1200
1204
@@ -1276,16 +1280,16 @@ def start_server(
1276
1280
raise DefaultException (message = str (e ))
1277
1281
1278
1282
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
1289
1293
) -> tuple [str , str ]:
1290
1294
logger .info (f"Start Server { servername } " )
1291
1295
@@ -1421,16 +1425,16 @@ def add_udp_security_group(self, server_id):
1421
1425
return
1422
1426
1423
1427
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 ,
1434
1438
) -> str :
1435
1439
logger .info (f"Add machine to { cluster_id } " )
1436
1440
image : Image = self .get_image (name_or_id = image_name , replace_inactive = True )
0 commit comments