Skip to content

Commit

Permalink
Merge pull request #51 from ARGOeu/devel
Browse files Browse the repository at this point in the history
Version 0.3.0
  • Loading branch information
themiszamani authored Jun 17, 2024
2 parents 3159c53 + 89cddb1 commit 3f680e8
Show file tree
Hide file tree
Showing 12 changed files with 1,301 additions and 972 deletions.
4 changes: 3 additions & 1 deletion argo-poem-tools.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Summary: Script installs packages on ARGO mon boxes.
Name: argo-poem-tools
Version: 0.2.7
Version: 0.3.0
Release: 1%{?dist}
Source0: %{name}-%{version}.tar.gz
License: ASL 2.0
Expand Down Expand Up @@ -52,6 +52,8 @@ rm -rf $RPM_BUILD_ROOT
%attr(0755,root,root) %dir %{_localstatedir}/log/argo-poem-tools/

%changelog
* Mon Jun 17 2024 Katarina Zailac <[email protected]> - 0.3.7-1%{?dist}
- ARGO-4661 Make tool multi-tenant aware
* Thu Apr 4 2024 Katarina Zailac <[email protected]> - 0.2.7-1%{?dist}
- ARGO-4502 Generalize method for fetching distro name
* Thu Aug 3 2023 Katarina Zailac <[email protected]> - 0.2.6-1%{?dist}
Expand Down
11 changes: 7 additions & 4 deletions config/argo-poem-tools.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[GENERAL]
Host = egi.tenant.com
[tenant1]
Host = tenant1.example.com
Token = some-token-1234

[PROFILES]
MetricProfiles = TEST_PROFILE1, TEST_PROFILE2

[tenant2]
Host = tenant2.example.com
Token = some-token-5678
MetricProfiles = TEST_PROFILE3, TEST_PROFILE4
135 changes: 63 additions & 72 deletions exec/argo-poem-packages.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/python3
import argparse
import configparser
import logging
import logging.handlers
import subprocess
import sys

import requests
from argo_poem_tools.config import Config
from argo_poem_tools.packages import Packages, PackageException
from argo_poem_tools.exceptions import ConfigException, PackageException, \
POEMException, MergingException
from argo_poem_tools.packages import Packages
from argo_poem_tools.poem import POEM, merge_tenants_data
from argo_poem_tools.repos import YUMRepos

LOGFILE = "/var/log/argo-poem-tools/argo-poem-tools.log"
Expand Down Expand Up @@ -63,95 +65,84 @@ def main():
subprocess.call(['yum', 'clean', 'all'])

config = Config()
token = config.get_token()
hostname = config.get_hostname()
profiles = config.get_profiles()
tenants_configurations = config.get_configuration()

logger.info(
'Sending request for profile(s): ' + ', '.join(profiles)
)
if backup_repos:
repos = YUMRepos(
hostname=hostname, token=token, profiles=profiles,
override=False
tenant_repos = dict()
for tenant, configuration in tenants_configurations.items():
logger.info(
f"{tenant}: Sending request for profile(s): "
f"{', '.join(configuration['metricprofiles'])}"
)

else:
repos = YUMRepos(hostname=hostname, token=token, profiles=profiles)

data = repos.get_data(include_internal=include_internal)

if not data:
logger.warning(
'No data for given metric profile(s): ' +
', '.join(profiles)
poem = POEM(
hostname=configuration["host"],
token=configuration["token"],
profiles=configuration["metricprofiles"]
)
sys.exit(2)

else:
logger.info('Creating YUM repo files...')

files = repos.create_file(include_internal=include_internal)
tenant_repos.update({
tenant: poem.get_data(include_internal=include_internal)
})

logger.info('Created files: ' + '; '.join(files))
data = merge_tenants_data(tenant_repos)

pkg = Packages(data)

if noop:
info_msg, warn_msg = pkg.no_op()

else:
info_msg, warn_msg = pkg.install()

# if there were repo files backed up, now they are restored
repos.clean()
if backup_repos:
repos = YUMRepos(data=data, override=False)

if info_msg:
for msg in info_msg:
logger.info(msg)
else:
repos = YUMRepos(data=data)

if warn_msg:
for msg in warn_msg:
logger.warning(msg)
logger.info("Creating YUM repo files...")

sys.exit(1)
files = repos.create_file(include_internal=include_internal)

else:
missing_packages_msg = ''
if repos.missing_packages:
missing_packages_msg = \
'Missing packages for given distro: ' + \
', '.join(repos.missing_packages)
logger.warning(missing_packages_msg)
logger.info(f"Created files: {'; '.join(files)}")

if not noop:
if missing_packages_msg:
print('WARNING: ' + missing_packages_msg)
pkg = Packages(data)

logger.info("The run finished successfully.")
sys.exit(0)
if noop:
info_msg, warn_msg = pkg.no_op()

except requests.exceptions.ConnectionError as err:
logger.error(err)
sys.exit(2)
else:
info_msg, warn_msg = pkg.install()

except requests.exceptions.RequestException as err:
logger.error(err)
sys.exit(2)
# if there were repo files backed up, now they are restored
repos.clean()

except configparser.ParsingError as err:
logger.error(err)
sys.exit(2)
if info_msg:
for msg in info_msg:
logger.info(msg)

except configparser.NoSectionError as err:
logger.error(err)
sys.exit(2)
if warn_msg:
for msg in warn_msg:
logger.warning(msg)

except configparser.NoOptionError as err:
logger.error(err)
sys.exit(2)
sys.exit(1)

except PackageException as err:
else:
missing_packages_msg = ''
if repos.missing_packages:
missing_packages_msg = (
f"Missing packages for given distro: "
f"{', '.join(repos.missing_packages)}"
)
logger.warning(missing_packages_msg)

if not noop:
if missing_packages_msg:
print(f"WARNING: {missing_packages_msg}")

logger.info("The run finished successfully.")
sys.exit(0)

except (
requests.exceptions.ConnectionError,
requests.exceptions.RequestException,
ConfigException,
POEMException,
MergingException,
PackageException
) as err:
logger.error(err)
sys.exit(2)

Expand Down
63 changes: 47 additions & 16 deletions modules/config.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
import configparser

conf = '/etc/argo-poem-tools/argo-poem-tools.conf'
from argo_poem_tools.exceptions import ConfigException


class Config:
def __init__(self):
self.conf = conf
def __init__(self, file="/etc/argo-poem-tools/argo-poem-tools.conf"):
self.file = file
self.conf = self._read()
self.tenants = self._get_tenants()

def read(self):
def _check_file_exists(self):
conf = configparser.ConfigParser()
try:
with open(self.file) as f:
conf.read_file(f)

except IOError:
raise ConfigException(f"File {self.file} does not exist")

def _read(self):
config = configparser.ConfigParser()
config.read(self.conf)
config.read(self.file)
return config

def get_hostname(self):
config = self.read()
return config.get('GENERAL', 'host')
def _get_tenants(self):
tenants = list()
for section in self.conf.sections():
if section != "GENERAL":
tenants.append(section)

return tenants

def get_configuration(self):
configuration = dict()
for tenant in self.tenants:
for entry in ["host", "token", "metricprofiles"]:
try:
if entry == "metricprofiles":
profiles_string = self.conf.get(tenant, entry)
value = [
p.strip() for p in profiles_string.split(',')
]

else:
value = self.conf.get(tenant, entry)

if tenant in configuration:
configuration[tenant].update({entry: value})

def get_token(self):
config = self.read()
return config.get('GENERAL', 'token')
else:
configuration.update({tenant: {entry: value}})

def get_profiles(self):
config = self.read()
profiles_string = config.get('PROFILES', 'metricprofiles')
profiles = [p.strip() for p in profiles_string.split(',')]
except configparser.NoOptionError:
raise ConfigException(
f"Missing '{entry}' entry for tenant '{tenant}'"
)

return profiles
return configuration
25 changes: 25 additions & 0 deletions modules/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class MyException(Exception):
def __init__(self, msg):
self.msg = msg

def __str__(self):
return str(self.msg)


class ConfigException(MyException):
def __str__(self):
return f"Configuration file error: {str(self.msg)}"


class PackageException(MyException):
pass


class POEMException(MyException):
def __str__(self):
return f"Error fetching YUM repos: {str(self.msg)}"


class MergingException(MyException):
def __str__(self):
return f"Error merging POEM data: {str(self.msg)}"
13 changes: 3 additions & 10 deletions modules/packages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import subprocess
from re import compile

from argo_poem_tools.exceptions import PackageException

_rpm_re = compile('(\S+)-(?:(\d*):)?(.*)-(~?\w+[\w.]*)')

Expand Down Expand Up @@ -69,10 +70,6 @@ def _compare_vr(vr1, vr2):
return _compare_versions(v1, v2)


class PackageException(Exception):
pass


class Packages:
def __init__(self, data):
self.data = data
Expand Down Expand Up @@ -455,9 +452,7 @@ def install(self):

except Exception as e:
self._failsafe_lock_versions()
raise PackageException('Error installing packages: {}'.format(
str(e))
)
raise PackageException(f"Error installing packages: {str(e)}")

def no_op(self):
try:
Expand Down Expand Up @@ -520,9 +515,7 @@ def no_op(self):

except Exception as e:
self._failsafe_lock_versions()
raise PackageException(
'Error analysing packages: {}'.format(str(e))
)
raise PackageException(f"Error analysing packages: {str(e)}")

def _lock_versions(self):
self._get_locked_versions()
Expand Down
Loading

0 comments on commit 3f680e8

Please sign in to comment.