77cc82ef85201e1b518217f7159be03cbf15443e
[infra/tools.git] / infra / jenkins / slave-setup / nordix-nsm-build-server / configure-image-ubuntu1804.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
20 set -o errexit
21 set -o pipefail
22
23 # update and upgrade
24 export DEBIAN_FRONTEND=noninteractive
25 sudo apt update
26 sudo apt upgrade -y
27
28 # install basic dependencies
29 sudo apt install -y make openjdk-11-jre-headless apt-transport-https ca-certificates curl gnupg jq software-properties-common build-essential
30
31 # If you have a issue with Let's Encrypt certificate when cloning repo due to DST Root CA X3 Expiration:
32 # https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/
33 # remove outdated certificate from system
34 sudo rm -rf /usr/share/ca-certificates/mozilla/DST_Root_CA_X3.crt
35 # update ca-certificates
36 sudo update-ca-certificates --fresh --verbose
37
38 # set versions of various things for NSM for visibility and ease of maintenance
39 DOCKER_CE_VERSION="5:20.10.17~3-0~ubuntu-bionic"
40 DOCKER_CE_CLI_VERSION="5:20.10.17~3-0~ubuntu-bionic"
41 CONTAINERD_IO_VERSION="1.6.6-1"
42 GO_VERSION="1.19"
43 GO_LINT_VERSION="1.47.2"
44 FOSSA_CLI_VERSION="1.1.7"
45 KUBECTL_VERSION="v1.25.0"
46 HELM_VERSION="v3.10.1"
47 YQ_VERSION="v4.21.1"
48
49 # install docker-ce, docker-ce-cli, containerd.io and mark them hold
50 sudo apt remove -y docker docker-engine docker.io containerd runc
51 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
52 echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
53   $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
54 sudo apt update
55 sudo apt install -y docker-ce=$DOCKER_CE_VERSION docker-ce-cli=$DOCKER_CE_CLI_VERSION containerd.io=$CONTAINERD_IO_VERSION
56 sudo apt-mark hold docker-ce docker-ce-cli containerd.io
57 sudo systemctl enable docker
58 sudo systemctl start docker
59
60 # install go related stuff
61 cd /tmp
62 # golang
63 wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz
64 tar xzvf go${GO_VERSION}.linux-amd64.tar.gz
65 sudo mv go /usr/local
66 # golangci-lint
67 wget https://github.com/golangci/golangci-lint/releases/download/v${GO_LINT_VERSION}/golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
68 sudo dpkg -i golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
69 /bin/rm -rf  go${GO_VERSION}.linux-amd64.tar.gz golangci-lint-${GO_LINT_VERSION}-linux-amd64.deb
70
71 # install fossa-cli
72 wget https://github.com/fossas/fossa-cli/releases/download/v${FOSSA_CLI_VERSION}/fossa-cli_${FOSSA_CLI_VERSION}_linux_amd64.tar.gz
73 tar xzvf fossa-cli_${FOSSA_CLI_VERSION}_linux_amd64.tar.gz
74 sudo mv fossa /usr/local/bin
75 sudo chmod +x /usr/local/bin/fossa
76 /bin/rm -rf fossa-cli_${FOSSA_CLI_VERSION}_linux_amd64.tar.gz
77
78 # Install kubectl 
79 wget https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl
80 sudo mv kubectl /usr/local/bin
81 sudo chmod +x /usr/local/bin/kubectl
82
83 # Install Helm
84 wget https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz
85 tar xzvf helm-${HELM_VERSION}-linux-amd64.tar.gz
86 sudo mv ./linux-amd64/helm /usr/local/bin
87 sudo chmod +x /usr/local/bin/helm
88 /bin/rm -rf helm-${HELM_VERSION}-linux-amd64.tar.gz
89
90 # Install yq
91 wget https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64
92 sudo mv yq_linux_amd64 /usr/local/bin/yq
93 sudo chmod +x /usr/local/bin/yq
94
95 # Create jenkins user, add it to required groups, configure sudoers and sshd_config
96 sudo useradd -G sudo,docker -d /home/jenkins -m -c "jenkins user" -s /bin/bash jenkins
97 # Create slave root directory
98 sudo mkdir -p /home/jenkins/nordix/slave_root
99 sudo chown -R jenkins:jenkins /home/jenkins/nordix/slave_root
100 sudo chmod -R 755 /home/jenkins/nordix/slave_root
101 # Modify sudoers - disable env_reset, !requiretty and passwordless sudo
102 sudo sed -i "s/^Defaults.*env_reset/#&\nDefaults:jenkins  \!requiretty/" /etc/sudoers
103 sudo sed -i "s/^%sudo.*ALL/%sudo   ALL=(ALL:ALL)   NOPASSWD: ALL/" /etc/sudoers
104 # Disable ssh password login, enable ssh with keys for jenkins user
105 sudo bash -c "echo PasswordAuthentication no >> /etc/ssh/sshd_config"
106 sudo bash -c "echo PubkeyAuthentication yes >> /etc/ssh/sshd_config"
107 sudo bash -c "echo AllowUsers jenkins >> /etc/ssh/sshd_config"
108 sudo systemctl restart sshd
109
110 # configure sysctl
111 sudo sysctl -w net.ipv4.tcp_keepalive_time=120
112 sudo sysctl -w net.ipv4.tcp_keepalive_intvl=30
113 sudo sysctl -w net.ipv4.tcp_keepalive_probes=8
114 sudo sysctl -w net.ipv4.tcp_fin_timeout=30
115
116 # update ~jenkins/.profile
117 sudo bash -c "echo 'export PATH=\$PATH:/usr/local/go/bin' >> /home/jenkins/.profile"
118
119 # get cloud-init script in place so we can place the keys into ~jenkins/.ssh
120 sudo bash -c 'cat << EOF > /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
121 #!/bin/bash
122 sudo mkdir -p /home/jenkins/.ssh
123 # append ssh key injected by openstack to authorized_keys
124 sudo cat /home/ubuntu/.ssh/authorized_keys >> /home/jenkins/.ssh/authorized_keys
125 # append user ssh public keys uploaded by packer to authorized_keys
126 sudo cat /home/ubuntu/authorized_keys.packer >> /home/jenkins/.ssh/authorized_keys
127 # remove /home/ubuntu/authorized_keys.packer
128 sudo rm -f /home/jenkins/authorized_keys.packer
129 sudo chown -R jenkins:jenkins /home/jenkins/.ssh
130 sudo chmod -R go-rwx /home/jenkins/.ssh
131 sudo userdel -f -r ubuntu
132 EOF'
133
134 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
135
136 # vim: set ts=2 sw=2 expandtab: