Skip to content

Commit

Permalink
Translate expected package names to installed ones
Browse files Browse the repository at this point in the history
This patch fixes an issue that caused an internal error to the amphora
agent and led amphorae to transition to FAILURE state. The issue was
that on CentOS-based amphorae HAproxy package name is 'haproxy18' while
the expected package name is 'haproxy'.

The patch introduces a package name per distro map that is consulted
whenever a package has a different name than the one expected. If
there's a miss, it falls back to the expected package name.

Story: 2002958
Task: 22960

Change-Id: I59dfa3f9bd5974302ec72343444813232ec2ea02
  • Loading branch information
cgoncalves committed Jul 12, 2018
1 parent 2a7f1e5 commit 1c4004c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
33 changes: 28 additions & 5 deletions octavia/amphorae/backends/agent/api_server/osutils.py
Expand Up @@ -40,17 +40,30 @@

class BaseOS(object):

PACKAGE_NAME_MAP = {}

def __init__(self, os_name):
self.os_name = os_name

@classmethod
def _get_subclasses(cls):
for subclass in cls.__subclasses__():
for sc in subclass._get_subclasses():
yield sc
yield subclass

@classmethod
def get_os_util(cls):
os_name = distro.id()
for subclass in BaseOS.__subclasses__():
for subclass in cls._get_subclasses():
if subclass.is_os_name(os_name):
return subclass(os_name)
raise octavia_exceptions.InvalidAmphoraOperatingSystem(os_name=os_name)

@classmethod
def _map_package_name(cls, package_name):
return cls.PACKAGE_NAME_MAP.get(package_name, package_name)

def get_network_interface_file(self, interface):
if CONF.amphora_agent.agent_server_network_file:
return CONF.amphora_agent.agent_server_network_file
Expand Down Expand Up @@ -223,7 +236,8 @@ def is_os_name(cls, os_name):
return os_name in ['ubuntu']

def cmd_get_version_of_installed_package(self, package_name):
return "dpkg-query -W -f=${{Version}} {name}".format(name=package_name)
name = self._map_package_name(package_name)
return "dpkg-query -W -f=${{Version}} {name}".format(name=name)

def get_network_interface_file(self, interface):
if CONF.amphora_agent.agent_server_network_file:
Expand Down Expand Up @@ -304,11 +318,11 @@ class RH(BaseOS):

@classmethod
def is_os_name(cls, os_name):
return os_name in ['fedora', 'rhel', 'centos']
return os_name in ['fedora', 'rhel']

def cmd_get_version_of_installed_package(self, package_name):
return "rpm -q --queryformat %{{VERSION}} {name}".format(
name=package_name)
name = self._map_package_name(package_name)
return "rpm -q --queryformat %{{VERSION}} {name}".format(name=name)

def get_network_interface_file(self, interface):
if CONF.amphora_agent.agent_server_network_file:
Expand Down Expand Up @@ -458,3 +472,12 @@ def bring_interfaces_up(self, ip, primary_interface, secondary_interface):

def has_ifup_all(self):
return False


class CentOS(RH):

PACKAGE_NAME_MAP = {'haproxy': 'haproxy18'}

@classmethod
def is_os_name(cls, os_name):
return os_name in ['centos']
Expand Up @@ -37,9 +37,13 @@ def setUp(self):
self.ubuntu_os_util = osutils.BaseOS.get_os_util()

with mock.patch('distro.id',
return_value='centos'):
return_value='rhel'):
self.rh_os_util = osutils.BaseOS.get_os_util()

with mock.patch('distro.id',
return_value='centos'):
self.centos_os_util = osutils.BaseOS.get_os_util()

def test_get_os_util(self):
with mock.patch('distro.id',
return_value='ubuntu'):
Expand All @@ -56,7 +60,7 @@ def test_get_os_util(self):
with mock.patch('distro.id',
return_value='centos'):
returned_cls = osutils.BaseOS.get_os_util()
self.assertIsInstance(returned_cls, osutils.RH)
self.assertIsInstance(returned_cls, osutils.CentOS)
with mock.patch('distro.id',
return_value='FakeOS'):
self.assertRaises(
Expand Down Expand Up @@ -169,6 +173,15 @@ def test_cmd_get_version_of_installed_package(self):
cmd_get_version_of_installed_package(package_name))
self.assertEqual(rh_cmd, returned_rh_cmd)

def test_cmd_get_version_of_installed_package_mapped(self):
package_name = 'haproxy'
centos_cmd = "rpm -q --queryformat %{VERSION} haproxy18"

returned_centos_cmd = (
self.centos_os_util.cmd_get_version_of_installed_package(
package_name))
self.assertEqual(centos_cmd, returned_centos_cmd)

def test_has_ifup_all(self):
self.assertTrue(self.base_os_util.has_ifup_all())
self.assertTrue(self.ubuntu_os_util.has_ifup_all())
Expand Down

0 comments on commit 1c4004c

Please sign in to comment.