8ba4783a0bb521d030fb3a6f7c1e77fa70ad2d5c
[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
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 echo "Info  : Install ORANSC build dependency packages"
54 sudo apt install -y  \
55     diffstat \
56     gcc \
57     build-essential 
58
59 # INSTALL GO
60 cd /tmp
61 # golang
62 wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz
63 tar xzvf go${GO_VERSION}.linux-amd64.tar.gz
64 sudo mv go /usr/local
65 # golangci-lint
66 wget https://github.com/golangci/golangci-lint/releases/download/v${GO_LINT_VERSION}/golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
67 sudo dpkg -i golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
68 /bin/rm -rf  go${GO_VERSION}.linux-amd64.tar.gz golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
69
70 # CREATE JENKINS USER
71 # Crete homedir, add to sudo group, add entry in /etc/passwd
72 sudo useradd -G sudo,docker -d /home/jenkins -m -c "jenkins user" -s /bin/bash jenkins
73 # Create slave root directory
74 sudo mkdir -p /home/jenkins/nordix/slave_root
75 sudo chown -R jenkins:jenkins /home/jenkins/nordix/slave_root
76 sudo chmod -R 755 /home/jenkins/nordix/slave_root
77 # Modify sudoers - disable env_reset, !requiretty and passwordless sudo
78 sudo sed -i "s/^Defaults.*env_reset/#&\nDefaults:jenkins  \!requiretty/" /etc/sudoers
79 sudo sed -i "s/^%sudo.*ALL/%sudo   ALL=(ALL:ALL)   NOPASSWD: ALL/" /etc/sudoers
80 # Disable ssh password login, enable ssh with keys for jenkins user
81 sudo bash -c "echo PasswordAuthentication no >> /etc/ssh/sshd_config"
82 sudo bash -c "echo PubkeyAuthentication yes >> /etc/ssh/sshd_config"
83 sudo bash -c "echo AllowUsers jenkins >> /etc/ssh/sshd_config"
84 sudo systemctl restart sshd
85
86 # configure sysctl
87 sudo sysctl -w net.ipv4.tcp_keepalive_time=120
88 sudo sysctl -w net.ipv4.tcp_keepalive_intvl=30
89 sudo sysctl -w net.ipv4.tcp_keepalive_probes=8
90 sudo sysctl -w net.ipv4.tcp_fin_timeout=30
91
92 # update ~jenkins/.profile
93 sudo bash -c "echo 'export PATH=\$PATH:/usr/local/go/bin' >> /home/jenkins/.profile"
94
95 echo "Info  : Create cloud-init script"
96 # get cloud-init script in place so we can place the keys into ~jenkins/.ssh
97 sudo bash -c 'cat << EOF > /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
98 #!/bin/bash
99 sudo mkdir -p /home/jenkins/.ssh
100 # append ssh key injected by openstack to authorized_keys
101 sudo cat /home/ubuntu/.ssh/authorized_keys >> /home/jenkins/.ssh/authorized_keys
102 # append user ssh public keys uploaded by packer to authorized_keys
103 sudo cat /home/ubuntu/authorized_keys.packer >> /home/jenkins/.ssh/authorized_keys
104 # remove /home/ubuntu/authorized_keys.packer
105 sudo rm -f /home/jenkins/authorized_keys.packer
106 sudo chown -R jenkins:jenkins /home/jenkins/.ssh
107 sudo chmod -R go-rwx /home/jenkins/.ssh
108 sudo userdel -f -r ubuntu
109 EOF'
110
111 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh