day1

PROD-18791

Change-Id: I2f47ea12d166676e583323b60d2798f184c8ef9a
diff --git a/day01-image/scripts/base.sh b/day01-image/scripts/base.sh
new file mode 100644
index 0000000..675bcfd
--- /dev/null
+++ b/day01-image/scripts/base.sh
@@ -0,0 +1,68 @@
+#!/bin/bash -xe
+
+UBUNTU_BASEURL="${UBUNTU_BASEURL:-mirror://mirrors.ubuntu.com/mirrors.txt}"
+
+## Base packages and setup
+export DEBIAN_FRONTEND=noninteractive
+echo -e '#!/bin/sh\nexit 101' > /usr/sbin/policy-rc.d
+chmod +x /usr/sbin/policy-rc.d
+
+# Configure apt. Please refer to
+# https://github.com/Mirantis/reclass-system-salt-model/blob/master/linux/system/single/debian.yml
+# and keep those structures with same naming convention - to prevent
+# misconfiguration between base system and salt state.
+echo "Acquire::CompressionTypes::Order gz;" >/etc/apt/apt.conf.d/99compression-workaround-salt
+echo "Acquire::EnableSrvRecords false;" >/etc/apt/apt.conf.d/99enablesrvrecords-false
+echo "Acquire::http::Pipeline-Depth 0;" > /etc/apt/apt.conf.d/99aws-s3-mirrors-workaround-salt
+echo "APT::Install-Recommends false;" > /etc/apt/apt.conf.d/99dont_install_recommends-salt
+echo "APT::Install-Suggests false;" > /etc/apt/apt.conf.d/99dont_install_suggests-salt
+echo "Acquire::Languages none;" > /etc/apt/apt.conf.d/99dont_acquire_all_languages-salt
+echo "APT::Periodic::Update-Package-Lists 0;" > /etc/apt/apt.conf.d/99dont_update_package_list-salt
+echo "APT::Periodic::Download-Upgradeable-Packages 0;" > /etc/apt/apt.conf.d/99dont_update_download_upg_packages-salt
+echo "APT::Periodic::Unattended-Upgrade 0;" > /etc/apt/apt.conf.d/99disable_unattended_upgrade-salt
+
+sysctl -w fs.file-max=100000
+# Overwrite default mirrors
+echo "deb [arch=amd64] ${UBUNTU_BASEURL} xenial main restricted multiverse universe" > /etc/apt/sources.list
+echo "deb [arch=amd64] ${UBUNTU_BASEURL} xenial-updates main restricted multiverse universe" >> /etc/apt/sources.list
+echo "deb [arch=amd64] ${UBUNTU_BASEURL} xenial-security main restricted multiverse universe" >> /etc/apt/sources.list
+#echo "deb [arch=amd64] ${UBUNTU_BASEURL} xenial-backports main restricted multiverse universe" >> /etc/apt/sources.list
+echo '127.0.0.1 localhost' >> /etc/hosts
+echo '127.0.0.1 cfg01 cfg' >> /etc/hosts
+
+apt-get clean
+apt-get update
+
+# Useful tools
+apt-get -y install byobu curl ethtool htop iputils-ping lsof strace tcpdump tmux traceroute tree vim-nox wget
+# Install common prerequisites
+apt-get -y install apt-transport-https libmnl0 python-apt python-m2crypto python-psutil acpid
+
+# Cleanup old kernels, ensure latest is installed via virtual package
+if [ ! -f /tmp/no_install_kernel ]; then
+    apt-get purge -y linux-image-* linux-headers-*
+    apt-get install -y linux-image-virtual-lts-xenial linux-image-extra-virtual-lts-xenial
+
+    # Update grub cmdline
+    sed -i 's|GRUB_CMDLINE_LINUX_DEFAULT=.*|GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200n8"|g' /etc/default/grub
+    sed -i 's|GRUB_CMDLINE_LINUX=.*|GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"|g' /etc/default/grub
+    update-grub
+fi
+
+apt-get -y upgrade
+apt-get -y dist-upgrade
+
+apt-get autoremove --purge
+
+# Tmux fixes
+cat << 'EOF' >> /etc/tmux.conf
+set -g default-terminal "screen-256color"
+set -g set-titles on
+set -g xterm-keys on
+EOF
+
+# Setup cloud-init
+apt-get -y install cloud-init
+
+# Disable apt-daily
+systemctl disable apt-daily.timer
diff --git a/day01-image/scripts/cleanup.sh b/day01-image/scripts/cleanup.sh
new file mode 100644
index 0000000..059fe44
--- /dev/null
+++ b/day01-image/scripts/cleanup.sh
@@ -0,0 +1,35 @@
+#!/bin/bash -xe
+#apt-get purge salt-formula-* -y
+apt-get -y autoremove --purge
+apt-get -y clean
+#rm -rf /var/lib/apt/lists/*
+#rm -rf /etc/apt/sources.list.d/*
+#echo "" > /etc/apt/sources.list
+#rm -rf '/srv/salt/reclass/classes/!(service)'
+chmod +x /var/lib/maas/.maas_login.sh
+rm -f /usr/sbin/policy-rc.d || true
+
+echo "cleaning up hostname"
+sed -i "/.*ubuntu.*/d" /etc/hosts
+sed -i "/.*salt.*/d" /etc/hosts
+
+echo "cleaning up guest additions"
+rm -rf VBoxGuestAdditions_*.iso VBoxGuestAdditions_*.iso.? || true
+
+echo "cleaning up dhcp leases"
+rm -rf /var/lib/dhcp/* || true
+
+echo "cleaning up udev rules"
+rm -f /etc/udev/rules.d/70-persistent-net.rules || true
+rm -rf /dev/.udev/ || true
+rm -f /lib/udev/rules.d/75-persistent-net-generator.rules || true
+
+echo "cleaning up minion_id for salt"
+rm -f /etc/salt/minion_id || true
+
+echo "cleaning up resolvconf"
+sed -i '/172\.18\.208\.44/d' /etc/resolvconf/resolv.conf.d/base
+
+# Clear\drop cache's
+sync
+echo 3 > /proc/sys/vm/drop_caches
diff --git a/day01-image/scripts/motd.sh b/day01-image/scripts/motd.sh
new file mode 100644
index 0000000..40b008e
--- /dev/null
+++ b/day01-image/scripts/motd.sh
@@ -0,0 +1,17 @@
+#!/bin/bash -xe
+# Motd
+apt-get -y install update-motd
+rm -vf /etc/update-motd.d/*
+echo "BUILD_TIMESTAMP=$(date '+%Y-%m-%d-%H-%M-%S' -u)" > /etc/image_version
+echo "BUILD_TIMESTAMP_RFC=\"$(date -u -R)\"" >> /etc/image_version
+cat << 'EOF' >> /etc/update-motd.d/00-header-mirantis
+#!/bin/sh
+#
+#    00-header - create the header of the MOTD
+#
+[ -r /etc/image_version ] && . /etc/image_version
+echo "Ubuntu 16.04 \"Xenial\" Mirantis day01 cloud image"
+echo "Build date: ${BUILD_TIMESTAMP_RFC}"
+EOF
+chmod +x /etc/update-motd.d/00-header-mirantis
+
diff --git a/day01-image/scripts/network.sh b/day01-image/scripts/network.sh
new file mode 120000
index 0000000..ab1de37
--- /dev/null
+++ b/day01-image/scripts/network.sh
@@ -0,0 +1 @@
+../../common/ubuntu_network.sh
\ No newline at end of file
diff --git a/day01-image/scripts/reboot.sh b/day01-image/scripts/reboot.sh
new file mode 100644
index 0000000..52adc95
--- /dev/null
+++ b/day01-image/scripts/reboot.sh
@@ -0,0 +1,2 @@
+pgrep -f "sshd: root@" | xargs kill -9
+reboot now
\ No newline at end of file
diff --git a/day01-image/scripts/salt.sh b/day01-image/scripts/salt.sh
new file mode 100644
index 0000000..6e2dcd0
--- /dev/null
+++ b/day01-image/scripts/salt.sh
@@ -0,0 +1,43 @@
+#!/bin/bash -xe
+
+FORMULA_VERSION=${FORMULA_VERSION:-testing}
+APT_MIRANTIS_SALT_REPO=${APT_MIRANTIS_SALT_REPO:-"http://apt.mirantis.com/xenial/ $FORMULA_VERSION salt"}
+MASTER=`salt-key | grep cfg01`
+
+salt-key
+
+salt-call -t 5 --retcode-passthrough reclass.validate_pillar
+
+echo "deb [arch=amd64] ${APT_MIRANTIS_SALT_REPO}" > /etc/apt/sources.list.d/mcp_salt.list
+apt-get update
+apt-get install salt-formula* -y
+salt-call saltutil.refresh_pillar
+salt-call saltutil.sync_all
+salt-call -t 5 --retcode-passthrough --no-color state.sls salt
+salt-call -t 5 --retcode-passthrough --no-color state.sls linux.system.repo,linux.system.package,linux.system.directory,linux.system.config
+salt-call -t 5 --retcode-passthrough --no-color state.sls linux.network
+salt-call -t 5 --retcode-passthrough --no-color state.sls git.server
+salt-call -t 5 --retcode-passthrough --no-color state.sls postgresql
+salt-call -t 5 --no-color state.sls maas
+# will not be needed with new maas formula
+apt-get install simplestreams -y
+sstream-mirror --keyring=/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg http://images.maas.io/ephemeral-v3/daily/ /var/www/html/maas/images/ephemeral-v3/daily 'arch=amd64' 'release~(xenial)' --max=1
+
+salt-call -t 5 --retcode-passthrough --no-color state.sls jenkins.master
+sleep 60
+salt-call -t 5 --no-color state.sls jenkins.client.plugin
+systemctl restart jenkins
+sleep 60
+# Jenkins Mirantis theme
+git clone https://github.com/Mirantis/docker-jenkins.git
+cp -r docker-jenkins/theme /var/lib/jenkins/userContent
+chown -R jenkins:jenkins /var/lib/jenkins/userContent/*
+# finish Jenkins setup
+salt-call -t 5 --no-color state.sls jenkins.client.plugin
+systemctl restart jenkins
+sleep 60
+salt-call -t 5 --no-color state.sls jenkins.client
+systemctl restart jenkins
+sleep 60
+salt-call -t 5 --retcode-passthrough --no-color state.sls jenkins.client
+
diff --git a/day01-image/scripts/salt_bootstrap.sh b/day01-image/scripts/salt_bootstrap.sh
new file mode 100644
index 0000000..73622d0
--- /dev/null
+++ b/day01-image/scripts/salt_bootstrap.sh
@@ -0,0 +1,50 @@
+#!/bin/bash -xe
+CLUSTER_NAME=${CLUSTER_NAME:-mcp-day01}
+CLUSTER_MODEL=${CLUSTER_MODEL:-https://github.com/jiribroulik/day01.git}
+CLUSTER_MODEL_REF=${CLUSTER_MODEL_REF:-master}
+FORMULA_VERSION=${FORMULA_VERSION:-testing}
+SALTSTACK_GPG=${SALTSTACK_GPG:-"https://repo.saltstack.com/apt/ubuntu/16.04/amd64/2016.3/SALTSTACK-GPG-KEY.pub"}
+SALTSTACK_REPO=${SALTSTACK_REPO:-"http://repo.saltstack.com/apt/ubuntu/16.04/amd64/2016.3 xenial main"}
+APT_MIRANTIS_GPG=${APT_MIRANTIS_GPG:-"http://apt.mirantis.com/public.gpg"}
+APT_MIRANTIS_SALT_REPO=${APT_MIRANTIS_SALT_REPO:-"http://apt.mirantis.com/xenial/ $FORMULA_VERSION salt"}
+GIT_SALT_FORMULAS_SCRIPTS=${GIT_SALT_FORMULAS_SCRIPTS:-"https://github.com/salt-formulas/salt-formulas-scripts"}
+GIT_SALT_FORMULAS_SCRIPTS_REF=${GIT_SALT_FORMULAS_SCRIPTS_REF:-master}
+
+wget -O - ${SALTSTACK_GPG} | sudo apt-key add -
+wget -O - ${APT_MIRANTIS_GPG} | apt-key add -
+echo "deb [arch=amd64] ${SALTSTACK_REPO}"  > /etc/apt/sources.list.d/saltstack.list
+apt-get update
+apt-get install git-core -y
+
+for g_host in ${CLUSTER_MODEL} ${GIT_SALT_FORMULAS_SCRIPTS} ; do
+  _tmp_host=$(echo ${g_host} | awk -F/ '{print $3}')
+  ssh-keyscan -T 1 -H ${_tmp_host} >> ~/.ssh/known_hosts || true
+done
+
+if [[ ! -d /srv/salt/reclass ]]; then
+  git clone --recursive ${CLUSTER_MODEL} /srv/salt/reclass
+  pushd /srv/salt/reclass/
+    git checkout ${CLUSTER_MODEL_REF}
+  popd
+fi
+
+if [[ ! -d /srv/salt/scripts ]]; then
+  git clone --recursive ${GIT_SALT_FORMULAS_SCRIPTS} /srv/salt/scripts
+  pushd /srv/salt/scripts/
+    git checkout ${GIT_SALT_FORMULAS_SCRIPTS_REF}
+  popd
+fi
+
+# bootstrap.sh opts
+export FORMULAS_SOURCE=pkg
+export HOSTNAME=cfg01
+export DOMAIN="${CLUSTER_NAME}.local"
+export EXTRA_FORMULAS="git ntp maas jenkins"
+export APT_REPOSITORY=" deb [arch=amd64] ${APT_MIRANTIS_SALT_REPO} "
+export APT_REPOSITORY_GPG=${APT_MIRANTIS_GPG}
+
+if [[ ! -f /srv/salt/scripts/bootstrap.sh ]]; then
+  echo "ERROR: File /srv/salt/scripts/bootstrap.sh not found"
+  exit 1
+fi
+bash -x /srv/salt/scripts/bootstrap.sh || true
diff --git a/day01-image/scripts/security.sh b/day01-image/scripts/security.sh
new file mode 120000
index 0000000..3bd840a
--- /dev/null
+++ b/day01-image/scripts/security.sh
@@ -0,0 +1 @@
+../../common/ubuntu_security.sh
\ No newline at end of file