blob: 91bdde255dec162d823818095dbd03685447f6e0 [file] [log] [blame]
Zack Williamsa57ea8c2017-08-28 22:49:37 -07001#!/usr/bin/env bash
2
3# Copyright 2017-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# vagrant-ssh-install.sh
18# Checks to see if vagrant SSH key configuration is installed.
19
20set -e -u -o pipefail
21
22VAGRANT_SSH_CONFIG="$1"
23
24SSH_INCLUDE="Include ${VAGRANT_SSH_CONFIG}"
25SSH_WILDCARD="$2"
26
27USER_SSH_DIR="${HOME}/.ssh"
28USER_SSH_CONFIG="$USER_SSH_DIR/config"
29
30# check if we have a new enough version of SSH to deal with "Include" directive
31# per: https://www.openssh.com/txt/release-7.3
32if `ssh -V 2>&1 | perl -ne '/OpenSSH_([\d\.]{3})/ && \$1 >= 7.3 ? exit 0 : exit 1'`
33then
34 # ssh is >= 7.3, supports "Include"
35 if [ -e $USER_SSH_CONFIG ]
36 then
37 if grep -F "$SSH_WILDCARD" $USER_SSH_CONFIG
38 then
39 echo "SSH configured to import Vagrant SSH config, done!"
40 else
41 echo "SSH not configured to import Vagrant SSH config."
42 echo "Please add this line to the *TOP* your $USER_SSH_CONFIG file:"
43 echo ""
44 echo "$SSH_WILDCARD"
45 echo ""
46 echo "Then reattempt the build."
47 exit 1
48 fi
49 else
50 echo "User SSH config file doesn't exist at $USER_SSH_CONFIG"
51 echo "Creating a minimal $USER_SSH_CONFIG file that imports $SSH_WILDCARD"
52 mkdir -p "$USER_SSH_DIR"
53 echo "$SSH_WILDCARD" > $USER_SSH_CONFIG
54 echo "Done!"
55 fi
56else
57 # ssh is < 7.3, doesn't support "Include"
58 if [ -e $USER_SSH_CONFIG ]
59 then
60 echo "User SSH config file exists at $USER_SSH_CONFIG"
61 echo "SSH is an older than 7.3, unable to Include ssh config file with Vagrant config."
62 if cmp -s "$VAGRANT_SSH_CONFIG" "$USER_SSH_CONFIG"
63 then
64 echo "Contents of $VAGRANT_SSH_CONFIG and $USER_SSH_CONFIG are identical. Done!"
65 else
66 echo "Add the contents of $VAGRANT_SSH_CONFIG to $USER_SSH_CONFIG manually,"
67 echo "replacing any previous similar entries, then reattempt the build."
68 exit 1
69 fi
70 else
71 echo "User SSH config file doesn't exist at $USER_SSH_CONFIG"
72 echo "SSH is an older than 7.3, unable to Include Vagrant config,"
73 echo "so copying $VAGRANT_SSH_CONFIG to $USER_SSH_CONFIG"
74 mkdir -p "$USER_SSH_DIR"
75 cp $VAGRANT_SSH_CONFIG $USER_SSH_CONFIG
76 echo "Done!"
77 fi
78fi
79