22The cluster creation (master's creation, key creation, ansible setup and execution, ...) is done here
33"""
44
5+ import os
56import shutil
67import subprocess
78import threading
89import traceback
9- import os
1010
1111import paramiko
1212import sympy
1717from bibigrid .core .utility import id_generation
1818from bibigrid .core .utility import image_selection
1919from bibigrid .core .utility .handler import ssh_handler
20- from bibigrid .models import exceptions
21- from bibigrid .models import return_threading
22- from bibigrid .models .exceptions import ExecutionException , ConfigurationException
2320from bibigrid .core .utility .paths import ansible_resources_path as a_rp
21+ from bibigrid .core .utility .paths .basic_path import CLUSTER_INFO_FOLDER , KEY_FOLDER , CLUSTER_MEMORY_PATH
2422from bibigrid .core .utility .statics .create_statics import AC_NAME , KEY_NAME , DEFAULT_SECURITY_GROUP_NAME , \
25- WIREGUARD_SECURITY_GROUP_NAME , KEY_FOLDER , CLUSTER_MEMORY_PATH , MASTER_IDENTIFIER , WORKER_IDENTIFIER , \
23+ WIREGUARD_SECURITY_GROUP_NAME , MASTER_IDENTIFIER , WORKER_IDENTIFIER , \
2624 VPNGTW_IDENTIFIER , UPLOAD_FILEPATHS
25+ from bibigrid .models import exceptions
26+ from bibigrid .models import return_threading
27+ from bibigrid .models .exceptions import ExecutionException , ConfigurationException
2728
2829
2930class Create : # pylint: disable=too-many-instance-attributes,too-many-arguments
@@ -69,6 +70,22 @@ def __init__(self, *, providers, configurations, config_path, log, debug=False,
6970 "useMasterWithPublicIp" , True )
7071 self .log .debug ("Keyname: %s" , self .key_name )
7172
73+ os .makedirs (os .path .join (CLUSTER_INFO_FOLDER ), exist_ok = True )
74+ self .write_cluster_state ({"floating_ip" : None , "state" : 202 ,
75+ "message" : "Create process has been started." })
76+
77+ def write_cluster_state (self , state ):
78+ state = {"cluster_id" : self .cluster_id , "ssh_user" : self .ssh_user , ** state }
79+ # last cluster
80+ with open (CLUSTER_MEMORY_PATH , mode = "w+" , encoding = "UTF-8" ) as cluster_memory_file :
81+ yaml .safe_dump (data = state , stream = cluster_memory_file )
82+ # all clusters
83+ cluster_info_path = os .path .normpath (os .path .join (CLUSTER_INFO_FOLDER , f"{ self .cluster_id } .yaml" ))
84+ if not cluster_info_path .startswith (os .path .normpath (CLUSTER_INFO_FOLDER )):
85+ raise ValueError ("Invalid cluster_id resulting in path traversal" )
86+ with open (cluster_info_path , mode = "w+" , encoding = "UTF-8" ) as cluster_info_file :
87+ yaml .safe_dump (data = state , stream = cluster_info_file )
88+
7289 def create_defaults (self ):
7390 self .log .debug ("Creating default files" )
7491 if not self .configurations [0 ].get ("customAnsibleCfg" , False ) or not os .path .isfile (a_rp .ANSIBLE_CFG_PATH ):
@@ -104,10 +121,6 @@ def generate_keypair(self):
104121 for provider in self .providers :
105122 provider .create_keypair (name = self .key_name , public_key = public_key )
106123
107- # write cluster_id to automatically read it on following calls if no cid is given
108- with open (CLUSTER_MEMORY_PATH , mode = "w+" , encoding = "UTF-8" ) as cluster_memory_file :
109- yaml .safe_dump (data = {"cluster_id" : self .cluster_id , "ssh_user" : self .ssh_user }, stream = cluster_memory_file )
110-
111124 def delete_old_vars (self ):
112125 """
113126 Deletes host_vars and group_vars
@@ -208,10 +221,10 @@ def start_vpn_or_master(self, configuration, provider): # pylint: disable=too-m
208221 configuration ["floating_ip" ] = \
209222 provider .attach_available_floating_ip (network = external_network , server = server )["floating_ip_address" ]
210223 if identifier == MASTER_IDENTIFIER :
211- with open ( CLUSTER_MEMORY_PATH , mode = "w+" , encoding = "UTF-8" ) as cluster_memory_file :
212- yaml . safe_dump (
213- data = { "cluster_id " : self . cluster_id , "floating_ip" : configuration [ "floating_ip" ]},
214- stream = cluster_memory_file )
224+ self . write_cluster_state ({ "cluster_id" : self . cluster_id , "floating_ip" : configuration [ "floating_ip" ],
225+ "state" : 202 ,
226+ "message " : "Create process has been started. Master has been created."
227+ } )
215228 self .log .debug (f"Added floating ip { configuration ['floating_ip' ]} to { name } ." )
216229 elif identifier == MASTER_IDENTIFIER :
217230 configuration ["floating_ip" ] = server ["private_v4" ] # pylint: enable=comparison-with-callable
@@ -560,6 +573,9 @@ def create(self): # pylint: disable=too-many-branches,too-many-statements
560573 else :
561574 return 0 # will be called if no exception occurred
562575 terminate .terminate (cluster_id = self .cluster_id , providers = self .providers , log = self .log , debug = self .debug )
576+ self .write_cluster_state ({"floating_ip" : self .configurations [0 ]["floating_ip" ],
577+ "state" : 500 ,
578+ "message" : "Cluster creation failed. Terminated remains." })
563579 return 1
564580
565581 def log_cluster_start_info (self ):
@@ -584,3 +600,6 @@ def log_cluster_start_info(self):
584600 self .log .log (42 , f"Detailed cluster info: ./bibigrid.sh -i '{ self .config_path } ' -l -cid { self .cluster_id } " )
585601 if self .configurations [0 ].get ("ide" ):
586602 self .log .log (42 , f"IDE Port Forwarding: ./bibigrid.sh -i '{ self .config_path } ' -ide -cid { self .cluster_id } " )
603+ self .write_cluster_state ({"floating_ip" : self .configurations [0 ]["floating_ip" ],
604+ "state" : 201 ,
605+ "message" : "Cluster successfully created." })
0 commit comments