Last active
December 3, 2017 00:12
-
-
Save skunkie/f21b9508d1c21f01b9f4 to your computer and use it in GitHub Desktop.
This bash script synchronizes local repositories with external repositories, RPM, reposync, createrepo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# requires bash version 4 | |
# This script synchronizes local repositories with | |
# external repositories | |
# | |
############################################################ | |
# PREREQUISITES | |
############################################################ | |
for util in reposync createrepo; do | |
if [ -z $(which $util) ]; then | |
echo "$util is not found" | |
echo -n "please verify that packages" | |
echo " yum-utils and createrepo are installed" | |
exit 1 | |
fi | |
done | |
configfile=$(dirname $(readlink -f $0))/'reposync.ini' | |
if [ ! -f $configfile ]; then | |
echo "config file ${configfile} is not found" | |
exit 1 | |
fi | |
############################################################ | |
# READ CONFIG FILE | |
############################################################ | |
while IFS='= ' read k v; do | |
if [[ $k == \[*\] ]]; then | |
section=$(echo "$k" | tr -d "[] ") | |
declare -A $section | |
elif [[ $v ]]; then | |
if [ ! $(echo $v | grep "^#") ]; then | |
eval $section[$k]=$(echo "$v" | tr -d " ") | |
fi | |
fi | |
done < $configfile | |
# path to the root of all the repositories | |
reposroot=${settings[reposroot]} | |
# proxy settings | |
export http_proxy=${settings[proxy]} | |
export https_proxy=${settings[proxy]} | |
############################################################ | |
# FUNCTIONS | |
############################################################ | |
usage(){ | |
echo " | |
Usage: $0 [OPTION] | |
-h --help show this help | |
-n --name [repo1] [repo2] synchronize only the repository(s) with the given name(s) | |
-l --list show the names of all the repositories | |
-a --all --syncall synchronize all the repositories | |
" | |
} | |
hashHasKey() { | |
# returns 1 if the key exists, otherwise 0 | |
eval 'local keys=${!'$1'[@]}'; | |
eval "case '$2' in | |
${keys// /|}) return 0 ;; | |
* ) return 1 ;; | |
esac"; | |
} | |
hashGetValue() { | |
# return value of the key, otherwise 0 | |
eval 'local keys=${!'$1'[@]}'; | |
eval "case '$2' in | |
${keys// /|}) echo \${$1[$2]};return 0 ;; | |
* ) return 1 ;; | |
esac"; | |
} | |
############################################################ | |
# USER INTERACTION | |
############################################################ | |
case $# in | |
0) | |
usage | |
exit 0 | |
;; | |
1) | |
case $1 in | |
-l|--list) | |
echo -e "\nAvailable repositories:\n" | |
(IFS=$'\n'; echo -e "${!reposlist[*]}\n") | |
exit 0 | |
;; | |
-a|--all|--syncall) | |
;; | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
*) | |
echo "$1 is an incorrect or incomplete option" | |
usage | |
exit 1 | |
;; | |
esac | |
;; | |
2) | |
case $1 in | |
-n|--name) | |
if hashHasKey reposlist $2; then | |
value=$(hashGetValue reposlist $2) | |
declare -A reposlist=( [$2]=$value ) | |
else | |
echo "$2 is an incorrect name of the repository, exiting" | |
exit 0 | |
fi | |
;; | |
*) | |
echo "$@ are incorrect options" | |
usage | |
exit 1 | |
;; | |
esac | |
;; | |
*) | |
case $1 in | |
-n|--name) | |
wrongname=false | |
declare -A temp | |
for name in ${@:2}; do | |
if ! hashHasKey reposlist $name; then | |
echo "$name is an incorrect name of the repository" | |
wrongname=true | |
else | |
value=$(hashGetValue reposlist $name) | |
temp[$name]=$value | |
fi | |
done | |
if $wrongname; then | |
echo "exiting" | |
exit 1 | |
else | |
reposlist=() | |
for idx in "${!temp[@]}"; do | |
reposlist[$idx]=${temp[$idx]} | |
done | |
fi | |
;; | |
*) | |
echo "$@ are incorrect options" | |
usage | |
exit 1 | |
;; | |
esac | |
;; | |
esac | |
############################################################ | |
# SYNCHRONIZE | |
############################################################ | |
for repo in ${!reposlist[@]}; do | |
if [ ${reposlist[$repo]} != "true" ] && | |
[ ${reposlist[$repo]} != "false" ]; then | |
${reposlist[$repo]} | |
continue | |
fi | |
if ${reposlist[$repo]}; then | |
reposync -dlnr $repo -p $reposroot | |
fi | |
if [ -f $reposroot/$repo/comps.xml ]; then | |
createrepo -s sha --update $reposroot/$repo -g $reposroot/$repo/comps.xml | |
else | |
createrepo -s sha --update $reposroot/$repo | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment