forked from galthaus/digitalrebar-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-from-ssh.sh
executable file
·77 lines (64 loc) · 1.8 KB
/
add-from-ssh.sh
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# Copyright 2015, RackN Inc
function usage() {
echo "Usage: $0 [--identity <identity_file>] [--clean] [--init-ident <file>] --admin-ip <Admin IP> <CIDR of Node>" >&2
exit 1
}
ID_FILE=""
CLEAN_IT=""
INIT_ID_FILE=""
ACCOUNT="--user root"
SUDO=""
while (( $# > 0 )); do
arg="$1"
case $arg in
--admin-ip) shift; ADMIN_IP=$1; shift;;
--clean) shift; CLEAN_IT="--clean";;
--identity) shift; ID_FILE="--identiy $1"; shift;;
--user) shift; ACCOUNT="--user $1"; shift;;
--init-ident) shift; INIT_ID_FILE="-init-ident $1"; shift;;
--help|-h) usage;;
*) break;;
esac
done
if [ "$ADMIN_IP" == "" ] ; then
echo "Must specify an Admin IP"
exit 1
fi
CIDRIP=$1
IP=${CIDRIP%/*}
CIDR=${CIDRIP##*/}
if [ "$CIDRIP" == "" ] ; then
echo "Please provide a CIDR IP"
exit 1
fi
if ! which jq &>/dev/null; then
echo "Please install jq!"
exit 1
fi
echo "Device ip = $IP/$CIDR"
ssh-keygen -f "~/.ssh/known_hosts" -R $IP
ssh -oBatchMode=yes -o StrictHostKeyChecking=no root@$IP date
if [ $? -ne 0 ]; then
scripts/ssh-copy-id.sh $CLEAN_IT $ID_FILE $ACCOUNT $INIT_ID_FILE root@$IP
fi
date
# Get the ssh keys and update authorized_keys
REBAR_ENDPOINT="https://$ADMIN_IP:3000"
REBAR_KEY="rebar:rebar1"
KEY_FILE1="/tmp/keys.$$"
KEY_FILE2="/tmp/keys2.$$"
success=$(curl -k -s -o $KEY_FILE1 -w "%{http_code}" --digest -u "$REBAR_KEY" \
-X GET "${REBAR_ENDPOINT}/api/v2/deployments/1/attribs/rebar-access_keys")
if [[ $success != 200 ]] ; then
echo "Failed to get keys"
exit -1
fi
jq -r '.value|to_entries[].value' $KEY_FILE1 > $KEY_FILE2
scp $KEY_FILE2 root@$IP:keys
rm -rf $KEY_FILE1 $KEY_FILE2
scp scripts/join_rebar.sh root@$IP:
ssh root@$IP /root/join_rebar.sh $ADMIN_IP
echo "=== HELPFUL COMMANDS ==="
echo "Packet Device ID: $DEVICE_ID"
echo "SSH: ssh root@$IP"