Skip to content

Commit fa6b66f

Browse files
Updating Packages Including The Version Of The OpenStack SDK (#692)
* updated packages and fixed openstack changes * fixed tests and updated versions correctly in all files * beginning of rework to get_free_resources * please linter. Improve action selection process. Refactor UPPER_CASE partials to snake_case * moved provider to contextmanager * fixed line too long * moved j2 template to template folder * fixed bcrypt 5 not being compatible with latest passlib version by setting bcrypt to 4.3 * pin passlib version, too
1 parent c5bf3ce commit fa6b66f

33 files changed

+693
-677
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ recursive=no
8989

9090
# When enabled, pylint would attempt to guess common misconfiguration and emit
9191
# user-friendly hints instead of false-positive error messages.
92-
suggestion-mode=yes
92+
# suggestion-mode=yes # deprecated
9393

9494
# Allow loading of arbitrary C extensions. Extensions are imported into the
9595
# active Python interpreter and may run arbitrary code.

bibigrid/core/actions/create.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from bibigrid.core.utility.paths import ansible_resources_path as a_rp
2222
from bibigrid.core.utility.paths.basic_path import CLUSTER_INFO_FOLDER, KEY_FOLDER
2323
from bibigrid.core.utility.statics.create_statics import AC_NAME, KEY_NAME, DEFAULT_SECURITY_GROUP_NAME, \
24-
WIREGUARD_SECURITY_GROUP_NAME, MASTER_IDENTIFIER, WORKER_IDENTIFIER, \
25-
VPNGTW_IDENTIFIER, UPLOAD_FILEPATHS
24+
WIREGUARD_SECURITY_GROUP_NAME, master_identifier, worker_identifier, \
25+
vpngtw_identifier, UPLOAD_FILEPATHS
2626
from bibigrid.models import exceptions
2727
from bibigrid.models import return_threading
2828
from bibigrid.models.exceptions import ExecutionException, ConfigurationException
@@ -178,7 +178,7 @@ def start_vpn_or_master(self, configuration, provider): # pylint: disable=too-m
178178
"""
179179
identifier, instance = self.prepare_vpn_or_master_args(configuration)
180180
external_network = provider.get_external_network(configuration["network"])
181-
if identifier == MASTER_IDENTIFIER: # pylint: disable=comparison-with-callable
181+
if identifier == master_identifier: # pylint: disable=comparison-with-callable
182182
name = identifier(cluster_id=self.cluster_id)
183183
else:
184184
name = identifier(cluster_id=self.cluster_id, # pylint: disable=redundant-keyword-arg
@@ -222,17 +222,17 @@ def start_vpn_or_master(self, configuration, provider): # pylint: disable=too-m
222222
raise ConfigurationException(f"MAC address for ip {configuration['private_v4']} not found.")
223223

224224
# pylint: disable=comparison-with-callable
225-
if identifier == VPNGTW_IDENTIFIER or (identifier == MASTER_IDENTIFIER and self.use_master_with_public_ip):
225+
if identifier == vpngtw_identifier or (identifier == master_identifier and self.use_master_with_public_ip):
226226
configuration["floating_ip"] = \
227227
provider.attach_available_floating_ip(network=external_network, server=server)["floating_ip_address"]
228-
if identifier == MASTER_IDENTIFIER:
228+
if identifier == master_identifier:
229229
write_cluster_state({"cluster_id": self.cluster_id, "ssh_user": self.ssh_user,
230230
"floating_ip": configuration["floating_ip"],
231231
"state": "starting",
232232
"message": "Create process has been started. Master has been created."
233233
})
234234
self.log.debug(f"Added floating ip {configuration['floating_ip']} to {name}.")
235-
elif identifier == MASTER_IDENTIFIER:
235+
elif identifier == master_identifier:
236236
configuration["floating_ip"] = server["private_v4"] # pylint: enable=comparison-with-callable
237237

238238
def start_worker(self, worker, worker_count, configuration, provider): # pylint: disable=too-many-locals
@@ -245,7 +245,7 @@ def start_worker(self, worker, worker_count, configuration, provider): # pylint
245245
@param provider:
246246
@return:
247247
"""
248-
name = WORKER_IDENTIFIER(cluster_id=self.cluster_id, additional=worker_count)
248+
name = worker_identifier(cluster_id=self.cluster_id, additional=worker_count)
249249
self.log.info(f"Starting server {name} on {provider.cloud_specification['identifier']}.")
250250
flavor = worker["type"]
251251
network = configuration["network"]
@@ -358,10 +358,10 @@ def prepare_vpn_or_master_args(self, configuration):
358358
"""
359359
if configuration.get("masterInstance"):
360360
instance_type = configuration["masterInstance"]
361-
identifier = MASTER_IDENTIFIER
361+
identifier = master_identifier
362362
elif configuration.get("vpnInstance"):
363363
instance_type = configuration["vpnInstance"]
364-
identifier = VPNGTW_IDENTIFIER
364+
identifier = vpngtw_identifier
365365
else:
366366
self.log.warning(
367367
f"Configuration {configuration['cloud_identifier']} "

bibigrid/core/actions/list_clusters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pprint
77
import re
88

9-
from bibigrid.core.utility.statics.create_statics import MASTER_IDENTIFIER
9+
from bibigrid.core.utility.statics.create_statics import master_identifier
1010

1111
SERVER_REGEX = re.compile(r"^bibigrid-((master)-([a-zA-Z0-9]+)|(worker|vpngtw)-([a-zA-Z0-9]+)-\d+)$")
1212

@@ -148,7 +148,7 @@ def get_master_access_ip(cluster_id, master_provider, log):
148148
"""
149149
# TODO: maybe move the method from list_clusters as it is now independent of list_clusters
150150
log.info("Finding master ip for cluster %s...", cluster_id)
151-
master = MASTER_IDENTIFIER(cluster_id=cluster_id)
151+
master = master_identifier(cluster_id=cluster_id)
152152
server = master_provider.get_server(master)
153153
if server:
154154
return server.get("public_v4") or server.get("public_v6") or server.get("private_v4")

bibigrid/core/actions/terminate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def delete_non_permanent_volumes(provider, cluster_id, log):
222222
if volume_regex.match(volume["name"]):
223223
log.info("Trying to delete volume %s on cloud %s.", volume['name'], provider.cloud_specification[
224224
'identifier'])
225-
cluster_volume_state.append(provider.delete_volume(volume))
225+
cluster_volume_state.append(provider.delete_volume(volume["id"]))
226226
return cluster_volume_state
227227

228228

bibigrid/core/actions/update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def update(creator, log):
1818
log.warning(f"There are still workers up! {workers}")
1919
return 1
2020
if master_ip and ssh_user and used_private_key:
21-
master = creator.MASTER_IDENTIFIER(cluster_id=creator.cluster_id)
21+
master = creator.master_identifier(cluster_id=creator.cluster_id)
2222
server = creator.providers[0].get_server(master)
2323
creator.master_ip = master_ip
2424
creator.configurations[0]["private_v4"] = server["private_v4"]

bibigrid/core/startup.py

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import time
99
import traceback
10+
from contextlib import contextmanager
1011

1112
import click
1213
import yaml
@@ -27,6 +28,16 @@
2728
logging.addLevelName(42, "PRINT")
2829

2930

31+
@contextmanager
32+
def managed_providers(configurations, log):
33+
providers = provider_handler.get_providers(configurations, log)
34+
try:
35+
yield providers
36+
finally:
37+
for provider in providers or []:
38+
provider.close()
39+
40+
3041
def get_cluster_id_from_mem():
3142
"""
3243
Reads the cluster_id of the last created cluster and returns it. Used if no cluster_id is given.
@@ -62,12 +73,12 @@ def set_logger_verbosity(verbosity):
6273

6374
def check_cid(cluster_id):
6475
if "-" in cluster_id:
65-
new_cid = cluster_id.split("-")[-1]
66-
LOG.info("-cid %s is not a cid, but probably the entire master name. Using '%s' as "
67-
"cid instead.", cluster_id, new_cid)
68-
return new_cid
76+
real_cid = cluster_id.split("-")[-1]
77+
LOG.warning(f"-cid {cluster_id} is not a cid, but probably the entire master name. Using '{real_cid}' as "
78+
"cid instead.")
79+
return real_cid
6980
if "." in cluster_id:
70-
LOG.info("-cid %s is not a cid, but probably the master's ip. "
81+
LOG.info(f"-cid {cluster_id} is not a cid, but probably the master's ip. "
7182
"Using the master ip instead of cid only works if a cluster key is in your systems default ssh key "
7283
"location (~/.ssh/). Otherwise bibigrid can't identify the cluster key.")
7384
if len(cluster_id) != id_generation.MAX_ID_LENGTH or not set(cluster_id).issubset(
@@ -98,50 +109,52 @@ def run_action(action, configurations, config_input, cluster_id, debug):
98109
exit_state = 0
99110

100111
try:
101-
providers = provider_handler.get_providers(configurations, LOG)
102-
if not providers:
103-
exit_state = 1
104-
105-
if providers:
106-
match action:
107-
case 'list':
108-
LOG.info("Action list selected")
109-
exit_state = list_clusters.log_list(cluster_id, providers, LOG)
110-
case 'check':
111-
LOG.info("Action check selected")
112-
exit_state = check.check(configurations, providers, LOG)
113-
case 'create':
114-
LOG.info("Action create selected")
115-
creator = create.Create(providers=providers, configurations=configurations, log=LOG, debug=debug,
116-
config_path=config_input, cluster_id=cluster_id)
117-
LOG.log(42,
118-
"Creating a new cluster takes about 10 or more minutes depending on your cloud "
119-
"provider and your configuration. Please be patient.")
120-
exit_state = creator.create()
121-
case _:
122-
if not cluster_id:
123-
cluster_id = get_cluster_id_from_mem()
124-
LOG.info("No cid (cluster_id) specified. Defaulting to last created cluster: %s",
125-
cluster_id or 'None found')
126-
if cluster_id:
127-
LOG.debug(f"CL Argument Cluster ID: {cluster_id}")
128-
match action:
129-
case 'terminate':
130-
LOG.info("Action terminate selected")
131-
exit_state = terminate.terminate(cluster_id=cluster_id, providers=providers, log=LOG,
132-
debug=debug)
133-
case 'ide':
134-
LOG.info("Action ide selected")
135-
exit_state = ide.ide(cluster_id, providers[0], configurations[0], LOG)
136-
case 'update':
137-
LOG.info("Action update selected")
138-
creator = create.Create(providers=providers, configurations=configurations, log=LOG,
139-
debug=debug, config_path=config_input, cluster_id=cluster_id)
140-
exit_state = update.update(creator, LOG)
141-
142-
for provider in providers:
143-
provider.close()
144-
112+
with managed_providers(configurations, LOG) as providers:
113+
if not providers:
114+
LOG.warning("No providers found. Aborting.")
115+
exit_state = 1
116+
return exit_state
117+
118+
if action in {'terminate', 'ide', 'update'} and not cluster_id:
119+
cluster_id = get_cluster_id_from_mem()
120+
LOG.info(f"No cid (cluster_id) specified. Defaulting to last created cluster: "
121+
f"{cluster_id or 'None found'}")
122+
if not cluster_id:
123+
LOG.warning("No cid specified and none found in memory but required. Aborting.")
124+
exit_state = 1
125+
return exit_state
126+
127+
if not exit_state:
128+
match action:
129+
case 'list':
130+
LOG.info("Action list selected")
131+
exit_state = list_clusters.log_list(cluster_id, providers, LOG)
132+
case 'check':
133+
LOG.info("Action check selected")
134+
exit_state = check.check(configurations, providers, LOG)
135+
case 'create':
136+
LOG.info("Action create selected")
137+
creator = create.Create(providers=providers, configurations=configurations, log=LOG,
138+
debug=debug, config_path=config_input, cluster_id=cluster_id)
139+
LOG.log(42,
140+
"Creating a new cluster takes about 10 or more minutes depending on your cloud "
141+
"provider and your configuration. Please be patient.")
142+
exit_state = creator.create()
143+
case 'terminate':
144+
LOG.info("Action terminate selected")
145+
exit_state = terminate.terminate(cluster_id=cluster_id, providers=providers, log=LOG,
146+
debug=debug)
147+
case 'ide':
148+
LOG.info("Action ide selected")
149+
exit_state = ide.ide(cluster_id, providers[0], configurations[0], LOG)
150+
case 'update':
151+
LOG.info("Action update selected")
152+
creator = create.Create(providers=providers, configurations=configurations, log=LOG,
153+
debug=debug, config_path=config_input, cluster_id=cluster_id)
154+
exit_state = update.update(creator, LOG)
155+
case _:
156+
LOG.warning("Unknown Action. Aborting.")
157+
exit_state = 1
145158
except Exception as _: # pylint: disable=broad-exception-caught
146159
exc_type, exc_value, exc_traceback = sys.exc_info()
147160
LOG.error("".join(traceback.format_exception(exc_type, exc_value, exc_traceback)))

bibigrid/core/utility/ansible_commands.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@
6060
UPDATE = ("sudo apt-get update", "Update apt repository lists.")
6161
PYTHON3_PIP = ("sudo apt-get install -y python3-pip python3-venv", "Install python3 pip and venv using apt.")
6262
VENV_SETUP = ("sudo python3 -m venv /opt/bibigrid-venv", " Create bibigrid virtual environment.")
63-
ANSIBLE_PASSLIB = ("sudo /opt/bibigrid-venv/bin/pip install ansible==10.7 passlib",
64-
"Install Ansible 10.7 and Passlib using pip.")
63+
ANSIBLE_PASSLIB = (
64+
"sudo /opt/bibigrid-venv/bin/pip install "
65+
"ansible==10.7 passlib==1.7.4 bcrypt==4.3",
66+
"Install Ansible 10.7, Passlib, and a pinned bcrypt version."
67+
)
6568
ANSIBLE_GALAXY = ("sudo /opt/bibigrid-venv/bin/ansible-galaxy collection install " +
6669
"-p /usr/share/ansible/collections community.zabbix==3.2.0",
6770
"Install necessary ansible-galaxy modules.")

bibigrid/core/utility/ansible_configurator.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from bibigrid.core.utility.handler import configuration_handler
1515
from bibigrid.core.utility.paths import ansible_resources_path as aRP
1616
from bibigrid.core.utility.paths.basic_path import ROOT_PATH
17-
from bibigrid.core.utility.statics.create_statics import MASTER_IDENTIFIER, VPNGTW_IDENTIFIER, WORKER_IDENTIFIER
17+
from bibigrid.core.utility.statics.create_statics import master_identifier, vpngtw_identifier, worker_identifier
1818
from bibigrid.core.utility.wireguard import wireguard_keys
1919

2020
PYTHON_INTERPRETER = "/usr/bin/python3"
@@ -72,7 +72,7 @@ def get_full_volume_name(volume, name, count):
7272
def get_worker_host_vars(*, cluster_id, worker, worker_count):
7373
write_host_vars_remote = []
7474
for worker_number in range(worker.get('count', 1)):
75-
name = WORKER_IDENTIFIER(cluster_id=cluster_id, additional=worker_count + worker_number)
75+
name = worker_identifier(cluster_id=cluster_id, additional=worker_count + worker_number)
7676
write_volumes = []
7777
for i, volume in enumerate(worker.get("volumes", [])):
7878
volume_name = get_full_volume_name(volume, name, i)
@@ -86,10 +86,10 @@ def get_worker_vars(*, provider, configuration, cluster_id, worker,
8686
worker_count): # pylint: disable-msg=too-many-locals
8787
write_worker_vars_remote = []
8888
flavor_dict = provider.create_flavor_dict(flavor=worker["type"])
89-
name = WORKER_IDENTIFIER(cluster_id=cluster_id,
89+
name = worker_identifier(cluster_id=cluster_id,
9090
additional=f"[{worker_count}-{worker_count + worker.get('count', 1) - 1}]")
9191
group_name = name.replace("[", "").replace("]", "").replace(":", "_").replace("-", "_")
92-
regexp = WORKER_IDENTIFIER(cluster_id=cluster_id, additional=r"\d+")
92+
regexp = worker_identifier(cluster_id=cluster_id, additional=r"\d+")
9393
partitions = worker.get("partitions", []) + [configuration["cloud_identifier"]]
9494
if not configuration.get("noAllPartition"):
9595
partitions.append("all")
@@ -124,11 +124,11 @@ def get_worker_vars(*, provider, configuration, cluster_id, worker,
124124

125125

126126
def get_vpn_vars(*, provider, configuration, cluster_id, vpngtw, vpn_count):
127-
name = VPNGTW_IDENTIFIER(cluster_id=cluster_id, additional=f"{vpn_count}")
127+
name = vpngtw_identifier(cluster_id=cluster_id, additional=f"{vpn_count}")
128128
wireguard_ip = f"10.0.0.{vpn_count + 2}" # skipping 0 and 1 (master)
129129
vpn_count += 1
130130
flavor_dict = provider.create_flavor_dict(flavor=vpngtw["type"])
131-
regexp = WORKER_IDENTIFIER(cluster_id=cluster_id, additional=r"\d+")
131+
regexp = worker_identifier(cluster_id=cluster_id, additional=r"\d+")
132132
vpngtw_dict = {"name": name, "regexp": regexp, "image": vpngtw["image"],
133133
"network": configuration["network"], "network_cidrs": configuration["subnet_cidrs"],
134134
"floating_ip": configuration["floating_ip"], "private_v4": configuration["private_v4"],
@@ -144,7 +144,7 @@ def get_vpn_vars(*, provider, configuration, cluster_id, vpngtw, vpn_count):
144144

145145
def get_master_vars(provider, configuration, cluster_id):
146146
master = configuration["masterInstance"]
147-
name = MASTER_IDENTIFIER(cluster_id=cluster_id)
147+
name = master_identifier(cluster_id=cluster_id)
148148
flavor_dict = provider.create_flavor_dict(flavor=master["type"])
149149
partitions = master.get("partitions", []) + [configuration["cloud_identifier"]]
150150
if not configuration.get("noAllPartition"):
@@ -269,7 +269,7 @@ def generate_ansible_hosts_yaml(ssh_user, configurations, cluster_id, log): # p
269269
@return: ansible_hosts yaml (dict)
270270
"""
271271
log.info("Generating ansible hosts file...")
272-
master_name = MASTER_IDENTIFIER(cluster_id=cluster_id)
272+
master_name = master_identifier(cluster_id=cluster_id)
273273
ansible_hosts_yaml = {"vpn": {"hosts": {},
274274
"children": {"master": {"hosts": {master_name: to_instance_host_dict(ssh_user)}},
275275
"vpngtw": {"hosts": {}}}}, "workers": {"hosts": {}, "children": {}}}
@@ -280,7 +280,7 @@ def generate_ansible_hosts_yaml(ssh_user, configurations, cluster_id, log): # p
280280
vpngtw_count = 0
281281
for configuration in configurations:
282282
for worker in configuration.get("workerInstances", []):
283-
name = WORKER_IDENTIFIER(cluster_id=cluster_id,
283+
name = worker_identifier(cluster_id=cluster_id,
284284
additional=f"[{worker_count}:{worker_count + worker.get('count', 1) - 1}]")
285285
worker_dict = to_instance_host_dict(ssh_user, ip="")
286286
group_name = name.replace("[", "").replace("]", "").replace(":", "_").replace("-", "_")
@@ -290,7 +290,7 @@ def generate_ansible_hosts_yaml(ssh_user, configurations, cluster_id, log): # p
290290
worker_count += worker.get('count', 1)
291291

292292
if configuration.get("vpnInstance"):
293-
name = VPNGTW_IDENTIFIER(cluster_id=cluster_id, additional=vpngtw_count)
293+
name = vpngtw_identifier(cluster_id=cluster_id, additional=vpngtw_count)
294294
vpngtw_dict = to_instance_host_dict(ssh_user, ip="")
295295
vpngtw_dict["ansible_host"] = configuration["floating_ip"]
296296
vpngtws[name] = vpngtw_dict

0 commit comments

Comments
 (0)