Skip to content

Commit

Permalink
osmorphing: Check installed packages before installing packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristi1324 committed Sep 4, 2024
1 parent 24c52ea commit 91b2075
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
4 changes: 4 additions & 0 deletions coriolis/osmorphing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def check_os_supported(cls, detected_os_info):
"OS compatibility check not implemented for tools class %s" % (
cls.__name__))

@abc.abstractmethod
def get_installed_packages(self, package_name):
pass

@abc.abstractmethod
def set_net_config(self, nics_info, dhcp):
pass
Expand Down
3 changes: 3 additions & 0 deletions coriolis/osmorphing/coreos.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def check_os_supported(cls, detected_os_info):
return True
return False

def get_installed_package(self):
pass

def disable_predictable_nic_names(self):
pass

Expand Down
11 changes: 11 additions & 0 deletions coriolis/osmorphing/debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ def set_net_config(self, nics_info, dhcp):
cfg_name = "%s/coriolis_netplan.yaml" % netplan_base
self._write_file_sudo(cfg_name, new_cfg)

def get_installed_packages(self):
cmd = 'apt list --installed'
try:
apt_list_output = self._exec_cmd_chroot(
cmd).splitlines()
for line in apt_list_output:
package_name, _ = line.decode('utf-8').split('/', 1)
self.installed_packages.append(package_name)
except exception.CoriolisException:
pass

def pre_packages_install(self, package_names):
super(BaseDebianMorphingTools, self).pre_packages_install(
package_names)
Expand Down
5 changes: 5 additions & 0 deletions coriolis/osmorphing/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ def morph_image(origin_provider, destination_provider, connection_info,
LOG.info("Post packages uninstall")
export_os_morphing_tools.post_packages_uninstall(packages_remove)

LOG.info("Checking for packages already installed")
import_os_morphing_tools.get_installed_packages()
packages_add = list(
set(packages_add) - set(import_os_morphing_tools.installed_packages))

LOG.info("Pre packages install")
import_os_morphing_tools.pre_packages_install(packages_add)

Expand Down
3 changes: 3 additions & 0 deletions coriolis/osmorphing/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def check_os_supported(cls, detected_os_info):
return True
return False

def get_installed_package(self):
pass

def disable_predictable_nic_names(self):
pass

Expand Down
16 changes: 9 additions & 7 deletions coriolis/osmorphing/redhat.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,17 @@ def set_net_config(self, nics_info, dhcp):
mac_addresses)
self._add_net_udev_rules(net_ifaces_info)

def _has_package_installed(self, package_name):
cmd = 'rpm -q %s' % ("".join(package_name))
def get_installed_packages(self):
cmd = 'rpm -qa --qf \'%{NAME}\n\''
try:
self._exec_cmd_chroot(cmd)
return True
apt_list_output = self._exec_cmd_chroot(
cmd).splitlines()
for line in apt_list_output:
package_name, _ = line.split('/', 1).decode('utf-8')
self.installed_packages.append(package_name)
except exception.CoriolisException:
LOG.warning(f"Package ${package_name} is not installed")
LOG.warning("Failed to get installed packages")
LOG.trace(utils.get_exception_details())
return False

def _yum_install(self, package_names, enable_repos=[]):
try:
Expand Down Expand Up @@ -295,7 +297,7 @@ def pre_packages_install(self, package_names):
super(BaseRedHatMorphingTools, self).pre_packages_install(
package_names)
self._yum_clean_all()
if not self._has_package_installed('grubby'):
if 'grubby' not in self.installed_packages:
self._yum_install(['grubby'])
else:
LOG.debug("Skipping package 'grubby' as it's already installed")
Expand Down
12 changes: 12 additions & 0 deletions coriolis/osmorphing/suse.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def set_net_config(self, nics_info, dhcp):
# TODO(alexpilotti): add networking support
pass

def get_installed_packages(self):
cmd = 'rpm -qa --qf \'%{NAME}\n\''
try:
apt_list_output = self._exec_cmd_chroot(
cmd).splitlines()
for line in apt_list_output:
package_name, _ = line.decode('utf-8').split('/', 1)
self.installed_packages.append(package_name)
except exception.CoriolisException:
LOG.warning("Failed to get installed packages")
LOG.trace(utils.get_exception_details())

def get_update_grub2_command(self):
location = self._get_grub2_cfg_location()
return "grub2-mkconfig -o %s" % location
Expand Down

0 comments on commit 91b2075

Please sign in to comment.