2 # ============LICENSE_START=======================================================
3 # Copyright (C) 2020 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"
25 # Wait for other apt process to finish by checking the dpkg lock file.
27 while sudo lsof ${DPKG_LOCK} >/dev/null 2>&1; do
28 echo "DPKG file locked: ${DPKG_LOCK}."
29 echo " Waiting for another pkg instalaltion process to finish ..."
31 if [[ ${try} -gt 60 ]]; then
32 echo "ERROR: Max number of re-tries reached, exiting..."
38 # list of basic packages to install
44 software-properties-common
60 # we need apt to proceed without any prompt asking for user input
61 export DEBIAN_FRONTEND=noninteractive
63 echo "Info : Install packages"
64 # update packages to their latest
66 sudo -H -E apt upgrade -y -q=3
69 sudo apt remove -y docker docker-engine docker.io containerd runc
70 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
71 sudo add-apt-repository \
72 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
76 sudo -H -E apt -y -q=3 --no-install-recommends install "${PKG_LIST[@]}"
78 # If you have a issue with Let's Encrypt certificate when cloning repo due to DST Root CA X3 Expiration:
79 # https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/
80 # remove outdated certificate from system
81 sudo rm -rf /usr/share/ca-certificates/mozilla/DST_Root_CA_X3.crt
82 # update ca-certificates
83 sudo update-ca-certificates --fresh --verbose
85 # Enable and start docker
87 sudo systemctl enable docker
88 sudo systemctl start docker
90 # install command-line YAML processor from Snappy
93 # remove unnecessary packages
94 sudo -H -E apt autoremove -y
96 echo "Info : Install additional Java version"
97 # INSTALL additional Java 11 and 12 while keep pointing alternatives to JDK8
98 sudo cp -R /etc/alternatives /etc/keep-alternatives
99 sudo add-apt-repository -y ppa:openjdk-r/ppa
101 sudo -H -E apt -y -q=3 install \
104 sudo rm -rf /etc/alternatives
105 sudo mv /etc/keep-alternatives /etc/alternatives
108 echo "Info : Enable time sync"
109 # ensure time sync is setup
110 sudo systemctl enable chrony --now
111 sudo chronyc -a 'burst 4/4' && sudo chronyc -a makestep
113 echo "Info : Create and configure jenkins user"
114 # create and configure jenkins user
115 sudo useradd -G sudo,docker -d /home/jenkins -m -c "jenkins user" -s /bin/bash jenkins
116 sudo mkdir -p /home/jenkins/nordix/slave_root
117 sudo chown -R jenkins:jenkins /home/jenkins/nordix/slave_root
118 sudo chmod -R 755 /home/jenkins/nordix/slave_root
120 # Modify sudoers - disable env_reset, !requiretty and passwordless sudo
121 sudo sed -i "s/^Defaults.*env_reset/#&\nDefaults:jenkins \!requiretty/" /etc/sudoers
122 sudo sed -i "s/^%sudo.*ALL/%sudo ALL=(ALL:ALL) NOPASSWD: ALL/" /etc/sudoers
124 # Disable ssh password login, enable ssh with keys for jenkins user
125 sudo bash -c "echo PasswordAuthentication no >> /etc/ssh/sshd_config"
126 sudo bash -c "echo PubkeyAuthentication yes >> /etc/ssh/sshd_config"
127 sudo bash -c "echo AllowUsers jenkins >> /etc/ssh/sshd_config"
128 sudo systemctl restart sshd
130 echo "Info : Create cloud-init script"
131 # get cloud-init script in place so we can place the keys into ~jenkins/.ssh
132 sudo bash -c 'cat << EOF > /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
134 sudo mkdir -p /home/jenkins/.ssh
135 # append ssh key injected by openstack to authorized_keys
136 sudo cat /home/ubuntu/.ssh/authorized_keys >> /home/jenkins/.ssh/authorized_keys
137 # append user ssh public keys uploaded by packer to authorized_keys
138 sudo cat /home/ubuntu/authorized_keys.packer >> /home/jenkins/.ssh/authorized_keys
139 # remove /home/ubuntu/authorized_keys.packer
140 sudo rm -f /home/jenkins/authorized_keys.packer
141 sudo chown -R jenkins:jenkins /home/jenkins/.ssh
142 sudo chmod -R go-rwx /home/jenkins/.ssh
143 sudo userdel -f -r ubuntu
146 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh