forked from opentelekomcloud/otc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscp_otc.sh
executable file
·118 lines (102 loc) · 3.68 KB
/
scp_otc.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# Little helper to scp into VMs
#
# (c) Kurt Garloff <[email protected]>, 1/2018, CC-BY-SA 3.0
usage()
{
echo "Usage: scp_otc.sh [options] SRC DST"
echo "Remote SRC or DST may be specified by UUID"
echo "IF the parser sees something that looks like a UUID, the IP address"
echo " will be substituted and -i ~/.ssh/KEYNAME.pem and username will be inserted"
exit 2
}
if test -z "$1"; then usage; fi
OTC_TENANT=${OTC_TENANT:-210}
#SSHKEY=~/SSHkey-$OTC_TENANT.pem
NORM="\e[0;0m"
YELLOW="\e[0;33m"
RED="\e[0;31m"
is_uuid() { echo "$1" | grep '^[0-9a-f]\{8\}\-[0-9a-f]\{4\}\-[0-9a-f]\{4\}\-[0-9a-f]\{4\}\-[0-9a-f]\{12\}$' >/dev/null 2>&1; }
is_ip() { echo "$1" | grep '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$' >/dev/null 2>&1; }
getVPC()
{
# By convention, VMs are normally tagged with the VPC in OTC
firsttag=$(echo "$VMINFO" | jq '.tags[0]' | tr -d '"')
# If not, then look for router ports
if is_uuid $firsttag; then echo $firsttag; return 0; fi
NET=$(echo "$VMINFO" | jq '.interfaceAttachments[].net_id' | tr -d '"')
VPC=$(otc.sh custom GET "\$NEUTRON_URL/v2.0/ports.json?device_owner=network\:router_interface_distributed\&network_id=$NET" 2>/dev/null | jq '.ports[].device_id' | tr -d '"')
if is_uuid $VPC; then echo $VPC; return 0; fi
return 1
}
getvm()
{
VM=$1
if ! is_uuid $VM; then
#echo "Looking up VM \"$VM\" ... " 1>&2
VM=$(otc.sh vm list name=$VM | head -n1 | awk '{ print $1; }')
if ! is_uuid $VM; then echo "No such VM \"$1\"" 1>&2 ; exit 2; fi
fi
VMINFO=$(otc.sh vm show $VM) || { echo "No such VM \"$VM\"" 1>&2; exit 2; }
IP=$(echo "$VMINFO" | jq '.interfaceAttachments[].fixed_ips[].ip_address' | tr -d '"' | head -n1)
NAME=$(echo "$VMINFO" | jq '.server.name' | tr -d '"')
FLAVOR=$(echo "$VMINFO" | jq '.server.flavor.id' | tr -d '"')
IMGID=$(echo "$VMINFO" | jq '.server.image.id' | tr -d '"')
KEYNAME=$(echo "$VMINFO" | jq '.server.key_name' | tr -d '"')
IMGINFO=$(otc.sh image show $IMGID 2>/dev/null)
if test $? != 0 -o -z "$IMGINFO"; then
if test -z "$OSVER"; then OSVER=UNKNOWN; fi
IMGNAME="?"
else
IMGNAME=$(echo "$IMGINFO" | jq '.name' | tr -d '"')
OSVER=$(echo "$IMGINFO" | jq '.__os_version' | tr -d '"')
fi
if [[ "$OSVER" = "Ubuntu"* ]] && [ "$USER" == "linux" ]; then USER=ubuntu; fi
echo -e "${YELLOW}#VM Info: $VM $NAME $FLAVOR $IMGNAME $OSVER${NORM}" 1>%2
# Check VPC and use EIP if present and needed
MYVPC=$(otc.sh mds meta_data 2>/dev/null | jq .meta.vpc_id | tr -d '"')
if test -z "$MYVPC" -o "$MYVPC" == "null" || test "$(getVPC)" != "$MYVPC"; then
PORT=$(echo "$VMINFO" | jq .interfaceAttachments[].port_id | head -n1 | tr -d '"')
EIP=$(otc.sh eip list | grep " $IP " | awk '{ print $2; }')
if test -n "$EIP"; then
echo "#Using EIP $EIP instead of IP $IP" 1>&2
IP=$EIP
fi
fi
}
getSSHkey()
{
if test -n "$SSH_AUTH_SOCK"; then
KEYS=$(ssh-add -l)
if echo "$KEYS" | grep "$KEYNAME" >/dev/null 2>&1; then unset SSHKEY; return; fi
fi
SSHKEY=~/.ssh/"$KEYNAME.pem"
test -r $SSHKEY || SSHKEY=~/"$KEYNAME.pem"
if ! test -r $SSHKEY; then
echo -e "${RED}#Need ~/.ssh/$KEYNAME.pem${NORM}" 1>&2
unset SSHKEY
else
SSHKEY="-i $SSHKEY"
fi
}
unset VM
declare -a ARGS=("$@")
for no in $(seq 0 ${#ARGS[@]}); do
opt="${ARGS[$no]}"
if [[ "$opt" == "-i"* ]]; then ISET=1; continue; fi
if [[ "$opt" == *:* ]]; then
USER="${opt%%@*}"
if [ "$USER" == "$opt" ]; then USER=linux; fi
opt="${opt#*@}"
ARGS[$no]="${USER}@$opt"
VM=${opt#*@}; VM=${VM%:*}
if is_ip $VM; then continue; fi
if getent hosts $VM >/dev/null 2>&1; then continue; fi
getvm $VM
ARGS[$no]="${USER}@$IP:${opt##*:}"
continue
fi
done
if test "$ISET" != 1; then getSSHkey; fi
echo "scp ${SSHKEY} ${ARGS[@]}"
exec scp $SSHKEY "${ARGS[@]}"