summaryrefslogtreecommitdiff
path: root/files/keypair.sh
blob: 0e8374d5447dffb9b86f78e9a4c05a79214b471a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash

keypair_name="${1}"
pubkey_path="${2}"

if [ "${keypair_name}" == "" ]; then
    echo "No keypair name was supplied." >&2
    exit 1
elif [ "${pubkey_path}" == "" ]; then
    echo "No pubkey path was supplied." >&2
    exit 1
fi

keypair_fingerprint=$(openstack keypair list | grep -F "${keypair_name}" | awk '{print $4;}')
if [ "${keypair_fingerprint}" == "" ]; then
    if [ "${pubkey_path}" == "" ]; then
        echo -n "Need to create an SSH keypair, but no path to a pubkey " >&2
        echo "was supplied." >&2
        exit 1
    elif [ ! -f "${pubkey_path}" ]; then
        echo -n "Need to create an SSH keypair, but the supplied path " >&2
        echo "to the pubkey is invalid:" >&2
        echo "${pubkey_path}" >&2
        exit 1
    fi

    openstack keypair create --public-key "${pubkey_path}" "${keypair_name}"
    keypair_fingerprint=$(openstack keypair list | grep -F "${keypair_name}" | awk '{print $4;}')
    if [ "${keypair_fingerprint}" == "" ]; then
        echo "Could not retrieve keypair after creation." >&2
        exit 1
    fi
fi

exit 0