Add proper qemu-caching for source image

  * Require command at slaves:
    'mkdir -p "${HOME}/packer_images_cache/"'
    since packer not able to do it.
  * set SALT_STOPSTART_WAIT=10 for salt bootstrap
  * Dd iptables to base installation

Change-Id: Icc5a50a21e848b93660aa80483b6fdd3e27de243
diff --git a/common/ubuntu_base.sh b/common/ubuntu_base.sh
index 6f314e9..da6c81d 100644
--- a/common/ubuntu_base.sh
+++ b/common/ubuntu_base.sh
@@ -32,13 +32,16 @@
 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
+EXTRA_PKGS="byobu curl ethtool iputils-ping lsof strace tcpdump traceroute wget iptables"
+# Pretty tools
+EXTRA_PKGS="${EXTRA_PKGS} byobu htop tmux tree vim-nox mc"
+# Common prerequisites
+EXTRA_PKGS="${EXTRA_PKGS} apt-transport-https libmnl0 python-apt python-m2crypto python-psutil acpid"
+apt-get -y install ${EXTRA_PKGS}
 
 # 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 purge -y linux-image-* linux-headers-* | grep -v 'is not installed, so not removed'
     apt-get install -y linux-image-virtual-lts-xenial linux-image-extra-virtual-lts-xenial
 
     # Update grub cmdline
diff --git a/common/ubuntu_salt_bootstrap.sh b/common/ubuntu_salt_bootstrap.sh
index ad3757e..daa0bca 100644
--- a/common/ubuntu_salt_bootstrap.sh
+++ b/common/ubuntu_salt_bootstrap.sh
@@ -43,6 +43,7 @@
 export EXTRA_FORMULAS=${EXTRA_FORMULAS:-"ntp aptly nginx iptables docker git maas jenkins"}
 export APT_REPOSITORY=" deb [arch=amd64] ${APT_MIRANTIS_SALT_REPO} "
 export APT_REPOSITORY_GPG=${APT_MIRANTIS_GPG}
+export SALT_STOPSTART_WAIT=${SALT_STOPSTART_WAIT:-10}
 if [[ ! -z "${PACKER_OFFLINE_BUILD}" ]];then
   echo "INFO: build in offline detected!"
   export BOOTSTRAP_SALTSTACK_COM="file:///tmp/bootstrap.saltstack.com.sh"
diff --git a/day01-image/run.example.sh b/day01-image/run.example.sh
index aeee71e..25efd13 100755
--- a/day01-image/run.example.sh
+++ b/day01-image/run.example.sh
@@ -21,14 +21,17 @@
 export FORMULA_VERSION=proposed
 export BINARY_MCP_VERSION=proposed
 export UBUNTU_BASEURL="http://mirror.mirantis.com/proposed/ubuntu/"
-export SALTSTACK_REPO="http://mirror.mirantis.com/proposed/saltstack-2016.3/xenial xenial main"
+export SALTSTACK_REPO="http://mirror.mirantis.com/proposed/saltstack-2017.7/xenial xenial main"
 export APT_MIRANTIS_GPG="http://apt.mirantis.com/public.gpg"
-export SALTSTACK_GPG="https://repo.saltstack.com/apt/ubuntu/16.04/amd64/2016.3/SALTSTACK-GPG-KEY.pub"
+export SALTSTACK_GPG="https://repo.saltstack.com/apt/ubuntu/16.04/amd64/2017.7/SALTSTACK-GPG-KEY.pub"
 export APT_MIRANTIS_SALT_REPO="http://apt.mirantis.com/xenial/ proposed salt"
 export GIT_SALT_FORMULAS_SCRIPTS=https://github.com/salt-formulas/salt-formulas-scripts.git
 export APT_REPOSITORY="deb [arch=amd64] http://apt.mirantis.com/xenial/ proposed salt"
 export APT_REPOSITORY_GPG=http://apt.mirantis.com/public.gpg
 ###
+# Hard-coded folder in template
+export PACKER_IMAGES_CACHE="${HOME}/packer_images_cache/"
+mkdir -p "${PACKER_IMAGES_CACHE}"
 
 export PACKER_LOG=1
 # For qemu test-build:
diff --git a/day01-image/scripts/cleanup.sh b/day01-image/scripts/cleanup.sh
index 2b5ccbc..9f745f9 100644
--- a/day01-image/scripts/cleanup.sh
+++ b/day01-image/scripts/cleanup.sh
@@ -42,7 +42,7 @@
 
 # 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"
+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
diff --git a/day01-image/scripts/salt.sh b/day01-image/scripts/salt.sh
index 78cdd79..a49b274 100644
--- a/day01-image/scripts/salt.sh
+++ b/day01-image/scripts/salt.sh
@@ -17,6 +17,7 @@
 salt-call ${SALT_OPTS} state.sls linux.network
 salt-call ${SALT_OPTS} state.sls openssh
 salt-call ${SALT_OPTS} state.sls git.server
+sleep 99h || true
 salt-call ${SALT_OPTS} state.sls postgresql
 # install only MAAS packages, don't configure anything.
 # Configuration will be processed during cfg01 initialization.
diff --git a/day01-image/template.json b/day01-image/template.json
index 1406680..c4ff8de 100644
--- a/day01-image/template.json
+++ b/day01-image/template.json
@@ -2,6 +2,8 @@
   "variables": {
     "user": "root",
     "password": "r00tme",
+    "images_cache": "{{ env `PACKER_IMAGES_CACHE` }}",
+    "disk_size": "50000",
     "do_api_token": "{{ env `DO_API_TOKEN` }}",
     "image_name": "{{ env `IMAGE_NAME` }}",
     "cluster_model": "{{ env `CLUSTER_MODEL` }}",
@@ -17,8 +19,7 @@
     "apt_mirantis_salt_repo": "{{ env `APT_MIRANTIS_SALT_REPO` }}",
     "git_salt_formulas_scripts": "{{ env `GIT_SALT_FORMULAS_SCRIPTS` }}",
     "apt_repository": "{{ env `APT_REPOSITORY` }}",
-    "apt_repository_gpg": "{{ env `APT_REPOSITORY_GPG` }}",
-    "disk_size": "50000"
+    "apt_repository_gpg": "{{ env `APT_REPOSITORY_GPG` }}"
 
   },
   "provisioners": [
@@ -125,6 +126,7 @@
       "iso_checksum": "566efef1d6f12e7d3a994c2405bdb642",
       "iso_checksum_type": "md5",
       "iso_url": "http://cloud-images.ubuntu.com/releases/xenial/release-20180306/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
+      "iso_target_path": "{{ user `images_cache`}}/release-20180306_xenial-server-cloudimg-amd64-disk1.img",
       "disk_image": true,
       "accelerator": "kvm",
       "disk_size": "{{ user `disk_size`}}",
diff --git a/day01-image/template.json.env b/day01-image/template.json.env
new file mode 100644
index 0000000..fb05cc3
--- /dev/null
+++ b/day01-image/template.json.env
@@ -0,0 +1 @@
+PACKER_IMAGES_CACHE="${HOME}/packer_images_cache/"
diff --git a/mirror-image/run.example.sh b/mirror-image/run.example.sh
index 3f7328c..f1161ac 100644
--- a/mirror-image/run.example.sh
+++ b/mirror-image/run.example.sh
@@ -12,6 +12,8 @@
 # http/bootstrap.saltstack.com.sh https://github.com/saltstack/salt-bootstrap
 #
 
+export PACKER_IMAGES_CACHE="${HOME}/packer_images_cache/"
+#
 export CLUSTER_MODEL="https://github.com/Mirantis/mcp-offline-model.git"
 export CLUSTER_MODEL_REF="master"
 export CLUSTER_NAME="mcp-offline"
@@ -32,20 +34,20 @@
 export APT_REPOSITORY_GPG=${APT_MIRANTIS_GPG}
 
 # Openstack-related variables
-export VM_FLAVOR="3c4fe514-3eb6-464d-a419-7be09d72cf1e"
-export VM_FLOATING_IP_POOL="095f43ed-65ba-48ce-8185-d4080e430a88"
-export IMAGE_NAME="mcp-offline-mirror-$(date '+%Y-%m-%d-%H-%M-%S')"
-export VM_IMAGE="9953a40d-1fa2-4f26-b146-0507dbc912dc" # ubuntu cloud img
+#export VM_FLAVOR="3c4fe514-3eb6-464d-a419-7be09d72cf1e"
+#export VM_FLOATING_IP_POOL="095f43ed-65ba-48ce-8185-d4080e430a88"
+#export IMAGE_NAME="mcp-offline-mirror-$(date '+%Y-%m-%d-%H-%M-%S')"
+#export VM_IMAGE="9953a40d-1fa2-4f26-b146-0507dbc912dc" # ubuntu cloud img
 # May be need, if cloud with keystone v3 used
 #export OS_TENANT_ID=$OS_PROJECT_ID
 #export OS_DOMAIN_NAME="xxxx"
 #export OS_TENANT_NAME="xxxxx"
 
+# Hard-coded folder in template
+mkdir -p "${PACKER_IMAGES_CACHE}"
+
 export PACKER_LOG=1
 # For qemu test-build:
-#cloud-localds  --hostname ubuntu --dsmode local config-drive/cloudata.iso  config-drive/user-data.yaml
-# packer build -only=qemu -parallel=false -on-error=ask template.json
+cloud-localds  --hostname ubuntu --dsmode local config-drive/cloudata.iso  config-drive/user-data.yaml
+packer build -only=qemu -parallel=false -on-error=ask template.json
 # rm -rf ~/.packer.d/
-
-# For openstack test-build
-packer build -only=openstack -parallel=false -on-error=ask template.json
diff --git a/mirror-image/template.json b/mirror-image/template.json
index 5abc542..48045c1 100644
--- a/mirror-image/template.json
+++ b/mirror-image/template.json
@@ -3,6 +3,7 @@
     "user": "root",
     "password": "r00tme",
     "disk_size": "100000",
+    "images_cache": "{{ env `PACKER_IMAGES_CACHE` }}",
     "do_api_token": "{{ env `DO_API_TOKEN` }}",
     "image_name": "{{ env `IMAGE_NAME` }}",
     "cluster_model": "{{ env `CLUSTER_MODEL` }}",
@@ -151,7 +152,7 @@
       "accelerator": "kvm",
       "disk_size": "{{ user `disk_size`}}",
       "iso_url": "http://cloud-images.ubuntu.com/releases/xenial/release-20180306/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
-      "iso_target_path": "/root/isos/xenial-server-cloudimg-amd64-disk1.img",
+      "iso_target_path": "{{ user `images_cache`}}/release-20180306_xenial-server-cloudimg-amd64-disk1.img",
       "iso_checksum_type": "md5",
       "disk_image": true,
       "disk_compression": true,
diff --git a/ubuntu-16.04/run.example.sh b/ubuntu-16.04/run.example.sh
new file mode 100644
index 0000000..df58f50
--- /dev/null
+++ b/ubuntu-16.04/run.example.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+# pre-requirments:
+# apt-get install cloud-localds
+# Cloudimg: wget https://cloud-images.ubuntu.com/xenial/XXXX/xenial-server-cloudimg-amd64-disk1.img
+# Packer: https://releases.hashicorp.com/packer/1.1.3/packer_1.1.3_linux_amd64.zip
+
+# Those script - only example for variables, which should be passed to packer and
+# overwrite variables under /scripts/ directory
+
+# External script sources:
+# http/bootstrap.saltstack.com.sh https://github.com/saltstack/salt-bootstrap
+#
+export IMAGE_NAME="ubuntu-16-04-x64"
+export UBUNTU_BASEURL="http://mirror.mirantis.com/proposed/ubuntu/"
+export SALTSTACK_REPO="deb [arch=amd64] http://mirror.mirantis.com/proposed/saltstack-2017.7/xenial xenial main"
+export SALTSTACK_GPG="https://repo.saltstack.com/apt/ubuntu/16.04/amd64/2017.7/SALTSTACK-GPG-KEY.pub"
+###
+# Hard-coded folder in template
+export PACKER_IMAGES_CACHE="${HOME}/packer_images_cache/"
+mkdir -p "${PACKER_IMAGES_CACHE}"
+
+export PACKER_LOG=1
+# For qemu test-build:
+cloud-localds  --hostname ubuntu --dsmode local config-drive/cloudata.iso  config-drive/user-data.yaml
+packer build -only=qemu -parallel=false -on-error=ask template.json
+#rm -rf ~/.packer.d/
+
diff --git a/ubuntu-16.04/template.json b/ubuntu-16.04/template.json
index 84e1a59..284237e 100644
--- a/ubuntu-16.04/template.json
+++ b/ubuntu-16.04/template.json
@@ -4,6 +4,7 @@
     "password": "ho5uo7Uome5d",
     "distro": "ubuntu-16-04-x64",
     "disk_size": "8000",
+    "images_cache": "{{ env `PACKER_IMAGES_CACHE` }}",
     "ubuntu_baseurl": "https://mirror.mirantis.com/proposed/ubuntu/",
     "saltstack_gpg": "{{ env `SALTSTACK_GPG` }}",
     "saltstack_repo": "{{ env `SALTSTACK_REPO` }}",
@@ -284,6 +285,7 @@
       "iso_checksum": "566efef1d6f12e7d3a994c2405bdb642",
       "iso_checksum_type": "md5",
       "iso_url": "http://cloud-images.ubuntu.com/releases/xenial/release-20180306/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
+      "iso_target_path": "{{ user `images_cache`}}/release-20180306_xenial-server-cloudimg-amd64-disk1.img",
       "disk_image": true,
       "disk_compression": true,
       "accelerator": "kvm",
diff --git a/ubuntu-16.04/template.json.env b/ubuntu-16.04/template.json.env
index 12be33e..ed6351e 100644
--- a/ubuntu-16.04/template.json.env
+++ b/ubuntu-16.04/template.json.env
@@ -1,5 +1,7 @@
 # Env variables and description:
-DO_API_TOKEN
+#DO_API_TOKEN
+PACKER_IMAGES_CACHE="${HOME}/packer_images_cache/"
+
 
 # base.sh
 UBUNTU_BASEURL=http://mirror.mirantis.com/stable/ubuntu/