Update jenkins build slave packer script
[infra/tools.git] / infra / jenkins / slave-setup / nordix-onap-jenkins-build-server / configure-image.sh
1 #!/bin/bash
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
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 # Wait for other apt process to finish by checking the dpkg lock file.
26 try=0
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 ..."
30   sleep 10
31   if [[ ${try} -gt 60 ]]; then
32     echo "ERROR: Max number of re-tries reached, exiting..."
33     exit 1
34   fi
35   try=$((try + 1))
36 done
37
38 # list of basic packages to install
39 PKG_LIST=(
40     apt-utils
41     apt-transport-https
42     ca-certificates
43     gnupg-agent
44     software-properties-common
45     git
46     vim
47     curl
48     wget
49     zip
50     unzip
51     chrony
52     curl
53     jq
54     docker-ce
55     docker-ce-cli
56     containerd.io
57     openjdk-8-jre
58     openjdk-8-jdk
59 )
60 # we need apt to proceed without any prompt asking for user input
61 export DEBIAN_FRONTEND=noninteractive
62
63 echo "Info  : Install packages"
64 # update packages to their latest
65 sudo -H -E apt update
66 sudo -H -E apt upgrade -y -q=3
67
68 # install packages
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 \
73    $(lsb_release -cs) \
74    stable"
75 sudo apt-get update
76 sudo -H -E apt -y -q=3 --no-install-recommends install "${PKG_LIST[@]}"
77
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
84
85 # Enable and start docker
86 docker --version
87 sudo systemctl enable docker
88 sudo systemctl start docker
89
90 # install command-line YAML processor from Snappy
91 sudo snap install yq
92
93 # remove unnecessary packages
94 sudo -H -E apt autoremove -y
95
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
100 sudo apt-get update
101 sudo -H -E apt -y -q=3 install \
102     openjdk-11-jdk \
103     openjdk-12-jdk
104 sudo rm -rf /etc/alternatives
105 sudo mv /etc/keep-alternatives /etc/alternatives
106 java -version
107
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
112
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
119
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
123
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
129
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
133 #!/bin/bash
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
144 EOF'
145
146 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh