forked from bdereims/cPodFactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpod_lease.sh
executable file
·100 lines (92 loc) · 3.87 KB
/
cpod_lease.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
#!/bin/bash
#afreslon
#requires the "at" package that allows the execution of a given command later in time (as a planned job)
#please make sure the atd service is started and "enabled" (aka will restart if unexpectidly killed)
. ./env
[ "${LEASE}" == "NO" ] && exit 0
create(){
TODAY=$( date +%s )
EXPIRATION_DATE=$( expr ${TODAY} + 1209600 )
EXPIRATION_DATE=$( date -d@${EXPIRATION_DATE} )
jobid=$( echo "bash compute/stop_vapp.sh ${1} && sleep 3s && sed -i \"/cpod-${1} ${2} stop/d\" \"extra/leases/leases.txt\" " | at now+2weeks 2>&1 | tail -n 1 | cut -d' ' -f 2 )
echo ${jobid} >> extra/leases/leases.txt
sed -i "$ s/$/ cpod-${1} ${2} stop $(date -d '+2 weeks' '+%d\/%m\/%y')/" "extra/leases/leases.txt"
echo "At Job n°${jobid} successfully created to stop the cpod-${1} on $(date -d '+2 weeks' '+%d/%m/%y')"
./extra/post_slack.sh ":warning: Your cPod *${1}* will be available until *${EXPIRATION_DATE}*. On this date it'll be stopped :warning:"
#NO LONGER DELETING THE CPODS : to have a deletion task just uncomment everything
#jobid=$( echo "bash delete_cpod.sh ${1} ${2} && sleep 3s && sed -i \"/cpod-${1} ${2} delete/d\" \"extra/leases/leases.txt\" " | at now+3weeks 2>&1 | tail -n 1 | cut -d' ' -f 2 )
#echo ${jobid} >> extra/leases/leases.txt
#sed -i "$ s/$/ cpod-${1} ${2} delete $(date -d '+3 weeks' '+%d\/%m\/%y')" "extra/leases/leases.txt"
#echo "At Job n°${jobid} successfully created to delete the cpod-${1} $(date -d '+3 weeks' '+%d/%m/%y')"
#./extra/post_slack.sh "/remind @${2} Your cPod *${1}* will be deleted in 3 days if you don't reach to our team in 11 days"
}
delete(){
jobid=$( grep "cpod-${1} ${2} stop" "extra/leases/leases.txt" | cut -d' ' -f 1 )
atrm ${jobid} 2>/dev/null
if [ $? -eq 0 ]
then
sed -i "/cpod-${1} ${2} stop/d" "extra/leases/leases.txt"
echo "At Job n°${jobid} successfully deleted to stop the cpod-${1} $( grep "cpod-${1} ${2} stop" "extra/leases/leases.txt" | cut -d' ' -f 5 )"
#NO LONGER DELETING CPODS : to have a deletion task just uncomment everything
#jobid=$( grep "cpod-${1} ${2} delete" "extra/leases/leases.txt"| cut -d' ' -f 1 )
#atrm ${jobid} 2>/dev/null
#sed -i "/cpod-${1} ${2} delete/d" "extra/leases/leases.txt"
#echo "At Job n°${jobid} successfully deleted to delete the cpod-${1} $(grep "cpod-${1} ${2} delete" "extra/leases/leases.txt" | cut -d' ' -f 5 )"
else
#echo "This cPod has already been stopped but will delete the deletion task"
#jobid=$( grep "cpod-${1} ${2} delete" "extra/leases/leases.txt"| cut -d' ' -f 1 )
#atrm ${jobid} 2>/dev/null
#if [ $? -eq 0 ]
#then
# sed -i "/cpod-${1} ${2} delete/d" "extra/leases/leases.txt"
# echo "At Job n°${jobid} successfully deleted to delete the cpod-${1} $(grep "cpod-${1} ${2} delete" "extra/leases/leases.txt" | cut -d' ' -f 5 )"
#else
echo "cPod ${1} either doesn't exist or doesn't have a lease applied to it"
#fi
fi
}
renew(){
delete ${1} ${2}
create ${1} ${2}
}
debug()
{
jobid=$( echo "bash extra/temp.sh ${1} ${2} && sleep 3s && sed -i \"/cpod-$1 $2 stop/d\" \"extra/lease/leases.txt\" " | at now+2minutes 2>&1 | tail -n 1 | cut -d' ' -f 2 )
echo ${jobid} >> extra/lease/leases.txt
sed -i "$ s/$/ cpod-${1} ${2} stop $(date -d '+2 weeks' '+%d\/%m\/%y')/" "extra/lease/leases.txt"
echo "At Job n°${jobid} successfully created to stop the cpod-${1} on $(date -d '+2 weeks' '+%d/%m/%y')"
}
list(){
echo "======cPod Leases"
echo "n° cPod Name Owner Action Date"
echo "-- --------- ------ ----- -----"
sed "s/\ /\t/g" "extra/leases/leases.txt"
}
main(){
if [ "${1}" == "" ] || [ "${2}" == "" ] || [ "${3}" == "" ]
then
echo 'bad usage : ./cpod_lease.sh <action> (i.e create, renew, delete or debug) <cpod name> <owner>'
exit 1
fi
case $1 in
create)
create $2 $3
;;
renew)
renew $2 $3
;;
delete)
delete $2 $3
;;
debug)
debug $2 $3
;;
list)
list
;;
*)
echo "${1} isn't a valid action"
;;
esac
}
main $1 $2 $3