Skip to content

Instantly share code, notes, and snippets.

@justinsoliz
Last active November 10, 2020 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justinsoliz/ce1d0d5cafdbce0efcdebb74fda2394d to your computer and use it in GitHub Desktop.
Save justinsoliz/ce1d0d5cafdbce0efcdebb74fda2394d to your computer and use it in GitHub Desktop.
Packer definition for jenkins ci executor

Jenkins Image

# validate packer image
packer validate ./images/jenkins_ci/jenkins-ci-packer.json

# build packer image
packer build ./images/jenkins_ci/jenkins-ci-packer.json
{
"variables": {
"aws_access_key": "",
"aws_secret_key": ""
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-west-2",
"source_ami": "ami-d732f0b7",
"instance_type": "m4.large",
"ssh_username": "ubuntu",
"ami_name": "jenkins-packer-ci-{{timestamp}}",
"launch_block_device_mappings": [
{
"device_name": "/dev/sda1",
"volume_size": 40,
"volume_type": "gp2",
"delete_on_termination": true
}
]
}],
"provisioners": [{
"type": "shell",
"script": "{{template_dir}}/jenkins_init.sh"
}]
}
#!/bin/bash
echo "Updating package list"
sudo apt-get update
echo "Installing dependencies"
sudo apt-get install -y unzip zip git-core wget python postgresql-client \
build-essential libssl-dev postgresql-client unzip
echo "Install AWS CLI"
sudo apt-get install -y python-pip
sudo pip install awscli
echo "Install Node.js"
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
echo "Install NVM"
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
echo "source ~/.nvm/nvm.sh" >> "$HOME/.bashrc"
echo "Installing node version dependencies for api and playbook"
nvm install 6.9.1
nvm install 4.6.2
echo "Install Yarn PKG manager"
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb http://dl.yarnpkg.com/debian/ stable main" \
| sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
cat << EOF > ./ubuntu-init.py
#!/usr/bin/python
import os
import httplib
import string
# To install run:
# sudo wget http://$JENKINS_URL/plugin/ec2/AMI-Scripts/ubuntu-init.py -O /usr/bin/userdata
# sudo chmod +x /etc/init.d/userdata
# add the following line to /etc/rc.local "python /usr/bin/userdata"
# If java is installed it will be zero
# If java is not installed it will be non-zero
hasJava = os.system("java -version")
if hasJava != 0:
os.system("sudo apt-get update")
os.system("sudo apt-get install openjdk-7-jre -y")
conn = httplib.HTTPConnection("169.254.169.254")
conn.request("GET", "/latest/user-data")
response = conn.getresponse()
userdata = response.read()
args = string.split(userdata, "&")
jenkinsUrl = ""
slaveName = ""
for arg in args:
if arg.split("=")[0] == "JENKINS_URL":
jenkinsUrl = arg.split("=")[1]
if arg.split("=")[0] == "SLAVE_NAME":
slaveName = arg.split("=")[1]
os.system("wget " + jenkinsUrl + "jnlpJars/slave.jar -O slave.jar")
os.system("java -jar slave.jar -jnlpUrl " + jenkinsUrl + "computer/" + slaveName + "/slave-agent.jnlp")
EOF
sudo mv ./ubuntu-init.py /usr/bin/userdata
sudo chmod +x /usr/bin/userdata
echo "Adding boot script to run after boot is complete"
sudo sed -i '/^[^#]/ s/exit 0/python \/usr\/bin\/userdata\n&/' /etc/rc.local
echo "Installing Terraform"
TERRAFORM_VER="0.8.4"
TERRAFORM_ZIP="terraform_$(echo $TERRAFORM_VER)_linux_amd64.zip"
wget "https://releases.hashicorp.com/terraform/$TERRAFORM_VER/$TERRAFORM_ZIP"
unzip ./$TERRAFORM_ZIP
sudo mv ./terraform /usr/bin
sudo chmod +x /usr/bin/terraform
echo "** installing docker **"
curl -fsSL https://get.docker.com/ | sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment