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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
23 DPKG_LOCK="/var/lib/dpkg/lock-frontend"
26 DOCKER_VERSION="5:20.10.11~3-0~ubuntu-focal"
28 # Wait for other apt process to finish by checking the dpkg lock file.
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 ..."
34 if [[ ${try} -gt 60 ]]; then
35 echo "ERROR: Max number of re-tries reached, exiting..."
41 # list of basic packages to install
47 software-properties-common
57 docker-ce=${DOCKER_VERSION}
58 docker-ce-cli=${DOCKER_VERSION}
63 # we need apt to proceed without any prompt asking for user input
64 export DEBIAN_FRONTEND=noninteractive
66 echo "Info : Install packages"
67 # update packages to their latest
69 sudo -H -E apt upgrade -y
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 \
79 sudo -H -E apt -y -q=3 --no-install-recommends install "${PKG_LIST[@]}"
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
88 # Enable and start docker
90 sudo systemctl enable docker
91 sudo systemctl start docker
93 # remove unnecessary packages
94 sudo -H -E apt autoremove -y
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
100 sudo -H -E apt -y -q=3 install \
103 sudo rm -rf /etc/alternatives
104 sudo mv /etc/keep-alternatives /etc/alternatives
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
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
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
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
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
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
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
150 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh