Packer: Add Infra build slave Ubuntu 18.04 image
[infra/tools.git] / infra / jenkins / slave-setup / geode-build-server1804 / configure-image.sh
1 #!/bin/bash
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2022 The Nordix Foundation. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19 set -o nounset
20 set -o errexit
21 set -o pipefail
22 #set -o xtrace
23 DPKG_LOCK="/var/lib/dpkg/lock-frontend"
24
25 # 3PP versions
26 DOCKER_VERSION="5:19.03.0~3-0~ubuntu-bionic"
27
28 # Wait for other apt process to finish by checking the dpkg lock file.
29 try=0
30 while sudo lsof ${DPKG_LOCK} >/dev/null 2>&1; do
31   echo "DPKG file locked: ${DPKG_LOCK}."
32   echo "   Waiting for another pkg instalaltion process to finish ..."
33   sleep 10
34   if [[ ${try} -gt 60 ]]; then
35     echo "ERROR: Max number of re-tries reached, exiting..."
36     exit 1
37   fi
38   try=$((try + 1))
39 done
40
41 # list of basic packages to install
42 PKG_LIST=(
43     apt-utils
44     apt-transport-https
45     ca-certificates
46     gnupg-agent
47     software-properties-common
48     git
49     vim
50     curl
51     wget
52     zip
53     unzip
54     chrony
55     curl
56     jq
57     docker-ce=${DOCKER_VERSION}
58     docker-ce-cli=${DOCKER_VERSION}
59     containerd.io
60     openjdk-11-jre
61     openjdk-11-jdk
62 )
63 # we need apt to proceed without any prompt asking for user input
64 export DEBIAN_FRONTEND=noninteractive
65
66 echo "Info  : Install packages"
67 # update packages to their latest
68 sudo -H -E apt update
69 sudo -H -E apt upgrade -y
70
71 # install packages
72 sudo apt remove -y docker docker.io containerd runc
73 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
74 sudo add-apt-repository \
75   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
76    $(lsb_release -cs) \
77    stable"
78 sudo apt-get update
79 sudo -H -E apt -y -q=3 --no-install-recommends install "${PKG_LIST[@]}"
80
81 # If you have a issue with Let's Encrypt certificate when cloning repo due to DST Root CA X3 Expiration:
82 # https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/
83 # remove outdated certificate from system
84 sudo rm -rf /usr/share/ca-certificates/mozilla/DST_Root_CA_X3.crt
85 # update ca-certificates
86 sudo update-ca-certificates --fresh --verbose
87
88 # Enable and start docker
89 docker --version
90 sudo systemctl enable docker
91 sudo systemctl start docker
92
93 # remove unnecessary packages
94 sudo -H -E apt autoremove -y
95
96 echo "Info  : Install additional Java version"
97 # INSTALL additional Java 8 while keep pointing alternatives to JDK11
98 sudo cp -R /etc/alternatives /etc/keep-alternatives
99 sudo apt-get update
100 sudo -H -E apt -y -q=3 install \
101     openjdk-8-jdk \
102     openjdk-8-jre
103 sudo rm -rf /etc/alternatives
104 sudo mv /etc/keep-alternatives /etc/alternatives
105 java -version
106
107 echo "Info  : Enable time sync"
108 # ensure time sync is setup
109 sudo systemctl enable chrony --now
110 sudo chronyc -a 'burst 4/4' && sudo chronyc -a makestep
111
112 echo "Info  : Create and configure geode user"
113 # create and configure geode user
114 sudo adduser --disabled-password --gecos "" --uid 93043 geode
115 sudo usermod -G docker -a geode
116
117 echo "Info  : Create and configure infra user"
118 # create and configure infra user
119 sudo useradd -G sudo,docker -d /home/infra -m -c "infra user" -s /bin/bash infra
120 sudo mkdir -p /home/infra/nordix/slave_root
121 sudo chown -R infra:infra /home/infra/nordix/slave_root
122 sudo chmod -R 755 /home/infra/nordix/slave_root
123
124 # Modify sudoers - disable env_reset, !requiretty and passwordless sudo
125 sudo sed -i "s/^Defaults.*env_reset/#&\nDefaults:infra  \!requiretty/" /etc/sudoers
126 sudo sed -i "s/^%sudo.*ALL/%sudo   ALL=(ALL:ALL)   NOPASSWD: ALL/" /etc/sudoers
127
128 # Disable ssh password login, enable ssh with keys for infra user
129 sudo bash -c "echo PasswordAuthentication no >> /etc/ssh/sshd_config"
130 sudo bash -c "echo PubkeyAuthentication yes >> /etc/ssh/sshd_config"
131 sudo bash -c "echo AllowUsers infra >> /etc/ssh/sshd_config"
132 sudo systemctl restart sshd
133
134 echo "Info  : Create cloud-init script"
135 # get cloud-init script in place so we can place the keys into ~infra/.ssh
136 sudo bash -c 'cat << EOF > /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
137 #!/bin/bash
138 sudo mkdir -p /home/infra/.ssh
139 # append ssh key injected by openstack to authorized_keys
140 sudo cat /home/ubuntu/.ssh/authorized_keys >> /home/infra/.ssh/authorized_keys
141 # append user ssh public keys uploaded by packer to authorized_keys
142 sudo cat /home/ubuntu/authorized_keys.packer >> /home/infra/.ssh/authorized_keys
143 # remove /home/ubuntu/authorized_keys.packer
144 sudo rm -f /home/infra/authorized_keys.packer
145 sudo chown -R infra:infra /home/infra/.ssh
146 sudo chmod -R go-rwx /home/infra/.ssh
147 sudo userdel -f -r ubuntu
148 EOF'
149
150 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh