-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild-image.sh
executable file
·134 lines (103 loc) · 4.42 KB
/
build-image.sh
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
#!/usr/bin/env bash
#
# Please refer to AUTHORS.md for a complete list of Copyright holders.
# Copyright (C) 2016-2022, Dockershelf Developers.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Exit early if there are errors and be verbose.
set -exuo pipefail
# Some default values.
BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
NODEMIRROR="https://deb.nodesource.com/node_${NODE_VER_NUM}.x"
# Some tools are needed.
DPKG_TOOLS_DEPENDS="sudo aptitude gnupg dirmngr"
NODE_PKGS="nodejs"
NODE_PKGS_VER=""
# Load helper functions
source "${BASEDIR}/library.sh"
# Apt: Install tools
# ------------------------------------------------------------------------------
# We need to install the packages defined at ${DPKG_TOOLS_DEPENDS} because
# some commands are needed to process information before installing
# actual dependencies
msginfo "Installing tools and upgrading image ..."
cmdretry apt-get update
cmdretry apt-get upgrade
cmdretry apt-get install ${DPKG_TOOLS_DEPENDS}
# Node: Configure sources
# ------------------------------------------------------------------------------
# We will use Nodesource's official repository to install the different versions
# of Node.
msginfo "Configuring /etc/apt/sources.list ..."
if [ "${NODE_VER_NUM}" == "16" ] || [ "${NODE_VER_NUM}" == "18" ] || [ "${NODE_VER_NUM}" == "20" ]; then
NODE_DISTRO_NAME="nodistro"
NODE_REPO_KEY="2F59B5F99B1BE0B4"
else
NODE_DISTRO_NAME="sid"
NODE_REPO_KEY="1655A0AB68576280"
fi
cmdretry dirmngr --debug-level guru
cmdretry gpg --lock-never --no-default-keyring \
--keyring /usr/share/keyrings/node.gpg \
--keyserver hkp://keyserver.ubuntu.com:80 \
--recv-keys ${NODE_REPO_KEY}
{
echo "deb [signed-by=/usr/share/keyrings/node.gpg] ${NODEMIRROR} ${NODE_DISTRO_NAME} main"
} | tee /etc/apt/sources.list.d/node.list >/dev/null
cmdretry apt-get update
# Node: Installation
# ------------------------------------------------------------------------------
# We will use the nodesource script to install node.
msginfo "Installing Node ..."
for PKG in ${NODE_PKGS}; do
PKG_VER="$(apt-cache madison ${PKG} | grep Packages |
grep deb.nodesource.com | head -n1 | awk -F'|' '{print $2}' | xargs)"
NODE_PKGS_VER="${NODE_PKGS_VER} ${PKG}=${PKG_VER}"
done
if [ "${DEBIAN_RELEASE}" == "sid" ]; then
cmdretry aptitude install libdb5.3t64 libreadline8t64
fi
cmdretry aptitude install ${NODE_PKGS_VER}
if [ ! -f "/usr/bin/nodejs" ]; then
ln -s /usr/bin/node /usr/bin/nodejs
fi
# Apt: Remove build depends
# ------------------------------------------------------------------------------
# We need to clear the filesystem of unwanted packages before installing python
# because some files might be confused with already installed python packages.
msginfo "Removing unnecessary packages ..."
cmdretry apt-get purge $(aptitude search -F%p ~c ~g)
cmdretry apt-get purge aptitude
cmdretry apt-get autoremove
# Bash: Changing prompt
# ------------------------------------------------------------------------------
# To distinguish images.
cat >>"/etc/bash.bashrc" <<'EOF'
# Node colors
COLOR_DARK_GREEN="\[\033[38;5;35m\]"
COLOR_LIGHT_GREEN="\[\033[38;5;77m\]"
PS1="${COLOR_LIGHT_GREEN}[\u@${COLOR_DARK_GREEN}\h]${COLOR_OFF}:\w\$ "
EOF
cat >>"/etc/skel/.bashrc" <<'EOF'
# Node colors
COLOR_DARK_GREEN="\[\033[38;5;35m\]"
COLOR_LIGHT_GREEN="\[\033[38;5;77m\]"
PS1="${COLOR_LIGHT_GREEN}[\u@${COLOR_DARK_GREEN}\h]${COLOR_OFF}:\w\$ "
EOF
# Final cleaning
# ------------------------------------------------------------------------------
# Buncha files we won't use.
msginfo "Removing unnecessary files ..."
find /usr -name "*.py[co]" -print0 | xargs -0r rm -rfv
find /usr -name "__pycache__" -type d -print0 | xargs -0r rm -rfv
rm -rfv "/tmp/"* "/usr/share/doc/"* "/usr/share/locale/"* "/usr/share/man/"* \
"/var/cache/debconf/"* "/var/cache/apt/"* "/var/tmp/"* "/var/log/"* \
"/var/lib/apt/lists/"*