Magellan Linux

Contents of /alx-src/branches/alxconf-060/init.d/alxsetstate

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *