manila-image-elements/elements/manila-ssh/install.d/fetch-public-ssh-keys.sh
Igor Malinovskiy 36ff48082d Update fetch-public-ssh-keys.sh
Increase timeout and exit on error.
2015-06-05 13:56:29 +03:00

41 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
set -x
MANILA_USER="manila"
MANILA_USER_HOME="/home/$MANILA_USER"
MANILA_USER_SSH_DIR="$MANILA_USER_HOME/.ssh"
if [ ! -d $MANILA_USER_SSH_DIR ]; then
mkdir -p $MANILA_USER_SSH_DIR
chmod 700 $MANILA_USER_SSH_DIR
chown -R $MANILA_USER $MANILA_USER_SSH_DIR
fi
# Fetch public key using HTTP
ATTEMPTS=60 # Wait up to 5 minutes
FAILED=0
while [ ! -f $MANILA_USER_SSH_DIR/authorized_keys ]; do
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/metadata-key 2>/dev/null
if [ $? -eq 0 ]; then
cat /tmp/metadata-key >> $MANILA_USER_SSH_DIR/authorized_keys
chmod 0600 $MANILA_USER_SSH_DIR/authorized_keys
chown $MANILA_USER $MANILA_USER_SSH_DIR/authorized_keys
rm -f /tmp/metadata-key
echo "Successfully retrieved public key from instance metadata"
echo "*****************"
echo "AUTHORIZED KEYS"
echo "*****************"
cat $MANILA_USER_SSH_DIR/authorized_keys
echo "*****************"
else
FAILED=`expr $FAILED + 1`
if [ $FAILED -ge $ATTEMPTS ]; then
echo "Failed to retrieve public key from instance metadata after $FAILED attempts, quitting"
exit 1
fi
echo "Could not retrieve public key from instance metadata (attempt #$FAILED/$ATTEMPTS), retrying in 5 seconds..."
sleep 5
fi
done