Annotation of /alx-src/branches/alxconf-060/init.d/alxsetstate
Parent Directory | Revision Log
Revision 3482 -
(hide annotations)
(download)
Mon Apr 16 08:38:27 2012 UTC (12 years, 5 months ago) by niro
File size: 3360 byte(s)
Mon Apr 16 08:38:27 2012 UTC (12 years, 5 months ago) by niro
File size: 3360 byte(s)
-use addconfig to create config and state files
1 | niro | 221 | #!/bin/bash |
2 | niro | 1975 | # $Id$ |
3 | niro | 221 | |
4 | niro | 2007 | #%rlevels: 3:s 0:k |
5 | niro | 221 | #%start: 99 |
6 | #%stop: 01 | ||
7 | |||
8 | #deps | ||
9 | #%needs: | ||
10 | #%before: | ||
11 | #%after: | ||
12 | |||
13 | niro | 1975 | source /etc/conf.d/rc |
14 | niro | 221 | source $rc_functions |
15 | |||
16 | niro | 347 | # mysql settings |
17 | niro | 226 | source /etc/alxconfig-ng/config.rc |
18 | niro | 2141 | # helper functions |
19 | niro | 2778 | source ${ALX_FUNCTIONS}/mysqlfunctions |
20 | source ${ALX_FUNCTIONS}/serial_functions | ||
21 | source ${ALX_FUNCTIONS}/common | ||
22 | niro | 221 | |
23 | niro | 347 | # unset vars which may kill us |
24 | unset ALX_SERIAL ALX_STATE ALX_IFACE | ||
25 | niro | 221 | |
26 | niro | 347 | # get current system state |
27 | niro | 228 | if [ -f /etc/alxconfig-ng/state/state ] |
28 | niro | 221 | then |
29 | niro | 228 | source /etc/alxconfig-ng/state/state |
30 | niro | 221 | else |
31 | ALX_STATE=error | ||
32 | fi | ||
33 | |||
34 | niro | 370 | if [ -f ${SETTINGSPATH}/confd-networking ] |
35 | then | ||
36 | ALX_IFACE="$(< ${SETTINGSPATH}/confd-networking)" | ||
37 | fi | ||
38 | niro | 347 | [[ -z ${ALX_IFACE} ]] && export ALX_IFACE=eth0 |
39 | |||
40 | niro | 221 | # need to put this to an extra init script which will be |
41 | # executed when the real network is up and not the preliminary. | ||
42 | # nice name is alx_connected_state or sth like this | ||
43 | niro | 347 | set_current_network_state() |
44 | { | ||
45 | niro | 3480 | local cur_ip cur_mac cur_mtime id |
46 | niro | 221 | |
47 | niro | 2141 | rc_mecho "Register system to database" |
48 | niro | 221 | |
49 | niro | 3480 | cur_ip=$(/sbin/ifconfig ${ALX_IFACE} | sed -n '/addr:/s/ [^r]*..//gp') |
50 | cur_mac=$(/sbin/ifconfig ${ALX_IFACE} | grep HWaddr | cut -d ' ' -f11) | ||
51 | cur_mtime=$(date +%s) | ||
52 | niro | 221 | |
53 | # validate current serial | ||
54 | niro | 3480 | if ! validate_serial "${ALX_SERIAL}" "${ALX_REG_DATE}" "${cur_mac}" |
55 | niro | 221 | then |
56 | niro | 306 | # abort on non valid serial |
57 | niro | 221 | ALX_STATE="invalid serial" |
58 | niro | 3482 | local CONFIG="/etc/alxconfig-ng/state/state" |
59 | clearconfig | ||
60 | addconfig "ALX_STATE=\"${ALX_STATE}\"" | ||
61 | niro | 306 | show_invalid_serial_msg |
62 | exit 1 | ||
63 | niro | 221 | fi |
64 | |||
65 | niro | 347 | # first check if an entry exist with my serial |
66 | niro | 221 | # if it exist update this entry else insert a new one |
67 | niro | 3480 | id=$(mysqldo "select serial from state_connected where serial=${ALX_SERIAL};") |
68 | niro | 221 | |
69 | niro | 3480 | if [ -n "${id}" ] |
70 | niro | 221 | then |
71 | niro | 347 | # run an update |
72 | niro | 221 | |
73 | niro | 347 | # nice status |
74 | niro | 221 | $CURS_UP |
75 | $SET_WCOL | ||
76 | niro | 2141 | rc_echo "[ SN: ${ALX_SERIAL}, U, ${ALX_STATE} ]" |
77 | niro | 221 | |
78 | niro | 347 | mysqldo "update state_connected set |
79 | niro | 221 | hostname='${HOSTNAME}', |
80 | niro | 3480 | ip='${cur_ip}', |
81 | mac='${cur_mac}', | ||
82 | niro | 221 | state='${ALX_STATE}', |
83 | niro | 3480 | mtime='${cur_mtime}' |
84 | niro | 221 | where serial=${ALX_SERIAL};" |
85 | else | ||
86 | niro | 347 | # run an insert |
87 | niro | 221 | |
88 | niro | 347 | # nice status |
89 | niro | 221 | $CURS_UP |
90 | $SET_WCOL | ||
91 | niro | 2141 | rc_echo "[ SN: ${ALX_SERIAL}, N, ${ALX_STATE} ]" |
92 | niro | 221 | |
93 | niro | 347 | mysqldo "insert into state_connected( |
94 | niro | 221 | serial, |
95 | hostname, | ||
96 | ip, | ||
97 | mac, | ||
98 | state, | ||
99 | mtime | ||
100 | ) | ||
101 | values( | ||
102 | '${ALX_SERIAL}', | ||
103 | '${HOSTNAME}', | ||
104 | niro | 3480 | '${cur_ip}', |
105 | '${cur_mac}', | ||
106 | niro | 221 | '${ALX_STATE}', |
107 | niro | 3480 | '${cur_mtime}' |
108 | niro | 221 | );" |
109 | fi | ||
110 | } | ||
111 | |||
112 | |||
113 | # need to put this to an extra init script which will | ||
114 | # be executed first when the system is going down. | ||
115 | # nice name is alx_connected_state or sth like this | ||
116 | niro | 347 | unset_alx_connected() |
117 | { | ||
118 | niro | 3480 | local cur_mac |
119 | niro | 221 | |
120 | niro | 3480 | cur_mac=$(/sbin/ifconfig ${ALX_IFACE} | grep HWaddr | cut -d ' ' -f11) |
121 | niro | 274 | |
122 | niro | 2141 | rc_mecho "Unregister system from database" |
123 | niro | 221 | |
124 | niro | 2123 | # nice status |
125 | $CURS_UP | ||
126 | $SET_WCOL | ||
127 | niro | 2141 | rc_echo "[ SN: ${ALX_SERIAL} ]" |
128 | |||
129 | niro | 3480 | mysqldo "delete from state_connected where serial='${ALX_SERIAL}' and mac='${cur_mac}';" |
130 | niro | 221 | evaluate_retval |
131 | } | ||
132 | |||
133 | niro | 347 | |
134 | niro | 221 | ########### starts here ################ |
135 | |||
136 | niro | 347 | # first of all get current system serial |
137 | niro | 228 | if [ -f /etc/alxconfig-ng/serial ] |
138 | niro | 221 | then |
139 | niro | 228 | source /etc/alxconfig-ng/serial |
140 | niro | 221 | else |
141 | ALX_SERIAL=0 | ||
142 | fi | ||
143 | |||
144 | case $1 in | ||
145 | start) | ||
146 | niro | 283 | # check if mysql server is reachable |
147 | # if not abort this script | ||
148 | reach_mysql_server || exit 1 | ||
149 | |||
150 | niro | 221 | set_current_network_state |
151 | niro | 283 | evaluate_retval |
152 | niro | 221 | ;; |
153 | |||
154 | stop) | ||
155 | niro | 283 | # check if mysql server is reachable |
156 | # if not abort this script | ||
157 | reach_mysql_server || exit 1 | ||
158 | |||
159 | niro | 221 | unset_alx_connected |
160 | niro | 283 | evaluate_retval |
161 | niro | 221 | ;; |
162 | *) | ||
163 | niro | 3479 | rc_echo "Usage: $0 {start|stop} ..." |
164 | niro | 221 | ;; |
165 | niro | 274 | esac |
166 | niro | 283 |
Properties
Name | Value |
---|---|
svn:executable | * |