[CORD-1811]
Create a separate Vagrant SSH config file, check if it's included in
~/.ssh/config, rather than overwriting.

Add clean-openstack target

Change-Id: Ifcaf78c40ccb77ad2231cb2013cfdf540ffba365
diff --git a/Makefile b/Makefile
index f20bd84..fe2775e 100644
--- a/Makefile
+++ b/Makefile
@@ -24,7 +24,7 @@
 M                ?= $(BUILD)/milestones
 LOGS             ?= $(BUILD)/logs
 
-PREP_MS          ?= $(M)/prereqs-check $(M)/build-local-bootstrap $(M)/ciab-ovs $(M)/vagrant-up $(M)/copy-cord $(M)/cord-config $(M)/copy-config $(M)/prep-buildnode $(M)/prep-headnode $(M)/deploy-elasticstack $(M)/prep-computenode
+PREP_MS          ?= $(M)/prereqs-check $(M)/build-local-bootstrap $(M)/ciab-ovs $(M)/vagrant-up $(M)/vagrant-ssh-install $(M)/copy-cord $(M)/cord-config $(M)/copy-config $(M)/prep-buildnode $(M)/prep-headnode $(M)/deploy-elasticstack $(M)/prep-computenode
 MAAS_MS          ?= $(M)/build-maas-images $(M)/maas-prime $(M)/publish-maas-images $(M)/deploy-maas
 OPENSTACK_MS     ?= $(M)/glance-images $(M)/deploy-openstack  $(M)/deploy-computenode $(M)/onboard-openstack
 XOS_MS           ?= $(M)/docker-images $(M)/core-image $(M)/publish-docker-images $(M)/start-xos $(M)/onboard-profile
@@ -58,7 +58,7 @@
 VAGRANT_VMS      ?= $(HEADNODE)
 VAGRANT_SWITCHES ?= leaf1
 VAGRANT_CWD      ?= $(SCENARIOS_D)/$(SCENARIO)/
-SSH_CONFIG       ?= ~/.ssh/config  # Vagrant modifies this, should it always?
+VAGRANT_SSH_CONF ?= $(GENCONFIG_D)/vagrant.ssh_config
 
 # Virsh config
 VIRSH_CORDDEV_DOMAIN ?= cord_corddev
@@ -82,39 +82,33 @@
 .DEFAULT: help
 
 help:
-	@echo "Please specify a target (config, build, teardown, ...)"
+	@echo "Please specify a target (config, build, ...)"
 
 # Config file generation
 config: $(CONFIG_FILES)
+	@echo ""
+	@echo "CORD is configured with profile: '$(PROFILE)', scenario: '$(SCENARIO)'"
+	@echo "Run 'make -j4 build' to continue."
 
 $(CONFIG_FILES):
 	test -e "$(PODCONFIG_PATH)" || { echo "PODCONFIG file $(PODCONFIG_PATH) doesn't exist!" ; exit 1; }
 	ansible-playbook -i 'localhost,' --extra-vars="cord_podconfig='$(PODCONFIG_PATH)' genconfig_dir='$(GENCONFIG_D)' scenarios_dir='$(SCENARIOS_D)'" $(BUILD)/ansible/genconfig.yml $(LOGCMD)
 
 printconfig:
-	@echo "Scenario: $(SCENARIO)"
-	@echo "Profile: $(PROFILE)"
+	@echo "Scenario: '$(SCENARIO)'"
+	@echo "Profile: '$(PROFILE)'"
 
 # Primary Targets
-# Many of these targets use target-specific variables
-# https://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html
-
 build: $(BUILD_TARGETS)
 
-# Utility targets
 
+# Utility targets
 ansible-ping:
 	$(ANSIBLE) -m ping all $(LOGCMD)
 
 ansible-setup:
 	$(ANSIBLE) -m setup all $(LOGCMD)
 
-collect-diag:
-	$(ANSIBLE_PB) $(PI)/collect-diag-playbook.yml $(LOGCMD)
-
-compute-node-refresh:
-	$(SSH_HEAD) "cd /opt/cord/build; $(ANSIBLE_PB_MAAS) --private-key ~/.ssh/cord_rsa $(PI)/compute-node-refresh-playbook.yml" $(LOGCMD)
-
 clean-images:
 	rm -f $(M)/docker-images $(M)/local-docker-images $(M)/core-image $(M)/local-core-image $(M)/build-maas-images $(M)/build-onos-apps $(M)/publish-maas-images $(M)/publish-docker-images $(M)/publish-onos-apps
 
@@ -131,6 +125,20 @@
 clean-local: clean-profile clean-genconfig
 	rm -f $(LOCAL_MILESTONES)
 
+clean-onos:
+	$(ANSIBLE_PB) $(PI)/teardown-onos.yml $(LOGCMD)
+	rm -f $(M)/deploy-onos $(M)/onos-debug
+
+clean-openstack:
+	$(SSH_HEAD) "/opt/cord/build/platform-install/scripts/clean_openstack.sh" $(LOGCMD)
+	rm -f $(M)/onboard-openstack
+
+collect-diag:
+	$(ANSIBLE_PB) $(PI)/collect-diag-playbook.yml $(LOGCMD)
+
+compute-node-refresh:
+	$(SSH_HEAD) "cd /opt/cord/build; $(ANSIBLE_PB_MAAS) --private-key ~/.ssh/cord_rsa $(PI)/compute-node-refresh-playbook.yml" $(LOGCMD)
+
 .PHONY: docs
 docs:
 	cd docs; make
@@ -140,7 +148,7 @@
 
 vagrant-destroy:
 	$(VAGRANT) destroy -f $(LOGCMD)
-	rm -f $(M)/vagrant-up
+	rm -f $(M)/vagrant-up $(M)/vagrant-ssh-install $(VAGRANT_SSH_CONF)
 
 virsh-domain-destroy:
 	virsh destroy ${VIRSH_CORDDEV_DOMAIN} || true
@@ -186,19 +194,22 @@
 
 $(M)/vagrant-up: | $(VAGRANT_UP_PREREQS)
 	$(VAGRANT) up $(VAGRANT_VMS) --provider $(VAGRANT_PROVIDER) $(LOGCMD)
-	@echo "Configuring SSH for VM's..."
-	$(VAGRANT) ssh-config $(VAGRANT_VMS) > $(SSH_CONFIG)
+	touch $@
+
+$(M)/vagrant-ssh-install: | $(M)/vagrant-up
+	$(VAGRANT) ssh-config $(VAGRANT_VMS) > $(VAGRANT_SSH_CONF) $(LOGCMD)
+	$(BUILD)/scripts/vagrant-ssh-install.sh "$(VAGRANT_SSH_CONF)" "Include $(abspath $(GENCONFIG_D))/*.ssh_config" $(LOGCMD)
 	touch $@
 
 $(M)/config-ssh-key: | $(M)/vagrant-up
 	$(ANSIBLE_PB) $(BUILD)/ansible/config-ssh-key.yml $(LOGCMD)
 	touch $@
 
-$(M)/copy-cord: | $(M)/vagrant-up $(COPY_CORD_PREREQS)
+$(M)/copy-cord: | $(M)/vagrant-ssh-install $(COPY_CORD_PREREQS)
 	$(ANSIBLE_PB) $(PI)/copy-cord-playbook.yml $(LOGCMD)
 	touch $@
 
-$(M)/cord-config: | $(M)/vagrant-up $(CORD_CONFIG_PREREQS)
+$(M)/cord-config: | $(M)/vagrant-ssh-install $(CORD_CONFIG_PREREQS)
 	$(ANSIBLE_PB) $(PI)/cord-config-playbook.yml $(LOGCMD)
 	cp -r $(GENCONFIG_D) $(CONFIG_CORD_PROFILE_DIR)/genconfig
 	touch $@
@@ -207,13 +218,13 @@
 	$(ANSIBLE_PB) $(PI)/copy-profile-playbook.yml $(LOGCMD)
 	touch $@
 
-$(M)/prep-buildnode: | $(M)/vagrant-up $(M)/cord-config $(PREP_BUILDNODE_PREREQS)
+$(M)/prep-buildnode: | $(M)/vagrant-ssh-install $(M)/cord-config $(PREP_BUILDNODE_PREREQS)
 	$(ANSIBLE_PB) $(PI)/prep-buildnode-playbook.yml $(LOGCMD)
 	@echo Waiting 20 seconds to timeout SSH ControlPersist, and so future ansible commands gain docker group membership
 	sleep 20
 	touch $@
 
-$(M)/prep-headnode: | $(M)/vagrant-up $(M)/cord-config $(PREP_HEADNODE_PREREQS)
+$(M)/prep-headnode: | $(M)/vagrant-ssh-install $(M)/cord-config $(PREP_HEADNODE_PREREQS)
 	$(ANSIBLE_PB) $(PI)/prep-headnode-playbook.yml $(LOGCMD)
 	touch $@
 
@@ -261,6 +272,10 @@
 	$(ANSIBLE_PB) $(PI)/deploy-onos-playbook.yml $(LOGCMD)
 	touch $@
 
+$(M)/onos-debug: | $(M)/onboard-profile
+	$(SSH_HEAD) "cd /opt/cord/build; $(ANSIBLE_PB_LOCAL) $(PI)/onos-debug-playbook.yml" $(LOGCMD)
+	touch $@
+
 
 # XOS targets
 $(M)/docker-images: | $(M)/prep-buildnode $(DOCKER_IMAGES_PREREQS)
@@ -298,10 +313,6 @@
 	$(ANSIBLE_PB) $(PI)/deploy-computenode-playbook.yml $(LOGCMD)
 	touch $@
 
-$(M)/onos-debug: | $(M)/onboard-profile
-	$(SSH_HEAD) "cd /opt/cord/build; $(ANSIBLE_PB_LOCAL) $(PI)/onos-debug-playbook.yml" $(LOGCMD)
-	touch $@
-
 $(M)/onboard-openstack: | $(M)/deploy-computenode $(M)/glance-images $(M)/deploy-onos $(M)/onboard-profile
 	$(SSH_HEAD) "cd /opt/cord/build; $(ANSIBLE_PB_LOCAL) $(PI)/onboard-openstack-playbook.yml" $(LOGCMD)
 	touch $@
@@ -335,6 +346,7 @@
 fabric-pingtest: $(M)/refresh-fabric
 	$(SSH_HEAD) "cd /opt/cord/build; $(ANSIBLE_PB_MAAS) $(PI)/cord-fabric-pingtest.yml" $(LOGCMD)
 
+
 # Local Targets, bring up XOS containers without a VM
 $(M)/local-cord-config:
 	$(ANSIBLE_PB) $(PI)/cord-config-playbook.yml $(LOGCMD)
diff --git a/ansible.cfg b/ansible.cfg
index 073b1f5..0902052 100644
--- a/ansible.cfg
+++ b/ansible.cfg
@@ -5,5 +5,5 @@
 timeout=30
 
 [ssh_connection]
-ssh_args = -o ControlMaster=auto -o ControlPersist=15s
+ssh_args = -o ControlMaster=auto -o ControlPersist=15s -o ConnectionAttempts=20
 
diff --git a/scripts/cord-bootstrap.sh b/scripts/cord-bootstrap.sh
index ebcff0e..9ddc21c 100755
--- a/scripts/cord-bootstrap.sh
+++ b/scripts/cord-bootstrap.sh
@@ -17,7 +17,7 @@
 # cord-bootstrap.sh
 # Bootstraps a dev system for CORD, downloads source
 
-set -e -u
+set -e -u -o pipefail
 
 # start time, used to name logfiles
 START_T=$(date -u "+%Y%m%d%H%M%SZ")
diff --git a/scripts/vagrant-ssh-install.sh b/scripts/vagrant-ssh-install.sh
new file mode 100755
index 0000000..91bdde2
--- /dev/null
+++ b/scripts/vagrant-ssh-install.sh
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+# vagrant-ssh-install.sh
+# Checks to see if vagrant SSH key configuration is installed.
+
+set -e -u -o pipefail
+
+VAGRANT_SSH_CONFIG="$1"
+
+SSH_INCLUDE="Include ${VAGRANT_SSH_CONFIG}"
+SSH_WILDCARD="$2"
+
+USER_SSH_DIR="${HOME}/.ssh"
+USER_SSH_CONFIG="$USER_SSH_DIR/config"
+
+# check if we have a new enough version of SSH to deal with "Include" directive
+# per: https://www.openssh.com/txt/release-7.3
+if `ssh -V 2>&1 | perl -ne '/OpenSSH_([\d\.]{3})/ && \$1 >= 7.3 ? exit 0 : exit 1'`
+then
+  # ssh is >= 7.3, supports "Include"
+  if [ -e $USER_SSH_CONFIG ]
+  then
+    if grep -F "$SSH_WILDCARD" $USER_SSH_CONFIG
+    then
+      echo "SSH configured to import Vagrant SSH config, done!"
+    else
+      echo "SSH not configured to import Vagrant SSH config."
+      echo "Please add this line to the *TOP* your $USER_SSH_CONFIG file:"
+      echo ""
+      echo "$SSH_WILDCARD"
+      echo ""
+      echo "Then reattempt the build."
+      exit 1
+    fi
+  else
+    echo "User SSH config file doesn't exist at $USER_SSH_CONFIG"
+    echo "Creating a minimal $USER_SSH_CONFIG file that imports $SSH_WILDCARD"
+    mkdir -p "$USER_SSH_DIR"
+    echo "$SSH_WILDCARD" > $USER_SSH_CONFIG
+    echo "Done!"
+  fi
+else
+  # ssh is < 7.3, doesn't support "Include"
+  if [ -e $USER_SSH_CONFIG ]
+  then
+    echo "User SSH config file exists at $USER_SSH_CONFIG"
+    echo "SSH is an older than 7.3, unable to Include ssh config file with Vagrant config."
+    if cmp -s "$VAGRANT_SSH_CONFIG" "$USER_SSH_CONFIG"
+    then
+      echo "Contents of $VAGRANT_SSH_CONFIG and $USER_SSH_CONFIG are identical. Done!"
+    else
+      echo "Add the contents of $VAGRANT_SSH_CONFIG to $USER_SSH_CONFIG manually,"
+      echo "replacing any previous similar entries, then reattempt the build."
+      exit 1
+    fi
+  else
+    echo "User SSH config file doesn't exist at $USER_SSH_CONFIG"
+    echo "SSH is an older than 7.3, unable to Include Vagrant config,"
+    echo "so copying $VAGRANT_SSH_CONFIG to $USER_SSH_CONFIG"
+    mkdir -p "$USER_SSH_DIR"
+    cp $VAGRANT_SSH_CONFIG $USER_SSH_CONFIG
+    echo "Done!"
+  fi
+fi
+