https://code.engpro.totvs.com.br/smarterp/sre-tools

docker run \
-e namespaces=c0fbc5 \
-e desiredVersion="0.0.1" \
-e forceUpdate=true \
-e KUBECONFIG=$CONF \
-e BUCKET_NAME=smarterp-charts \
-e BUCKET_PROVIDER=gcp \
-e BRANCH_NAME=develop \
-e DRONE_TOKEN=xxxxxxxxxxx \
-v ./devops:/devops \
--entrypoint ./devops/update-topology.sh \
gcr.io/eng-protheus/sre-tools:latest 

Listar todas as topologias de um cluster:



#!/bin/bash
set -e
kubectl get ns --no-headers -L UpdaterDesiredVersion,BaseVersion,Owner,External_id,Topology_id,Extra -l External_id --sort-by=.metadata.creationTimestamp

Mudar o contexto de um NS dentro do KUBECONFIG


#!/bin/bash

echo ""
if [ ! "$1" == "" ]; then
        kubectl config set-context --current --namespace=$1
fi

echo "+--------------------------------------------"
echo "| Cluster: "$(kubectl config view --minify -o json | jq -r '.contexts[0].context.cluster')
echo "| Namespace: "$(kubectl config view --minify -o json | jq -r '.contexts[0].context.namespace')
echo "+--------------------------------------------"

echo ""

Utilizados como encurtador de comandos. Adicionar no ~/.bash do seu terminal.

alias k="kubectl"

alias po="kubectl get pods -o wide"
alias ing="kubectl get ing"
alias svc="kubectl get svc"
alias delpo="kubectl delete pods --force --grace-period=0"

alias kk="kubectl2"

Aplicação para execução do CI das soluções SmartSRE


exportar o DRONE_TOKEN (Pegar na ferramenta do DRONE) antes de executar a ferramenta.

#!/bin/bash

THIS=$(basename $0)
NOW=$(date '+%Y%m%d')

function usage() {
    echo -e "$THIS 0.2\t\nAuthor: Renato Campos <[email protected]>\t"
    echo -e "\n$THIS [option] [-j backoffice|taf|release|sistemico]" 
    echo -e "\nOptions:"
    echo -e "\t-r, build rpo "
    echo -e "\t-i, build image"
    echo -e "\t-u, flag upddistr"
    echo -e "\t-v, new version (12.1.2310). If empty, build version will be 12.1.2210"
    echo -e "\n$THIS -h (shows this help)"
    echo -e "If used without options, this script will return fail\n"
    echo -e "Remember export DRONE_TOKEN in your environment\n"

    exit -1
}

function getJobId() {
	DroneAPI=$1
	numberBuild=$2

	curl --request GET -s \
		 --url "$DroneAPI/builds/$numberBuild" \
		 --header 'Authorization: Bearer '$DRONE_TOKEN \
		 --header 'Content-Type: application/json' | jq -r '. | select( .status == "running")' | jq -r .number
}

function getJobSTEP() {
	DroneAPI=$1
	numberBuild=$2
	
	curl --request GET -s \
		 --url "$DroneAPI/builds/$numberBuild" \
		 --header 'Authorization: Bearer '$DRONE_TOKEN \
		 --header 'Content-Type: application/json' | jq -r '.stages | .[].steps | .[] | select ( .status == "running" )' | jq -r .name
}

function waitProcJob(){
	DroneAPI=$1
	numberBuild=$2

	echo "Job $numberBuild started in $DroneAPI"
	while [[ "$(getJobId $DroneAPI $numberBuild)" != "" ]]; do
		echo "Job $numberBuild - STEP: "$(getJobSTEP $DroneAPI $numberBuild)" - running..." 
		sleep 10
	done
}

function buildWithParameters(){
	DroneAPI=$1
	param=$2

	curl --request POST -s \
	     --url "$DroneAPI/builds?$param" \
 		 --header 'Authorization: Bearer '$DRONE_TOKEN \
		 --header 'Content-Type: application/json' | jq -r .number
}

function gerarpo() {
	branch=$1
	release=$2

	DroneAPI="https://drone.engpro.totvs.com.br/api/repos/smarterp/pipeline-rpo"
	param="branch=$branch&RELEASE=$release&BUILDIMAGE=false"

	numberBuild=$(buildWithParameters $DroneAPI $param)
	sleep 10

	waitProcJob $DroneAPI $numberBuild
}


function geraimage() {
	branch=$1
	release=$2
	upddistr=$3

	DroneAPI="https://drone.engpro.totvs.com.br/api/repos/smarterp/pipeline-imagem"
	param="branch=$branch&RELEASE=$release&UPDDISTR=$upddistr&GERA_IMAGEM=true&GERA_CHART=true"

	numberBuild=$(buildWithParameters $DroneAPI $param)
	sleep 10

	waitProcJob $DroneAPI $numberBuild
}


# check whether user had supplied -h or --help . If yes display usage
if [[ ( $# == "--help") ||  $# == "-h" ]] ;then
    usage
fi

if [ $# -le 1 ]; then
    usage
fi

if [ $# -ge 2 ]; then
    rpo="false"
    image="false"
	upddistr="false"
	JOB=""
	VERSION="12.1.2210"

    while getopts ": hriu j:" opt; do
        case $opt in
            "h") usage;;
            "r") rpo="true" ;;
            "i") image="true" ;;
			"u") upddistr="true" ;;
			"v") VERSION="12.1.2310" ;;
			"j") JOB=${OPTARG} # defina $ JOB para o valor especificado.
			;;
		    :) 
		       echo "Erro: - $ {OPTARG} requer um argumento."
		       usage
		       ;;

		    *)
		       usage
		       ;;
        esac
    done


    if [ "$rpo" == "true" ]; then
    	echo "Starting CI SmartAPI - RPO: $JOB - $VERSION"
    	gerarpo $JOB $VERSION
    fi

    if [ "$image" == "true" ]; then

        if [ "$upddistr" == "true" ]; then
    		echo "Starting CI SmartBuild - IMAGE: $JOB - with UPDDISTR"
		else
    		echo "Starting CI SmartBuild - IMAGE: $JOB - without UPDDISTR"
    	fi

		geraimage $JOB $VERSION $upddistr
    fi

	echo "SmartERP-CI terminate. See file /tmp/ci-smarterp-xxxx.txt"
fi

exit 0

Realiza a limpeza do NS.

#!/bin/bash

namespace=$1

if [ "$namespace" == "" ]; then
   echo need to provide namespace
   exit 0
fi

helm get values $1 >> /totvs/ambientes/inativos/$1.yaml

kubectl -n $namespace delete deploy --all
kubectl -n $namespace delete cronjob --all
kubectl -n $namespace delete job --all
##kubectl -n $namespace delete configmap --all
#kubectl -n $namespace delete service $(kubectl -n $namespace get service --no-headers | grep -v $namespace | cut -f1 -d ' ')
kubectl -n $namespace delete ingress --all
kubectl -n $namespace delete secrets $(kubectl -n $namespace get secrets --no-headers | grep -v protheus-secrets-license | cut -f1 -d ' ')
kubectl -n $namespace delete serviceaccount --all
kubectl -n $namespace delete role --all
kubectl -n $namespace delete rolebinding --all

kubectl delete clusterrole release-updater-$namespace
kubectl delete clusterrolebinding release-updater-$namespace

kubectl -n $namespace delete pods --all

kubectl -n $namespace delete pdb --all
kubectl -n $namespace patch pvc protheus-data-claim -p '{"metadata":{"finalizers": null}}' & kubectl -n $namespace delete PersistentVolume protheus-volume-$namespace & kubectl patch pv protheus-volume-$namespace -p '{"metadata":{"finalizers": null}}'

Aplicação para desligar os pods das topologias que não estão em uso.


#!/bin/bash
set -e

envs=($1)

time=61

if [ ${#envs[@]} -eq 0 ]; then
  envs=($(kubectl get pods -A --no-headers --field-selector=status.phase=Running | grep -i 'protheus' | awk '{print $1}' | sort | uniq -c | sort -nr | grep -v ' 1 ' | awk '{print $2}'))
fi

for env in ${envs[@]}; do
  echo ----------------------------
  echo $env
  goToSleep=0

  hypnusPod=$(kubectl -n ${env} get pod -l app=protheus-sleeper --no-headers | awk '{print $1}')

  runningPodsBase=$(kubectl -n ${env} get deploy -l base=true -o jsonpath='{range .items[?(@.status.availableReplicas>=1)]}{@.metadata.name}{"\n"}{end}' | wc -l)
  runningPodsNBase=$(kubectl -n ${env} get deploy -l base=false -o jsonpath='{range .items[?(@.status.availableReplicas>=1)]}{@.metadata.name}{"\n"}{end}' | wc -l)
  runningJobs=$(kubectl -n ${env} get pods -l tolerationValue=jobs --no-headers | grep -iv 'completed' | wc -l)

  echo "${env} -> pods de jobs rodando: ${runningJobs}"
  echo "${env} -> pods base rodando: ${runningPodsBase}"
  echo "${env} -> pods publicos rodando: ${runningPodsNBase}"

  if [ "${hypnusPod}" != "" ] ; then
    lastResetInterval=$(kubectl -n ${env} logs ${hypnusPod} | grep -i 'reset interval' | cut -d'[' -f2 | cut -d']' -f1 | awk 'END{print}')

    if [ "${lastResetInterval}" != "" ]; then
      hora=$(echo "${lastResetInterval}" | cut -d' ' -f2)
      ano=$(echo "${lastResetInterval}" | cut -d'/' -f3 | cut -d' ' -f1)
      mes=$(echo "${lastResetInterval}" | cut -d'/' -f2)
      dia=$(echo "${lastResetInterval}" | cut -d'/' -f1)
      lastResetIntervalInDate=$(date -d "${ano}/${mes}/${dia} ${hora}-00:00")

      differenceFromNow=$(echo "$(($(date '+%s') - $(date -d "${ano}/${mes}/${dia} ${hora}-00:00" '+%s')))")
      echo "${env} -> faz ${differenceFromNow} segundos que o hypnus não reinicia o contador"

      if [ ${differenceFromNow} -gt $time -a ${runningJobs} -eq 0 ]; then
        goToSleep=1
      fi
    else
      echo "${env} -> data em branco"
      echo "${env} -> verifique o motivo de não haver logs no hypnus"

  #    if [ ${runningPodsBase} -ge 3 -a ${runningJobs} -eq 0 -a ${runningPodsNBase} -eq 0 ]; then
      if [ ${runningJobs} -eq 0 -a ${runningPodsNBase} -eq 0 ]; then
        goToSleep=1
      else
        echo "${env} -> nada será feito aqui"
      fi
    fi

    if [ ${goToSleep} -eq 1 ]; then
      deploymentsToSleep=$(kubectl -n ${env} get deploy --no-headers | awk '$3 != 0' | grep -v protheus-sleeper | awk '{print $1}')
      kubectl -n ${env} scale deploy ${deploymentsToSleep} --replicas=0
      for deploy in $(echo "${deploymentsToSleep}" | grep 'protheus-appserver'); do
        kubectl -n ${env} patch service ${deploy}-svc -p '{"metadata":{"annotations":{"tks.sh/sleeper.user":""}}}'
      done
      #kubectl -n ${env} delete pod -l app=protheus-sleeper --force --grace-period=0
      kubectl -n ${env} delete pods --all --force --grace-period=0
      # sleep 15s

      # ./verificaNodesVazios base
      # ./verificaNodesVazios base-d
    fi
  else
    echo "${env} -> POD DO HYPNUS não encontrado - nada será feito aqui"
  fi
done

Gera o script para execução do restore de um banco de dados.

#!/bin/bash
set +ef
set +x

ccode=$1
type=$2

if [ "$ccode" = "" ]; then
    echo "Required env variable ccode"
    exit 0
fi

if [ "$type" = "" ]; then
    type="producao"
fi

echo "Preparando o ambiente para restore do banco. Seguir os passos a seguir:"
kubectl -n $ccode scale deploy -l base=true --replicas=1 &> /dev/null
kubectl -n $ccode scale deploy $(kubectl -n $ccode get deploy --no-headers | grep -i config | awk '{print $1}') --replicas=0 &> /dev/null
kubectl -n $ccode scale deploy $(kubectl -n $ccode get deploy --no-headers | grep -i rest | awk '{print $1}') --replicas=0 &> /dev/null
kubectl -n $ccode scale deploy protheus-sleeper --replicas=0 &> /dev/null

echo -e "\033[2;36mIMPORTANTE: SERÁ UTILIZADO O ULTIMO BACKUP DE PRODUCAO GERADO. VERIFIQUE A DATA DO ULTIMO BACKUP"

dbaccess=$(kubectl -n $ccode get pod --no-headers | grep -i dbaccess | awk '{print $1}')
endpoint=$(kubectl -n $ccode get configmap protheus-config -o json | jq -r .data.DATABASE_ENDPOINT)
password=$(kubectl -n $ccode get secrets protheus-secrets-db-master -o json | jq -r .data.DATABASE_PASSWORD | base64 -d)
user=$(kubectl -n $ccode get secrets protheus-secrets-db-master -o json | jq -r .data.DATABASE_USER | base64 -d)
echo ""
echo -e "\033[35mPGPASSWORD=$""DATABASE_PASSWORD_$type psql -U $""DATABASE_USER_$type -h $""DATABASE_ENDPOINT -d $type\033[0m"
echo ""
echo -e "\033[2;33mPasso 1 - Entrar no container no dbaccess\033[0m"
echo "kubectl -n $ccode exec -it $dbaccess -- bash"
echo ""
echo -e "\033[2;33mPasso 2 - Matar as threads do banco\033[0m"
echo "PGPASSWORD=$password psql -U $user -h $endpoint -d postgres -c "'"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid();"'
echo ""
echo -e "\033[2;33mPasso 3 - Dropar o banco\033[0m"
echo "PGPASSWORD=$password psql -U $user -h $endpoint -d postgres -c "'"DROP DATABASE '$type';"'
echo ""
echo -e "\033[2;33mPasso 4 - Se não conseguir dropar o banco, rodar o passo 2 novamente\033[0m"
echo ""
echo -e "\033[2;33mPasso 5 - Sair do container do dbaccess e reinicia-lo\033[0m"
echo "exit"
echo "kubectl -n $ccode delete pod $dbaccess --force --grace-period=0"
echo ""
echo -e "\033[2;33mPasso 6 - Acompanhar o log do restore do banco de dados.\033[0m"
echo "kubectl -n $ccode logs -f deploy/protheus-dbaccess -c database-setup"
echo ""
echo -e "\033[2;33mPasso 7 - Concluindo.... Favor reiniciar o ambiente\033[0m"
echo "kubectl -n $ccode scale deploy -l base=true --replicas=1"
echo "kubectl -n $ccode scale deploy protheus-sleeper --replicas=1"
echo "kubectl -n $ccode delete pods --all --force --grace-period=0"
echo ""
echo ""

exit 0
echo "================================================================================="
echo "Processo de subida manual"

echo "> kubectl -n $ccode exec -it $dbaccess -c database-setup -- bash"

echo "> PGPASSWORD=$password psql -U $user -h $endpoint -d postgres -c "'"CREATE USER '$type' WITH LOGIN CREATEROLE INHERIT NOREPLICATION CONNECTION LIMIT -1 PASSWORD '"'"$password"'"';"'
echo "> PGPASSWORD=$password psql -U $user -h $endpoint -d postgres -c "'"CREATE DATABASE '$type' WITH LC_COLLATE='"'C'"' LC_CTYPE='"'C'"' ENCODING='"'WIN1252'"' TEMPLATE=template0;"'
echo "> PGPASSWORD=$password psql -U $user -h $endpoint -d postgres -c "'"ALTER DATABASE '$type' OWNER TO '$type';"'

echo "> PGPASSWORD=$password psql -U $user -h $endpoint -d $type -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'"

echo "> PGPASSWORD=$password psql -U $user -h $endpoint -d $type -c "'"GRANT ALL ON DATABASE '$type' TO '$type';"'
echo "> PGPASSWORD=$password psql -U $user -h $endpoint -d $type -c "'"GRANT USAGE, CREATE ON SCHEMA public TO '$type';"'

echo "> PGPASSWORD=$""DATABASE_PASSWORD_$type psql -U $""DATABASE_USER_$type -h $""DATABASE_ENDPOINT -d $type -c 'CREATE TABLE DUMP_OK ();'"

echo "> PGPASSWORD=$""DATABASE_PASSWORD_$type pg_restore -U $""DATABASE_USER_$type -h $""DATABASE_ENDPOINT -d $type --no-tablespaces --no-owner --no-acl --verbose < /tmp/"
# echo "PGPASSWORD=$""DATABASE_PASSWORD_$type psql -U $""DATABASE_USER_$type -h $""DATABASE_ENDPOINT -d $type < /backup/smarterp-backup-production/$ccode/dump/database-dump-producao.sql;'"
echo "================================================================================="