Update of Packer builder for Vagrant and various clouds

Change-Id: I71d3a6fb21ce2b3c54993072f42749a5869fae96
Signed-off-by: ramonsalguer <javier.ramon@telefonica.com>
diff --git a/packer/packer_templates/_common/check_vagrant_user.sh b/packer/packer_templates/_common/check_vagrant_user.sh
new file mode 100644
index 0000000..bd832a0
--- /dev/null
+++ b/packer/packer_templates/_common/check_vagrant_user.sh
@@ -0,0 +1,26 @@
+#!/bin/bash -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+# This script is intended for VIMs-type builders such as OpenStack, which
+# use cloud images where the `vagrant` user does not exist by default
+
+USER=vagrant
+USER_NAME=vagrant
+PASSWORD=vagrant
+
+# If the `vagrant` user does not exist, it is created
+(id ${USER} >/dev/null 2>&1) || adduser --disabled-password --gecos "${USER_NAME}" "${USER}"
+
+# Comment if no password should be set
+echo "${USER}:${PASSWORD}" | sudo chpasswd
diff --git a/packer/packer_templates/_common/metadata.sh b/packer/packer_templates/_common/metadata.sh
new file mode 100644
index 0000000..4721aec
--- /dev/null
+++ b/packer/packer_templates/_common/metadata.sh
@@ -0,0 +1,18 @@
+#!/bin/sh -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+mkdir -p /etc;
+cp /tmp/bento-metadata.json /etc/bento-metadata.json;
+chmod 0444 /etc/bento-metadata.json;
+rm -f /tmp/bento-metadata.json;
diff --git a/packer/packer_templates/_common/minimize.sh b/packer/packer_templates/_common/minimize.sh
new file mode 100644
index 0000000..0b0e476
--- /dev/null
+++ b/packer/packer_templates/_common/minimize.sh
@@ -0,0 +1,48 @@
+#!/bin/sh -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+case "$PACKER_BUILDER_TYPE" in
+  qemu) exit 0 ;;
+esac
+
+# Whiteout root
+count=$(df --sync -kP / | tail -n1  | awk -F ' ' '{print $4}')
+count=$(($count-1))
+dd if=/dev/zero of=/tmp/whitespace bs=1M count=$count || echo "dd exit code $? is suppressed";
+rm /tmp/whitespace
+
+# Whiteout /boot
+count=$(df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}')
+count=$(($count-1))
+dd if=/dev/zero of=/boot/whitespace bs=1M count=$count || echo "dd exit code $? is suppressed";
+rm /boot/whitespace
+
+set +e
+swapuuid="`/sbin/blkid -o value -l -s UUID -t TYPE=swap`";
+case "$?" in
+    2|0) ;;
+    *) exit 1 ;;
+esac
+set -e
+
+if [ "x${swapuuid}" != "x" ]; then
+    # Whiteout the swap partition to reduce box size
+    # Swap is disabled till reboot
+    swappart="`readlink -f /dev/disk/by-uuid/$swapuuid`";
+    /sbin/swapoff "$swappart";
+    dd if=/dev/zero of="$swappart" bs=1M || echo "dd exit code $? is suppressed";
+    /sbin/mkswap -U "$swapuuid" "$swappart";
+fi
+
+sync;
diff --git a/packer/packer_templates/_common/motd.sh b/packer/packer_templates/_common/motd.sh
new file mode 100644
index 0000000..72016da
--- /dev/null
+++ b/packer/packer_templates/_common/motd.sh
@@ -0,0 +1,32 @@
+#!/bin/sh -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+my_custom_motd='
+This system was built by Telefónica I+D for ETSI Open Source MANO (ETSI OSM)'
+
+if [ -d /etc/update-motd.d ]; then
+    MOTD_CONFIG='/etc/update-motd.d/99-custom'
+
+    cat >> "$MOTD_CONFIG" <<FINAL
+#!/bin/sh
+
+cat <<'EOF'
+$my_custom_motd
+EOF
+FINAL
+
+    chmod 0755 "$MOTD_CONFIG"
+else
+    echo "$my_custom_motd" >> /etc/motd
+fi
diff --git a/packer/packer_templates/_common/parallels.sh b/packer/packer_templates/_common/parallels.sh
new file mode 100644
index 0000000..3010df1
--- /dev/null
+++ b/packer/packer_templates/_common/parallels.sh
@@ -0,0 +1,47 @@
+#!/bin/sh -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+# set a default HOME_DIR environment variable if not set
+HOME_DIR="${HOME_DIR:-/home/vagrant}";
+
+case "$PACKER_BUILDER_TYPE" in
+parallels-iso|parallels-pvm)
+    mkdir -p /tmp/parallels;
+    mount -o loop $HOME_DIR/prl-tools-lin.iso /tmp/parallels;
+    VER="`cat /tmp/parallels/version`";
+
+    echo "Parallels Tools Version: $VER";
+
+    /tmp/parallels/install --install-unattended-with-deps \
+      || (code="$?"; \
+          echo "Parallels tools installation exited $code, attempting" \
+          "to output /var/log/parallels-tools-install.log"; \
+          cat /var/log/parallels-tools-install.log; \
+          exit $code);
+    umount /tmp/parallels;
+    rm -rf /tmp/parallels;
+    rm -f $HOME_DIR/*.iso;
+
+    # Parallels Tools for Linux includes native auto-mount script,
+    # which causes losing some of Vagrant-relative shared folders.
+    # So, we should disable this behavior.
+    # https://github.com/Parallels/vagrant-parallels/issues/325#issuecomment-418727113
+    auto_mount_script='/usr/bin/prlfsmountd'
+    if [ -f "${auto_mount_script}" ]; then
+        echo -e '#!/bin/sh\n'\
+        '# Shared folders auto-mount is disabled by Vagrant ' \
+        > "${auto_mount_script}"
+    fi
+    ;;
+esac
diff --git a/packer/packer_templates/_common/sshd.sh b/packer/packer_templates/_common/sshd.sh
new file mode 100644
index 0000000..2c37906
--- /dev/null
+++ b/packer/packer_templates/_common/sshd.sh
@@ -0,0 +1,32 @@
+#!/bin/sh -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+SSHD_CONFIG="/etc/ssh/sshd_config"
+
+# ensure that there is a trailing newline before attempting to concatenate
+sed -i -e '$a\' "$SSHD_CONFIG"
+
+USEDNS="UseDNS no"
+if grep -q -E "^[[:space:]]*UseDNS" "$SSHD_CONFIG"; then
+    sed -i "s/^\s*UseDNS.*/${USEDNS}/" "$SSHD_CONFIG"
+else
+    echo "$USEDNS" >>"$SSHD_CONFIG"
+fi
+
+GSSAPI="GSSAPIAuthentication no"
+if grep -q -E "^[[:space:]]*GSSAPIAuthentication" "$SSHD_CONFIG"; then
+    sed -i "s/^\s*GSSAPIAuthentication.*/${GSSAPI}/" "$SSHD_CONFIG"
+else
+    echo "$GSSAPI" >>"$SSHD_CONFIG"
+fi
diff --git a/packer/packer_templates/_common/vagrant.sh b/packer/packer_templates/_common/vagrant.sh
new file mode 100644
index 0000000..36b533a
--- /dev/null
+++ b/packer/packer_templates/_common/vagrant.sh
@@ -0,0 +1,31 @@
+#!/bin/sh -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+# set a default HOME_DIR environment variable if not set
+HOME_DIR="${HOME_DIR:-/home/vagrant}";
+
+pubkey_url="https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub";
+mkdir -p $HOME_DIR/.ssh;
+if command -v wget >/dev/null 2>&1; then
+    wget --no-check-certificate "$pubkey_url" -O $HOME_DIR/.ssh/authorized_keys;
+elif command -v curl >/dev/null 2>&1; then
+    curl --insecure --location "$pubkey_url" > $HOME_DIR/.ssh/authorized_keys;
+elif command -v fetch >/dev/null 2>&1; then
+    fetch -am -o $HOME_DIR/.ssh/authorized_keys "$pubkey_url";
+else
+    echo "Cannot download vagrant public key";
+    exit 1;
+fi
+chown -R vagrant $HOME_DIR/.ssh;
+chmod -R go-rwsx $HOME_DIR/.ssh;
diff --git a/packer/packer_templates/_common/virtualbox.sh b/packer/packer_templates/_common/virtualbox.sh
new file mode 100644
index 0000000..943ea56
--- /dev/null
+++ b/packer/packer_templates/_common/virtualbox.sh
@@ -0,0 +1,31 @@
+#!/bin/sh -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+# set a default HOME_DIR environment variable if not set
+HOME_DIR="${HOME_DIR:-/home/vagrant}";
+
+case "$PACKER_BUILDER_TYPE" in
+virtualbox-iso|virtualbox-ovf)
+    VER="`cat $HOME_DIR/.vbox_version`";
+    ISO="VBoxGuestAdditions_$VER.iso";
+    mkdir -p /tmp/vbox;
+    mount -o loop $HOME_DIR/$ISO /tmp/vbox;
+    sh /tmp/vbox/VBoxLinuxAdditions.run \
+        || echo "VBoxLinuxAdditions.run exited $? and is suppressed." \
+            "For more read https://www.virtualbox.org/ticket/12479";
+    umount /tmp/vbox;
+    rm -rf /tmp/vbox;
+    rm -f $HOME_DIR/*.iso;
+    ;;
+esac
diff --git a/packer/packer_templates/_common/vmware.sh b/packer/packer_templates/_common/vmware.sh
new file mode 100644
index 0000000..2fc524c
--- /dev/null
+++ b/packer/packer_templates/_common/vmware.sh
@@ -0,0 +1,46 @@
+#!/bin/sh -eux
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+# set a default HOME_DIR environment variable if not set
+HOME_DIR="${HOME_DIR:-/home/vagrant}";
+
+case "$PACKER_BUILDER_TYPE" in
+vmware-iso|vmware-vmx)
+
+    # make sure we have /sbin in our path. RHEL systems lack this
+    PATH=/sbin:$PATH
+    export PATH
+
+    mkdir -p /tmp/vmware;
+    mkdir -p /tmp/vmware-archive;
+    mount -o loop $HOME_DIR/linux.iso /tmp/vmware;
+
+    TOOLS_PATH="`ls /tmp/vmware/VMwareTools-*.tar.gz`";
+    VER="`echo "${TOOLS_PATH}" | cut -f2 -d'-'`";
+    MAJ_VER="`echo ${VER} | cut -d '.' -f 1`";
+
+    echo "VMware Tools Version: $VER";
+
+    tar xzf ${TOOLS_PATH} -C /tmp/vmware-archive;
+    if [ "${MAJ_VER}" -lt "10" ]; then
+        /tmp/vmware-archive/vmware-tools-distrib/vmware-install.pl --default;
+    else
+        /tmp/vmware-archive/vmware-tools-distrib/vmware-install.pl --force-install;
+    fi
+    umount /tmp/vmware;
+    rm -rf  /tmp/vmware;
+    rm -rf  /tmp/vmware-archive;
+    rm -f $HOME_DIR/*.iso;
+    ;;
+esac