-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendcommandtar.sh
executable file
·141 lines (116 loc) · 3.87 KB
/
sendcommandtar.sh
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
#!/bin/bash
PREFIX=$(dirname $0)
if [ ! -e $PREFIX'/sdi.conf' ]; then
echo "ERROR: The $PREFIX/sdi.conf file does not exist or can not be accessed"
exit 1
fi
source $PREFIX'/sdi.conf'
#test if config is loaded
if test $? != 0; then
echo "ERROR: failed to load $PREFIX/sdi.conf file (launchsditunnel.sh)"
exit 1
elif ! source $PREFIX/misc.sh; then
echo "ERROR: failed to load $PREFIX/misc.sh file (launchsditunnel.sh)"
exit 1
elif ! source $PREFIX/parser.sh; then
echo "ERROR: failed to load $PREFIX/parser.sh file (launchsditunnel.sh)"
exit 1
elif ! source $PREFIX/sendfile.sh; then
echo "WARNING: failed to load $PREFIX/sendfile.sh file"
echo "WARNING: you will not be able to send files to hosts through SDI (fatal error)"
exit 1
fi
usage()
{
echo "Usage:"
echo " $0 [options]"
echo "Options:"
echo " --container=/full/patch/container.tar.gz Send and execute a docker container"
}
chose_host(){
# verifica se esta online
# verifica se suporta docker
# sistema de controle se esta ocupado ou com X (X < limit) containers em execucao
chose="$(cat .chose)"
if [ $chose -eq '0' ];then
HOST_DESTINO="10.132.111.184"
echo 1 > .chose
else
HOST_DESTINO="10.132.111.160"
echo 0 > .chose
fi
}
generate_id(){
# Source: https://gist.github.com/earthgecko/3089509
#
# bash generate random alphanumeric string
#
# bash generate random 64 character alphanumeric string (upper and lowercase) and
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
#check if pool folder exist
if [ ! -d "$CONTAINER_POOL" ]; then
mkdir $CONTAINER_POOL
if [ ! -d "$CONTAINER_POOL" ]; then
printf "ERRO: Pasta $CONTAINER_POOL não existe e não pode ser criada...\n"
exit 1
fi
fi
#check if the ticket is uniq
while [ -d "$CONTAINER_POOL/$NEW_UUID" ]
do
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
done
}
sendtarcommand(){
TARFILE=$1
TIMELIMIT=$2
#verifica se o container existe
if [ ! -f $TARFILE ]; then
echo "ERRO: O arquivo tar não existe ou não pode ser acessado..."
exit 1
fi
#escolhe um host para processar o container
#printf "Definindo cliente para envio...\n"
chose_host;
#printf "O container sera enviado para o cliente $HOST_DESTINO\n"
#generate the ID of the container
generate_id
#printf "Ticket de acompanhamento: $NEW_UUID\n"
printf $NEW_UUID"\n"
#copia para a pasta do container no pool
mkdir $CONTAINER_POOL/$NEW_UUID
cp $TARFILE $CONTAINER_POOL/$NEW_UUID/"tarfile"
#send the container
scp -q $CONTAINER_POOL/$NEW_UUID/"tarfile" $HOST_DESTINO:/tarfiletoexecute/$NEW_UUID
#insert the execution in the "bd"
echo -e "$(date)" > $CONTAINER_POOL/$NEW_UUID/"date"
echo -e $HOST_DESTINO > $CONTAINER_POOL/$NEW_UUID/"destination_host"
echo -e "SENT-RUNNING" > $CONTAINER_POOL/$NEW_UUID/"status"
echo -e 1 > $CONTAINER_POOL/$NEW_UUID/"attempts"
echo -e $TIMELIMIT > $CONTAINER_POOL/$NEW_UUID/"timelimit"
}
case $1 in
--tar=?*)
case $2 in
--tl=?*)
TIMELIMIT=$(echo $2| cut -d'=' -f2)
if [ $TIMELIMIT = "--" ]; then
printf "WARNING: Time limit option was not set! The default limit will be used (1 hour).\n"
TIMELIMIT=3600
fi
;;
*)
printf "WARNING: Time limit option was not set! The default limit will be used (1 hour).\n"
TIMELIMIT=3600
;;
esac
#printf "Time limit: "$TIMELIMIT" second(s)\n"
sendtarcommand $(echo $1| cut -d'=' -f2) $TIMELIMIT
exit 0
;;
*)
printf "Unknown option."
usage
exit 1
;;
esac