#!/bin/bash

set -e

# HACK - virt-resize might not be able to resize our xfs rootfs,
# depending on how it was compiled and which plugins are installed,
# and will just silently not do it.  So we do it here.
#
df --output=source,fstype / | tail -n1 | while read source fstype; do
    case $fstype in
    ext*)
	    resize2fs $source
	    ;;
    xfs*)
	    xfs_growfs /
	    ;;
    esac
done

df -Th /

# If the file /root/.skip_repos is present on the machine,
# all actions regarding the repositories will be skipped:
# subscriptions, adding repos, deleting existing entries
SKIP_REPO_FLAG="/root/.skip_repos"

if [ ! -f "$SKIP_REPO_FLAG" ]; then
    # Remove any internal files
    rm /etc/yum.repos.d/download.devel.redhat.com.repo

    # register system
    subscription-manager register --auto-attach --username=`cat ~/.rhel/login` --password=`cat ~/.rhel/pass`
    # remove credentials from test machine
    rm -rf ~/.rhel
fi


# Only start logging here.  Otherwise the subscription credentials
# appear in the output above.
#
set -x

if [ ! -f "$SKIP_REPO_FLAG" ]; then
    # Configure repositories.
    #
    # if disabling the repos doesn't work, do without
    yum -y --disablerepo=rhel-7-server-htb-rpms --disablerepo=rhel-sjis-for-rhel-7-server-rpms install yum-utils || yum -y install yum-utils
    yum-config-manager --enable rhel-7-server-optional-rpms
    yum-config-manager --enable rhel-7-server-extras-rpms

    # the following don't necessarily need to work
    yum-config-manager --disable rhel-sjis-for-rhel-7-server-rpms || true
    yum-config-manager --disable rhel-7-server-htb-rpms || true
fi

yum --nogpgcheck -y update

echo foobar | passwd --stdin root

# We install all dependencies of the cockpit packages since we want
# them to not spontaneously change from one test run to the next when
# the distribution repository is updated.
COCKPIT_DEPS="\
atomic \
device-mapper-multipath \
docker \
etcd \
glib-networking \
json-glib \
kexec-tools \
kubernetes \
libssh \
libvirt \
libvirt-client \
NetworkManager-team \
pcp-libs \
pcp \
python-pip \
qemu \
realmd \
selinux-policy-targeted \
setroubleshoot-server \
subscription-manager \
storaged \
storaged-lvm2 \
storaged-iscsi \
sos \
tuned \
"

# We also install the packages necessary to join a FreeIPA domain so
# that we don't have to go to the network during a test run.
# on epel/rhel we have ipa-client instead of freeipa-client
IPA_CLIENT_PACKAGES="\
ipa-client \
oddjob \
oddjob-mkhomedir \
sssd \
"

TEST_PACKAGES="\
systemtap-runtime-virtguest \
valgrind \
gdb \
yum-utils \
virt-install \
qemu-system-x86 \
qemu-kvm \
"

yum install --nogpgcheck -y $TEST_PACKAGES $COCKPIT_DEPS $IPA_CLIENT_PACKAGES

# For debugging storaged crashes
debuginfo-install -y storaged storaged-lvm2 storaged-iscsi

# Prepare for building

# only install mock and build if TEST_SOURCE is set
if [ -n "$TEST_SOURCE" ]; then
    yum -y install rpm-build

    # enable epel for mock
    if [ ! -f "$SKIP_REPO_FLAG" ]; then
        mkdir /tmp/dep
        cd /tmp/dep
        yum -y install wget
        wget -T 15 -t 4 http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
        yum -y remove wget
        rpm -Uvh epel-release-*.rpm
        cd
        rm -rf /tmp/dep
    fi
    yum -y install mock

    # disable epel again
    yum-config-manager --disable 'epel*'

    useradd -c Builder -G mock builder
    srpm=$(/var/lib/testvm/make-srpm $TEST_SOURCE)
    su builder -c "/usr/bin/mock --verbose --installdeps $srpm"
fi

yum clean all || true

if [ ! -f "$SKIP_REPO_FLAG" ]; then
    subscription-manager unregister
fi

# For the D-Bus test server
if type "firewall-cmd" >/dev/null 2>&1; then
    FIREWALL_STATE=$(firewall-cmd --state || true)
    if [ "$FIREWALL_STATE" == "running" ]; then
        firewall-cmd --permanent --add-port 8765/tcp
    fi
fi

echo 'NETWORKING=yes' > /etc/sysconfig/network

useradd -c Administrator -G wheel admin
echo foobar | passwd --stdin admin

# To enable persistent logging
mkdir -p /var/log/journal

# Configure kubernetes
/var/lib/testvm/kubernetes.setup

if type "docker" >/dev/null 2>&1; then
    # HACK: docker falls over regularly, print its log if it does
    systemctl start docker || journalctl -u docker

    docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest

    # docker images that we need for integration testing
    /var/lib/testvm/docker-images.setup
fi

/var/lib/testvm/zero-disk.setup --keep-mock-cache

# HACK - kdump.service interferes with our storage tests, by loading
# the system for some time after boot and thereby causing a race
# between parted and udevd to turn out for the worse.  Disabling
# kdump.service helps somewhat, but the race is still there, and
# parted still fails occasionally.
#
# https://bugzilla.redhat.com/show_bug.cgi?id=1245144
# Fixed in parted-3.1-23.el7
#
systemctl disable kdump.service

# Final tweaks

rm -rf /var/log/journal/*
echo "kernel.core_pattern=|/usr/lib/systemd/systemd-coredump %p %u %g %s %t %e" > /etc/sysctl.d/50-coredump.conf

touch /.autorelabel

# Audit events to the journal
rm -f '/etc/systemd/system/multi-user.target.wants/auditd.service'
rm -rf /var/log/audit/
