0
0
mirror of https://github.com/letic/Shell-Scripts.git synced 2024-09-28 05:56:04 +00:00
AWS_Shell-Scripts/scp-remote-to-local.sh

100 lines
2.7 KiB
Bash
Raw Normal View History

2016-03-22 11:19:18 +00:00
#!/bin/bash
#
# Dependencies:
# brew install jq
#
# Example:
# /bin/bash ./results.sh <cert_path> <component_name> <cosmos_user>
#
# Description:
2016-03-22 18:06:24 +00:00
# Grabs list of running instances for specified Cosmos component (TEST environment)
2016-03-22 11:19:18 +00:00
# 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:-}
2016-03-22 18:06:24 +00:00
api="https://api.live.bbc.co.uk/cosmos/env/test/component/$component"
2016-03-22 11:19:18 +00:00
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
If you have any issues with SCP then
make sure you've given your user SSH access via Cosmos.
This is something I'd like to automate via this script in future.
EOF
exit 1
fi
logdir=$(mktemp -d logs.XXXX)
2016-03-23 09:47:50 +00:00
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>
2016-03-22 18:06:24 +00:00
for ((n = 0; n < $data_len; n++))
2016-03-22 11:19:18 +00:00
do
2016-03-22 18:06:24 +00:00
ssh_success=false
valid="current"
2016-03-23 09:47:50 +00:00
# parse array indexes needed to extract data
id=$(($n * 3))
2016-03-22 18:06:24 +00:00
ip=$(($id + 1))
2016-03-23 09:47:50 +00:00
ti=$(($id + 2))
2016-03-22 18:06:24 +00:00
instance_id=${data[$id]}
instance_ip=${data[$ip]}
2016-03-23 09:47:50 +00:00
launch_time=${data[$ti]}
2016-03-22 18:06:24 +00:00
2016-03-23 09:47:50 +00:00
printf "\n######################################\n"
printf "\nrequesting ssh access for: $instance_id\n"
# use cosmos api to generate ssh access token
2016-03-22 18:06:24 +00:00
response=$(curl --silent \
--cert $cert \
--header "Content-Type: application/json" \
--request POST \
--data "{\"instance_id\":\"$instance_id\"}" \
"$api/logins/create")
2016-03-23 09:47:50 +00:00
# parse token from api response
2016-03-22 18:06:24 +00:00
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" = "$valid" ]; then
ssh_success=true
printf "\n"
2016-03-23 09:47:50 +00:00
echo "ssh access granted for instance $(($n + 1)): $instance_id ($instance_ip)"
2016-03-22 18:06:24 +00:00
printf "\n"
else
2016-03-23 09:47:50 +00:00
echo -ne "status == $status "\\r
2016-03-22 18:06:24 +00:00
fi
done
2016-03-23 09:47:50 +00:00
scp -r "$user@$instance_ip,eu-west-1:/var/log/component/app.log" "./$logdir/$launch_time-$instance_ip.log"
2016-03-22 11:19:18 +00:00
done
2016-03-23 09:47:50 +00:00
# removed 'wait' command here and '&' backgrounding of scp process
# as stdout feedback was getting interleaved and really confusing
2016-03-22 11:19:18 +00:00
2016-03-23 09:47:50 +00:00
printf "\n######################################\n\n"
echo "all logs copied successfully"