Skip to content

Instantly share code, notes, and snippets.

@cesc1989
Last active July 28, 2020 19:09
Show Gist options
  • Save cesc1989/495642524dbce1ec35bc8c601219b229 to your computer and use it in GitHub Desktop.
Save cesc1989/495642524dbce1ec35bc8c601219b229 to your computer and use it in GitHub Desktop.
Todos mis scripts personales que uso o dejé de usar.
#!/bin/bash -xe
#
# Deploy latest revision without going to the AWS web console (used for failed deployments)
#
# jq needs to be installed
# https://stedolan.github.io/jq/
aws --region us-west-2 \
deploy list-application-revisions \
--application-name staging \
--sort-by lastUsedTime \
--sort-order descending \
--max-item 1 \
> revision.json
etag=$(cat revision.json | jq '.revisions[0] | .s3Location.eTag')
key=$(cat revision.json | jq '.revisions[0] | .s3Location.key')
bucket=$(cat revision.json | jq '.revisions[0] | .s3Location.bucket')
echo "The eTag value is: $etag"
echo "The key value is: $key"
echo "The bucket value is: $bucket"
aws --region us-west-2 \
deploy create-deployment \
--application-name staging \
--deployment-group-name api-in-staging \
--ignore-application-stop-failures \
--auto-rollback-configuration enabled=false \
--description "From AWS CLI" \
--s3-location bundleType=zip,bucket=$bucket,eTag=$etag,key=$key
rm ./revision.json
#!/bin/bash
# How to use this script
# ./log_to_ecs_container.sh "github-user" "sh -lc 'bundle exec rake request_execution:for_physicians_list'"
options=("Production" "Staging" "UAT")
clear
PS3="Select environment 1-${#options[@]} : "
select opt in "${options[@]}"
do
case $opt in
"Production")
cluster_name="api-production"
break
;;
"Staging")
cluster_name="api-staging"
filters="\-uat"
break
;;
"UAT")
cluster_name="api-staging"
break
;;
*)echo "invalid option $REPLY";;
esac
done
# List all services
if [ "$cluster_name" == "api-production" ]
then
services=($(aws ecs list-services --cluster $cluster_name | awk -F 'service/' '{ print $2 }' | awk -F '\",' '{ print $1 }' | sed 's/"//g'))
elif [ "$cluster_name" == "api-staging" ] && [ "$filters" == "\-uat" ]
then
services=($(aws ecs list-services --cluster $cluster_name | grep -v $filters | awk -F 'service/' '{ print $2 }' | awk -F '\",' '{ print $1 }' | sed 's/"//g'))
else
services=($(aws ecs list-services --cluster $cluster_name | grep "\-uat" | awk -F 'service/' '{ print $2 }' | awk -F '\",' '{ print $1 }' | sed 's/"//g'))
fi
printf "\nSelected option: $opt\n"
printf "\nServices found on cluster: $cluster_name\n"
echo " "
PS3="Select service 1-${#services[@]}: "
select opt in "${services[@]}"
do
printf "\nYou selected: $opt \n"
break
done
# Searching for a task"
printf "\ntask_arn:\n"
task_arn=$(aws ecs list-tasks --service-name $opt --cluster $cluster_name | grep "arn" | cut -d '"' -f 2)
IFS=' ' read -r task string <<< "$task_arn"
echo $task
container_instance=$(aws ecs describe-tasks \
--cluster $cluster_name \
--tasks $task \
| grep "containerInstanceArn" | awk -F 'containerInstanceArn' '{ print $2 }' |cut -d '"' -f 3)
container_id=$(aws ecs describe-tasks --cluster $cluster_name --tasks $task | grep "runtimeId" | awk -F 'runtimeId' '{ print $2 }' |cut -d '"' -f 3 | cut -c1-12)
printf "\ncontainer_id:\n"
echo $container_id
ec2_instance=$(aws ecs describe-container-instances --cluster $cluster_name \
--container-instance $container_instance | grep ec2InstanceId | awk -F 'ec2InstanceId' '{ print $2 }' |cut -d '"' -f 3)
ip=$(aws ec2 describe-instances --instance-ids $ec2_instance \
| grep PublicIpAddress | awk '{ print $2 }' | cut -d '"' -f 2)
printf "\ninstance_ip:\n"
echo $ip
echo "username: $1"
echo "command: $2"
printf "\nlogin into desired container:\n"
ssh -t $1@$ip docker exec -it $container_id $2
#!/bin/bash
# Para encontrar el proyecto frontend
# Nombre de la imagen.
# HOST/IMAGE:TAG
# bash ~/projects/scripts/search_container.sh staging.*therapist_frontend sh
# Para encontrar el proyecto backend en staging
# Nombre de la imagen:
# HOST/IMAGE:TAG
# bash ~/projects/scripts/search_container.sh api-staging:feature_staging_268 sh
# Para encontrar el proyecto backend en UAT
# Nombre de la imagen:
# HOST/IMAGE:TAG
# bash ~/projects/scripts/search_container.sh api-staging:develop_270 sh
project_name=$1
command=$2
username="ec2-user"
ips_file_path=~/.ips
ec2_instance_ips=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=luna-api-*" | grep PublicIpAddress | awk '{ print $2 }' | cut -d '"' -f 2)
if [ -n "$ec2_instance_ips" ]
then
# Comento porque no los guarda línea por línea y el comando explota
# echo $ec2_instance_ips > $ips_file_path
while read p
do
echo "Accessing IP address: $p"
echo "Project Name: $project_name"
echo "Command to Run in Docker Container: $command"
echo ""
container_id=$(ssh -n $username@$p docker ps | grep $project_name | head -n 1 | awk '{ print $1 }')
echo "Found Container ID: $container_id"
echo ""
if [ -n "$container_id" ]
then
echo "EC2 Instance IP: $p"
instance_ip=$p
break
fi
done < $ips_file_path
if [ -n "$container_id" ]
then
ssh -t $username@$instance_ip docker exec -it $container_id $command
fi
fi
#!/bin/bash
# The script to find the Docker container is quite tedious to user because
# I need to know many things to use the command.
#
# With this one I only need to know the stage and the build number generated by
# Circle CI.
# environment is one of: [staging, develop]
environment=$1
# the build number is found in Circle CI workflow
build_number=$2
# default command run in the docker container
command=sh
if [ $environment == "staging" ]
then
tag_name="feature_staging"
elif [ $environment == "uat" ]
then
tag_name="develop"
fi
bash ~/projects/scripts/search_container.sh \
"luna-api-staging:"$tag_name"_"$build_number \
$command
#!/bin/bash
# Copy vagrant + ansible files for provisioning Rails project
from="$HOME/projects/vagrant-ansible"
dest=$1
cp -R $from/group_vars \
$from/hosts \
$from/playbook.yml \
$from/roles \
$from/Vagrantfile \
$dest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment