From 3e107ee97893155d1d024f8529aa1ca7bdad1ac8 Mon Sep 17 00:00:00 2001 From: Siddharth Rawat Date: Thu, 19 Oct 2023 20:31:33 -0400 Subject: [PATCH] feat: configure AMI to include Jenkins configuration Update install script to include Jenkins plugins installation through a `plugins.txt` file. Include Configurations as Code `yaml` file that configures a basic multi-branch pipeline seed job for job-dsl, written in Groovy. Fixes issues #21 #23 --- scripts/casc.yaml | 16 ++++++ scripts/install.sh | 132 +++++++++++++++++++++++++++++++++++--------- scripts/plugins.txt | 23 ++++++++ ubuntu.ami.pkr.hcl | 13 ++++- 4 files changed, 157 insertions(+), 27 deletions(-) create mode 100644 scripts/casc.yaml mode change 100644 => 100755 scripts/install.sh create mode 100644 scripts/plugins.txt diff --git a/scripts/casc.yaml b/scripts/casc.yaml new file mode 100644 index 0000000..a69c8f2 --- /dev/null +++ b/scripts/casc.yaml @@ -0,0 +1,16 @@ +obs: + - script: > + multibranchPipelineJob('test-seed') { + branchSources { + git { + id('92098798334') + remote('https://github.com/username/example') + includes('*') + } + } + orphanedItemStrategy { + discardOldItems { + numToKeep(5) + } + } + } diff --git a/scripts/install.sh b/scripts/install.sh old mode 100644 new mode 100755 index aeaf0fb..d842c4b --- a/scripts/install.sh +++ b/scripts/install.sh @@ -8,7 +8,7 @@ echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" echo "| |" -echo "| INSTALL SCRIPT v1.0 |" +echo "| INSTALL SCRIPT v2.0 |" echo "| |" echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" @@ -17,12 +17,31 @@ sudo apt update --quiet && sudo apt upgrade -y echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" echo "| |" -echo "| INSTALL JAVA 11 |" +echo "| INSTALL JAVA 17 |" echo "| |" echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" -# Install Java for Jenkins -sudo sudo apt install openjdk-11-jdk -y +# Install Java for Jenkins: (https://adoptium.net/installation/linux/) + +# Ensure necessary packages are present: +sudo apt install -y wget apt-transport-https + +# Download the Eclipse Adoptium GPG key: +sudo mkdir -p /etc/apt/keyrings +wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee \ + /etc/apt/keyrings/adoptium.asc + +# Configure the Eclipse Adoptium apt repository: +# To check the full list of versions supported take a look at the list in the tree at https://packages.adoptium.net/ui/native/deb/dists/. +# For Linux Mint (based on Ubuntu) you have to replace VERSION_CODENAME with UBUNTU_CODENAME. +echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] \ +https://packages.adoptium.net/artifactory/deb \ +$(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | sudo tee \ + /etc/apt/sources.list.d/adoptium.list + +# Install the Temurin version you require: +sudo apt update # required to refresh apt with the newly installed keys +sudo apt install temurin-17-jdk -y # Validate Java installation JAVA=$? @@ -32,13 +51,7 @@ else echo "Unable to install Java" fi -echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" -echo "| |" -echo "| CHECK JAVA VERSION |" -echo "| |" -echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" - -# Check Java version +# Check Java version: echo "Java $(java -version)" echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" @@ -47,8 +60,7 @@ echo "| INSTALL J echo "| |" echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" -# Update local package index -sudo apt update --quiet +# Jenkins setup on Debian (stable): https://pkg.jenkins.io/debian-stable/ # Debian package repository of Jenkins to automate installation and upgrade. # To use this repository, first add the key to the system: @@ -60,8 +72,8 @@ echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian-stable binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list >/dev/null -# Update local package index, then finally install Jenkins: -sudo apt update --quiet +# Install Jenkins: +sudo apt update # required to refresh apt with the newly installed keys sudo apt install jenkins -y # Validate Jenkins installation @@ -73,13 +85,7 @@ else fi # Check the status of Jenkins service -sudo systemctl status jenkins - -echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" -echo "| |" -echo "| CHECK JENKINS VERSION |" -echo "| |" -echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" +sudo systemctl --full status jenkins # Check Jenkins version echo "Jenkins $(jenkins --version)" @@ -91,17 +97,17 @@ echo "| INSTALL CA echo "| |" echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" -# Update local package index -sudo apt update --quiet +# Caddy(stable) installation docs: https://caddyserver.com/docs/install#debian-ubuntu-raspbian +# Install and configure keyring for caddy stable release: sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo \ gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee \ /etc/apt/sources.list.d/caddy-stable.list -# Update local package index, then finally install caddy: -sudo apt update --quiet +# Install caddy: +sudo apt update # required to refresh apt with the newly installed keys sudo apt install caddy -y # Validate caddy installation: @@ -111,3 +117,77 @@ if [ $CADDY -eq 0 ]; then else echo "Unable to install the Caddy Service" fi + +# Check the status of Caddy service +sudo systemctl --full status caddy + +# https://caddyserver.com/docs/quick-starts/reverse-proxy +# For details on configuring reverse-proxy: +# https://github.com/cyse7125-fall2023-group05/infra-jenkins/blob/master/modules/ec2/userdata.sh +# To remove reverse proxy error: Jenkins->manage->configure->Jenkins URL->"caddy1" + +# Jenkins Configuration +echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" +echo "| |" +echo "| CONFIGURE JENKINS |" +echo "| |" +echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" + +# Install Jenkins plugin manager tool: +wget https://github.com/jenkinsci/plugin-installation-manager-tool/releases/download/2.12.13/jenkins-plugin-manager-2.12.13.jar + +# Install plugins with jenkins-plugin-manager tool: +sudo java -jar ./jenkins-plugin-manager-2.12.13.jar --war /usr/share/java/jenkins.war \ + --plugin-download-directory /var/lib/jenkins/plugins --plugin-file plugins.txt + +# Update users and group permissions to `jenkins` for all installed plugins: +cd /var/lib/jenkins/plugins/ || exit +sudo chown jenkins:jenkins * + +# Configure JAVA_OPTS to disable setup wizard +sudo mkdir -p /etc/systemd/system/jenkins.service.d/ +{ + echo "[Service]" + echo "Environment=\"JAVA_OPTS=-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false -Dcasc.jenkins.config=/var/lib/jenkins/casc.yaml\"" +} | sudo tee /etc/systemd/system/jenkins.service.d/override.conf +sudo systemctl daemon-reload +sudo systemctl stop jenkins +sudo systemctl start jenkins + +# Docker Setup +echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" +echo "| |" +echo "| INSTALL DOCKER |" +echo "| |" +echo "+-----------------------------------------------------------------------------------------------------------------------------------------+" + +# Add Docker's official GPG key: +sudo apt install ca-certificates curl gnupg +sudo install -m 0755 -d /etc/apt/keyrings +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg +sudo chmod a+r /etc/apt/keyrings/docker.gpg + +# Add the repository to Apt sources: +echo \ + "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ + "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | + sudo tee /etc/apt/sources.list.d/docker.list >/dev/null + +# Install Docker: +sudo apt update +sudo apt install docker-ce -y + +# Validate docker installation: +DOCKER=$? +if [ $DOCKER -eq 0 ]; then + echo "Successfully installed Docker" +else + echo "Unable to install Docker" +fi + +# Provide relevant permissions +sudo chmod 666 /var/run/docker.sock +sudo usermod -a -G docker jenkins + +# Check Docker version +echo "Docker $(docker --version)" diff --git a/scripts/plugins.txt b/scripts/plugins.txt new file mode 100644 index 0000000..cfa1649 --- /dev/null +++ b/scripts/plugins.txt @@ -0,0 +1,23 @@ +ant:latest +antisamy-markup-formatter:latest +authorize-project:latest +build-timeout:latest +cloudbees-folder:latest +configuration-as-code:latest +credentials-binding:latest +email-ext:latest +git:latest +github-branch-source:latest +gradle:latest +ldap:latest +mailer:latest +matrix-auth:latest +pam-auth:latest +pipeline-github-lib:latest +pipeline-stage-view:latest +ssh-slaves:latest +timestamper:latest +workflow-aggregator:latest +ws-cleanup:latest +job-dsl:latest +configuration-as-code-groovy:latest diff --git a/ubuntu.ami.pkr.hcl b/ubuntu.ami.pkr.hcl index 9c77bb7..2a36541 100644 --- a/ubuntu.ami.pkr.hcl +++ b/ubuntu.ami.pkr.hcl @@ -125,6 +125,12 @@ source "amazon-ebs" "ubuntu" { build { sources = ["source.amazon-ebs.ubuntu"] + # https://www.packer.io/docs/provisioners/file#uploading-files-that-don-t-exist-before-packer-starts + provisioners "file" { + source = "./scripts/plugins.txt" + destination = "/home/ubuntu/plugins.txt" + } + provisioner "shell" { environment_vars = [ "DEBIAN_FRONTEND=noninteractive", @@ -135,8 +141,13 @@ build { ] } + provisioners "file" { + source = "./scripts/casc.yaml" + destination = "/var/lib/jenkins/casc.yaml" + } + post-processor "manifest" { output = "manifest.json" strip_path = true } -} \ No newline at end of file +}