Theo Chatzimichos d05fd1
#!/bin/bash
Theo Chatzimichos d05fd1
Theo Chatzimichos f6d741
# See the description at the help()
Theo Chatzimichos d05fd1
Theo Chatzimichos dcff80
set -e
5884cc
if [ ! -r /etc/os-release ]; then
5884cc
    echo "Could not read /etc/os-release - exiting" >&2
5884cc
    exit 1
5884cc
fi
5884cc
source /etc/os-release
5884cc
SECRETS="False"
5884cc
PKG=''
Theo Chatzimichos dcff80
Theo Chatzimichos dcff80
if [[ $(whoami) != 'root' ]]; then
a79fbf
    echo 'Can only be run as root'
a79fbf
    exit 1
Theo Chatzimichos dcff80
fi
Theo Chatzimichos dcff80
fa45db
help() {
Theo Chatzimichos f6d741
    echo "Prepares the CI runner or workstation environment to run highstate or show_highstate tests"
fa45db
    echo
fa45db
    echo "Arguments:"
Theo Chatzimichos f6d741
    echo "-p <pkg1,pkg2> Comma-separated list of additional packages to be installed"
ef2551
    echo "-o <OS>        OPTIONAL: Specify different OS. Examples: \"Leap,15,5\""
Theo Chatzimichos 759754
    echo "-g             OPTIONAL: Make preparation for show_highstate"
Theo Chatzimichos 759754
    echo "-s             OPTIONAL: Include secrets files (disabed because CI runner can't decrypt them due to lack of GPG key)"
18a6fd
    echo "-n             OPTIONAL: Delete all repositories to speed up tests which do not install additional packages"
fc6714
    echo "-c             OPTIONAL: Do not install Git formulas"
fa45db
    echo
fa45db
}
fa45db
fa45db
[[ $1 == '--help' ]] && help && exit
fa45db
fc6714
while getopts p:o:gsnch arg; do
fa45db
    case ${arg} in
ed5236
        p) PKG=( ${OPTARG//,/ } ) ;;
ed5236
        o) OS=( ${OPTARG//,/ } ) ;;
Theo Chatzimichos a452cb
        g) HIGHSTATE=1 ;;
Theo Chatzimichos 754b1a
        s) SECRETS="True" ;;
18a6fd
        n) REPOSITORIES='False' ;;
fc6714
        c) FORMULAS='False' ;;
fa45db
        h) help && exit ;;
fa45db
        *) help && exit 1 ;;
fa45db
    esac
fa45db
done
fa45db
6571df
DOMAIN='infra.opensuse.org'
Theo Chatzimichos 759754
18a6fd
if [ -z "$REPOSITORIES" ]
18a6fd
then
18a6fd
  sed -i 's/download.opensuse.org/download-prg.infra.opensuse.org/' /etc/zypp/repos.d/*
18a6fd
  
ed5236
  if [ -n "${PKG[*]}" ]; then
a79fbf
      zypper --gpg-auto-import-keys ref
a79fbf
      zypper -qn install --no-recommends "${PKG[@]}"
18a6fd
  fi
18a6fd
elif [ "$REPOSITORIES" == 'False' ]
18a6fd
then
18a6fd
  rm /etc/zypp/repos.d/*
5884cc
fi
5884cc
fc6714
if [ -z "$FORMULAS" ]
fc6714
then
fc6714
  bin/clone_formulas.sh
fc6714
fi
fc6714
d6cb32
bin/replace_secrets.sh
a79fbf
rm -rf /srv/{salt,pillar} 2>/dev/null
74c720
74c720
ID=$(/usr/bin/hostname -f)
37637c
IDFILE="pillar/id/${ID//./_}.sls"
37637c
IDFILE_BASE="$IDFILE.base.sls"
5884cc
b6186d
printf "grains:\n  country: de\n  hostusage: test\n  reboot_safe: no\n" > "$IDFILE"
37637c
cp "$IDFILE" "$IDFILE_BASE"
5884cc
5884cc
if [[ -n "$HIGHSTATE" ]]; then
ed5236
    printf 'country:\ndomain: %s\ninclude_secrets: %s' "$DOMAIN" "$SECRETS" > /etc/salt/grains
ed5236
    [[ -n "${OS[0]}" ]] && printf '\nosfullname: %s\nosmajorrelease: %s\nosrelease_info: [%s, %s]\n' "${OS[0]}" "${OS[1]}" "${OS[1]}" "${OS[2]}" >> /etc/salt/grains
ed5236
    bin/get_roles.py -o yaml >> "$IDFILE"
ce572e
ce572e
    if [ ! -d /etc/salt/minion.d ]
ce572e
    then
ce572e
	    mkdir /etc/salt/minion.d
ce572e
    fi
7e6b84
    echo 'features: {"x509_v2": true}' > /etc/salt/minion.d/features_x509_v2.conf
ce572e
    tee /etc/salt/minion.d/roots.conf <<-EOF
ce572e
	file_roots:
ce572e
	  base:
ce572e
	    - /srv/salt
ce572e
	    - /usr/share/salt-formulas/states
fc6714
	    - /srv/formulas
ce572e
	EOF
37637c
37637c
    cp "$IDFILE_BASE" "$IDFILE"
Theo Chatzimichos 818e20
fi
10bd28
a79fbf
ln -s "$PWD/salt" /srv/salt
702de5
702de5
salt-call --local saltutil.runner saltutil.sync_runners
702de5
salt-call --local saltutil.sync_modules
702de5
salt-call --local saltutil.sync_states
702de5
702de5
# we reference custom modules in the pillar, hence only link it after they are available
a79fbf
ln -s "$PWD/pillar" /srv/pillar
702de5
ed5236
ln -s "$PWD" /srv/salt-git
3ab48e
3ab48e
grep -q search /etc/resolv.conf || echo 'search infra.opensuse.org' >> /etc/resolv.conf