Skip to content

Commit

Permalink
salt: Add function to add/remove archive from bootstrap config
Browse files Browse the repository at this point in the history
Refs: #3079
  • Loading branch information
alexandre-allard committed Feb 4, 2021
1 parent c7016ee commit f78d76c
Showing 1 changed file with 77 additions and 20 deletions.
97 changes: 77 additions & 20 deletions salt/_modules/metalk8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import tempfile
import textwrap
import time
import yaml

from salt.pillar import get_pillar
from salt.exceptions import CommandExecutionError
Expand All @@ -25,6 +26,8 @@

__virtualname__ = 'metalk8s'

BOOTSTRAP_CONFIG = "/etc/metalk8s/bootstrap.yaml"


def __virtual__():
return __virtualname__
Expand Down Expand Up @@ -149,6 +152,28 @@ def _get_archive_info(info):
}


def archive_info_from_product_txt(archive):
if os.path.isdir(archive):
info = archive_info_from_tree(archive)
info.update({
'iso': None,
'path': archive,
})
elif os.path.isfile(archive):
info = archive_info_from_iso(archive)
info.update({
'iso': archive,
'path': '/srv/scality/metalk8s-{0}'.format(info['version']),
})
else:
raise CommandExecutionError(
'Invalid archive path {0}, should be an iso or a directory.'
.format(archive)
)

return info


def archive_info_from_tree(path):
"""Extract archive information from a directory
Expand Down Expand Up @@ -214,26 +239,8 @@ def get_archives(archives=None):

res = {}
for archive in archives:
if os.path.isdir(archive):
iso = None
info = archive_info_from_tree(archive)
path = archive
version = info['version']
elif os.path.isfile(archive):
iso = archive
info = archive_info_from_iso(archive)
version = info['version']
path = '/srv/scality/metalk8s-{0}'.format(version)
else:
log.warning(
'Skip, invalid archive path %s, should be an iso or a '
'directory.',
archive
)
continue

info.update({'path': path, 'iso': iso})
env_name = 'metalk8s-{0}'.format(version)
info = archive_info_from_product_txt(archive)
env_name = 'metalk8s-{0}'.format(info['version'])

# Warn if we have 2 archives with the same version
if env_name in res:
Expand Down Expand Up @@ -615,3 +622,53 @@ def get_from_map(value, saltenv=None):
input_data=tmplstr,
saltenv=saltenv
)


def _read_bootstrap_config():
try:
with open(BOOTSTRAP_CONFIG, 'r') as fd:
config = yaml.safe_load(fd)
except IOError as exc:
msg = 'Failed to load bootstrap config file at "{}"'.format(
BOOTSTRAP_CONFIG
)
raise CommandExecutionError(message=msg) from exc

return config


def _write_bootstrap_config(config):
try:
with open(BOOTSTRAP_CONFIG, 'w') as fd:
yaml.safe_dump(config, fd, default_flow_style=False)
except Exception as exc:
msg = 'Failed to write bootstrap config file at "{}"'.format(
BOOTSTRAP_CONFIG
)
raise CommandExecutionError(message=msg) from exc


def configure_archive(archive, remove=False):
"""Add (or remove) a MetalK8s archive in the bootstrap config file."""
# raise if the archive does not exist or is invalid
archive_info_from_product_txt(archive)
config = _read_bootstrap_config()

if remove:
try:
config['archives'].remove(archive)
msg = "Archive '{}' removed from bootstrap configuration"
except ValueError:
msg = "Archive '{}' already absent in bootstrap configuration"
else:
if archive in config['archives']:
msg = "Archive '{}' already present in bootstrap configuration"
else:
config['archives'].append(archive)
msg = "Archive '{}' added to bootstrap configuration"

_write_bootstrap_config(config)

msg = msg.format(archive)
log.info(msg)
return msg

0 comments on commit f78d76c

Please sign in to comment.