-#!/bin/bash -ex
+#!/bin/bash -e
 # shellcheck disable=SC2034,SC2154,SC1090,SC1091
 ##############################################################################
 # Copyright (c) 2017 Ericsson AB, Mirantis Inc., Enea AB and others.
 # BEGIN of Exit handlers
 #
 do_exit () {
+    local RC=$?
     clean
-    echo "Exiting ..."
+    if [ ${RC} -eq 0 ]; then
+        notify "\n[OK] MCP: Openstack installation finished succesfully!\n\n" 2
+    else
+        notify "\n[ERROR] MCP: Openstack installation threw a fatal error!\n\n"
+    fi
 }
 #
 # End of Exit handlers
 $(notify "USAGE:" 2)
   $(basename "$0") -b base-uri -l lab-name -p pod-name -s deploy-scenario \\
     [-B PXE Bridge [-B Mgmt Bridge [-B Internal Bridge [-B Public Bridge]]]] \\
-    [-S storage-dir] [-L /path/to/log/file.tar.gz] [-f] [-F] [-e] [-d]
+    [-S storage-dir] [-L /path/to/log/file.tar.gz] \\
+    [-f [-f]] [-F] [-e] [-d] [-D]
 
 $(notify "OPTIONS:" 2)
   -b  Base-uri for the stack-configuration structure
   -B  Bridge(s): 1st usage = PXE, 2nd = Mgmt, 3rd = Internal, 4th = Public
   -d  Dry-run
+  -D  Debug logging
   -e  Do not launch environment deployment
-  -f  Deploy on existing Salt master
+  -f  Deploy on existing Salt master (use twice to also skip config sync)
   -F  Do only create a Salt master
   -h  Print this message and exit
   -l  Lab-name
    while "mcpcontrol" is used to provision the infrastructure VMs only.
    The default is 'pxebr'.
 -d Dry-run - Produce deploy config files, but do not execute deploy
+-D Debug logging - Enable extra logging in sh deploy scripts (set -x)
 -e Do not launch environment deployment
--f Deploy on existing Salt master
+-f Deploy on existing Salt master. It will skip infrastructure VM creation,
+   but it will still sync reclass configuration from current repo to Salt
+   Master node. If specified twice (e.g. -f -f), config sync will also be
+   skipped.
 -F Do only create a Salt master
 -h Print this message and exit
 -L Deployment log path and name, eg. -L /home/jenkins/job.log.tar.gz
 ##############################################################################
 # BEGIN of variables to customize
 #
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
 REPO_ROOT_PATH=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")
 DEPLOY_DIR=$(cd "${REPO_ROOT_PATH}/mcp/scripts"; pwd)
 STORAGE_DIR=$(cd "${REPO_ROOT_PATH}/mcp/deploy/images"; pwd)
 #
 set +x
 OPNFV_BRIDGE_IDX=0
-while getopts "b:B:dfFl:L:p:s:S:he" OPTION
+while getopts "b:B:dDfFl:L:p:s:S:he" OPTION
 do
     case $OPTION in
         b)
                 if [ -n "${bridge}" ]; then
                     OPNFV_BRIDGES[${OPNFV_BRIDGE_IDX}]="${bridge}"
                 fi
-                OPNFV_BRIDGE_IDX=$((OPNFV_BRIDGE_IDX + 1))
+                ((OPNFV_BRIDGE_IDX+=1))
             done
             IFS=${OIFS}
             ;;
         d)
             DRY_RUN=1
             ;;
+        D)
+            CI_DEBUG=1
+            ;;
         f)
-            USE_EXISTING_INFRA=1
+            ((USE_EXISTING_INFRA+=1))
             ;;
         F)
             INFRA_CREATION_ONLY=1
     exit 1
 fi
 
-set -x
+[[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
 
 # Enable the automatic exit trap
 trap do_exit SIGINT SIGTERM EXIT
 # Check scenario file existence
 SCENARIO_DIR="../config/scenario"
 if [ ! -f  "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml" ]; then
-    notify "[WARN] ${DEPLOY_SCENARIO}.yaml not found! \
-            Setting simplest scenario (os-nosdn-nofeature-noha)\n" 3
+    notify "[WARN] ${DEPLOY_SCENARIO}.yaml not found!\n" 3
+    notify "[WARN] Setting simplest scenario (os-nosdn-nofeature-noha)\n" 3
     DEPLOY_SCENARIO='os-nosdn-nofeature-noha'
     if [ ! -f  "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml" ]; then
         notify "[ERROR] Scenario definition file is missing!\n" 1>&2
 eval "$(parse_yaml "${SCENARIO_DIR}/defaults-$(uname -i).yaml")"
 eval "$(parse_yaml "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml")"
 eval "$(parse_yaml "${LOCAL_PDF_RECLASS}")"
-set -x
+[[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
 
 export CLUSTER_DOMAIN=${cluster_domain}
 
 if [ ${DRY_RUN} -eq 1 ]; then
     notify "[NOTE] Dry run, skipping all deployment tasks\n" 2 1>&2
     exit 0
-elif [ ${USE_EXISTING_INFRA} -eq 1 ]; then
+elif [ ${USE_EXISTING_INFRA} -gt 0 ]; then
     notify "[NOTE] Use existing infra\n" 2 1>&2
     check_connection
 else
     update_mcpcontrol_network
     start_vms virtual_nodes
     check_connection
-    ./salt.sh "${LOCAL_PDF_RECLASS}"
+fi
+if [ ${USE_EXISTING_INFRA} -lt 2 ]; then
+    wait_for 5 "./salt.sh ${LOCAL_PDF_RECLASS}"
 fi
 
 # Openstack cluster setup
     for state in "${cluster_states[@]}"; do
         notify "[STATE] Applying state: ${state}\n" 2
         # shellcheck disable=SC2086,2029
-        ssh ${SSH_OPTS} "${SSH_SALT}" \
-            sudo "/root/fuel/mcp/config/states/${state} || true"
+        wait_for 5 "ssh ${SSH_OPTS} ${SSH_SALT} \
+            sudo /root/fuel/mcp/config/states/${state}"
     done
 fi
 
 
 popd > /dev/null
 
-notify "\n[DONE] MCP: Openstack installation finished succesfully!\n\n" 2
-
 #
 # END of main
 ##############################################################################
 
-#!/bin/bash -x
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 # shellcheck disable=SC1090
 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
 
 
-#!/bin/bash -x
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 # shellcheck disable=SC1090
 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
 
 
-#!/bin/bash -x
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 # Determine public network based on external IPs from compute node
 # NOTE: mask currently hardcoded to /24
 PUBLIC_NET=$(salt --out yaml 'cmp*' pillar.get _param:external_address | \
 
-#!/bin/bash -x
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 salt -I 'neutron:compute' state.sls neutron
 salt -I 'neutron:compute' file.append /etc/sudoers.d/neutron_sudoers \
   args='neutron ALL = (root) NOPASSWD: /usr/bin/neutron-rootwrap-daemon /etc/neutron/rootwrap.conf'
 
-#!/bin/bash -x
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 salt -I 'opendaylight:server' state.sls opendaylight
 salt -I 'opendaylight:client' state.sls opendaylight.client
 salt -I 'opendaylight:client' cmd.run 'neutron-odl-ovs-hostconfig --noovs_dpdk'
 
-#!/bin/bash -x
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 salt -I 'nfs:server' file.mkdir /srv/nova/instances
 salt -I 'nfs:server' state.sls nfs
 salt -I 'nfs:client' state.sls nfs
 
-#!/bin/bash -x
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 # shellcheck disable=SC1090
 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
 
 salt -I 'keystone:server' cmd.run ". /root/keystonercv3; openstack service list"
 
 salt -I 'glance:server' state.sls glance -b 1
-salt -I 'glusterfs:client' state.sls glusterfs.client
 salt -I 'keystone:server' state.sls keystone.server
 
 salt -I 'nova:controller' state.sls nova -b 1
 salt -I 'neutron:gateway' state.sls neutron.gateway
 
 salt -I 'nova:compute' state.sls nova
+salt -I 'glusterfs:client' state.sls glusterfs.client
 
 salt -I 'mongodb:server' state.sls mongodb
 wait_for 90 "salt -C 'I@mongodb:server and *01*' cmd.run 'mongo localhost:27017/admin'"
 
-#!/bin/bash -x
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 # shellcheck disable=SC1090
 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
 
 
 salt -C '* and not cfg01* and not mas01*' state.apply linux,ntp
 
-salt -C 'kvm*' state.sls libvirt
+wait_for 5 "salt -C 'kvm*' state.sls libvirt"
 
 salt -C '* and not cfg01* and not mas01*' state.apply salt
 salt -C 'kvm*' saltutil.sync_all
 
-#!/bin/bash -ex
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 if [ -r "$1" ]; then
   while IFS=': ' read -r p_dest p_file; do
-    if [[ "${p_dest}" =~ $2 ]]; then
-      patch -fd "${p_dest}" -p1 < "/root/fuel/mcp/patches/${p_file}"
+    if [[ ! "${p_dest}" =~ '^#' ]] && [[ "${p_dest}" =~ $2 ]] && \
+      ! patch --dry-run -Rd "${p_dest}" -r - -s -p1 < \
+        "/root/fuel/mcp/patches/${p_file}" > /dev/null; then
+          patch -fd "${p_dest}" -p1 < "/root/fuel/mcp/patches/${p_file}"
     fi
   done < "$1"
 fi
 
-#!/bin/bash
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc. and others.
 # All rights reserved. This program and the accompanying materials
 # (using the -k/--ssh-key option) and a user-data blog (using the
 # -u/--user-data option).
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+
 usage () {
        echo "usage: ${0##*/}: [--ssh-key <pubkey>] [--vendor-data <file>] [--user-data <file>] [--hostname <hostname>] <imagename>"
 }
 
-#!/bin/bash -ex
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Ericsson AB, Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 ##############################################################################
 
 # Global variables
+export CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
 export SSH_KEY=${SSH_KEY:-"/var/lib/opnfv/mcp.rsa"}
 export SALT_MASTER=${INSTALLER_IP:-10.20.0.2}
 export SALT_MASTER_USER=${SALT_MASTER_USER:-ubuntu}
 
-#!/bin/bash
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
   # shellcheck disable=SC2034
   for attempt in $(seq "${total_attempts}"); do
     # shellcheck disable=SC2015
-    eval "${cmdstr}" && break || true
+    eval "${cmdstr}" && return 0 || true
     echo -n '.'; sleep "${sleep_time}"
   done
+  return 1
 }
 
-#!/bin/bash
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # Collect /var/log from all cluster nodes via Salt Master
 #
 
+# shellcheck disable=SC1090
+[ -n "${SSH_OPTS}" ] || source "$(dirname "${BASH_SOURCE[0]}")/globals.sh"
+
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
 DEPLOY_LOG=$1
 OPNFV_TMP_LOG="opnfv_fuel_logs"
 
 [ -n "${DEPLOY_LOG}" ] || exit 0
-# shellcheck disable=SC1090
-[ -n "${SSH_OPTS}" ] || source "$(dirname "${BASH_SOURCE[0]}")/globals.sh"
 
 # ssh to cfg01
 # shellcheck disable=SC2086,2087
 
-#!/bin/bash
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
 # All rights reserved. This program and the accompanying materials
 # Deploy Salt Master
 #
 
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
 F_GIT_ROOT=$(git rev-parse --show-toplevel)
 OPNFV_TMP_DIR="/home/${SALT_MASTER_USER}/fuel"
 OPNFV_FUEL_DIR="/root/fuel"
   echo ' done'
 
   mkdir -p /srv/salt /usr/share/salt-formulas/reclass
+  rm -rf ${OPNFV_FUEL_DIR}
   mv ${OPNFV_TMP_DIR} ${OPNFV_FUEL_DIR} && chown -R root.root ${OPNFV_FUEL_DIR}
-  ln -s ${OPNFV_FUEL_DIR}/mcp/reclass /srv/salt/reclass
-  ln -s ${OPNFV_FUEL_DIR}/mcp/deploy/scripts /srv/salt/scripts
-  cd /srv/salt/${OPNFV_RDIR} && ln -s "\$(uname -i)" arch
+  ln -sf ${OPNFV_FUEL_DIR}/mcp/reclass /srv/salt
+  ln -sf ${OPNFV_FUEL_DIR}/mcp/deploy/scripts /srv/salt
+  cd /srv/salt/${OPNFV_RDIR} && rm -f arch && ln -sf "\$(uname -i)" arch
 
   cp -r ${OPNFV_FUEL_DIR}/mcp/metadata/service /usr/share/salt-formulas/reclass
   cd /srv/salt/reclass/classes/service && \
-    ln -s /usr/share/salt-formulas/reclass/service/opendaylight
+    ln -sf /usr/share/salt-formulas/reclass/service/opendaylight
   cd ${OPNFV_FUEL_DIR}/mcp/patches && ./patch.sh patches.list reclass
 
   cd /srv/salt/scripts