Merge "nsm: Create build server packer file"
[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 # remove unnecessary packages
63 sudo -H -E apt autoremove -y
64
65 echo "Info  : Enable time sync"
66 # ensure time sync is setup
67 sudo systemctl enable chrony --now
68 sudo chronyc -a 'burst 4/4' && sudo chronyc -a makestep
69
70 echo "Info  : Enable nested virtualization"
71 # enable nested virtualization
72 sudo bash -c 'cat << EOF > /etc/modprobe.d/qemu-system-x86.conf
73 options kvm-intel nested=y enable_apicv=n
74 EOF'
75 sudo modprobe -r kvm_intel kvm
76 sudo modprobe -a kvm_intel kvm
77 sudo lsmod | grep kvm_intel
78 sudo cat /sys/module/kvm_intel/parameters/nested
79
80 echo "Info  : Create and configure jenkins user"
81 # create and configure jenkins user
82 sudo useradd -G sudo -d /home/jenkins -m -c "jenkins user" -s /bin/bash jenkins
83 sudo mkdir -p /home/jenkins/nordix/slave_root
84 sudo chown -R jenkins:jenkins /home/jenkins/nordix/slave_root
85 sudo chmod -R 755 /home/jenkins/nordix/slave_root
86
87 # modify sudoers - disable env_reset, !requiretty and passwordless sudo
88 sudo sed -i "s/^Defaults.*env_reset/#&\nDefaults:jenkins  \!requiretty/" /etc/sudoers
89 sudo sed -i "s/^%sudo.*ALL/%sudo   ALL=(ALL:ALL)   NOPASSWD: ALL/" /etc/sudoers
90
91 # disable ssh password login, enable ssh with keys for jenkins user
92 sudo bash -c "echo PasswordAuthentication no >> /etc/ssh/sshd_config"
93 sudo bash -c "echo PubkeyAuthentication yes >> /etc/ssh/sshd_config"
94 sudo bash -c "echo AllowUsers jenkins >> /etc/ssh/sshd_config"
95 sudo systemctl restart sshd
96
97 echo "Info  : Create cloud-init script"
98 # get cloud-init script in place so we can place the keys into ~jenkins/.ssh
99 sudo bash -c 'cat << EOF > /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
100 #!/bin/bash
101 sudo mkdir -p /home/jenkins/.ssh
102 # append ssh key injected by openstack to authorized_keys
103 sudo cat /home/ubuntu/.ssh/authorized_keys >> /home/jenkins/.ssh/authorized_keys
104 # append user ssh public keys uploaded by packer to authorized_keys
105 sudo cat /home/ubuntu/authorized_keys.packer >> /home/jenkins/.ssh/authorized_keys
106 # remove /home/ubuntu/authorized_keys.packer
107 sudo rm -f /home/jenkins/authorized_keys.packer
108 sudo chown -R jenkins:jenkins /home/jenkins/.ssh
109 sudo chmod -R go-rwx /home/jenkins/.ssh
110 sudo userdel -f -r ubuntu
111 EOF'
112
113 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh