summaryrefslogtreecommitdiff
path: root/build_medkit.sh
blob: 9b42cb26b8642bbc5de882d188919d3fb4eec7f8 (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
#!/bin/sh
set -e
## Define variables with their default values ##
BRANCH=
MODEL=
L10N=en,cs
LISTS=
VERSION=
OVERLAY=
UPDATER_SCRIPT=
TEST_BRANCH=false

## Parse arguments ##
while [ $# -gt 0 ]; do
	case "$1" in
		-h|--help)
			echo "This script generates Turris medkit using packages lists and updater-ng."
			echo "Usage: $0 [OPTION].. MODEL [BRANCH]"
			echo
			echo "Model:"
			echo "  Model is Turris device model. Only supported ones at the moment"
			echo "  are 'turris' for Turris 1.x and 'omnia' for Turris Omnia."
			echo "Branch:"
			echo "  This optionally specifies source branch for packages."
			echo "  If no branch is specified then deploy is used (branch"
			echo "  identified only just by model, no branch name appended)."
			echo "Options:"
			echo "  --help, -h"
			echo "    Print this text and exit."
			echo "  --localization, -l LOCALIZATION,.."
			echo "    After this argument a list of language codes to be added to"
			echo "    medkit should be specified. Language codes should be"
			echo "    separated by comma. In default en,cs is used."
			echo "  --lists, -p PKGLIST,.."
			echo "    What lists should be added to medkit. In default no"
			echo "    additional lists will be added. Multiple lists can be"
			echo "    specified by separating them by commas."
			echo "  --updater-script FILE"
			echo "    Run file as updater's script. It is executed after primary"
			echo "    entry script of this tool."
			echo "  --overlay PATH"
			echo "    This allows you to overwrite or add some files to medkit."
			echo "    PATH is expected to be directory and whole content is copied"
			echo "    to newly generated root. This is handy if you want to change"
			echo "    some default settings for example."
			exit 0
			;;
		--localization|-l)
			shift
			L10N="$1"
			;;
		--lists|-p)
			shift
			LISTS="$1"
			;;
		--updater-script)
			shift
			UPDATER_SCRIPT="$1"
			;;
		--overlay)
			shift
			OVERLAY="$1"
			;;
		*)
			if [ -z "$MODEL" ]; then
				MODEL="$1"
			elif [ -z "$BRANCH" ]; then
				if [ "$1" != "deploy" ]; then
					BRANCH="$1"
					TEST_BRANCH=true
					case "$1" in
						rc)
							TEST_BRANCH=false
							;;
					esac
				fi
			else
				echo "Unknown option: $1" >&2
				exit 1
			fi
			;;
	esac
	shift
done

[ -n "$MODEL" ] || {
	echo "You have to specify Turris router model." >&2
	exit 1
}

# Branch name with dash (yes for real)
DBRANCH="-$BRANCH"


## Build needed tools ##
# Git puller helper function (git_pull output_path source_url)
git_pull() {
	if [ ! -d $1 ]; then
		git clone $2 $1
		(
		cd $1
		git submodule update --init --recursive
		)
	else
		(
		cd $1
		git fetch
		if ! git diff --quiet HEAD origin/HEAD; then
			git clean -Xdf
			git reset --hard origin/master
			git submodule update --init --recursive
		fi
		)
	fi
}
# Wget puller helper function (wget_pull output_file source_url)
wget_pull() {
	if [ ! -e $1 ] || [ $(expr $(date -u +%s) - $(stat -c %Z $1)) -gt 86400 ]; then
		wget $2 -O $1
	fi
}

# Usign
echo "==== Getting usign up and running ===="
git_pull .usign git://git.openwrt.org/project/usign.git
(
cd .usign
cmake .
make
)

# Updater-ng
echo "==== Getting updater-ng up and running ===="
git_pull .updater https://gitlab.labs.nic.cz/turris/updater.git
make -C .updater NO_DOC=1 LUA_COMPILE:=no

# Get public parts of signing keys
echo "==== Checking public keys ===="
for K in release standby test; do
	wget_pull .$K.pub https://gitlab.labs.nic.cz/turris/turris-os-packages/raw/test/cznic/cznic-repo-keys/files/$K.pub
done

## Export some variables that are used by pkgupdate lua script
export L10N
export LISTS
export OVERLAY
export TEST_BRANCH
export ROOT="$(readlink -f "root-$MODEL$DBRANCH")"


## Dump updater-ng entry script ##
export UPDATER_CONFIG=".updater-$MODEL$DBRANCH.lua"
cat > "$UPDATER_CONFIG" <<EOF
-- Load requested localizations
l10n = {}
for lang in os.getenv('L10N'):gmatch('[^,]+') do
	table.insert(l10n, lang)
end
Export('l10n')

-- This is helper function for including localization packages.
-- (This is copy of standard entry function that can be found in pkgupdate entry.lua)
function for_l10n(fragment)
	for _, lang in pairs(l10n or {}) do
		Install(fragment .. lang, {ignore = {'missing'}})
	end
end
Export('for_l10n')

local script_options = {
	security = 'Remote',
	ca = system_cas,
	crl = no_crl,
	pubkey = {
		'file://./.release.pub',
		'file://./.standby.pub',
		'file://./.test.pub'
	}
}
base_url = 'https://repo.turris.cz/$MODEL$DBRANCH/lists/'

-- Aways include base script
Script('base',  base_url .. 'base.lua', script_options)

-- Now include any additional lists
local userlists_file = io.open('userlists', 'r')
for list in os.getenv('LISTS'):gmatch('[^,]+') do
	Script('userlist-' .. list,  base_url .. list .. '.lua', script_options)
end

-- If branch was detected as testing one then also add test keys
if os.getenv('TEST_BRANCH') then
	WARN('Branch detected as testing. Adding test keys')
	Install('cznic-repo-keys-test')
end
EOF
# Append user specified script (if given)
if [ -n "$UPDATER_SCRIPT" ]; then
	cat >> "$UPDATER_CONFIG" <<EOF
Script('user-script', 'file://./$UPDATER_SCRIPT')
EOF
fi

# Create /tmp/sysinfo files
# TODO we should use some internal option of updater it self
mkdir -p /tmp/sysinfo
if [ "$MODEL" = "omnia" ]; then
	echo "Turris Omnia" > /tmp/sysinfo/model
else # Else Turris 1.x (yes this is wrong but it's just temporally)
	echo "Turris" > /tmp/sysinfo/model
fi
# We are only using board_name anyway atm.
echo "rtunknown" > /tmp/sysinfo/board_name


## Generate root ##
echo "==== Building new root ===="
fakeroot -- sh -s <<EOF
set -e

mkdir -p "$ROOT"
## Create base filesystem for updater
ln -sf tmp "$ROOT/var"
# Create lock required by updater
mkdir -p "$ROOT/tmp/lock"
# Create opkg status file and info file
mkdir -p "$ROOT/usr/lib/opkg/info"
touch "$ROOT/usr/lib/opkg/status"
# And updater directory
mkdir -p "$ROOT/usr/share/updater"

## Run updater it self
.updater/bin/pkgupdate --out-of-root --usign=.usign/usign -R "$ROOT" \
	--batch "file://$UPDATER_CONFIG"

## Change /etc/config/updater to match given preferences
UPDCNF="$ROOT/etc/config/updater"
echo > "\$UPDCNF"
[ -z "$BRANCH" ] || cat >> "\$UPDCNF" <<EEOF
config override 'override'
	option branch '$BRANCH'

EEOF
echo "config pkglists 'pkglists'" >> "\$UPDCNF"
while IFS=',' read LIST; do
	echo "	list lists '\$LIST'" >> "\$UPDCNF"
done <<< "\$LISTS"
echo >> "\$UPDCNF"
echo "config l10n 'l10n'" >> "\$UPDCNF"
while IFS=',' read LANG; do
	echo "	list langs '\$LANG'" >> "\$UPDCNF"
done <<< "\$L10N"
echo >> "\$UPDCNF"

## Overlay user's files
if [ -n "$OVERLAY" ]; then
	cp -r "$OVERLAY/." "$ROOT/"
fi

## Tar root in current directory
(
cd "$ROOT"
# Do cleanups
rm -f var/lock/opkg.lock
rm -f usr/share/updater/flags
rm -rf usr/share/updater/unpacked
rm -rf var/opkg-collided
# Create archive
tar -czf "../medkit-$MODEL$DBRANCH.tar.gz" .
)
EOF

## Cleanup ##
echo "==== Cleaning up ===="
rm -rf "$ROOT"
rm -f "$UPDATER_CONF"