Make aio.sh preserve host's ssh keys if possible 34/89634/4
authorKonrad Bańka <k.banka@samsung.com>
Mon, 10 Jun 2019 12:03:45 +0000 (14:03 +0200)
committerKonrad Bańka <k.banka@samsung.com>
Fri, 21 Jun 2019 09:22:37 +0000 (11:22 +0200)
aio.sh script creates rsa key that is also added to host's authorized_keys.
Currently, existing id_rsa keys are removed in order to generate fresh key each time.

This change makes setup script reuse existing key when possible.

Issue-ID: MULTICLOUD-676

Change-Id: I3b6b837f64dabd3cb6a14aa5ea65e67c6b8db6a8
Signed-off-by: Konrad Bańka <k.banka@samsung.com>
kud/hosting_providers/baremetal/aio.sh

index c1aac6a..c9fac09 100755 (executable)
@@ -18,7 +18,7 @@ cd ${aio_dir}/../vagrant
 # For aio inventory by default get ovn central ip from local host default interface.
 # This variable used only in this file, but env variable defined to enable user to override it prior calling aio.sh.
 OVN_CENTRAL_IP_ADDRESS=${OVN_CENTRAL_IP_ADDRESS:-$(hostname -I | cut -d ' ' -f 1)}
-
+echo "Preparing inventory for ansible"
 cat <<EOL > inventory/hosts.ini
 [all]
 localhost ansible_ssh_host=${OVN_CENTRAL_IP_ADDRESS} ansible_ssh_port=22
@@ -46,9 +46,14 @@ kube-node
 kube-master
 EOL
 
-rm -f ~/.ssh/id_rsa
-echo -e "\n\n\n" | ssh-keygen -t rsa -N ""
-cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
+if ! [ -f ~/.ssh/id_rsa ]; then
+        echo "Generating rsa key for this host"
+        ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa <&-
+fi
+if ! grep -qF "$(ssh-keygen -y -f ~/.ssh/id_rsa)" ~/.ssh/authorized_keys; then
+        echo "Allowing present ~/.ssh/id_rsa key to be used for login to this host"
+        ssh-keygen -y -f ~/.ssh/id_rsa >> ~/.ssh/authorized_keys
+fi
 chmod og-wx ~/.ssh/authorized_keys
 
 echo "Enabling nested-virtualization"