Add JDK 17 in nodes used in jenkins verify jobs in ONAP & O-RAN-SC
[infra/tools.git] / infra / jenkins / slave-setup / cloud-infra / configure-image-ubuntu1804.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
20 set -o nounset
21 set -o pipefail
22
23 # ensure apt is not running before proceeding with the rest
24 echo "Info  : Wait for completion of an existing apt process before proceeding..."
25 while true; do
26   pkg_mgr_process=$(pgrep -f apt | cat)
27   if [[ -n $pkg_mgr_process ]]; then
28     sleep 10
29   else
30     break
31   fi
32 done
33 echo "Info  : apt process done. Continuing..."
34
35 # list of basic packages to install
36 PKG_LIST=(
37     apt-utils
38     apt-transport-https
39     ca-certificates
40     gnupg-agent
41     software-properties-common
42     git
43     vim
44     curl
45     wget
46     chrony
47     openjdk-11-jre-headless
48     jq
49 )
50
51 # we need apt to proceed without any prompt asking for user input
52 export DEBIAN_FRONTEND=noninteractive
53
54 echo "Info  : Install packages"
55 # update packages to their latest
56 sudo -H -E apt update
57 sudo -H -E apt upgrade -y -q=3
58
59 # install packages
60 sudo -H -E apt -y -q=3 install ${PKG_LIST[@]}
61
62 # If you have a issue with Let's Encrypt certificate when cloning repo due to DST Root CA X3 Expiration:
63 # https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/
64 # remove outdated certificate from system
65 sudo rm -rf /usr/share/ca-certificates/mozilla/DST_Root_CA_X3.crt
66 # update ca-certificates
67 sudo update-ca-certificates --fresh --verbose
68
69 # remove unnecessary packages
70 sudo -H -E apt autoremove -y
71
72 echo "Info  : Enable time sync"
73 # ensure time sync is setup
74 sudo systemctl enable chrony --now
75 sudo chronyc -a 'burst 4/4' && sudo chronyc -a makestep
76
77 echo "Info  : Enable nested virtualization"
78 # enable nested virtualization
79 sudo bash -c 'cat << EOF > /etc/modprobe.d/qemu-system-x86.conf
80 options kvm-intel nested=y enable_apicv=n
81 EOF'
82 sudo modprobe -r kvm_intel kvm
83 sudo modprobe -a kvm_intel kvm
84 sudo lsmod | grep kvm_intel
85 sudo cat /sys/module/kvm_intel/parameters/nested
86
87 echo "Info  : Create and configure jenkins user"
88 # create and configure jenkins user
89 sudo useradd -G sudo -d /home/jenkins -m -c "jenkins user" -s /bin/bash jenkins
90 sudo mkdir -p /home/jenkins/nordix/slave_root
91 sudo chown -R jenkins:jenkins /home/jenkins/nordix/slave_root
92 sudo chmod -R 755 /home/jenkins/nordix/slave_root
93
94 # modify sudoers - disable env_reset, !requiretty and passwordless sudo
95 sudo sed -i "s/^Defaults.*env_reset/#&\nDefaults:jenkins  \!requiretty/" /etc/sudoers
96 sudo sed -i "s/^%sudo.*ALL/%sudo   ALL=(ALL:ALL)   NOPASSWD: ALL/" /etc/sudoers
97
98 # disable ssh password login, enable ssh with keys for jenkins user
99 sudo bash -c "echo PasswordAuthentication no >> /etc/ssh/sshd_config"
100 sudo bash -c "echo PubkeyAuthentication yes >> /etc/ssh/sshd_config"
101 sudo bash -c "echo AllowUsers jenkins >> /etc/ssh/sshd_config"
102 sudo systemctl restart sshd
103
104 echo "Info  : Create cloud-init script"
105 # get cloud-init script in place so we can place the keys into ~jenkins/.ssh
106 sudo bash -c 'cat << EOF > /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
107 #!/bin/bash
108 sudo mkdir -p /home/jenkins/.ssh
109 # append ssh key injected by openstack to authorized_keys
110 sudo cat /home/ubuntu/.ssh/authorized_keys >> /home/jenkins/.ssh/authorized_keys
111 # append user ssh public keys uploaded by packer to authorized_keys
112 sudo cat /home/ubuntu/authorized_keys.packer >> /home/jenkins/.ssh/authorized_keys
113 # remove /home/ubuntu/authorized_keys.packer
114 sudo rm -f /home/jenkins/authorized_keys.packer
115 sudo chown -R jenkins:jenkins /home/jenkins/.ssh
116 sudo chmod -R go-rwx /home/jenkins/.ssh
117 sudo userdel -f -r ubuntu
118 EOF'
119
120 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh