cloner: fix a spelling error just for Bruce
[bootimage-initfs.git] / etc / library.sh
1 #!/bin/ash
2
3 eval `stty size 2>/dev/null | (read L C; echo LINES=${L:-24} COLUMNS=${C:-80})`
4
5 PRODUCT_NAME="Bootimage"
6 PRODUCT_VERSION="0.05"
7
8
9 ANSI_GREEN="\033[1;32m"
10 ANSI_RED="\033[1;31m"
11 ANSI_BLUE="\033[1;34m"
12 ANSI_PURPLE="\033[1;35m"
13 ANSI_LEFT="\033[${COLUMNS}G\033[10D"
14 ANSI_DONE="\033[0m"
15
16 PRINT_OK=`echo -n ${ANSI_LEFT}${ANSI_GREEN} [ OK ] ${ANSI_DONE}`
17 PRINT_FAIL=`echo -n ${ANSI_LEFT}${ANSI_RED} [ FAIL ] ${ANSI_DONE}`
18
19 if [ -e /tmp/servers.conf ]
20 then
21         . /tmp/servers.conf
22 fi
23
24 if [ -e /tmp/hardware.conf ]
25 then
26         . /tmp/hardware.conf
27 fi
28
29 if [ -e /tmp/breakin.conf ]
30 then
31         . /tmp/breakin.conf
32 fi
33
34 if [ -e /tmp/network.dhcp ]
35 then
36         . /tmp/network.dhcp
37 fi
38
39
40 msg() {
41         ARG1=$1
42         ARG2=$2
43
44         if [ "$ARG1" = "-n" ]
45         then
46                 echo -en ${ARG2} 
47         else 
48                 echo -e ${ARG1} 
49         fi
50 }
51
52 fail_msg() {
53         MSG=$1
54
55         # echo wget a error message to the server
56         msg "  ${PRINT_FAIL}"
57 }
58
59 ok_msg() {
60         MSG=$1
61
62         # echo wget a error message to the server
63         msg "  ${PRINT_OK}"
64 }
65
66 ok_or_fail() {
67         RETURN=$1
68
69         if [ "$RETURN" = "0" ]
70         then
71                 ok_msg
72         else
73                 fail_msg
74         fi
75 }
76
77 header() {
78         MSG=$1
79
80         msg -n "${ANSI_GREEN}"
81         msg "================================="
82         msg -n "${ANSI_BLUE}"
83         msg " ${MSG}"
84         msg -n "${ANSI_GREEN}"
85         msg "================================="
86         msg -n ${ANSI_DONE}
87
88 }
89
90 fatal_error() {
91         MSG=$1
92
93         msg ""
94         msg -n "${ANSI_RED}"
95         msg "========================================================"
96         msg "           Fatal Error - halting ${PRODUCT_NAME} ${PRODUCT_VERSION}"
97         msg "========================================================"
98         msg -n "${ANSI_BLUE}"
99         msg " ${MSG}"
100         msg -n "${ANSI_RED}"
101         msg "========================================================"
102         msg -n ${ANSI_DONE}
103         msg ""
104         exit 1
105 }
106
107 modprobe_module() {
108
109         MODULE_NAME=${1}
110
111         msg -n "Trying to load ${ANSI_BLUE}${MODULE_NAME}${ANSI_DONE}"
112         /sbin/modprobe "${MODULE_NAME}"  >> /tmp/stdout.log 2>> /tmp/stderr.log
113         if [ "$?" = "1" ];
114         then
115                 fail_msg
116                 return 1
117         else
118                 ok_msg
119                 return 0
120         fi
121 }
122
123 load_module() {
124
125         MODULE_PATH=${1}
126         MODULE_NAME=`basename ${MODULE_PATH}`
127
128         msg -n "Trying to load ${MODULE_NAME}"
129         /sbin/insmod "${MODULE_PATH}"  >> /tmp/stdout.log 2>> /tmp/stderr.log
130         if [ "$?" = "1" ];
131         then
132                 fail_msg
133                 return 1
134         else
135                 ok_msg
136                 return 0
137         fi
138 }
139
140 post_file_to_server() {
141
142         MSG=${1}
143         URIPATH=${2}
144         FILENAME=${3}
145
146         if [ "${BREAKIN_ID}" -lt 0 ]
147         then
148                 return 1
149         fi
150
151         msg -n "$MSG -> ${HTTP_SERVER}:${HTTP_PORT}"
152
153         URL="http://${HTTP_SERVER}:${HTTP_PORT}/${URIPATH}/${NET_MACADDR}/${BREAKIN_ID}"
154
155         wget --server-response -O /tmp/post.out --post-file=${FILENAME} --timeout=10 \
156                 --tries=3 ${URL} >> /tmp/stdout.log 2>> /tmp/stderr.log
157
158         if [ "$?" = 0 ]
159         then
160                 ok_msg  
161                 return 0
162         else
163                 fail_msg
164                 return 1
165         fi
166 }
167
168 breakin_start() {
169
170         msg -n "Notifing server that we've started the breakin process"
171         URL="http://${HTTP_SERVER}:${HTTP_PORT}/cgi-bin/breakin/start/${NET_MACADDR}"
172
173         wget --server-response -O /tmp/post.out --timeout=10 \
174                 --output-document=/tmp/breakin.conf \
175                 --tries=3 ${URL} 2>> /tmp/stderr.log
176
177         . /tmp/breakin.conf
178
179         if [ "$?" = 0 ]
180         then
181                 ok_msg  
182                 return 0
183         else
184                 fail_msg
185                 return 1
186         fi
187 }
188
189 breakin_stop() {
190
191         msg -n "Notifing server that we're stoping the breakin process"
192         URL="http://${HTTP_SERVER}:${HTTP_PORT}/cgi-bin/breakin/stop/${NET_MACADDR}/${BREAKIN_ID}"
193
194         wget --server-response -O /tmp/post.out --timeout=10 \
195                 --tries=3 ${URL} >> /tmp/stdout.log 2>> /tmp/stderr.log
196
197         if [ "$?" = 0 ]
198         then
199                 ok_msg  
200                 return 0
201         else
202                 fail_msg
203                 return 1
204         fi
205 }
206
207 breakin_update() {
208
209         if [ "${BREAKIN_ID}" -lt 0 ]
210         then
211                 return 0
212         fi
213
214         LOADAVG=0
215         TEMPS=""
216
217         if [ -e /proc/loadavg ]
218         then
219                 LOADAVG=`cat /proc/loadavg | awk '{print $1}'`
220         fi
221
222         if [ "${SENSOR_ENABLE}" != "" ]
223         then
224                 TEMPS=""
225                 if [ -e "/etc/sensors.conf" ]
226                 then
227                         for temp_i in `sensors | grep "^CPU[0-9] Temp" | cut -d":" -f2 | awk '{print $1 $2}' 2> /dev/null`
228                         do
229                                 TEMPS="${TEMPS} ${temp_i}"      
230                         done
231                 fi
232         fi
233
234         echo "LOADAVG=${LOADAVG}" > /tmp/update.dat
235         echo "TEMPERATURE=${TEMPS}" >> /tmp/update.dat
236
237         URL="http://${HTTP_SERVER}:${HTTP_PORT}/cgi-bin/breakin/update/${NET_MACADDR}/${BREAKIN_ID}"
238         wget --server-response -O /tmp/post.out --post-file=/tmp/update.dat --timeout=10 \
239                 --tries=3 ${URL} >> /tmp/stdout.log 2>> /tmp/stderr.log
240
241         if [ "$?" = 0 ]
242         then
243                 return 0
244         else
245                 return 1
246         fi
247 }
248
249
250
251
252
253 log_burnin_error() {
254         MSG=$1
255
256         # create a file to notify the LED process there is an error
257         touch /tmp/error_led
258         echo "${MSG}" >> /var/log/breakin.log
259 }