deleted subcommands file ; changed pkg working directories ; moved commands to comman...
[opensuse:pack-tools.git] / commands / wfb
1 #!/bin/bash
2 #wait for build
3 #script periodically checks if all build results succeeded, with respect of timeout and other results(failed & unresolvable)
4
5 #un/comment for debug/release
6 #set -x
7
8 #check all results of build in actual directory
9 function check_dir()
10 {
11         #get results from osc, 5. column contains status message
12         #exclude excluded status and merge repeating into one
13         _results=$(mktemp)
14         osc r --csv "$archopt$repoopt" | cut -d '|' -f 5 | grep -v excluded | grep -v disabled |  sort -u > $_results
15         
16         cat $_results
17         
18         #check for lines containing failed or unresolvable
19         if  grep -q failed $_results ; then
20                 return 2
21         fi
22
23         if  grep -q unresolvable $_results ; then
24                 return 3
25         fi
26
27         #check for succeeded as the last one, always
28         if  grep -q succeeded $_results ; then
29                 if [ "`cat $_results | wc -l`" = "1" ]
30                 then
31                         return 0
32                 fi
33         fi
34
35         #otherwise building...
36         return 1
37 }
38
39 #prints basic information about this script
40 function wfb_usage()
41 {
42         echo "Usage: waitforbuild.sh [-i|--interval SECONDS] [-l|--loops NUM] [-a|--arch=ARCH_1] ... [-a|--arch=ARCH_N] [-r|--repo=REPO_1] ... [-r|--repo=REPO_N] [path] [path2 ...]"
43         echo "or as function do_waitforbuild (+ same argument list)"
44         echo ""
45         echo "Periodically check all paths for build status - exit after all succeeded OR exit with fail after time out"
46         echo "Default values: loop count = 60; interval = 10 seconds; directory = ."
47         echo "Return codes: 0 = succeeded; 1 = time out; 2 = failed; 3 = unresolvable"
48 }
49
50 #periodically check for build status
51 function do_waitforbuild()
52 {
53         _loops=60;
54         _interval=10
55
56         #if there are no parameters
57         if [ -z "${1}" ]; then
58                 #check if cwd is a valid osc directory
59                 if [ -e "./.osc/_package" ]; then
60                         #add cwd to arguments
61                         eval set -- '.'
62                 else
63                         #just print usage and exit
64                         wfb_usage
65                         exit 1
66                 fi
67         fi
68
69         #parsing arguments(i, l, a, r - with parameters; h)     
70         ARGS="$(/usr/bin/getopt -a -o i: --long interval: -o l: --long loops: -o a: --long arch: -o r: --long repo: -o h --long 'help' -- "${@}")"
71         eval set -- ${ARGS}
72         
73         while [ -n "${1}" ]; do
74         case "${1}" in
75                 -i|--interval)
76                         _interval="${2}"
77                         shift 2
78                 ;;
79
80                 -l|--loops)
81                         _loops="${2}"
82                         shift 2
83                 ;;
84                 
85                 -a|--arch)
86                         #add to archs array
87                         archs[$[${#archs[@]}+1]]="${2}";
88                         shift 2         
89                 ;;
90                 
91                 -r|--repo)      
92                         #add to repos array
93                         repos[$[${#repos[@]}+1]]="${2}"
94                         shift 2
95                 ;;
96
97                 -h|--help)
98                         wfb_usage
99                         exit 1
100                 ;;
101
102                 --)
103                         shift 1
104                 ;;      
105
106                 *)
107                         break
108                 ;;
109         esac
110         done
111
112
113         #after archs and repos are full, populate them into archopt and repoopt for osc results as arguments
114         archopt=""
115         repoopt=""
116                 
117         for a in "${archs[@]}"
118         do
119                 #concat actual archopt + '-a' + iterator
120                 archopt=$archopt' -a '$a
121         done    
122         
123         for r in "${repos[@]}"
124         do
125                 #same as with archopt
126                 repoopt=$repoopt' -r '$r
127         done
128
129
130         #if no directories where set, make cwd the default one
131         if [ -z "${1}" ] ; then
132                 eval set -- '.'
133         fi
134         
135         #fill status array with false values, check if all arguments are directories
136         declare -A STATUS
137         while [ -n "${1}" ]; do
138                 if [ ! -d "${1}" ]; then
139                         echo "WARNING: ${1} does not exists, skipping"
140                         shift 1
141                         continue
142                 fi
143       
144                 #check of valid directory
145                 if [ ! -e "${1}/.osc/_project" ]; then
146                         echo "WARNING: ${1}/.osc/_project does not exist, skipping"
147                         shift 1
148                         continue
149                 fi
150
151                 #check of valid directory
152                 if [ ! -e "${1}/.osc/_package" ]; then
153                         echo "WARNING: ${1}/.osc/_package does not exist, skipping"
154                         shift 1
155                         continue
156                 fi
157
158                 STATUS[${1}]=false
159                 shift 1
160         done
161         
162         #repeat _loops times, if still not done, time out
163         for (( i=1; i<= _loops ; i++ ))
164         do
165                 ALL_SUCCEEDED=false
166                 for KEY in ${!STATUS[@]}; do
167                         # skip dirs with succesfull status
168                         if ${STATUS[${KEY}]}; then
169                                 ALL_SUCCEEDED=true
170                                 continue
171                         fi
172         
173                         #if build inside dir is successful, flag is set to true
174                         #else flag is set to false & break out of cycle
175                         pushd ${KEY} &>/dev/null
176                         if check_dir; then  
177                                 STATUS[${KEY}]=true
178                                 ALL_SUCCEEDED=true
179                         else
180                                 #check return value from check_dir, if 2 or 3 = exit with appropriate return code
181                                 if [ "$?" = "2" ]; then
182                                         echo "error: some package has failed"
183                                         return 2;
184                                 fi
185                                 if [ "$?" = "3" ]; then
186                                         echo "error: some package is unresolvable"
187                                         return 3;
188                                 fi
189                                 ALL_SUCCEEDED=false;
190                                 break;
191                         fi
192                         popd &>/dev/null
193                 done
194
195                 if $ALL_SUCCEEDED ; then
196                         echo "success: all done";
197                         return 0;
198                 fi
199
200                 sleep $_interval;
201         done
202
203         echo "error: time out";
204         return 1;
205 }
206
207 do_waitforbuild $@
208
209