Skip to content

Instantly share code, notes, and snippets.

@denoww
Last active June 14, 2024 14:32
Show Gist options
  • Save denoww/a1acfd75198e743e31e1f6a127244ced to your computer and use it in GitHub Desktop.
Save denoww/a1acfd75198e743e31e1f6a127244ced to your computer and use it in GitHub Desktop.
# How to install
# wget -O - https://gist.githubusercontent.com/denoww/a1acfd75198e743e31e1f6a127244ced/raw | bash -s install
# Para ver exemplos de como usar faça
# $ sc
LIB_DIR="/usr/local/bin"
LIB_PATH="$LIB_DIR/sc"
REGION='us-east-1'
tasks_from_service(){
cluster=$1
svc=$2
arns=$(aws ecs list-tasks --cluster $cluster --service-name $svc | jq -r ".taskArns | join(\" \")")
# lista=$(aws ecs describe-tasks --region $REGION --cluster $cluster --tasks $arns | jq -r ".tasks[].attachments[].details[] | select(.name==\"privateDnsName\").value | select(.name)")
echo $(aws ecs describe-tasks --region $REGION --cluster $cluster --tasks $arns | jq -r ".tasks[].attachments[].details | map( { (.name|tostring): .value } ) | add")
# TASKS=`aws ecs describe-tasks --region $REGION --cluster $cluster --tasks $TASKARN | jq -r ".tasks | map({ taskArn: .taskArn, taskDefinitionArn: .taskDefinitionArn, group: .group })| .[] | select(.group | contains(\"$1\"))"`
}
get_public_ip(){
NET_ID=$1
PUBLIC_IP=$(aws ec2 describe-network-interfaces --network-interface-ids $NET_ID | jq -r '.NetworkInterfaces[].Association.PublicIp')
echo $PUBLIC_IP
}
listar_tasks(){
cluster=$1
svc=$2
_print
_print
_print
_print_points
_print "$svc $cluster"
_print_points
for row in $(echo "$(tasks_from_service $cluster $svc)" | jq -r ' @base64'); do
((idx+=1))
get() {
echo ${row} | base64 --decode | jq -r ".${1}"
}
name=$(get 'privateDnsName')
net_id=$(get 'networkInterfaceId')
MAQ_NET_IDS["$idx"]=$net_id
_print "$idx - $name"
done
}
escolher_maquina(){
tipo_maquina=$1
erp_production_cluster="seucondominio"
erp_staging_cluster="seucondominio-staging"
case $tipo_maquina in
"erp")
listar_tasks $erp_production_cluster "web"
listar_tasks $erp_production_cluster "worker"
listar_tasks $erp_staging_cluster "web"
listar_tasks $erp_staging_cluster "worker"
;;
"channelio")
listar_tasks $erp_production_cluster "channelio"
;;
*)
echo "comando inválido dentro de escolher_maquina"
exit 1
;;
esac
_print
_print
_print_points
read -p 'Digite o numero da máquina: ' MAQ_ESCOLHIDA
}
_print_points(){
_print '.......................................'
}
_print(){
echo $1 >&2
}
escolher_ip(){
# idx=`escolher_maquina`
tipo_maquina=$1
escolher_maquina $tipo_maquina
echo $(get_public_ip ${MAQ_NET_IDS[$MAQ_ESCOLHIDA]})
}
copyfile(){
// filesourcepath=$*
filesourcepath="${*:2}"
tipo_maquina=$1
ip=$(escolher_ip $tipo_maquina)
# inicio decoracao
#
_print
_print_points
_print "Baixando $filesourcepath em $ip para $(pwd)"
_print "Aguarde..."
_print_points
_print
_print
_print
#
# fim decoracao
run="scp -i $HOME/id_rsa_sc_ecs -oStrictHostKeyChecking=no root@$ip:$filesourcepath ."
$run
download_success=$?
if [[ $download_success -eq 0 ]]; then
_print_points
_print "baixado $(pwd)"
_print_points
else
_print "Erro ao baixar!!!!!"
fi
}
run_ssh(){
tipo_maquina=$1
cmd2="${*:2}"
ip=$(escolher_ip $tipo_maquina)
# inicio decoracao
#
_print
_print_points
_print "Entrando em $ip via SSh..."
_print "Aguarde..."
_print_points
_print
_print
_print
#
# fim decoracao
run="ssh -i $HOME/id_rsa_sc_ecs -oStrictHostKeyChecking=no -t root@$ip"
if [ -z "$cmd2" ]; then
# entra direto no ssh sem executar comando nenhum
$run
else
_print_points
echo "Executando $cmd2"
_print_points
$run "bash -ic \"$cmd2\""
fi
}
ssh_cmd(){
cmdcompleto=$*
cmd1=$1
cmd2=$2
rake="bundle exec rake"
rails="bundle exec rails"
case $cmd1 in
"channelio") run_ssh channelio;;
"scc") run_ssh erp $rails c;;
"scdbconsole") run_ssh erp $rails dbconsole;;
"scmigrationstatus") run_ssh erp $rake db:migrate:status;;
"rake") run_ssh erp $rake $cmd2;;
"rails") run_ssh erp $rails $cmd2;;
"copyfile") copyfile erp $cmd2;;
"logs") run_ssh erp tail -f log/production.log;;
"htop") run_ssh erp htop;;
# "scsetupdependents") run_ssh erp $rake sc:db:pg:setup_dependents;;
*) run_ssh erp $cmdcompleto;; #
esac
}
tunnel() {
ip_porta_maquina=$1
tunnelporta=$2
sctunnel_host=sctunnel1.seucondominio.com.br
if [ -z "$tunnelporta" ]; then
_tunnel_portas=$(curl -s --request GET http://${sctunnel_host}:3020/unused_ports?qtd=1 )
tunnelporta=$(echo $_tunnel_portas | jq -r '.portas[0]')
fi
echo "$ip_porta_maquina -> $sctunnel_host:$tunnelporta"
ssh -N -o ServerAliveInterval=20 -i "~/scTunnel.pem" ubuntu@$sctunnel_host -R $tunnelporta:$ip_porta_maquina
}
install() {
sudo wget -O $LIB_DIR/_sc https://gist.githubusercontent.com/denoww/a1acfd75198e743e31e1f6a127244ced/raw;
download_success=$?
echo "downloading new sc"
if [[ $download_success -eq 0 ]]; then
# sucesso ao baixar
sudo rm -f $LIB_PATH
sudo mv $LIB_DIR/_sc $LIB_PATH
sudo chmod 777 $LIB_PATH;
# sudo chmod +x $LIB_PATH;
else
# erro ao baixar
sudo rm -f $LIB_DIR/_sc
echo "Erro ao baixar sc"
fi
}
git_log_days() {
# git_log_days $dev $ultimos_x_dias
#
local dev="$1"
local days="$2"
# Loop sobre os últimos X dias
for (( i=days-1; i>=0; i-- )); do
# Calcular a data para cada dia no formato brasileiro DD/MM/AAAA
date=$(date -d "$i days ago" +%Y-%m-%d)
day_of_week=$(date -d "$i days ago" +%A)
date_formatted=$(date -d "$date" +%d/%m/%Y)
# Formatar as datas de início e fim para o comando git log
since="$date 00:00am"
until="$date 23:59pm"
# Executar o comando git log
log_output=$(git log --oneline --color=always --shortstat --author="$dev" --all --since="$since" --until="$until" --no-merges --decorate)
# Verificar se a saída está vazia
if [ -n "$log_output" ]; then
local ini_cor="\033[0;32m"
local fim_cor="\033[0m"
nenhum_commit=""
txt="$log_output"
else
# nenhum commit
local ini_cor="\033[0;31m"
local fim_cor="\033[0m"
nenhum_commit="- nenhum commit"
txt=""
fi
# Imprimir
echo -e "$ini_cor=====================================================================================$fim_cor"
echo -e "$ini_cor$date_formatted - $day_of_week $nenhum_commit $fim_cor"
if [ -n "$log_output" ]; then
echo -e "$ini_cor=====================================================================================$fim_cor"
#echo -e "$log_output"
fi
# pegar url do commit
echo "$log_output" | while read -r line; do
if [ -z "$line" ]; then
continue
fi
# echo '0'
url=""
if [[ ! $line =~ ^[0-9]+\ file[s]?\ changed,\ .* ]]; then
# echo '1'
# Extraia o ID do commit (primeira palavra da linha)
commit_id=$(echo $line | awk '{print $1}')
# Verifique se o ID do commit está presente
if [[ -n "$commit_id" ]]; then
# Monte a URL
url="https://github.com/denoww/seucondominio/commit/$commit_id"
# Imprima a URL
fi
echo -e $line
else
# 2 files changed, 3 insertions(+), 4 deletions(-)
echo -e $line
echo ""
fi
if [ -n "$url" ]; then
echo $url
fi
done
done
}
help(){
_print
_print_points
echo "::: ERP: Como usar? :::"
_print_points
echo " sc ssh"
echo " sc ssh scc"
echo " sc ssh htop"
echo " sc ssh logs"
echo " sc ssh scc"
echo " sc ssh scdbconsole"
echo " sc ssh scmigrationstatus"
echo " sc ssh rake MEUCOMANDO"
echo " sc ssh rails MEUCOMANDO"
echo " sc ssh echo 1"
echo " sc ssh comando qualquer"
echo " sc ssh copyfile /tmp/meu_arquivo"
_print_points
echo "::: channelio :::"
_print_points
echo " sc ssh channelio"
_print_points
echo "::: sctunnel (parecido com ngrok) :::"
_print_points
echo " sc tunnel localhost:3000"
echo " sc tunnel localhost:3000 4444"
echo " sc tunnel 192.168.1.100:3000"
_print_points
# echo "::: git_log_days :::"
# _print_points
# echo " sc git_log_days \$nome_do_dev \$qtd_dias"
# echo " sc git_log_days denoww 10"
# echo " sc git_log_days [email protected] 10"
# echo " sc git_log_days [email protected] 10"
# _print_points
echo "::: Para receber atualização desse software :::"
_print_points
echo " sc update "
_print_points
echo "::: Para editar esse software :::"
_print_points
echo " sudo subl /usr/local/bin/sc"
echo " edite, teste e salve em:"
echo " https://gist.github.com/denoww/a1acfd75198e743e31e1f6a127244ced/edit"
_print_points
# _print_points
# echo "::: :::"
# _print_points
echo
}
# public commands
cmd=$*
cmd="${cmd//$1/}" # regex para remover o primeiro comando do restante
case $1 in
install) install;;
update) install;;
tunnel) tunnel $cmd;;
git_log_days) git_log_days $cmd;;
ssh) ssh_cmd $cmd;;
*) help;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment