-
Notifications
You must be signed in to change notification settings - Fork 42
/
scp-remote-to-local.sh
113 lines (91 loc) · 2.98 KB
/
scp-remote-to-local.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
#!/bin/bash
#
# Dependencies:
# brew install jq
#
# Example:
# /bin/bash ./scp-remote-to-local.sh <cert_path> <component_name> <cosmos_user>
#
# Description:
# Grabs list of running instances for specified Cosmos component (TEST environment)
# SCP's known log locations from remote to new local directory
# Enable a form of 'strict mode' for Bash
set -euo pipefail
IFS=$'\n\t'
# Define our expected variables up front
cert=${1:-}
component=${2:-}
user=${3:-}
failed_access=()
api="https://api.live.bbc.co.uk/cosmos/env/test/component/$component"
if [ "$#" -ne 3 ]; then
cat <<EOF
Please check the arguments are provided correctly:
1. cert path (pem)
2. component name
3. cosmos user
If you have any curl/cert issues try:
brew install curl --with-openssl
If you have any parsing issues try:
brew install jq
EOF
exit 1
fi
logdir=$(mktemp -d "$component.logs.XXXX")
data=($(curl --silent --cert $cert "$api/instances" | jq --raw-output ".[] | .id,.private_ip_address,.launch_time"))
data_len=$((${#data[@]} / 3)) # we know we'll always have a triad of data -> <id>,<ip>,<launch_time>
for ((n = 0; n < $data_len; n++))
do
ssh_success=false
failed=false
# parse array indexes needed to extract data
id=$(($n * 3))
ip=$(($id + 1))
ti=$(($id + 2))
instance_id=${data[$id]}
instance_ip=${data[$ip]}
launch_time=${data[$ti]}
printf "\n######################################\n"
printf "\nrequesting ssh access for: $instance_id\n"
# use cosmos api to generate ssh access token
response=$(curl --silent \
--cert $cert \
--header "Content-Type: application/json" \
--request POST \
--data "{\"instance_id\":\"$instance_id\"}" \
"$api/logins/create")
# parse token from api response
checkpoint_id=$(echo $response | jq --raw-output .url | cut -d '/' -f 7)
until $ssh_success
do
status=$(curl --silent --cert $cert "$api/login/$checkpoint_id" | jq --raw-output .status)
if [ "$status" = "current" ]; then
ssh_success=true
printf "\n\nssh access granted for instance $(($n + 1)): $instance_id ($instance_ip)\n\n"
elif [ "$status" = "failed" ]; then
failed_access+=("$instance_id - $instance_ip - $launch_time")
failed=true
break
else
echo -ne "status == $status "\\r
fi
done
if [ "$failed" = true ]; then
continue
else
scp -r "$user@$instance_ip,eu-west-1:/var/log/component/app.log" "./$logdir/$launch_time-$instance_ip.log"
fi
done
# removed 'wait' command here and '&' backgrounding of scp process
# as stdout feedback was getting interleaved and really confusing
printf "\n######################################\n\n"
echo "all logs copied successfully"
failed_len=$(echo ${#failed_access[@]})
if [ "$failed_len" -gt 0 ]; then
printf "\n######################################\n\n"
echo "there were '$failed_len' failed ssh attempts"
for i in "${failed_access[@]}"
do
printf "\t - $i\n"
done
fi