How to build a local Yumrepo Server

part 4 of 4

Posted by paul on 2015.03.25

How to build a local Yumrepo Server - part 4 of 4

2015.03.25 Wed

  1. Part 1: Intro
  2. Part 2: Yumrepo 6.6basic - RPMs from DVDs only
  3. Part 3: Yumrepo 6.6 - exact mirror of a full Yumserver
  4. Part 4: How to use yumupdate.sh

Overview

When yum updating yumclients that are configured to use a local yumrepo, you should use a script like yumupdate.sh (shown below). You should not simply run "yum -y update". At certain times, CentOS-Base.repo is readded when you run "yum update". You don't want both CentOS-Base.repo and 66.repo at the same time on a yumclient. Thus use the script which will do some house cleaning before running yum update.

I will also show how to keep yumrepo 6.6 synced with latest updates released by CentOS organization.

Prep script, yumupdate.sh

  1. On a yumclient, create /root/bin/yumupdate.sh with code below and make it executable.
  2. #!/bin/bash
    NowDate=`date "+%Y%m%d-%H%M"`
    LogsYum="/root/logsyum"
    LogFile="yum-update-$NowDate.log"
    
    mkdir -p $LogsYum 2> /dev/null
    
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$NowDate 2> /dev/null
    yum clean all
    yum -y update 2>&1 | tee $LogsYum/$LogFile
    
    KernelTest=$(grep -ic "kernel" $LogsYum/$LogFile)
    if [ $KernelTest -ge 1 ]; then
    	echo "# Kernel updated" >> $LogsYum/$LogFile
    	mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$NowDate 2> /dev/null
    	reboot
    	exit 0
    else
    	echo "# Kernel NOT updated." >> $LogsYum/$LogFile
    	mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$NowDate 2> /dev/null
        exit 0
    fi
    
  3. When yumupdate.sh is executed, following will happen
    1. If /etc/yum.repos.d/CentOS-Base.repo is found, it is renamed to /etc/yum.repos.d/CentOS-Base.repo.yyyymmdd-hhmm
    2. Run "yum clean all".
    3. Run "yum -y update".
    4. All standard and error output are piped to /root/logsyum/yum-update-yyyymmdd-hhmm.log
    5. If kernel rpm is updated, the OS will reboot automatically.
    6. If /etc/yum.repos.d/CentOS-Base.repo is found, it is renamed to /etc/yum.repos.d/CentOS-Base.repo.yyyymmdd-hhmm
  4. Test yumupdate.sh on a yumclient.

Pushing out yumupdate.sh to other yumclients using a script.

You have a working local yumrepo and yumupdate.sh. You have everything needed to take advantage of having a local yumserver. Last hurdle is configuring your fleet of CentOS servers to use the local yumrepo. Now you need to find a way to configure all yumclients to use the local yumrepo. In order to accomplish it, you would do perhaps do the following 2 steps.

  1. Write a script that does following. Calling the script setyum-66.sh.
    1. Rename CentOS-Base.repo to CentOS-Base.repo.orig
    2. add 66.repo.
    3. Run "yum clean all".
  2. Push out the script to your fleet of CentOS servers.
    1. Create/update inventory to find out which servers will use the local yumrepo. Often, you will need to keep track of status of each server.
    2. Copy setyum-66.sh out to all CentOS servers, while keeping track of what servers has it and what don't.
    3. Make setyum-66.sh executable on remote CentOS severs.
    4. Remotely execute setyum-66.sh on each CentOS server as root.

This will mean lots of typing, even in a smaller environment. However if you have a larger environment with dozens of CentOS servers, this will not work. And this is where I recommend using SaltStack. With SaltStack, you can avoid having to perform manual-intensive, error-prone procedure. With SaltStack, it will take very minimal typing and no need to keep track of each server as you push out the clients.

Optional: Pushing out yumupdate.sh using Salt.

Using Salt is optional BUT I highly recommend it. If you are up for adding a few rpms, you are ready to use Salt. Salt has many features but using 'remote execute' is very simple and that's all you need to push out 66.repo to many yumclients easily.

To set up a Salt Master and a Salt Minion to test remote execute feature, check out the tutorial I wrote. Test some "remote execute" commands with commands like "ls /root" or "date". In my next blog, I will explain how to use Salt to configure your CentOS servers use your local yumrepo.