Update mirror-image

  * Allign to offline-build example

Change-Id: I96448007b75ef98dd8f658b0be14c0b56c8117ba
diff --git a/mirror-image/scripts/base.sh b/mirror-image/scripts/base.sh
index a070f10..1487f42 100644
--- a/mirror-image/scripts/base.sh
+++ b/mirror-image/scripts/base.sh
@@ -1,16 +1,21 @@
 #!/bin/bash -xe
+
+UBUNTU_BASEURL="${UBUNTU_BASEURL:-"mirror://mirrors.ubuntu.com/mirrors.txt"}"
+
 ## Base packages and setup
 export DEBIAN_FRONTEND=noninteractive
 echo "exit 101" > /usr/sbin/policy-rc.d
 chmod +x /usr/sbin/policy-rc.d
 
 echo "Acquire::CompressionTypes::Order gz;" >/etc/apt/apt.conf.d/99compression-workaround-salt
+echo "Acquire::EnableSrvRecords false;" >/etc/apt/apt.conf.d/99enablesrvrecords-false
 
+sysctl -w fs.file-max=100000
 # Overwrite default mirrors
-echo "deb mirror://mirrors.ubuntu.com/mirrors.txt xenial main restricted multiverse universe" > /etc/apt/sources.list
-echo "deb mirror://mirrors.ubuntu.com/mirrors.txt xenial-updates main restricted multiverse universe" >> /etc/apt/sources.list
-echo "deb mirror://mirrors.ubuntu.com/mirrors.txt xenial-security main restricted multiverse universe" >> /etc/apt/sources.list
-echo "deb mirror://mirrors.ubuntu.com/mirrors.txt xenial-backports main restricted multiverse universe" >> /etc/apt/sources.list
+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
 
 apt-get clean
 apt-get update
@@ -21,12 +26,9 @@
 apt-get -y install apt-transport-https libmnl0 python-apt python-m2crypto python-psutil
 
 # Cleanup old kernels, ensure latest is installed via virtual package
-apt-get purge -y linux-image-* linux-headers-*
 if [ ! -f /tmp/no_install_kernel ]; then
-    # Use HWE kernel
-    ## Temporary disable latest hwe due to: https://bugs.launchpad.net/ubuntu/+source/linux-hwe-edge/+bug/1679823
-    #apt-get install -y linux-image-generic-hwe-16.04
-    apt-get install -y linux-image-4.8.0-41-generic linux-image-extra-4.8.0-41-generic
+    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
diff --git a/mirror-image/scripts/mirrors.sh b/mirror-image/scripts/mirrors.sh
index 6cb2ced..9dd7599 100644
--- a/mirror-image/scripts/mirrors.sh
+++ b/mirror-image/scripts/mirrors.sh
@@ -1,22 +1,15 @@
 #!/bin/bash -xe
 #docker registry
 salt-call -t 5 --retcode-passthrough --no-color state.sls docker.host
-docker run --restart always -d -p 5000:5000 --name registry registry:2
+#docker run --restart always -d -p 5000:5000 --name registry registry:2
 salt-call -t 5 --retcode-passthrough state.sls docker.client.registry
 docker system prune --all --force
 
 #aptly
 salt-call -t 5 --no-color state.sls aptly
-salt-call -t 5 --retcode-passthrough --no-color state.sls aptly
+salt-call -t 5 --retcode-passthrough --no-color state.sls aptly.server
 sudo -i -u aptly aptly_mirror_update.sh -sv
 sudo -i -u aptly aptly_publish_update.sh -acrfv
 
-#debmirror
-/srv/scripts/debmirror.sh
-
 #git
 salt-call -t 5 --retcode-passthrough --no-color state.sls git.server
-
-#pypi
-#pip install pip2pi
-#pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt
\ No newline at end of file
diff --git a/mirror-image/scripts/salt.sh b/mirror-image/scripts/salt.sh
index 276a83d..9c8a837 100644
--- a/mirror-image/scripts/salt.sh
+++ b/mirror-image/scripts/salt.sh
@@ -1,21 +1,14 @@
 #!/bin/bash -xe
-wget -O - https://repo.saltstack.com/apt/ubuntu/16.04/amd64/2016.3/SALTSTACK-GPG-KEY.pub | sudo apt-key add -
-wget -O - http://apt.mirantis.com/public.gpg | apt-key add -
-echo "deb http://repo.saltstack.com/apt/ubuntu/16.04/amd64/2016.3 xenial main" >/etc/apt/sources.list.d/saltstack.list
+
+echo "deb [arch=amd64] ${APT_MIRANTIS_SALT_REPO}" > /etc/apt/sources.list.d/mcp_salt.list
 apt-get update
-apt-get install git -y
-git clone --recursive -b $CLUSTER_MODEL_REF $CLUSTER_MODEL /srv/salt/reclass
-git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts
-export FORMULAS_SOURCE=pkg
-export HOSTNAME=apt01
-export DOMAIN=$CLUSTER_NAME.local
-export EXTRA_FORMULAS="ntp aptly nginx iptables docker"
-/srv/salt/scripts/bootstrap.sh
-echo "deb [arch=amd64] http://apt.mirantis.com/xenial/ ${FORMULA_VERSION} salt" > /etc/apt/sources.list.d/mcp_salt.list
 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.directory,linux.system.package,linux.system.file
+salt-call -t 5 --retcode-passthrough --no-color state.sls linux.system.repo,linux.system.directory,linux.system.package
+salt-call -t 5 --retcode-passthrough --no-color state.sls linux.system.file
+salt-call -t 5 --retcode-passthrough --no-color state.sls docker.host
+salt-call -t 5 --retcode-passthrough --no-color state.sls docker exclude=docker.client.registry
 salt-call -t 5 --retcode-passthrough --no-color state.sls linux.network
 salt-call -t 5 --retcode-passthrough --no-color state.sls nginx
diff --git a/mirror-image/scripts/salt_bootstrap.sh b/mirror-image/scripts/salt_bootstrap.sh
new file mode 100644
index 0000000..9732123
--- /dev/null
+++ b/mirror-image/scripts/salt_bootstrap.sh
@@ -0,0 +1,35 @@
+#!/bin/bash -xe
+
+# CLUSTER_MODEL_REF=
+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"}
+
+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
+
+git clone --recursive -b ${CLUSTER_MODEL_REF} ${CLUSTER_MODEL} /srv/salt/reclass || true
+git clone ${GIT_SALT_FORMULAS_SCRIPTS} /srv/salt/scripts || true
+# bootstrap.sh opts
+export FORMULAS_SOURCE=pkg
+export HOSTNAME=apt01
+export DOMAIN="${CLUSTER_NAME}.local"
+export EXTRA_FORMULAS="ntp aptly nginx iptables docker"
+export APT_REPOSITORY=" deb [arch=amd64] ${APT_MIRANTIS_SALT_REPO} "
+export APT_REPOSITORY_GPG=${APT_MIRANTIS_GPG}
+if [[ ! -z "${PACKER_OFFLINE_BUILD}" ]];then
+  echo "offline detected!"
+  export BOOTSTRAP_SALTSTACK_COM="http://${PACKER_HTTP_ADDR}/bootstrap.saltstack.com.sh"
+  export BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dXr $BOOTSTRAP_SALTSTACK_VERSION }
+fi
+#
+bash -x /srv/salt/scripts/bootstrap.sh || true