Add JDK 17 in nodes used in jenkins verify jobs in ONAP & O-RAN-SC
[infra/tools.git] / infra / jenkins / slave-setup / oransc-build-server / configure-image.sh
1 #!/bin/bash
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2021 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 -ex
20
21 # 3PP versions
22 DOCKER_VERSION="5:20.10.11~3-0~ubuntu-focal"
23 GO_VERSION="1.17"
24 GO_LINT_VERSION="1.39.0"
25
26 sudo apt-get update
27 sudo DEBIAN_FRONTEND=noninteractive apt-get -y upgrade
28 sudo apt-get install -y unzip
29
30 # INSTALL JAVA
31 sudo apt-get install -y openjdk-11-jdk openjdk-17-jdk 
32 java -version
33
34 # INSTALL DOCKER
35 sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \
36     apt-transport-https \
37     ca-certificates \
38     curl \
39     gnupg-agent \
40     software-properties-common
41 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
42 sudo add-apt-repository \
43    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
44    $(lsb_release -cs) \
45    stable"
46 sudo apt-get update
47 sudo apt-get install -y docker-ce=${DOCKER_VERSION} docker-ce-cli=${DOCKER_VERSION} containerd.io
48 docker --version
49
50 sudo systemctl enable docker
51 sudo systemctl start docker
52
53 # If you have a issue with Let's Encrypt certificate when cloning repo due to DST Root CA X3 Expiration:
54 # https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/
55 # remove outdated certificate from system
56 sudo rm -rf /usr/share/ca-certificates/mozilla/DST_Root_CA_X3.crt
57 # update ca-certificates
58 sudo update-ca-certificates --fresh --verbose
59
60 echo "Info  : Install ORANSC build dependency packages"
61 sudo apt install -y  \
62     diffstat \
63     gcc \
64     build-essential 
65
66 # INSTALL GO
67 cd /tmp
68 # golang
69 wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz
70 tar xzvf go${GO_VERSION}.linux-amd64.tar.gz
71 sudo mv go /usr/local
72 # golangci-lint
73 wget https://github.com/golangci/golangci-lint/releases/download/v${GO_LINT_VERSION}/golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
74 sudo dpkg -i golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
75 /bin/rm -rf  go${GO_VERSION}.linux-amd64.tar.gz golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
76
77 # CREATE JENKINS USER
78 # Crete homedir, add to sudo group, add entry in /etc/passwd
79 sudo useradd -G sudo,docker -d /home/jenkins -m -c "jenkins user" -s /bin/bash jenkins
80 # Create slave root directory
81 sudo mkdir -p /home/jenkins/nordix/slave_root
82 sudo chown -R jenkins:jenkins /home/jenkins/nordix/slave_root
83 sudo chmod -R 755 /home/jenkins/nordix/slave_root
84 # Modify sudoers - disable env_reset, !requiretty and passwordless sudo
85 sudo sed -i "s/^Defaults.*env_reset/#&\nDefaults:jenkins  \!requiretty/" /etc/sudoers
86 sudo sed -i "s/^%sudo.*ALL/%sudo   ALL=(ALL:ALL)   NOPASSWD: ALL/" /etc/sudoers
87 # Disable ssh password login, enable ssh with keys for jenkins user
88 sudo bash -c "echo PasswordAuthentication no >> /etc/ssh/sshd_config"
89 sudo bash -c "echo PubkeyAuthentication yes >> /etc/ssh/sshd_config"
90 sudo bash -c "echo AllowUsers jenkins >> /etc/ssh/sshd_config"
91 sudo systemctl restart sshd
92
93 # configure sysctl
94 sudo sysctl -w net.ipv4.tcp_keepalive_time=120
95 sudo sysctl -w net.ipv4.tcp_keepalive_intvl=30
96 sudo sysctl -w net.ipv4.tcp_keepalive_probes=8
97 sudo sysctl -w net.ipv4.tcp_fin_timeout=30
98
99 # update ~jenkins/.profile
100 sudo bash -c "echo 'export PATH=\$PATH:/usr/local/go/bin' >> /home/jenkins/.profile"
101
102 echo "Info  : Create cloud-init script"
103 # get cloud-init script in place so we can place the keys into ~jenkins/.ssh
104 sudo bash -c 'cat << EOF > /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
105 #!/bin/bash
106 sudo mkdir -p /home/jenkins/.ssh
107 # append ssh key injected by openstack to authorized_keys
108 sudo cat /home/ubuntu/.ssh/authorized_keys >> /home/jenkins/.ssh/authorized_keys
109 # append user ssh public keys uploaded by packer to authorized_keys
110 sudo cat /home/ubuntu/authorized_keys.packer >> /home/jenkins/.ssh/authorized_keys
111 # remove /home/ubuntu/authorized_keys.packer
112 sudo rm -f /home/jenkins/authorized_keys.packer
113 sudo chown -R jenkins:jenkins /home/jenkins/.ssh
114 sudo chmod -R go-rwx /home/jenkins/.ssh
115 sudo userdel -f -r ubuntu
116 EOF'
117
118 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh