-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathquick-setup.sh
280 lines (231 loc) · 10.2 KB
/
quick-setup.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
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
277
278
279
280
DISTRO_TYPE=""
SETUP_SSHD="${SETUP_SSHD:-true}"
# Docker version that will be installed by this install script.
DOCKER_VERSION="26.1.4"
function check_os {
if [ -f /etc/os-release ]; then
. /etc/os-release
if [ "$ID" = "debian" ]; then
DISTRO_TYPE="debian"
elif [ "$ID" = "ubuntu" ]; then
DISTRO_TYPE="ubuntu"
elif [ "$ID" = "fedora" ]; then
DISTRO_TYPE="fedora"
elif [[ "$ID" = "rocky" || "$ID" = "rhel" || "$ID" = "centos" ]]; then
DISTRO_TYPE="rhel"
else
echo "This is not a supported OS. (Debian, Ubuntu, Fedora, Rocky, CentOS, RHEL)"
fi
else
echo "Cannot determine the operating system"
fi
}
function install-docker {
# when this script is used to install just docker
# we need to run check_os to detect the distro
if [ -z "${DISTRO_TYPE}" ]; then
check_os
fi
if [ "${DISTRO_TYPE}" = "debian" ]; then
install-docker-debian
elif [ "${DISTRO_TYPE}" = "ubuntu" ]; then
install-docker-ubuntu
elif [ "${DISTRO_TYPE}" = "rhel" ]; then
install-docker-rhel
elif [ "${DISTRO_TYPE}" = "fedora" ]; then
install-docker-fedora
fi
}
function install-docker-debian {
# using instructions from:
# https://docs.docker.com/engine/install/debian/#install-using-the-repository
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove -y $pkg; done
# Add Docker's official GPG key:
sudo apt-get update -y
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
DOCKER_PKG_NAME=$(apt-cache madison docker-ce | awk '{ print $3 }' | grep ${DOCKER_VERSION} | head -n 1)
sudo apt-get -y install docker-ce=${DOCKER_PKG_NAME} docker-ce-cli=${DOCKER_PKG_NAME} containerd.io docker-buildx-plugin docker-compose-plugin
}
function install-docker-ubuntu {
# using instructions from:
# https://docs.docker.com/engine/install/debian/#install-using-the-repository
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove -y $pkg; done
# Add Docker's official GPG key:
sudo apt-get update -y
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
DOCKER_PKG_NAME=$(apt-cache madison docker-ce | awk '{ print $3 }' | grep ${DOCKER_VERSION} | head -n 1)
sudo apt-get -y install docker-ce=${DOCKER_PKG_NAME} docker-ce-cli=${DOCKER_PKG_NAME} containerd.io docker-buildx-plugin docker-compose-plugin
}
function install-docker-rhel {
# using instructions from:
# https://docs.docker.com/engine/install/rhel/#install-using-the-repository
sudo yum remove -y docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine \
podman \
runc
sudo yum install -y yum-utils
sudo yum-config-manager -y --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
DOCKER_PKG_NAME=$(yum list docker-ce --showduplicates | awk '{ print $2 }' | grep ${DOCKER_VERSION} | head -n 1)
DOCKER_CLI_PKG_NAME=$(yum list docker-ce-cli --showduplicates | awk '{ print $2 }' | grep ${DOCKER_VERSION} | head -n 1)
sudo yum install -y docker-ce-${DOCKER_PKG_NAME} docker-ce-cli-${DOCKER_CLI_PKG_NAME} containerd.io docker-buildx-plugin docker-compose-plugin
# diverges from the instructions. This means docker daemon starts on each boot.
sudo systemctl enable --now docker
}
function install-docker-fedora {
# using instructions from:
# https://docs.docker.com/engine/install/rhel/#install-using-the-repository
sudo dnf remove -y docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager -y --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
DOCKER_PKG_NAME=$(dnf list docker-ce --showduplicates | awk '{ print $2 }' | grep ${DOCKER_VERSION} | head -n 1)
DOCKER_CLI_PKG_NAME=$(dnf list docker-ce-cli --showduplicates | awk '{ print $2 }' | grep ${DOCKER_VERSION} | head -n 1)
sudo dnf install -y docker-ce-${DOCKER_PKG_NAME} docker-ce-cli-${DOCKER_CLI_PKG_NAME} containerd.io docker-buildx-plugin docker-compose-plugin
# diverges from the instructions. This means docker daemon starts on each boot.
sudo systemctl enable --now docker
}
function post-install-docker {
# instructions from:
# https://docs.docker.com/engine/install/linux-postinstall/
sudo groupadd docker
sudo usermod -aG docker "$SUDO_USER"
}
function setup-sshd {
# increase max auth tries so unknown keys don't lock ssh attempts
sudo sed -i 's/^#*MaxAuthTries.*/MaxAuthTries 50/' /etc/ssh/sshd_config
if [[ "${DISTRO_TYPE}" = "rhel" || "${DISTRO_TYPE}" = "fedora" ]]; then
sudo systemctl restart sshd
else
sudo systemctl restart ssh
fi
}
function install-make {
if [[ "${DISTRO_TYPE}" = "rhel" || "${DISTRO_TYPE}" = "fedora" ]]; then
sudo dnf install -y make
else
sudo apt install -y make
fi
}
function install-gh-cli {
if [[ "${DISTRO_TYPE}" = "rhel" || "${DISTRO_TYPE}" = "fedora" ]]; then
install-gh-cli-rhel
else
install-gh-cli-debian
fi
}
function install-gh-cli-debian {
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install -y gh
}
function install-gh-cli-rhel {
sudo dnf install -y 'dnf-command(config-manager)'
sudo dnf config-manager -y --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
sudo dnf install git -y
sudo dnf install -y gh --repo gh-cli
}
function setup-bash-prompt {
# Check if the prompt is already set up
if grep -q "function promptcmd" ~/.bashrc; then
echo "Bash prompt already configured in .bashrc"
return 1
fi
cat << 'EOF' >> ~/.bashrc
# Custom Bash Prompt Configuration
WHITE='\[\033[1;37m\]'; LIGHTRED='\[\033[1;31m\]'; LIGHTGREEN='\[\033[1;32m\]'; LIGHTBLUE='\[\033[1;34m\]'; DEFAULT='\[\033[0m\]'
cLINES=$WHITE; cBRACKETS=$WHITE; cERROR=$LIGHTRED; cSUCCESS=$LIGHTGREEN; cHST=$LIGHTGREEN; cPWD=$LIGHTBLUE; cCMD=$DEFAULT
promptcmd() {
PREVRET=$?
PS1="\n"
if [ $PREVRET -ne 0 ]; then
PS1="${PS1}${cBRACKETS}[${cERROR}x${cBRACKETS}]${cLINES}\342\224\200"
else
PS1="${PS1}${cBRACKETS}[${cSUCCESS}*${cBRACKETS}]${cLINES}\342\224\200"
fi
PS1="${PS1}${cBRACKETS}[${cHST}\h${cBRACKETS}]${cLINES}\342\224\200"
PS1="${PS1}[${cPWD}\w${cBRACKETS}]"
PS1="${PS1}\n${cLINES}\342\224\224\342\224\200\342\224\200> ${cCMD}"
}
PROMPT_COMMAND=promptcmd
EOF
}
# keep SSH_AUTH_SOCK env var when using sudo
# to extract keys from the original user' agent
function add-ssh-socket-env-for-sudo {
echo 'Defaults env_keep += "SSH_AUTH_SOCK"' | sudo tee /etc/sudoers.d/ssh_auth_sock
}
function install-containerlab {
# when this script is used to install just containerlab
# we need to run check_os to detect the distro
if [ -z "${DISTRO_TYPE}" ]; then
check_os
fi
if [ "${DISTRO_TYPE}" = "rhel" ]; then
sudo yum-config-manager -y --add-repo=https://netdevops.fury.site/yum/ && \
echo "gpgcheck=0" | sudo tee -a /etc/yum.repos.d/netdevops.fury.site_yum_.repo
sudo yum install -y containerlab
elif [ "${DISTRO_TYPE}" = "fedora" ]; then
# Fedora 41 onwards ships with dnf5 instead of dnf 4 (packaged just as 'dnf')
# and requires a slightly different syntax.
if rpm --quiet -q dnf; then
sudo dnf config-manager -y --add-repo "https://netdevops.fury.site/yum/" && \
echo "gpgcheck=0" | sudo tee -a /etc/yum.repos.d/netdevops.fury.site_yum_.repo
else # dnf5
sudo dnf config-manager addrepo --set=baseurl="https://netdevops.fury.site/yum/" && \
echo "gpgcheck=0" | sudo tee -a /etc/yum.repos.d/netdevops.fury.site_yum_.repo
fi
sudo dnf install -y containerlab
else
echo "deb [trusted=yes] https://netdevops.fury.site/apt/ /" | \
sudo tee -a /etc/apt/sources.list.d/netdevops.list
sudo apt update -y && sudo apt install containerlab -y
fi
}
function all {
# check OS to determine distro
check_os
if [ "${SETUP_SSHD}" = "true" ]; then
setup-sshd
fi
install-docker
post-install-docker
install-make
install-gh-cli
add-ssh-socket-env-for-sudo
install-containerlab
}
"$@"