Skip to content

Instantly share code, notes, and snippets.

@gonoph
Last active September 15, 2018 03:41
Show Gist options
  • Save gonoph/a81d76480e882eb222012d16e1ea9ba0 to your computer and use it in GitHub Desktop.
Save gonoph/a81d76480e882eb222012d16e1ea9ba0 to your computer and use it in GitHub Desktop.
container image based on RHEL 7.5 for AWS RHUI Hosts
#!/bin/bash
# This will build a container image based on RHEL 7.5
# for use in AWS with RHUI
DEFAULT_FROM=rhel7/rhel:7.5
DEFAULT_TAG='devel/rhel:latest'
BZ='https://bugzilla.redhat.com/show_bug.cgi?id=1498628'
declare -r DEFAULT_FROM DEFAULT_TAG BZ
err() {
echo -e '\e[1;31mERROR:\e[0;31m' "$@" '\e[0m' >&2
}
info() {
echo -e '\e[0;32m* INFO\e[0m' "$@" '\e[0m'
}
[ $UID -ne 0 ] && err "Due to this BZ[1], we need to run as root!" && err "[1]: $BZ" && exit 1
OPTS=`getopt -o hfut: --long help,from:,user:,tag: -- "$@"`
[ $? != 0 ] && err "Failed parsing options." && exit 1
eval set -- "$OPTS"
FROM=$DEFAULT_FROM
TAG=$DEFAULT_TAG
while true; do
case "$1" in
-f | --from ) FROM="$2"; shift 2;;
-t | --tag ) TAG="$2" ; shift 2;;
-- ) shift; break ;;
* ) break ;;
esac
done
info "FROM = $FROM"
info "TAG = $TAG"
set -x
set -e -E
errexit() {
err "There was an error in the last command!"
clean
exit 1
}
clean() {
[ "x$ctr1" != "x" ] && buildah rm $ctr1
}
ctr1=$(buildah from $FROM)
trap errexit EXIT
# ensure the RHUI stuff is in the container
buildah copy $ctr1 /etc/yum.repos.d /etc/yum.repos.d
buildah copy $ctr1 /etc/pki/rhui /etc/pki/rhui
buildah copy $ctr1 /usr/lib/yum-plugins /usr/lib/yum-plugins
buildah copy $ctr1 /etc/yum/pluginconf.d /etc/yum/pluginconf.d
# remove the cache and add a user
buildah run $ctr1 -- yum install -y sudo
buildah run $ctr1 -- rm -rf /var/cache/yum/x86_64
buildah run $ctr1 -- useradd devel
buildah run $ctr1 -- echo 'devel ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/devel
## Include some buildtime annotations
buildah config --annotation "com.example.build.host=$(uname -n)" $ctr1
buildah config --env PS1='\[\033[01;30m\](\[\033[01;32m\]container\[\033[01;30m\])\[\033[0m\] [\u@\h \[\033[01;34m\]\W\[\033[0m\]]$ ' $ctr1
buildah config --arch x86_64 $ctr1
buildah config --os RHEL $ctr1
buildah config --author 'Billy Holmes <billy.holmes@redhat.com>' $ctr1
## Run our server and expose the port
# buildah config --cmd "/usr/sbin/httpd -DFOREGROUND" $ctr1
# buildah config --port 80 $ctr1
## Commit this container to an image name
buildah commit $ctr1 $TAG
buildah push $TAG docker-daemon:localhost/$TAG
trap 'clean' EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment