aboutsummaryrefslogtreecommitdiffstats
path: root/INSTALL/setup_image.sh
blob: 6acef667a085e3749d7e4949478391959e5e5121 (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
#!/bin/bash -x

### this script is called when building the docker image: it's executing in a temp container

echo "------------------------ Starting $(basename $0) -----------------------"

function debug() {
	set +x
	echo "-------------------------"
	echo "Command failed."
	echo "Running sleep for 1 day. To proceed:"
	echo "* run 'killall sleep' to continue"
	echo "* run 'killall -9 sleep' to abort the build"
	sig=0
	sleep 86400 || sig=$?
	# if killed -9, then abort
	[[ $sig == 137 ]] && exit 1 # abort
	set -x
	return 0 # continue
}
# get the INSTALL dir (the one where we were launched)
INSTDIR=$(cd $(dirname $0) && pwd -P)
echo "Detected container install dir = $INSTDIR"

if [[ -f $INSTDIR/DEBUG ]]; then
	echo "#### DEBUG MODE IS ACTIVE ####"
	trap debug ERR # on error, run a sleep tp debug container
fi


################################## variables #################################

# source variables in conf file
. $INSTDIR/image.conf

# source flavour config file (generated by top Makefile)
. $INSTDIR/flavour.conf

if [[ -z "$CONTAINER_TYPE" ]]; then
	grep -q docker /proc/self/cgroup && CONTAINER_TYPE="docker"
fi

################################## install docker endpoint #####################

# install the entrypoint script in /usr/bin
install --mode=755 $INSTDIR/wait_for_net.sh /usr/bin/

################################## install first-run service ###################
# all operations requiring runnning daemons (inc. systemd) must be run at first
# container instanciation

if [[ "$FIRSTRUN" == "yes" ]]; then
	install --mode=755 $INSTDIR/firstrun.sh      /root/firstrun.sh
	install --mode=644 $INSTDIR/image.conf       /root/firstrun.conf
	[[ -d $INSTDIR/firstrun.d ]] && cp -a $INSTDIR/firstrun.d /root/
	mkdir -p /etc/systemd/system/multi-user.target.wants/
	cat <<EOF >/etc/systemd/system/multi-user.target.wants/firstrun.service
[Unit]
Description=Firstrun service
After=network.target

[Service]
Type=oneshot
ExecStart=-/bin/bash -c /root/firstrun.sh
TimeoutSec=0
StandardInput=null
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF
fi

# helper func to install a firstrun hook
function firstrun_add() {
	script=$1
	level=${2:-50}
	name=${3:-$(basename $script)}

	mkdir -p /root/firstrun.d
	cp $script /root/firstrun.d/${level}_$name
}

################################## adjust system timezone ############################

ln -sf ../usr/share/zoneinfo/$TIMEZONE /etc/localtime

################################## run other scripts in turn ##############

function enumerate_tasks() {
	for script in $INSTDIR/common.d/*; do
		case $(basename $script) in
			[0-9][0-9]_*)
				echo $(basename $script):$script
				;;
		esac
	done

	for tsk in $(cat $INSTDIR/flavours/$FLAVOUR.tasks | sed 's/#.*$//g'); do
		if [[ -f $INSTDIR/tasks.d/$tsk ]]; then
			echo $tsk:$INSTDIR/tasks.d/$tsk
		else
			# fail to find task
			echo "$INSTDIR/flavours/$FLAVOUR.tasks: invalid task '$tsk'" >&2
			return 1
		fi
	done
	return 0
}


for script in $(enumerate_tasks | sort -k1 -t':' | cut -f2 -d':'); do
	echo "--------------------- start script $script ---------------------"
	. $script
	echo "--------------------- end of script $script ---------------------"
done

############################### cleanup ###################################

cd /
apt-get clean # clean apt caches
rm -rf /var/lib/apt/lists/*
rm -rf $INSTDIR # yes, I can auto-terminate myself !

# cleanup /tmp without removing the dir
for x in $(find /tmp -mindepth 1); do
	rm -rf $x || true
done

echo "------------------------ $(basename $0) finished -----------------------"