misc updates

  * Wait for salt-master and salt-minion to wake up after restart
  * fix aptly publisher config path
  * refactor cleanup
  * remove multiverse from ubuntu_base.
    It was never packed into offline - so, should be safe to remove.

Change-Id: Ic5bd8d8085af5db10d52063bff50be41031ef248
diff --git a/common/ubuntu_base.sh b/common/ubuntu_base.sh
index 336662b..10f42c0 100644
--- a/common/ubuntu_base.sh
+++ b/common/ubuntu_base.sh
@@ -1,6 +1,7 @@
 #!/bin/bash -xe
 
-if [ -f /tmp/done_ubuntu_base ] ; then
+# Don't use /tmp/ - some templates do node reboot
+if [ -f /done_ubuntu_base ] ; then
   echo "INFO: ubuntu_base already finished.Skipping.."
   exit 0
 fi
@@ -27,10 +28,10 @@
 
 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 "deb [arch=amd64] ${UBUNTU_BASEURL} xenial main restricted universe" > /etc/apt/sources.list
+echo "deb [arch=amd64] ${UBUNTU_BASEURL} xenial-updates main restricted universe" >> /etc/apt/sources.list
+echo "deb [arch=amd64] ${UBUNTU_BASEURL} xenial-security main restricted universe" >> /etc/apt/sources.list
+#echo "deb [arch=amd64] ${UBUNTU_BASEURL} xenial-backports main restricted universe" >> /etc/apt/sources.list
 
 apt-get clean
 apt-get update
@@ -77,4 +78,4 @@
   systemctl stop ${s} || true
 done
 
-touch /tmp/done_ubuntu_base
+touch /done_ubuntu_base
diff --git a/common/ubuntu_cleanup.sh b/common/ubuntu_cleanup.sh
index 4eb9c7f..919f64a 100644
--- a/common/ubuntu_cleanup.sh
+++ b/common/ubuntu_cleanup.sh
@@ -2,9 +2,11 @@
 
 apt-get -y autoremove --purge
 apt-get -y clean
-rm -rf /var/lib/apt/lists/*
 
-rm -f /usr/sbin/policy-rc.d || true
+rm -rf /var/lib/apt/lists/*
+rm -rv /etc/apt/sources.list.d/* || true
+echo > /etc/apt/sources.list  || true
+rm -vf /usr/sbin/policy-rc.d || true
 
 echo "cleaning up hostname"
 sed -i "/.*ubuntu.*/d" /etc/hosts
@@ -15,19 +17,23 @@
 
 echo "cleaning up dhcp leases"
 rm -rf /var/lib/dhcp/* || true
-rm -rf /var/lib/ntp/ntp.conf.dhcp || true
+rm -rfv /var/lib/ntp/ntp.conf.dhcp || true
 
 echo "cleaning up udev rules"
-rm -f /etc/udev/rules.d/70-persistent-net.rules || true
+rm -fv /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
+rm -fv /lib/udev/rules.d/75-persistent-net-generator.rules || true
 
 echo "cleaning up minion_id for salt"
-rm -f /etc/salt/minion_id || true
+rm -vf /etc/salt/minion_id || true
 
 echo "cleaning up resolvconf"
 sed -i '/172\.18\.208\.44/d' /etc/resolvconf/resolv.conf.d/base
 
+# Remove flags
+rm -v /done_ubuntu_base || true
+rm -v /done_ubuntu_salt_bootstrap || true
+
 # Clear\drop cache's
 sync
 echo 3 > /proc/sys/vm/drop_caches
diff --git a/common/ubuntu_cleanup_salt.sh b/common/ubuntu_cleanup_salt.sh
new file mode 100644
index 0000000..4e77783
--- /dev/null
+++ b/common/ubuntu_cleanup_salt.sh
@@ -0,0 +1,28 @@
+#!/bin/bash -xe
+
+apt-get purge salt-formula-* -y
+
+echo "removing all previously accepted salt keys"
+salt-key -D -y || true
+
+echo "cleaning up reclass"
+rm -rf /srv/salt/reclass || true
+rm -rf /srv/salt/scripts || true
+rm -rf /usr/share/salt-formulas/env || true
+#
+mkdir -p /srv/salt/reclass/
+mkdir -p /usr/share/salt-formulas/reclass/service/
+mkdir -p /usr/share/salt-formulas/env/
+
+# stop and disable services, for healthy zerodisk
+# They should be enabled after VCP init
+stop_services="salt-api salt-master salt-minion"
+for s in ${stop_services} ; do
+  systemctl stop ${s} || true
+# Enable this, after refactoring salt:control:virtng
+#  systemctl disable ${s} || true
+done
+
+# Clear\drop cache's
+sync
+echo 3 > /proc/sys/vm/drop_caches
diff --git a/common/ubuntu_salt_bootstrap.sh b/common/ubuntu_salt_bootstrap.sh
index 0b45762..a5a04f3 100644
--- a/common/ubuntu_salt_bootstrap.sh
+++ b/common/ubuntu_salt_bootstrap.sh
@@ -1,6 +1,6 @@
 #!/bin/bash -xe
 
-if [ -f '/tmp/done_ubuntu_salt_bootstrap' ]; then
+if [ -f '/done_ubuntu_salt_bootstrap' ]; then
   echo "INFO: ubuntu_salt_bootstrap already finished! Skipping.."
   exit 0
 fi
@@ -62,4 +62,4 @@
   exit 1
 fi
 bash -x /srv/salt/scripts/bootstrap.sh || true
-touch /tmp/done_ubuntu_salt_bootstrap
+touch /done_ubuntu_salt_bootstrap
diff --git a/day01-image/scripts/cleanup.sh b/day01-image/scripts/cleanup.sh
deleted file mode 100644
index 0f5adec..0000000
--- a/day01-image/scripts/cleanup.sh
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/bin/bash -xe
-
-apt-get purge salt-formula-* -y
-apt-get -y autoremove --purge
-apt-get -y clean
-
-rm -rf /var/lib/ntp/ntp.conf.dhcp || true
-rm -rf /var/lib/apt/lists/*
-rm -rv /etc/apt/sources.list.d/* || true
-echo > /etc/apt/sources.list  || true
-rm -vf /usr/sbin/policy-rc.d || true
-
-
-echo "removeing all previously accepted salt keys"
-salt-key -D -y || true
-
-echo "cleaning up reclass"
-rm -rf /srv/salt/reclass || true
-rm -rf /srv/salt/scripts || true
-rm -rf /usr/share/salt-formulas/env || true
-#
-mkdir -p /srv/salt/reclass/
-mkdir -p /usr/share/salt-formulas/reclass/service/
-mkdir -p /usr/share/salt-formulas/env/
-
-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
-
-# stop and disable services, for healthy zerodisk
-# They should be enabled after cfg01 init
-stop_services="postgresql.service salt-api salt-master salt-minion jenkins maas-rackd.service maas-regiond.service bind9"
-for s in ${stop_services} ; do
-  systemctl stop ${s} || true
-  systemctl disable ${s} || true
-done
-
-echo 3 > /proc/sys/vm/drop_caches
-sync
diff --git a/day01-image/scripts/cleanup.sh b/day01-image/scripts/cleanup.sh
new file mode 120000
index 0000000..cf970b6
--- /dev/null
+++ b/day01-image/scripts/cleanup.sh
@@ -0,0 +1 @@
+../../common/ubuntu_cleanup.sh
\ No newline at end of file
diff --git a/day01-image/scripts/cleanup_day01.sh b/day01-image/scripts/cleanup_day01.sh
new file mode 100644
index 0000000..89cd6e7
--- /dev/null
+++ b/day01-image/scripts/cleanup_day01.sh
@@ -0,0 +1,12 @@
+#!/bin/bash -xe
+
+# stop and disable services, for healthy zerodisk
+# They should be enabled after cfg01 init
+stop_services="postgresql.service salt-api salt-master salt-minion jenkins maas-rackd.service maas-regiond.service bind9"
+for s in ${stop_services} ; do
+  systemctl stop ${s} || true
+  systemctl disable ${s} || true
+done
+
+echo 3 > /proc/sys/vm/drop_caches
+sync
diff --git a/day01-image/scripts/cleanup_salt.sh b/day01-image/scripts/cleanup_salt.sh
new file mode 120000
index 0000000..72d6f4e
--- /dev/null
+++ b/day01-image/scripts/cleanup_salt.sh
@@ -0,0 +1 @@
+../../common/ubuntu_cleanup_salt.sh
\ No newline at end of file
diff --git a/day01-image/template.json b/day01-image/template.json
index c4ff8de..cfaa73f 100644
--- a/day01-image/template.json
+++ b/day01-image/template.json
@@ -87,6 +87,8 @@
       "scripts": [
         "scripts/security.sh",
         "scripts/cleanup.sh",
+        "scripts/cleanup_salt.sh",
+        "scripts/cleanup_day01.sh",
         "scripts/info.sh",
         "scripts/zerodisk.sh"
       ]
@@ -135,7 +137,7 @@
       "ssh_password": "{{user `password`}}",
       "ssh_host_port_min": 7000,
       "ssh_host_port_max": 7050,
-      "shutdown_command": "shutdown -P now",
+      "shutdown_command": "pgrep -f 'sshd: root@' | xargs kill -9 ; shutdown -P now",
       "boot_wait": "2s",
       "ssh_wait_timeout": "360s"
     }
diff --git a/mirror-image/run.example.sh b/mirror-image/run.example.sh
old mode 100644
new mode 100755
index cab6393..eea8081
--- a/mirror-image/run.example.sh
+++ b/mirror-image/run.example.sh
@@ -12,6 +12,7 @@
 # http/bootstrap.saltstack.com.sh https://github.com/saltstack/salt-bootstrap
 #
 
+export IMAGE_NAME="mcp-offline-mirror-$(date '+%Y-%m-%d-%H-%M-%S')"
 export PACKER_IMAGES_CACHE="${HOME}/packer_images_cache/"
 #
 export CLUSTER_MODEL="https://github.com/Mirantis/mcp-offline-model.git"
diff --git a/mirror-image/scripts/cleanup_salt.sh b/mirror-image/scripts/cleanup_salt.sh
new file mode 120000
index 0000000..72d6f4e
--- /dev/null
+++ b/mirror-image/scripts/cleanup_salt.sh
@@ -0,0 +1 @@
+../../common/ubuntu_cleanup_salt.sh
\ No newline at end of file
diff --git a/mirror-image/scripts/mirrors.sh b/mirror-image/scripts/mirrors.sh
index 764597a..8e4bcb3 100644
--- a/mirror-image/scripts/mirrors.sh
+++ b/mirror-image/scripts/mirrors.sh
@@ -9,7 +9,7 @@
 salt-call ${SALT_OPTS} state.sls aptly.publisher
 salt-call ${SALT_OPTS} state.sls aptly || salt-call ${SALT_OPTS} state.sls aptly
 aptly_mirror_update.sh -sv
-aptly-publisher --url http://10.99.0.1:18084 --timeout=1200 publish -v -d -c /etc/aptly-publisher.yaml --architectures amd64 --recreate --force-overwrite
+aptly-publisher --url http://10.99.0.1:18084 --timeout=1200 publish -v -d -c /etc/aptly/publisher.yaml --architectures amd64 --recreate --force-overwrite
 
 #docker registry
 salt-call ${SALT_OPTS} state.sls docker.client.registry
diff --git a/mirror-image/scripts/salt.sh b/mirror-image/scripts/salt.sh
index 9655ec0..ea3d147 100644
--- a/mirror-image/scripts/salt.sh
+++ b/mirror-image/scripts/salt.sh
@@ -1,10 +1,11 @@
 #!/bin/bash -xe
 
+# Wait for salt-master and salt-minion to wake up after restart
+# since we just perform 'reboot.sh'
+salt-call --timeout=120 test.ping
+
 SALT_OPTS="-l debug -t 10 --retcode-passthrough --no-color"
 
-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
 
diff --git a/mirror-image/template.json b/mirror-image/template.json
index 5b942d0..71706f9 100644
--- a/mirror-image/template.json
+++ b/mirror-image/template.json
@@ -2,7 +2,7 @@
   "variables": {
     "user": "root",
     "password": "r00tme",
-    "disk_size": "100000",
+    "disk_size": "700000",
     "images_cache": "{{ env `PACKER_IMAGES_CACHE` }}",
     "do_api_token": "{{ env `DO_API_TOKEN` }}",
     "image_name": "{{ env `IMAGE_NAME` }}",
@@ -79,7 +79,7 @@
       "environment_vars": [
         "APT_MIRANTIS_SALT_REPO={{ user `apt_mirantis_salt_repo` }}"
       ],
-      "pause_before": "120s",
+      "pause_before": "60s",
       "type": "shell",
       "expect_disconnect": "true",
       "scripts": [
@@ -92,12 +92,13 @@
         "MCP_VERSION={{ user `mcp_version` }}"
       ],
       "type": "shell",
-      "pause_before": "120s",
+      "pause_before": "60s",
       "scripts": [
         "scripts/mirrors.sh",
         "scripts/security.sh",
         "scripts/info.sh",
         "scripts/cleanup.sh",
+        "scripts/cleanup_salt.sh",
         "scripts/zerodisk.sh"
       ]
     },
@@ -161,7 +162,7 @@
       "headless": true,
       "ssh_username": "{{user `user`}}",
       "ssh_password": "{{user `password`}}",
-      "shutdown_command": "sync ; shutdown -P now",
+      "shutdown_command": "pgrep -f 'sshd: root@' | xargs kill -9 ; shutdown -P now",
       "vnc_bind_address": "0.0.0.0",
       "vnc_port_max": "5956",
       "vnc_port_min": "5956",
diff --git a/ubuntu-16.04/template.json b/ubuntu-16.04/template.json
index 284237e..e36e506 100644
--- a/ubuntu-16.04/template.json
+++ b/ubuntu-16.04/template.json
@@ -176,7 +176,7 @@
       "ssh_password": "{{user `password`}}",
       "ssh_port": 22,
       "ssh_wait_timeout": "10000s",
-      "shutdown_command": "shutdown -P now",
+      "shutdown_command": "pgrep -f 'sshd: root@' | xargs kill -9 ; shutdown -P now",
       "headless": true,
       "vboxmanage": [
         [
@@ -253,7 +253,7 @@
       "ssh_password": "{{user `password`}}",
       "ssh_port": 22,
       "ssh_wait_timeout": "10000s",
-      "shutdown_command": "shutdown -P now",
+      "shutdown_command": "pgrep -f 'sshd: root@' | xargs kill -9 ; shutdown -P now",
       "vmx_data": {
         "memsize": "1024",
         "numvcpus": "1",
@@ -295,7 +295,7 @@
       "ssh_password": "{{user `password`}}",
       "ssh_host_port_min": 7000,
       "ssh_host_port_max": 7050,
-      "shutdown_command": "shutdown -P now",
+      "shutdown_command": "pgrep -f 'sshd: root@' | xargs kill -9 ; shutdown -P now",
       "boot_wait": "2s",
       "ssh_wait_timeout": "360s"
     },