Release version 4.0.7.
[carnet-upgrade.git] / src / scripts / check_services
1 #!/bin/sh
2
3 # Load debconf stuff
4 . /usr/share/debconf/confmodule
5
6 # Load nase funkcije
7 . /usr/share/carnet-upgrade/data.sh
8 . /usr/share/carnet-upgrade/messages.sh
9 . /usr/share/carnet-upgrade/functions.sh
10
11 # provjeri da li su paketi iz prvog polja instalirani i to strpaj u debconf
12 cs_preupgrade () {
13   local services i
14
15   for i in $(echo "$services_to_check" | sed 's/                */      /g'| awk -F'    ' '{print $1}')
16   do
17     if pkg $i; then
18       if ! echo $services | grep -q $i; then
19         services="$services $i"
20       fi
21     fi
22   done
23
24   if [ -n "$services" ]; then
25     db_set carnet-upgrade/check-services "$services"
26     db_go || true
27   fi
28 }
29
30 cs_pgrep () {
31     local failed_services
32
33     while read line; do
34         # get service param from stdin
35         user=$(echo "$line" | awk -F '  ' '{print $2}')
36         psname=$(echo "$line" | awk -F '        ' '{print $3}')
37         nicename=$(echo "$line" | awk -F '      ' '{print $4}')
38
39         # check if service is running
40         if ! pgrep -u $user -f "$psname" > /dev/null; then
41             failed_services="$failed_services, $nicename"
42
43         fi
44     done
45
46     echo ${failed_services#, }
47 }
48
49 # uzmi iz debconfa instalirane servise, i provjeri rade li
50 cs_postupgrade () {
51   local services i failed_services msg_header
52
53   case "$queuename" in
54     upgrade) msg_header="$msg_finished_upgrade"
55       ;;
56     *)
57       msg_header="$msg_finished"
58       ;;
59   esac
60
61   db_get carnet-upgrade/check-services || true
62   services="${RET// /|}"
63
64   failed_services=$(
65     echo "$services_to_check" \
66     | grep -E "^($services)" \
67     | sed 's/           */      /g' \
68     | cs_pgrep
69   )
70
71   if [ -n "$failed_services" ]; then
72     notice "$msg_header$msg_failed_services$failed_services"
73   else
74     notice "$msg_header"
75   fi
76 }
77
78 # checkiraj da li je sto zapisano u debconf
79 cs_check () {
80   db_get carnet-upgrade/check-services || true
81   debconf="$RET"
82
83   if [ -n "$debconf" ]; then
84     exit 0
85   else
86     exit 1
87   fi
88 }
89
90 # prikazi sto je zapisano u debconf
91 cs_show () {
92   db_get carnet-upgrade/check-services || true
93   echo "$RET"
94 }
95
96 case "$1" in
97 preupgrade)     cs_preupgrade
98         ;;
99 postupgrade)    cs_postupgrade
100         ;;
101 check)  cs_check
102         ;;
103 show)   cs_show
104         ;;
105 *)      echo "Usage: $0 (preupgrade|postupgrade|check|show)"
106         exit 1 
107         ;;
108 esac
109 exit 0
110