summaryrefslogtreecommitdiff
path: root/1-prereqs.sh
blob: b4b162410492e530fc2798444b136883b53f1fce (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
#!/usr/bin/env bash

source ./auto/helper-logic

# Clear the screen and print the header
PrintHeader

# Ensure we are running as root
EnsureRoot

#region Update apt sources
Run "apt-get update" \
  "Updating apt sources..." \
  "Failed to update apt sources." \
  "Updated apt sources."
#endregion

#region Download cURL
if [ $(dpkg-query -W -f='${Status}' curl 2>/dev/null | grep -c "ok installed") -eq 1 ]; then
  PrintOkIndicator "cURL is already installed."
else
  function DownloadCurl {
    apt-get install curl -y
  }

  Run "DownloadCurl" \
    "Installing cURL..." \
    "Failed to install cURL." \
    "Installed cURL."
fi
#endregion

#region Set up sources.list.d files for Jenkins
if [ -f /etc/apt/sources.list.d/jenkins.list ]; then
  PrintOkIndicator "Jenkins sources.list.d has already been set up."
else
  function SetupJenkinsSourceListD {
    curl -s -S https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key -o /usr/share/keyrings/jenkins-keyring.asc --fail-with-body
    echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" https://pkg.jenkins.io/debian-stable binary/ | tee /etc/apt/sources.list.d/jenkins.list
  }

  Run "SetupJenkinsSourceListD" \
    "Setting up Jenkins sources.list.d files..." \
    "Failed to download or set up Jenkins sources.list.d files." \
    "Jenkins sources.list.d files has been set up."
fi
#endregion

#region Set up sources.list.d files for Docker
if [ -f /etc/apt/sources.list.d/docker.list ]; then
  PrintOkIndicator "Docker sources.list.d has already been set up."
else
  function SetupDockerSourceListD {
    curl -s -S https://download.docker.com/linux/debian/gpg -o /usr/share/keyrings/docker.asc --fail-with-body
    echo "deb [signed-by=/usr/share/keyrings/docker.asc]" https://download.docker.com/linux/debian bookworm stable | tee /etc/apt/sources.list.d/docker.list
  }

  Run "SetupDockerSourceListD" \
    "Setting up Docker sources.list.d files..." \
    "Failed to download or set up Docker sources.list.d files." \
    "Docker sources.list.d files has been set up."
fi
#endregion

#region Update apt sources again
Run "apt-get update" \
  "Updating apt sources..." \
  "Failed to update apt sources." \
  "Updated apt sources."
#endregion

#region Jenkins user and group
if grep -q "^jenkins:" /etc/group; then
  PrintOkIndicator "Jenkins group already exist."
else
  function CreateJenkinsGroup {
    groupadd --gid 1006 jenkins
  }

  Run "CreateJenkinsGroup" \
    "Creating jenkins group with GID 1006..." \
    "Failed to create jenkins group." \
    "Created jenkins group."
fi

if id -u jenkins > /dev/null 2>&1; then
  PrintOkIndicator "Jenkins user already exist."
else
  function CreateJenkinsUser {
    useradd --comment Jenkins --shell /bin/bash --uid 1006 --gid 1006 --home-dir /var/lib/jenkins jenkins
  }

  Run "CreateJenkinsUser" \
    "Creating jenkins user with UID 1006..." \
    "Failed to create jenkins user." \
    "Created jenkins user."
fi
#endregion

#region Install needed tools
declare -A tools
tools["Git"]="git"
tools["GnuPG"]="gpg"
tools["SSH"]="openssh-server openssh-client openssh-sftp-server"
tools["Reprepro"]="reprepro"
tools["XMLStarlet"]="xmlstarlet"
tools["Jq"]="jq"
tools["NGINX"]="nginx"
tools["Java"]="openjdk-17-jre fontconfig"
tools["Docker"]="docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin"
tools["OCaml"]="opam ocaml socat"
tools["Jenkins"]="jenkins"
tools["AptCacherNg"]="apt-cacher-ng"

for key in "${!tools[@]}"
do
  VALUES=$(sed -e 's/ /\n/g' <<< "${tools[$key]}")

  ALL_INSTALLED="true"

  for package in $VALUES; do
    if [ $(dpkg-query -W -f='${Status}' $package 2>/dev/null | grep -c "ok installed") -ne 1 ]; then
      ALL_INSTALLED="false"
    fi
  done

  if [[ $ALL_INSTALLED == "true" ]]; then
    PrintOkIndicator "$key is already installed."
  else
    function InstallTool {
      DEBIAN_FRONTEND=noninteractive apt-get -yq install $@
    }

    Run "InstallTool ${tools[$key]}" \
      "Installing $key..." \
      "Failed to install $key." \
      "Installed $key."
  fi
done
#endregion

#region Add jenkins user to docker group
if grep "^docker:" /etc/group |grep -q ":jenkins"; then
  PrintOkIndicator "Jenkins user is already added to the Docker group."
else
  function AddJenkinsUserToDockerGroup {
    usermod -a -G docker jenkins
  }

  function RestartJenkins {
    systemctl restart jenkins.service
  }

  Run "AddJenkinsUserToDockerGroup" \
    "Adding Jenkins user to Docker group..." \
    "Failed to add Jenkins user to Docker group." \
    "Added Jenkins user to Docker group."

  Run "RestartJenkins" \
    "Restarting Jenkins..." \
    "Failed to restart Jenkins." \
    "Restarted Jenkins."
fi
#endregion

#region Add dummy0 interface
if [ -f /etc/network/interfaces.d/dummy0.interface ]; then
  PrintOkIndicator "Network interface dummy0 has already been configured."
else
  function CopyInterfaceFile {
    cp ./auto/dummy0.interface /etc/network/interfaces.d
  }

  Run "CopyInterfaceFile" \
    "Configuring network interface dummy0..." \
    "Failed to configure network interface dummy0." \
    "Network interface dummy0 has been configured."
fi
#endregion

#region Start network interface dummy0
if ip a show dummy0 2>/dev/null > /dev/null; then
  PrintOkIndicator "Network interface dummy0 is already started."
else
  Run "ifup dummy0" \
    "Starting network interface dummy0..." \
    "Failed to start network interface dummy0." \
    "Started network interface dummy0."
fi
#endregion

#region Install Docker Registry
if [ -f /etc/docker/daemon.json ]; then
  PrintOkIndicator "Docker Registry has already been configured."
else
  function CreateDockerRegistryConfig {
    cp ./auto/daemon.json /etc/docker/
  }

  function RestartDocker {
    systemctl restart docker.service
  }

  Run "CreateDockerRegistryConfig" \
    "Configuring Docker Registry..." \
    "Failed to configure Docker Registry." \
    "Docker registry has been configured."

  Run "RestartDocker" \
    "Restarting Docker..." \
    "Failed to restart Docker." \
    "Restarted Docker."
fi
#endregion

#region Pull image if it doesn't exist locally.
if docker image inspect registry:2.7 > /dev/null 2>&1; then
  PrintOkIndicator "Docker Registry image has already been pulled."
else
  function PullDockerRegistryImage {
    docker pull registry:2.7
  }

  Run "PullDockerRegistryImage" \
    "Pulling Docker Registry image..." \
    "Failed to pull Docker Registry image." \
    "Docker Registry image has been pulled."
fi
#endregion

#region Start Docker Registry
if docker container inspect registry > /dev/null 2>&1; then
  PrintOkIndicator "Docker Registry is already running."
else
  function StartDockerRegistry {
    docker run -d -p 5000:5000 --restart always --name registry registry:2.7
  }

  Run "StartDockerRegistry" \
    "Starting Docker Registry..." \
    "Failed to start Docker Registry." \
    "Docker Registry has been started."
fi
#endregion

echo
echo "Part 1 of the installer is now done."
echo "Please run part two (2-jenkins.sh) to set up Jenkins."

# Create marker file
CreateMarkerFile 1