Update jenkins build slave packer script
[infra/tools.git] / infra / jenkins / slave-setup / uds-build-server / configure-image.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 set -ex
20
21 sudo apt-get update
22 sudo apt-get install -y unzip
23
24 # INSTALL JAVA
25 sudo apt-get install -y openjdk-11-jdk
26 java -version
27
28 # INSTALL DOCKER
29 sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \
30     apt-transport-https \
31     ca-certificates \
32     curl \
33     gnupg-agent \
34     software-properties-common
35 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
36 sudo add-apt-repository \
37    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
38    $(lsb_release -cs) \
39    stable"
40 sudo apt-get update
41 sudo apt-get install -y docker-ce=5:19.03.12~3-0~ubuntu-bionic docker-ce-cli=5:19.03.12~3-0~ubuntu-bionic containerd.io
42 docker --version
43
44 sudo systemctl enable docker
45 sudo systemctl start docker
46
47 # If you have a issue with Let's Encrypt certificate when cloning repo due to DST Root CA X3 Expiration:
48 # https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/
49 # remove outdated certificate from system
50 sudo rm -rf /usr/share/ca-certificates/mozilla/DST_Root_CA_X3.crt
51 # update ca-certificates
52 sudo update-ca-certificates --fresh --verbose
53
54 # CREATE JENKINS USER
55 # Crete homedir, add to sudo group, add entry in /etc/passwd
56 sudo useradd -G sudo,docker -d /home/jenkins -m -c "jenkins user" -s /bin/bash jenkins
57 # Create slave root directory
58 sudo mkdir -p /home/jenkins/nordix/slave_root
59 sudo chown -R jenkins:jenkins /home/jenkins/nordix/slave_root
60 sudo chmod -R 755 /home/jenkins/nordix/slave_root
61 # Modify sudoers - disable env_reset, !requiretty and passwordless sudo
62 sudo sed -i "s/^Defaults.*env_reset/#&\nDefaults:jenkins  \!requiretty/" /etc/sudoers
63 sudo sed -i "s/^%sudo.*ALL/%sudo   ALL=(ALL:ALL)   NOPASSWD: ALL/" /etc/sudoers
64 # Disable ssh password login, enable ssh with keys for jenkins user
65 sudo bash -c "echo PasswordAuthentication no >> /etc/ssh/sshd_config"
66 sudo bash -c "echo PubkeyAuthentication yes >> /etc/ssh/sshd_config"
67 sudo bash -c "echo AllowUsers jenkins >> /etc/ssh/sshd_config"
68 sudo systemctl restart sshd
69
70 # get cloud-init script in place so we can place the keys into ~jenkins/.ssh
71 sudo bash -c 'cat << EOF > /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh
72 #!/bin/bash
73 sudo mkdir -p /home/jenkins/.ssh
74 sudo cat /home/ubuntu/.ssh/authorized_keys >> /home/jenkins/.ssh/authorized_keys
75 sudo chown -R jenkins:jenkins /home/jenkins/.ssh
76 sudo chmod -R go-rwx /home/jenkins/.ssh
77 sudo userdel -f -r ubuntu
78 EOF'
79 sudo chmod +x /var/lib/cloud/scripts/per-instance/copykeystojenkins.sh