Skip to content

Commit

Permalink
implemented switching to available similar named image when no exact …
Browse files Browse the repository at this point in the history
…image match is found.
  • Loading branch information
XaverStiensmeier committed Aug 30, 2023
1 parent 6b26d2a commit 64800d2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
2 changes: 2 additions & 0 deletions bibigrid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#waitForServices: # existing service name that runs after an instance is launched. BiBiGrid's playbook will wait until service is "stopped" to avoid issues
# - de.NBI_Bielefeld_environment.service # uncomment for cloud site Bielefeld

# nearestImage: True # if true, the nearest fitting image will be picked if your image doesn't match exactly

# master configuration
masterInstance:
type: # existing type/flavor on your cloud. See launch instance>flavor for options
Expand Down
40 changes: 28 additions & 12 deletions bibigrid/core/actions/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
The cluster creation (master's creation, key creation, ansible setup and execution, ...) is done here
"""

import difflib
import logging
import os
import subprocess
Expand All @@ -20,7 +21,8 @@
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
from bibigrid.models.exceptions import ExecutionException, ConfigurationException, ImageDeactivatedException, \
ImageNotFoundException

PREFIX = "bibigrid"
SEPARATOR = "-"
Expand Down Expand Up @@ -99,7 +101,8 @@ def generate_keypair(self):
generate_keypair makes use of the fact that files in tmp are automatically deleted
ToDo find a more pythonic way to create an ECDSA keypair
See here for why using python module ECDSA wasn't successful
https://stackoverflow.com/questions/71194770/why-does-creating-ecdsa-keypairs-via-python-differ-from-ssh-keygen-t-ecdsa-and
https://stackoverflow.com/questions/71194770/why-does-creating-ecdsa-keypairs-via-python-differ-from-ssh
-keygen-t-ecdsa-and
:return:
"""
# create KEY_FOLDER if it doesn't exist
Expand Down Expand Up @@ -175,8 +178,23 @@ def start_vpn_or_master_instance(self, configuration, provider):
network = configuration["network"]

# 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,
volumes=volumes, security_groups=configuration["security_groups"], wait=True)
try:
server = provider.create_server(name=name, flavor=flavor, key_name=self.key_name, image=image,
network=network, volumes=volumes,
security_groups=configuration["security_groups"], wait=True)
except ImageDeactivatedException as exc:
LOG.warning(f"Image {image} no longer active!")
new_images = difflib.get_close_matches(image, provider.get_active_images())
if configuration.get("nearestImage", True):
if new_images:
LOG.warning(f"Falling back to {new_images[0]} from the nearest active images: {new_images}.")
server = provider.create_server(name=name, flavor=flavor, key_name=self.key_name,
image=new_images[0], network=network, volumes=volumes,
security_groups=configuration["security_groups"], wait=True)
else:
LOG.warning("No active image found that is close enough in name to fall back on.")
raise ImageNotFoundException("No active image could be found being similar enough "
f"to the proposed image {image}.") from exc
configuration["private_v4"] = server["private_v4"]

# get mac address for given private address
Expand All @@ -196,6 +214,7 @@ def start_vpn_or_master_instance(self, configuration, provider):
elif identifier == MASTER_IDENTIFIER:
configuration["floating_ip"] = server["private_v4"] # pylint: enable=comparison-with-callable
configuration["volumes"] = provider.get_mount_info_from_server(server)

def prepare_vpn_or_master_args(self, configuration, provider):
"""
Prepares start_instance arguments for master/vpn
Expand Down Expand Up @@ -295,15 +314,12 @@ def upload_data(self):

ansible_configurator.configure_ansible_yaml(providers=self.providers, configurations=self.configurations,
cluster_id=self.cluster_id)
commands = [ssh_handler.get_ac_command(self.providers,
AC_NAME.format(cluster_id=self.cluster_id))] + ssh_handler.ANSIBLE_START
ssh_handler.execute_ssh(floating_ip=self.master_ip, private_key=KEY_FOLDER + self.key_name,
username=self.ssh_user,
filepaths=[(aRP.PLAYBOOK_PATH, aRP.PLAYBOOK_PATH_REMOTE),
(biRP.BIN_PATH, biRP.BIN_PATH_REMOTE)],
commands=[
ssh_handler.get_ac_command(
self.providers,
AC_NAME.format(
cluster_id=self.cluster_id))] + ssh_handler.ANSIBLE_START)
username=self.ssh_user, filepaths=[(aRP.PLAYBOOK_PATH, aRP.PLAYBOOK_PATH_REMOTE),
(biRP.BIN_PATH, biRP.BIN_PATH_REMOTE)],
commands=commands)

def start_start_instance_threads(self):
"""
Expand Down
8 changes: 8 additions & 0 deletions bibigrid/models/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ class ConfigurationException(Exception):

class ConflictException(Exception):
""" Conflict exception"""


class ImageDeactivatedException(Exception):
""" Image deactivated exception"""


class ImageNotFoundException(Exception):
""" Image not found exception"""
4 changes: 3 additions & 1 deletion bibigrid/openstack/openstack_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from bibigrid.core import provider
from bibigrid.core.actions import create
from bibigrid.core.actions import version
from bibigrid.models.exceptions import ExecutionException, ConflictException
from bibigrid.models.exceptions import ExecutionException, ConflictException, ImageDeactivatedException

LOG = logging.getLogger("bibigrid")

Expand Down Expand Up @@ -117,6 +117,8 @@ def create_server(self, name, flavor, image, network, key_name=None, wait=True,
server = self.conn.create_server(name=name, flavor=flavor, image=image, network=network, key_name=key_name,
volumes=volumes, security_groups=security_groups)
except openstack.exceptions.BadRequestException as exc:
if "is not active" in str(exc):
raise ImageDeactivatedException() from exc
raise ConnectionError() from exc
except openstack.exceptions.SDKException as exc:
raise ExecutionException() from exc
Expand Down

0 comments on commit 64800d2

Please sign in to comment.