summaryrefslogtreecommitdiff
path: root/i/pc104/initrd/update.sh
blob: 1d7b31b220a66b2fab4c854952808529450374b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
# Script for updating the marvin program in the initrd.

#set -e
set -u

# Variables - Default values
# Local directory name containing the representation for the initrd
ROBOT_LOCAL_DIR="./robot"
# Remote directory in the initrd
ROBOT_REMOTE_DIR="/robot"
# Name of the script to launch the robot program
ROBOT_START_SCRIPT="start.sh"
# Build directory
ROBOT_BUILD_DIR="."
# Config directory
ROBOT_CONF_DIR="$ROBOT_BUILD_DIR/rc"
# Max size of the config directory (in bytes)
ROBOT_MAX_SIZE="4000000"
# Hostname of the robot
ROBOT_HOST="pilipili"
# In autonomous mode, wait time for bringing up the ssh deamon
ROBOT_WAIT_TIME="2.5s"
# Compression
ROBOT_NOCOMPRESS=0
# Quiet ?
ROBOT_QUIET=0
# SSH options
ROBOT_SSH_OPTS="-o ConnectTimeout 1"
# Force autonomous mode
ROBOT_FORCE_AUTONOMOUS=0

if [ $# -gt 6 ]
then    
	usage
fi
while getopts "b:s:r:zaqh" option
do      
	case $option in
		b) ROBOT_BUILD_DIR="$OPTARG";;
#		d) ROBOT_LOCAL_DIR="$OPTARG";;
		s) ROBOT_CONF_MAX_SIZE="$OPTARG";;
		r) ROBOT_HOST="$OPTARG";;
		z) ROBOT_NOCOMPRESS=1;;
		a) ROBOT_FORCE_AUTONOMOUS=1;;
		q) ROBOT_QUIET=1;;
		h) help_initrd;;
		\?) help_initrd;;
	esac
done

# Log function
# First parameter : "error message"
# Seconde parameter : "level"
log ()
{
	if (( $ROBOT_QUIET == 0 )); then
		log_prefix="[II]"
		log_line=1
		if [ $# -eq 2 ]; then
			case $2 in
				1) log_prefix="[II]";;
				2) log_prefix="     >>>";;
				3) log_line=0;;
				*) log_prefix="[II]";;
			esac
		fi
		if (( $log_line == 1 )); then
			echo -en "$log_prefix $1 : "
		else
			echo -e "$1"
		fi
	fi
}

# Log error function
# First parameter : "error message"
log_error ()
{
	log "Error" 3
	echo -e "[EE] $1" >&2
}

# Usage - Help
usage ()
{
	echo "Usage: $0 [-b <dir>] [-s <size>] [-r <host>] [-z]"
	echo "	-b <dir>	: build directory of marvin"
	echo "	-s <size>	: size max of binaries and config (in bytes)"
	echo "	-r <host>	: robot host"
	echo "	-z              : disable compression"
	echo "	-z              : force automous mode (for second update)"
	echo "	-q		: be quiet (expect for errors)"
	echo "  -h              : this message"
	exit
}

check_rsync ()
{
	if [[ "x$(whereis rsync | cut -d ':' -f 2)" == "x" ]]
	then
		log_error "Please install rsync"
		exit 1
	fi
}
# Check the structure (directory robot and startup script)
check_create_structure ()
{
	log "Checking directory structure" 1
	# Check the local directory exist
	if [[ ! -d $ROBOT_LOCAL_DIR ]]
	then
		mkdir -p $ROBOT_LOCAL_DIR/lib
	fi
	# Check we have a start script
	if [[ ! -x $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT ]]
	then
		# Create a default one
		cat <<EOF > $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT
# Startup script launched when the PC104 boot.
# It will be executde by other script.
# IMPORTANT : the commande must begin with a "./" !
# Example :
# ./test_ai -z
EOF
		chmod +x $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT
		# Need to tune it by the user
		log_error "Please tune your $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT"
		# XXX vim it ?
		exit 2
	else
		# A little bit of cleaning
		for file in $ROBOT_LOCAL_DIR/*
		do
			if [[ ! -d $file && $file != "$ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT" ]]
			then
				rm -f $file
			fi
		done
	fi
	log "Ok" 3
}

# Check that there is a line in the startup script for launchin a program and
# a binary existing
check_install_binary ()
{
	log "Checking binary and libraries" 1
	if [[ $(grep -c "^\\./" $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT) -eq 0 ]]
	then
		log_error "No command found in your $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT"
		exit 3
	fi
	for line in "$(grep "^\\./" $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT)"
	do
		bin="$(echo "$line" | cut -d ' ' -f 1)"
		if [[ ! -x $ROBOT_BUILD_DIR/$bin ]]
		then
			log_error "Binary not found : $bin"
			exit 4
		else
			# Copy the binary to the local dir
			cp -p $ROBOT_BUILD_DIR/$bin $ROBOT_LOCAL_DIR/
			# Remove useless symbol
			strip -p $ROBOT_LOCAL_DIR/$bin
			# Copy needed libraries
			for lib in $(ldd $ROBOT_BUILD_DIR/$bin | cut -d ' ' -f 3)
			do
				cp -p $lib $ROBOT_LOCAL_DIR/lib
			done
			# For the one that begins with a /
			for lib in $(ldd $ROBOT_BUILD_DIR/$bin | grep -E "^[[:space:]]*/" | cut -f 2 |cut -f 1 -d ' ')
			do
				cp -p $lib $ROBOT_LOCAL_DIR/lib
			done
		fi
	done
	log "Ok" 3
}

# Install the configuration
check_install_config ()
{
	log "Checking configuration ($ROBOT_CONF_DIR)" 1
	if [ ! -d $ROBOT_CONF_DIR ]
	then
		log_error "Configuration directory not found ($ROBOT_CONF_DIR)"
		exit 5
	fi
	# There is no way to know the files needed, so we rsync everything
	rsync -azLu --exclude=.svn $ROBOT_CONF_DIR $ROBOT_LOCAL_DIR/
	log "Ok" 3
}

check_size ()
{
	log "Checking size" 1
	# First we check the size
	if [[ $(du -bs $ROBOT_LOCAL_DIR | cut -f 1) -gt $ROBOT_MAX_SIZE ]]
	then
		log_error "$ROBOT_LOCAL_DIR is too big (> $ROBOT_MAX_SIZE)"
		exit 6
	fi
	log "Ok" 3
}

upload_data ()
{
	log "Uploading data to $ROBOT_HOST" 1
	log "" 3
	log "Checking $ROBOT_HOST is up" 2
	# Check if host exist and is up
	ping -c 1 -w 1 -q $ROBOT_HOST > /dev/null
	case $? in
		0)
		# Robot is online !
		log "Ok" 3
		;;
		1)
		# Host exist but is offline
		log_error "$ROBOT_HOST is currently down"
		exit 6
		;;
		2)
		# Host does not exist
		log_error "$ROBOT_HOST does not exist"
		exit 7
		;;
		*)
		# Unknown error...
		log_error "Ping failed, unknown error"
		exit 8
		;;
	esac

	ROBOT_AUTONOMOUS=0
	# Are we in automous mode ?
	log "Autonomous mode" 2
	if (( $ROBOT_FORCE_AUTONOMOUS == 1 )); then
		ROBOT_AUTONOMOUS=1
	fi
	nc -w 1 $ROBOT_HOST 1234 > /dev/null 2>&1
	if [[ $? -eq 0 ]]
	then
		log "Yes" 3
		ROBOT_AUTONOMOUS=1
		sleep $ROBOT_WAIT_TIME
	else
		log "No" 3
	fi

	# Mount the initrd
	log "Mouting the initrd" 2
	
	if (( $ROBOT_AUTONOMOUS == 1 )); then
		ssh "$ROBOT_SSH_OPTS" root@$ROBOT_HOST "\
		gunzip -f -c /initrd/last > /dev/shm/initrd_cur && \
		mount -o loop /dev/shm/initrd_cur /mnt/initrd"
	else
		# TODO not implemented yet
		log "Not implemented..." 3
	fi
	log "Ok" 3

	# Rsync data
	log "Rsyncing data" 2
	rsync -au $ROBOT_LOCAL_DIR/ root@$ROBOT_HOST:/mnt/initrd/$ROBOT_REMOTE_DIR/
	log "Ok" 3
	
	# D�monter et recompresser l'initrd voir rotater tout
	# TODO Backup, rotation, et less cp !
	if (( $ROBOT_AUTONOMOUS == 1 )); then
		if (( $ROBOT_NOCOMPRESS == 1 )); then
			log "Umounting the initrd and move it" 2
			ssh "$ROBOT_SSH_OPTS" root@$ROBOT_HOST "\
			umount /mnt/initrd && \
			mount -oremount,rw / && \
			mv /dev/shm/initrd_cur /initrd/initrd1.gz && \
			rm -f /dev/shm/initrd_cur && \
			sync && \
			mount -oremount,ro / && \
			kill -SIGTERM 1"
		else
			log "Umounting the initrd and compress it" 2
			# XXX Remove /tmp/initrd_cur after compression ?
			ssh "$ROBOT_SSH_OPTS" root@$ROBOT_HOST "\
			umount /mnt/initrd && \
			mount -oremount,rw / && \
			gzip -f -4 -c /dev/shm/initrd_cur > /initrd/initrd1.gz && \
			rm -f /dev/shm/initrd_cur && \
			sync && \
			mount -oremount,ro / && \
			kill -SIGTERM 1"
		fi
	else
		log "Umounting the initrd and move it" 2
		# TODO
		log "Not implemented..." 3
	fi
	log "Ok... Rebooting" 3
}

check_rsync
check_create_structure
check_install_binary
check_install_config
check_size
upload_data