CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/build.sh
Views: 3950
1
#!/bin/bash
2
#
3
# Copyright (c) 2013-2021 Igor Pecovnik, igor.pecovnik@gma**.com
4
#
5
# This file is licensed under the terms of the GNU General Public
6
# License version 2. This program is licensed "as is" without any
7
# warranty of any kind, whether express or implied.
8
9
# DO NOT EDIT THIS FILE
10
# use configuration files like config-default.conf to set the build configuration
11
# Please check Orange Pi documentation for more info
12
# http://www.orangepi.cn/downloadresourcescn
13
# http://www.orangepi.org/downloadresources
14
15
SRC="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
16
17
# check for whitespace in ${SRC} and exit for safety reasons
18
grep -q "[[:space:]]" <<<"${SRC}" && { echo "\"${SRC}\" contains whitespace. Not supported. Aborting." >&2 ; exit 1 ; }
19
20
cd "${SRC}" || exit
21
22
if [[ "${ORANGEPI_ENABLE_CALL_TRACING}" == "yes" ]]; then
23
set -T # inherit return/debug traps
24
mkdir -p "${SRC}"/output/debug
25
echo -n "" > "${SRC}"/output/debug/calls.txt
26
trap 'echo "${BASH_LINENO[@]}|${BASH_SOURCE[@]}|${FUNCNAME[@]}" >> ${SRC}/output/debug/calls.txt ;' RETURN
27
fi
28
29
if [[ -f "${SRC}"/scripts/general.sh ]]; then
30
31
# shellcheck source=scripts/general.sh
32
source "${SRC}"/scripts/general.sh
33
34
else
35
36
echo "Error: missing build directory structure"
37
echo "Please clone the full repository by https://github.com/orangepi-xunlong/orangepi-build"
38
exit 255
39
40
fi
41
42
# Add the variables needed at the beginning of the path
43
check_args ()
44
{
45
46
for p in "$@"; do
47
48
case "${p%=*}" in
49
LIB_TAG)
50
# Take a variable if the branch exists locally
51
if [ "${p#*=}" == "$(git branch | \
52
gawk -v b="${p#*=}" '{if ( $NF == b ) {print $NF}}')" ]; then
53
echo -e "[\e[0;35m warn \x1B[0m] Setting $p"
54
eval "$p"
55
else
56
echo -e "[\e[0;35m warn \x1B[0m] Skip $p setting as LIB_TAG=\"\""
57
eval LIB_TAG=""
58
fi
59
;;
60
esac
61
62
done
63
64
}
65
66
67
check_args "$@"
68
69
70
update_src() {
71
72
cd "${SRC}" || exit
73
if [[ ! -f "${SRC}"/.ignore_changes ]]; then
74
echo -e "[\e[0;32m o.k. \x1B[0m] This script will try to update"
75
76
CHANGED_FILES=$(git diff --name-only)
77
if [[ -n "${CHANGED_FILES}" ]]; then
78
echo -e "[\e[0;35m warn \x1B[0m] Can't update since you made changes to: \e[0;32m\n${CHANGED_FILES}\x1B[0m"
79
while true; do
80
echo -e "Press \e[0;33m<Ctrl-C>\x1B[0m or \e[0;33mexit\x1B[0m to abort compilation"\
81
", \e[0;33m<Enter>\x1B[0m to ignore and continue, \e[0;33mdiff\x1B[0m to display changes"
82
read -r
83
if [[ "${REPLY}" == "diff" ]]; then
84
git diff
85
elif [[ "${REPLY}" == "exit" ]]; then
86
exit 1
87
elif [[ "${REPLY}" == "" ]]; then
88
break
89
else
90
echo "Unknown command!"
91
fi
92
done
93
elif [[ $(git branch | grep "*" | awk '{print $2}') != "${LIB_TAG}" && -n "${LIB_TAG}" ]]; then
94
git checkout "${LIB_TAG:-master}"
95
git pull
96
fi
97
fi
98
99
}
100
101
102
TMPFILE=$(mktemp)
103
chmod 644 "${TMPFILE}"
104
{
105
106
echo SRC="$SRC"
107
echo LIB_TAG="$LIB_TAG"
108
declare -f update_src
109
#echo "update_src"
110
111
} > "$TMPFILE"
112
113
#do not update/checkout git with root privileges to messup files onwership.
114
#due to in docker/VM, we can't su to a normal user, so do not update/checkout git.
115
if [[ $(systemd-detect-virt) == 'none' ]]; then
116
117
if [[ "${EUID}" == "0" ]]; then
118
su "$(stat --format=%U "${SRC}"/.git)" -c "bash ${TMPFILE}"
119
else
120
bash "${TMPFILE}"
121
fi
122
123
fi
124
125
126
rm "${TMPFILE}"
127
128
129
if [[ "${EUID}" == "0" ]] || [[ "${1}" == "vagrant" ]]; then
130
:
131
elif [[ "${1}" == docker || "${1}" == dockerpurge || "${1}" == docker-shell ]] && grep -q "$(whoami)" <(getent group docker); then
132
:
133
else
134
display_alert "This script requires root privileges, trying to use sudo" "" "wrn"
135
sudo "${SRC}/build.sh" "$@"
136
exit $?
137
fi
138
139
if [ "$OFFLINE_WORK" == "yes" ]; then
140
141
echo -e "\n"
142
display_alert "* " "You are working offline."
143
display_alert "* " "Sources, time and host will not be checked"
144
echo -e "\n"
145
sleep 3s
146
147
else
148
149
# check and install the basic utilities here
150
prepare_host_basic
151
152
fi
153
154
# Check for Vagrant
155
if [[ "${1}" == vagrant && -z "$(command -v vagrant)" ]]; then
156
display_alert "Vagrant not installed." "Installing"
157
sudo apt-get update
158
sudo apt-get install -y vagrant virtualbox
159
fi
160
161
# Purge Orange Pi Docker images
162
if [[ "${1}" == dockerpurge && -f /etc/debian_version ]]; then
163
display_alert "Purging Orange Pi Docker containers" "" "wrn"
164
docker container ls -a | grep orangepi | awk '{print $1}' | xargs docker container rm &> /dev/null
165
docker image ls | grep orangepi | awk '{print $3}' | xargs docker image rm &> /dev/null
166
shift
167
set -- "docker" "$@"
168
fi
169
170
# Docker shell
171
if [[ "${1}" == docker-shell ]]; then
172
shift
173
#shellcheck disable=SC2034
174
SHELL_ONLY=yes
175
set -- "docker" "$@"
176
fi
177
178
# Install Docker if not there but wanted. We cover only Debian based distro install. On other distros, manual Docker install is needed
179
if [[ "${1}" == docker && -f /etc/debian_version && -z "$(command -v docker)" ]]; then
180
181
DOCKER_BINARY="docker-ce"
182
183
# add exception for Ubuntu Focal until Docker provides dedicated binary
184
codename=$(cat /etc/os-release | grep VERSION_CODENAME | cut -d"=" -f2)
185
codeid=$(cat /etc/os-release | grep ^NAME | cut -d"=" -f2 | awk '{print tolower($0)}' | tr -d '"' | awk '{print $1}')
186
[[ "${codename}" == "debbie" ]] && codename="buster" && codeid="debian"
187
[[ "${codename}" == "ulyana" || "${codename}" == "jammy" ]] && codename="focal" && codeid="ubuntu"
188
189
# different binaries for some. TBD. Need to check for all others
190
[[ "${codename}" =~ focal|hirsute ]] && DOCKER_BINARY="docker containerd docker.io"
191
192
display_alert "Docker not installed." "Installing" "Info"
193
sudo bash -c "echo \"deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/${codeid} ${codename} stable\" > /etc/apt/sources.list.d/docker.list"
194
195
sudo bash -c "curl -fsSL \"https://download.docker.com/linux/${codeid}/gpg\" | apt-key add -qq - > /dev/null 2>&1 "
196
export DEBIAN_FRONTEND=noninteractive
197
sudo apt-get update
198
sudo apt-get install -y -qq --no-install-recommends ${DOCKER_BINARY}
199
display_alert "Add yourself to docker group to avoid root privileges" "" "wrn"
200
"${SRC}/build.sh" "$@"
201
exit $?
202
203
fi
204
205
EXTER="${SRC}/external"
206
207
# Create userpatches directory if not exists
208
mkdir -p "${SRC}"/userpatches
209
210
211
# Create example configs if none found in userpatches
212
if ! ls "${SRC}"/userpatches/{config-example.conf,config-docker.conf,config-vagrant.conf} 1> /dev/null 2>&1; then
213
214
# Migrate old configs
215
if ls "${SRC}"/*.conf 1> /dev/null 2>&1; then
216
display_alert "Migrate config files to userpatches directory" "all *.conf" "info"
217
cp "${SRC}"/*.conf "${SRC}"/userpatches || exit 1
218
rm "${SRC}"/*.conf
219
[[ ! -L "${SRC}"/userpatches/config-example.conf ]] && ln -fs config-example.conf "${SRC}"/userpatches/config-default.conf || exit 1
220
fi
221
222
display_alert "Create example config file using template" "config-default.conf" "info"
223
224
# Create example config
225
if [[ ! -f "${SRC}"/userpatches/config-example.conf ]]; then
226
cp "${EXTER}"/config/templates/config-example.conf "${SRC}"/userpatches/config-example.conf || exit 1
227
ln -fs config-example.conf "${SRC}"/userpatches/config-default.conf || exit 1
228
fi
229
230
# Create Docker config
231
if [[ ! -f "${SRC}"/userpatches/config-docker.conf ]]; then
232
cp "${EXTER}"/config/templates/config-docker.conf "${SRC}"/userpatches/config-docker.conf || exit 1
233
fi
234
235
# Create Docker file
236
if [[ ! -f "${SRC}"/userpatches/Dockerfile ]]; then
237
cp "${EXTER}"/config/templates/Dockerfile "${SRC}"/userpatches/Dockerfile || exit 1
238
fi
239
240
# Create Vagrant config
241
if [[ ! -f "${SRC}"/userpatches/config-vagrant.conf ]]; then
242
cp "${EXTER}"/config/templates/config-vagrant.conf "${SRC}"/userpatches/config-vagrant.conf || exit 1
243
fi
244
245
# Create Vagrant file
246
if [[ ! -f "${SRC}"/userpatches/Vagrantfile ]]; then
247
cp "${EXTER}"/config/templates/Vagrantfile "${SRC}"/userpatches/Vagrantfile || exit 1
248
fi
249
250
fi
251
252
if [[ -z "${CONFIG}" && -n "$1" && -f "${SRC}/userpatches/config-$1.conf" ]]; then
253
CONFIG="userpatches/config-$1.conf"
254
shift
255
fi
256
257
# usind default if custom not found
258
if [[ -z "${CONFIG}" && -f "${SRC}/userpatches/config-default.conf" ]]; then
259
CONFIG="userpatches/config-default.conf"
260
fi
261
262
# source build configuration file
263
CONFIG_FILE="$(realpath "${CONFIG}")"
264
265
if [[ ! -f "${CONFIG_FILE}" ]]; then
266
display_alert "Config file does not exist" "${CONFIG}" "error"
267
exit 254
268
fi
269
270
CONFIG_PATH=$(dirname "${CONFIG_FILE}")
271
272
# Source the extensions manager library at this point, before sourcing the config.
273
# This allows early calls to enable_extension(), but initialization proper is done later.
274
# shellcheck source=scripts/extensions.sh
275
source "${SRC}"/scripts/extensions.sh
276
277
display_alert "Using config file" "${CONFIG_FILE}" "info"
278
pushd "${CONFIG_PATH}" > /dev/null || exit
279
# shellcheck source=/dev/null
280
source "${CONFIG_FILE}"
281
popd > /dev/null || exit
282
283
[[ -z "${USERPATCHES_PATH}" ]] && USERPATCHES_PATH="${CONFIG_PATH}"
284
285
# Script parameters handling
286
while [[ "${1}" == *=* ]]; do
287
288
parameter=${1%%=*}
289
value=${1##*=}
290
shift
291
display_alert "Command line: setting $parameter to" "${value:-(empty)}" "info"
292
eval "$parameter=\"$value\""
293
294
done
295
296
297
if [[ "${BUILD_ALL}" == "yes" || "${BUILD_ALL}" == "demo" ]]; then
298
299
# shellcheck source=scripts/build-all-ng.sh
300
source "${SRC}"/scripts/build-all-ng.sh
301
302
else
303
304
# shellcheck source=scripts/main.sh
305
source "${SRC}"/scripts/main.sh
306
307
fi
308
309