Skip to content

Commit

Permalink
changed image selection. Now image can be a regex, too.
Browse files Browse the repository at this point in the history
  • Loading branch information
XaverStiensmeier committed Sep 14, 2023
1 parent f2e49a8 commit d53e30f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
25 changes: 4 additions & 21 deletions bibigrid/core/actions/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@
The cluster creation (master's creation, key creation, ansible setup and execution, ...) is done here
"""

import difflib
import logging
import os
import subprocess
import threading
import traceback
import re
from functools import partial

import paramiko
import yaml

from bibigrid.core.actions import terminate_cluster
from bibigrid.core.utility import ansible_configurator
from bibigrid.core.utility import ansible_configurator, image_selection
from bibigrid.core.utility import id_generation
from bibigrid.core.utility.handler import ssh_handler
from bibigrid.core.utility.paths import ansible_resources_path as aRP
from bibigrid.core.utility.paths import bin_path as biRP
from bibigrid.models import exceptions
from bibigrid.models import return_threading
from bibigrid.models.exceptions import ExecutionException, ConfigurationException, ImageDeactivatedException
from bibigrid.models.exceptions import ExecutionException, ConfigurationException

PREFIX = "bibigrid"
SEPARATOR = "-"
Expand Down Expand Up @@ -156,7 +154,7 @@ def generate_security_groups(self):
_ = provider.create_security_group(name=self.wireguard_security_group_name)["id"]
configuration["security_groups"].append(self.wireguard_security_group_name) # store in configuration

def start_vpn_or_master_instance(self, configuration, provider): # pylint: disable=too-many-branches
def start_vpn_or_master_instance(self, configuration, provider): # pylint: disable=too-many-branches
"""
Start master/vpn-worker of a provider
:param configuration: dict configuration of said provider
Expand All @@ -176,22 +174,7 @@ def start_vpn_or_master_instance(self, configuration, provider): # pylint: disab
flavor = instance_type["type"]
network = configuration["network"]
image = instance_type["image"]
# check if image is active
active_images = provider.get_active_images()
if image not in active_images:
old_image = image
image = None
LOG.warning(f"Image '{image}' not active or doesn't exist.")
if isinstance(configuration.get("fallbackOnOtherImage"), str):
image = next(elem for elem in active_images if re.match(configuration["fallbackOnOtherImage"], elem))
LOG.info(f"Taking first regex ('{configuration['fallbackOnOtherImage']}') match '{image}'")
elif configuration.get("fallbackOnOtherImage", False):
image = difflib.get_close_matches(old_image, active_images)[0]
LOG.info(f"Taking closest (in name) active image '{image}'.")
if configuration.get("fallbackOnOtherImage", False) and image:
LOG.warning(f"Taking '{image}' instead of '{old_image}' as that image doesn't exist or is not active.")
else:
raise ImageDeactivatedException(f"Image {old_image} no longer active or doesn't exist.")
image = image_selection.select_image(configuration, provider, image, LOG)

# create a server and block until it is up and running
server = provider.create_server(name=name, flavor=flavor, key_name=self.key_name, image=image, network=network,
Expand Down
9 changes: 6 additions & 3 deletions bibigrid/core/utility/ansible_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def write_host_and_group_vars(configurations, providers, cluster_id): # pylint:
worker_dict = {"name": name, "regexp": regexp, "image": worker["image"],
"network": configuration["network"], "flavor": flavor_dict,
"gateway_ip": configuration["private_v4"],
"cloud_identifier": configuration["cloud_identifier"]}
"cloud_identifier": configuration["cloud_identifier"],
"fallback_on_other_image": configuration.get("fallbackOnOtherImage", False)}

worker_features = worker.get("features", [])
if isinstance(worker_features, str):
Expand All @@ -120,7 +121,8 @@ def write_host_and_group_vars(configurations, providers, cluster_id): # pylint:
"flavor": flavor_dict,
"wireguard_ip": wireguard_ip,
"cloud_identifier": configuration[
"cloud_identifier"]}
"cloud_identifier"],
"fallback_on_other_image": configuration.get("fallbackOnOtherImage", False)}
if configuration.get("wireguard_peer"):
vpngtw_dict["wireguard"] = {"ip": wireguard_ip,
"peer": configuration.get(
Expand All @@ -136,7 +138,8 @@ def write_host_and_group_vars(configurations, providers, cluster_id): # pylint:
"floating_ip": configuration["floating_ip"], "flavor": flavor_dict,
"private_v4": configuration["private_v4"],
"cloud_identifier": configuration["cloud_identifier"],
"volumes": configuration["volumes"]}
"volumes": configuration["volumes"],
"fallback_on_other_image": configuration.get("fallbackOnOtherImage", False)}
if configuration.get("wireguard_peer"):
master_dict["wireguard"] = {"ip": "10.0.0.1", "peer": configuration.get("wireguard_peer")}
write_yaml(os.path.join(aRP.GROUP_VARS_FOLDER, "master.yml"), master_dict)
Expand Down
30 changes: 30 additions & 0 deletions bibigrid/core/utility/image_selection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Methods for image selection
"""
import difflib
import re

from bibigrid.models.exceptions import ImageDeactivatedException


def select_image(configuration, provider, image, log):
# check if image is active
active_images = provider.get_active_images()
if image not in active_images:
old_image = image
log.info(f"Image '{old_image}' has no direct match. Maybe it's a regex? Trying regex match.")
image = next((elem for elem in active_images if re.match(image, elem)), None)
if not image:
log.warning(f"Couldn't find image '{old_image}'.")
if isinstance(configuration.get("fallbackOnOtherImage"), str):
image = next(elem for elem in active_images if re.match(configuration["fallbackOnOtherImage"], elem))
log.info(f"Taking first regex ('{configuration['fallbackOnOtherImage']}') match '{image}'.")
elif configuration.get("fallbackOnOtherImage", False):
image = difflib.get_close_matches(old_image, active_images)[0]
log.info(f"Taking closest active image (in name) '{image}' instead.")
else:
raise ImageDeactivatedException(f"Image {old_image} no longer active or doesn't exist.")
log.info(f"Using alternative '{image}' instead of '{old_image}'. You should change the configuration.")
else:
log.info(f"Taking first regex match: '{image}'")
return image

0 comments on commit d53e30f

Please sign in to comment.