-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatebots.sh
More file actions
156 lines (132 loc) · 4.49 KB
/
createbots.sh
File metadata and controls
156 lines (132 loc) · 4.49 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
CURRENT_DATE=$(date +"%d/%m/%Y")
DEFAULT_DB="postgres"
# DIM / APP
HOST_SERVER_APP=""
USER_SERVER_APP=""
DB_SERVER_APP=""
PORT_SERVER_APP=""
PASS_SERVER_APP=""
script_path=`dirname "$BASH_SOURCE"`
argsArray=("$@")
argsOpt=("cod-ibge" "cidade" "jid" "type")
bashUsageSample="> ./createbots.sh cod-ibge=261110 cidade=Petrolina-PE jid=pe_petrolina type=S"
# DB Operations
DELETE_ROBO="DELETE FROM robo WHERE cod_ibge_municipio= "
INSERT_ROBO="INSERT INTO robo (name, last_seen, status, version, active, cod_ibge_municipio, jid, system_category_id) VALUES "
QUERY_CHECK_CONN="SELECT 'DB Connection OK' FROM pg_database LIMIT 1"
if ! [ -x "$(command -v psql)" ]
then
echo "ERROR: postgresql-client nao esta instalado. Instale uma versao do postgresql-client e tente novamente"
exit -1
elif [ ${#argsArray[@]} -eq 0 ]
then
echo "ERROR: nenhum argumento informado"
echo "INFO: argumentos validos ==> ${argsOpt[@]}"
echo "INFO: exemplo de uso"
echo $bashUsageSample
exit -1
fi
for ARGUMENT in "$@"
do
KEY=$(echo $ARGUMENT | cut -f1 -d=)
VALUE=$(echo $ARGUMENT | cut -f2 -d=)
case "$KEY" in
cod-ibge)
CODIBGE=${VALUE}
;;
cidade)
CIDADE=${VALUE}
;;
jid)
JID=${VALUE}
;;
type)
TYPE=${VALUE}
;;
custom-sql)
CUSTOM_SQL_COMMAND=${VALUE}
;;
*)
esac
done
deleteRegistrosRobo() {
sqlInstruction="$DELETE_ROBO'"$CODIBGE"';"
echo "INFO: $sqlInstruction"
execSQLCommand $sqlInstruction "deleteRegistrosRobo()"
}
# Executa uma instrucao SQL
# $1 Instrução
# $2 Mensagem para debugger (opcional)
execSQLCommand() {
result=`PGPASSWORD="$PASS_SERVER_APP" psql -U "$USER_SERVER_APP" -d "$DB_SERVER_APP" -h "$HOST_SERVER_APP" -p "$PORT_SERVER_APP" -X -A -t -c "$1"`
if ! [ $? -eq 0 ] ; then
echo "Error: problema ao executar instrucao SQL. Instrucao: $1"
echo "Base: $DB_SERVER_APP"
exit -1
fi
echo "DEBUG: return for [$2] instruction is => $result"
}
main() {
ARRAY_SISTEMA_S=(SIM-SINASC eSUS CNES SINAN)
ARRAY_SISTEMA_X=(SIM-SINASC-SINAN eSUS CNES)
if [ "$1" == "S" ] ; then
for sis in ${ARRAY_SISTEMA_S[@]}
do
validaArgsRobo
registraNovoRobo $sis
done
elif [ "$1" == "X" ] ; then
for sis in ${ARRAY_SISTEMA_X[@]}
do
validaArgsRobo
registraNovoRobo $sis
done
elif [ "$1" == "R" ] ; then
validaArgsRemocaoRobos
# deleteRegistrosRobo FIX
else
echo "WARN: opcoes disponiveis: S - Simples, X - SIM|SINAN|SINASC juntos, R - Deleta os cadastros para uma dada cidade"
exit -1
fi
}
registraNovoRobo() {
jidPart=$(sed "s/[\-]\+//g" <<< $1)
jidPart="$JID""_""${jidPart,,}"
sysPart=$(sed "s/[\-]\+/\ \-\ /g" <<< $1)
sqlInstruction="$INSERT_ROBO""('$CIDADE - $sysPart', now(), 'DOWN', 11, TRUE , '$CODIBGE', '$jidPart@service.in/service', -23119);"
echo "INFO: $sqlInstruction"
execSQLCommand "$sqlInstruction" "registraNovoRobo()"
}
validaArgsRemocaoRobos() {
if [ -z "$CODIBGE" ]
then
echo "ERROR: o argumento [cod-ibge] nao foi informado. Informe o codigo do IBGE para que o cadastro de robo possa prosseguir corretamente"
echo $bashUsageSample
exit -1
fi
}
validaArgsRobo() {
if [ -z "$CODIBGE" ]
then
echo "ERROR: o argumento [cod-ibge] nao foi informado. informe o codigo do IBGE para que o cadastro de robo possa prosseguir corretamente"
echo $bashUsageSample
exit -1
fi
if [ -z "$CIDADE" ]
then
echo "ERROR: o argumento [cidade] nao foi informado. informe o nome da cidade para que o cadastro de robo possa prosseguir corretamente"
echo $bashUsageSample
exit -1
fi
if [ -z "$JID" ]
then
echo "ERROR: o argumento [jid] nao foi informado. informe o nome da cidade para que o cadastro de robo possa prosseguir corretamente"
echo $bashUsageSample
exit -1
fi
}
execSQLCommand "$QUERY_CHECK_CONN" "check connection with $DB_SERVER_APP"
main "$TYPE"
echo "INFO: processo finalizado com sucesso!"
exit 0