HostOnNet Blog

Python Script to update yum

Some times when you update server software with yum, it fail to update due to few package conflicts. This will result in none of the packages getting updated.

When i update a cpanel server, i get following error

--> Running transaction check
---> Package ea-nghttp2.x86_64 0:1.20.0-2.2.3.cpanel will be installed
---> Package ea-openssl.x86_64 0:1.0.2k-7.7.1.cpanel will be installed
---> Package nss_compat_ossl.x86_64 0:0.9.6-8.6.amzn1 will be installed
--> Processing Conflict: ea-apache24-mod_ruid2-0.9.8-14.14.20.cpanel.x86_64 conflicts ea-apache24-mod_cache
--> Finished Dependency Resolution
Error: ea-apache24-mod_ruid2 conflicts with ea-apache24-mod_cache-2.4.27-6.6.1.cpanel.x86_64
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
root@cpanel [~]#

I normally do manual updates of packages, this is time consuming task. Today i decided to create a python script. I am not much of a python coder, but making the script in python make it work on Linux servers with out installing anything as python is present in all linux servers by default.

fix-yum.py

Save following code as fix-yum.py

#!/usr/bin/python

import subprocess
import os

result = subprocess.Popen("yum check-update", shell=True, stdout=subprocess.PIPE).stdout.read()

result_lines = result.split("\n")

for line in result_lines:
    line_parts = line.split()
    if len(line_parts) == 3:
        package_name = line_parts[0]
        #process = subprocess.Popen(["yum", "-y", "upgrade", package_name])
        #process.wait()
        os.system("yum -y upgrade " + package_name)

There is a problem with this script, that won’t wait for the yum process completely finish execution. I tried subprocess.Popen and os.system, that did not made much difference. But it is not a big problem as you run yum again, it wait for running yum to exit, so it just shows the annoying message that yum already running.

Script available on github at

https://github.com/HostOnNet/server-tools/blob/master/fix-yum

Once the script is run, you will be left with conflicting packages, all other packages get updated. Now you need to deal with the conflicting packages, this depends on the package. In this case, i have to remove one of the conflicting package, i decided to remove ea-apache24-mod_cache, you could remove ea-apache24-mod_ruid2. Just remove what you think you can live with out.


Posted in CentOS