summaryrefslogtreecommitdiff
path: root/i/pc104/initrd/update.sh
blob: ae7b0cfc957c1fbe6052be13ae304cf1606864b2 (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
#!/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/../runtime"
# Max size of the config directory (in bytes)
ROBOT_CONF_MAX_SIZE="100000"
# Hostname of the robot
ROBOT_HOST="nenuphar"
# In autonomous mode, wait time for bringing up the ssh deamon
ROBOT_WAIT_TIME="3s"

check_rsync ()
{
	if [[ "x$(whereis rsync | cut -d ':' -f 2)" == "x" ]]
	then
		echo "Please install rsync"
		exit 1
	fi
}
# Check the structure (directory robot and startup script)
check_create_structure ()
{
	# 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 sourced by other script.
# Example :
# ./test_ai -z
EOF
		chmod +x $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT
		# Need to tune it by the user
		echo "Please tune your $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT."
		# XXX vim it ?
		exit 2
	fi
}

# Check that there is a line in the startup script for launchin a program and
# a binary existing
check_install_binary ()
{
	if [[ $(grep -c "^\\./" $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT) -eq 0 ]]
	then
		echo "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
			echo "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
		fi
	done
}

# Install the configuration
check_install_config ()
{
	# First we check the size
	if [[ $(du -bs --exclude=.svn $ROBOT_CONF_DIR | cut -f 1) -gt $ROBOT_CONF_MAX_SIZE ]]
	then
		echo "Config directory ($ROBOT_CONF_DIR) is too big."
		exit 5
	fi
	# There is no way to know the files needed, so we rsync everything
	rsync -aq --exclude=.svn $ROBOT_CONF_DIR/ $ROBOT_LOCAL_DIR
}

upload_data ()
{
	# Check if host exist and is up
	echo "Ping"
	ping -c 1 -w 1 -q $ROBOT_HOST > /dev/null
	case $? in
		0)
		# Robot is online !
		;;
		1)
		# Host exist but is offline
		echo "$ROBOT_HOST is currently down."
		exit 6
		;;
		2)
		# Host does not exist
		echo "$ROBOT_HOST does not exist."
		exit 7
		;;
		*)
		# Unknown error...
		echo "ping failed, unknown error."
		exit 8
		;;
	esac
	echo "nc"
	# Are we in automous mode ?
	if [[ $(nc -w 1 $ROBOT_HOST 1234) -eq 0 ]]
	then
		sleep $ROBOT_WAIT_TIME
	fi
	# Mount the initrd
	echo "Mounting"
	# TODO Best manage compressed initrd or not
#	&& gunzip -c /initrd/last > /tmp/initrd_cur \
	ssh -o "ConnectTimeout 1" root@$ROBOT_HOST "mount -oremount,rw / \
	&& cp /initrd/last /tmp/initrd_cur \
	&& mount -o loop /tmp/initrd_cur /mnt/initrd"
	# Rsyncer
	echo "rsync"
	# TODO Remove old files !
	rsync -av $ROBOT_LOCAL_DIR/ root@$ROBOT_HOST:/mnt/initrd/$ROBOT_REMOTE_DIR/
	echo "umount"
	# D�monter et recompresser l'initrd voir rotater tout
	# TODO Best manage compressed initrd or not
	# TODO Backup, rotation, et less cp !
#	&& gzip -f -9 -c /tmp/initrd_cur > /initrd/initrd1.gz\
	ssh -o "ConnectTimeout 1" root@$ROBOT_HOST "umount /mnt/initrd \
	&& cp /tmp/initrd_cur /initrd/initrd1.gz \
	&& sync \
	&& mount -oremount,ro /"
# TODO Change between NFS/Autonomous...
#	&& kill	-SIGTERM 1"
}

# TODO Parameter

check_rsync
check_create_structure
check_install_binary
check_install_config
upload_data