Rebuild CentOS Environment

Published On: Aug 21, 2017

Today as part of one of the projects that I am working on we needed to rebuild an environment and the hosting provider wouldn’t give us the pieces that we needed, so with a couple of quick min I managed to build a quick script that I thought that I would share. The problem is that we have a client that we are doing work for an one of the modules on thier site isn’t behaving as we would expect. So the first step in this process was going through and making sure that our environment matched as much as possible. We talked with the hosting provider and they were more than happy to sell us another environment.

First I had to get the infromation about all of the packages installed on the server, and where they were coming from.

yum list installed > installed.txt
yum -v repolist > repolist.txt

Once this was done it was easy enough to parse out the infromation and push it to build a build script.

import io
import re

def build_repos(output):
    file = open('repolist.txt')
    print('[+] Reading from the repolist file...')
    repoFileName = ""
    output = output + "# installing repos\n"
    for l in file:
        if l.startswith("Repo-baseurl : "):
            link = l.replace('Repo-baseurl : ','').split(' ')[0].strip()
            output = output + "sudo yum-config-manager --add-repo " + link + " \n"
    print('[+] Writing out to the')
    output = output + "\n\n"
    return output

def build_install(output):
    file = open('installed.txt','r')
    print('[+] Reading from installed packages file...')
    output = output + "# installing packages \n"
    output = output + "sudo yum install"
    finder = re.compile(r"[0-9]{1,3}:")
    for l in file:
        if l.startswith("Loaded plugins:"):
        if l.startswith("Installed Packages"):
        alllineparts = l.split(' ')
        lineparts = []
        for item in alllineparts:
            if item:
        if len(lineparts) > 2:
            package = finder.sub("", lineparts[0].replace('.x86_64','').replace('.noarch',''))
            version = finder.sub("", lineparts[1])

            # Apparently the MariaDB people are not friendly with old versions
            if (package.startswith("MariaDB")):
                output = output + " " + package + " "
                output = output + " " + package + "-" + version
    output = output + " --nogpgcheck"
    return output

def write_file(output):
    print('[+] Writing install of packages to')
    outputfile = open('', 'w')

output = ""

output = build_repos(output)
output = build_install(output)


At the end of the process I ended up spending more time waiting on the CentOS iso to download than processing and building a build scirpt. Pretty useful stuff. At this point I am trying to figure out if it’s worth the effort for syncing up the configurations, or if I should just use this as a one off.

Anyway thought that I would pass this along in the event that someone finds it useful.