Skip to content

Instantly share code, notes, and snippets.

@mcenirm
Created January 11, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcenirm/0b6af8ab2dfcf9880e55aa1dddb8fa4f to your computer and use it in GitHub Desktop.
Save mcenirm/0b6af8ab2dfcf9880e55aa1dddb8fa4f to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
shopt -s nullglob
roots=( root* )
if [ ${#roots[*]} -lt 1 ] ; then
roots=( root1 root2 )
fi
gpgkey_url='https://www.centos.org/keys/RPM-GPG-KEY-CentOS-7'
gpgkey=dist/${gpgkey_url##*/}
mirror='http://www.gtlib.gatech.edu/pub/centos/7.3.1611/os/x86_64/Packages/'
declare -A packages
packages=(
[bash]=4.2.46-20.el7_2.x86_64
[glibc]=2.17-157.el7.x86_64
[ncurses-libs]=5.9-13.20130511.el7.x86_64
[libgcc]=4.8.5-11.el7.x86_64
[libstdc++]=4.8.5-11.el7.x86_64
[ncurses-base]=5.9-13.20130511.el7.noarch
[basesystem]=10.0-7.el7.centos.noarch
[glibc-common]=2.17-157.el7.x86_64
[nss-softokn-freebl]=3.16.2.3-14.4.el7.x86_64
[libselinux]=2.5-6.el7.x86_64
[pcre]=8.32-15.el7_2.1.x86_64
[libsepol]=2.5-6.el7.x86_64
[tzdata]=2016g-2.el7.noarch
[setup]=2.8.71-7.el7.noarch
[centos-release]=7-3.1611.el7.centos.x86_64
[filesystem]=3.2-21.el7.x86_64
)
grab () {
local file=$1
if ! [ -f "${file}" ] ; then
local url=$2
local cmd=( curl -sLR -o "${file}" )
if [ -f "${file}" ] ; then
cmd+=( -z "${file}" )
fi
cmd+=( "${url}" )
( set -x
"${cmd[@]}"
)
fi
}
get_package_filename () {
local name=$1
local qualifier=$2
echo "${name}-${qualifier}.rpm"
}
get_package_location () {
local name=$1
local qualifier=$2
local filename=$( get_package_filename "${name}" "${qualifier}" )
echo "dist/${filename}"
}
mkdir -p dist
grab "${gpgkey}" "${gpgkey_url}"
for key in "${!packages[@]}" ; do
package_filename=$( get_package_filename "${key}" "${packages[${key}]}" )
package_location=$( get_package_location "${key}" "${packages[${key}]}" )
grab "${package_location}" "${mirror}${package_filename}"
done
for rootname in "${roots[@]}" ; do
root=${PWD}/${rootname}
echo
echo "#### ${root}"
echo
rpm=( rpm "--root=${root}" )
rpminstargs=( -ivh --noscripts --relocate "/=${root}" )
( set -x
mkdir -pv "${root}"
"${rpm[@]}" -qa | sort
"${rpm[@]}" --import "${gpgkey}"
)
declare -A missing
missing=()
for key in "${!packages[@]}" ; do
value=${packages[${key}]}
package_location=$( get_package_location "${key}" "${value}" )
if ! "${rpm[@]}" -q "${key}" ; then
#if ! ( set -x ; "${rpm[@]}" "${rpminstargs[@]}" "${package_location}" ) ; then
missing[${key}]=${value}
#fi
fi
done
num_missing=${#missing[*]}
if [ "${num_missing}" -gt 0 ] ; then
installation_order=()
missing_keys=( "${!missing[@]}" )
max_i=$(( 2 ** ${num_missing} - 1 ))
i=1
while [ "${#missing_keys[@]}" -gt 0 -a "${i}" -le "${max_i}" ] ; do
bits=$( echo "obase=2;${i}" | bc )
#declare -p bits
to_install_keys=()
for key in "${missing_keys[@]}" ; do
if [ "${bits-0}" -lt 1 ] ; then
break
fi
if [ "${bits: -1}" = 1 ] ; then
to_install_keys+=( "${key}" )
fi
bits=0${bits%?}
done
if [ "${#to_install_keys[@]}" -gt 0 ] ; then
to_install=()
for key in "${to_install_keys[@]}" ; do
value=${packages[${key}]}
to_install+=( $( get_package_location "${key}" "${value}" ) )
done
if ( set +x ; "${rpm[@]}" "${rpminstargs[@]}" "${to_install[@]}" 2>/dev/null ) ; then
declare -p to_install_keys
for key in "${to_install_keys[@]}" ; do
unset missing_keys[${key}]
done
installation_order+=( "${to_install_keys[@]}" )
fi
fi
let ++i
done
declare -p installation_order
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment