Skip to content

Instantly share code, notes, and snippets.

@zhiyinglei
Forked from aweijnitz/Vagrantfile
Last active April 15, 2016 00:30
Show Gist options
  • Save zhiyinglei/bd0376c1b4a26ed586e30744d2b6f22e to your computer and use it in GitHub Desktop.
Save zhiyinglei/bd0376c1b4a26ed586e30744d2b6f22e to your computer and use it in GitHub Desktop.
This is a Vagrant file and a provisioning script to create a Debian-based Jenkins server, including Java, Ant and Tomcat. Also see "provision.sh" below
#!/bin/bash
if [ ! -f /usr/bin/svn ];
then
echo "-------- PROVISIONING SUBVERSION ------------"
echo "---------------------------------------------"
## Install subverison
apt-get update
apt-get -y install subversion
else
echo "CHECK - Subversion already installed"
fi
if [ ! -f /usr/lib/jvm/java-7-oracle/bin/java ];
then
echo "-------- PROVISIONING JAVA ------------"
echo "---------------------------------------"
## Make java install non-interactive
## See http://askubuntu.com/questions/190582/installing-java-automatically-with-silent-option
echo debconf shared/accepted-oracle-license-v1-1 select true | \
debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | \
debconf-set-selections
## Install java 1.7
## See http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee /etc/apt/sources.list.d/webupd8team-java.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886
apt-get update
apt-get -y install oracle-java7-installer
else
echo "CHECK - Java already installed"
fi
if [ ! -f /etc/init.d/jenkins ];
then
echo "-------- PROVISIONING JENKINS ------------"
echo "------------------------------------------"
## Install Jenkins
#
# URL: http://localhost:6060
# Home: /var/lib/jenkins
# Start/Stop: /etc/init.d/jenkins
# Config: /etc/default/jenkins
# Jenkins log: /var/log/jenkins/jenkins.log
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
apt-get update
apt-get -y install jenkins
# Move Jenkins to port 6060
sed -i 's/8080/6060/g' /etc/default/jenkins
/etc/init.d/jenkins restart
else
echo "CHECK - Jenkins already installed"
fi
### Everything below this point is not stricly needed for Jenkins to work
###
if [ ! -f /etc/init.d/tomcat7 ];
then
echo "-------- PROVISIONING TOMCAT ------------"
echo "-----------------------------------------"
## Install Tomcat (port 8080)
# This gives us something to deploy builds into
# CATALINA_BASE=/var/lib/tomcat7
# CATALINE_HOME=/usr/share/tomcat7
export JAVA_HOME="/usr/lib/jvm/java-7-oracle"
apt-get -y install tomcat7
# Work around a bug in the default tomcat start script
sed -i 's/export JAVA_HOME/export JAVA_HOME=\"\/usr\/lib\/jvm\/java-7-oracle\"/g' /etc/init.d/tomcat7
/etc/init.d/tomcat7 stop
/etc/init.d/tomcat7 start
else
echo "CHECK - Tomcat already installed"
fi
if [ ! -f /usr/local/lib/ant/apache-ant-1.9.4/bin/ant ];
then
echo "-------- PROVISIONING ANT ---------------"
echo "-----------------------------------------"
mkdir -p /usr/local/lib/ant
cd /usr/local/lib/ant
wget -q http://ftp.halifax.rwth-aachen.de/apache//ant/binaries/apache-ant-1.9.4-bin.tar.gz
tar xzf apache-ant-1.9.4-bin.tar.gz
rm apache-ant-1.9.4-bin.tar.gz
ln -s /usr/local/lib/ant/apache-ant-1.9.4/bin/ant /usr/local/bin/ant
echo "Ant installed"
else
echo "CHECK - Ant already installed"
fi
echo "-------- PROVISIONING DONE ------------"
echo "-- Jenkins: http://localhost:6060 "
echo "-- Tomcat7: http://localhost:7070 "
echo "---------------------------------------"
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Every Vagrant virtual environment requires a box to build off of.
# Named boxes, like this one, don't need a URL, since the are looked up
# in the "vagrant cloud" (https://vagrantcloud.com)
# config.vm.box = "chef/debian-7.4"
config.vm.box = "ubuntu/trusty64"
# Publish guest port 6060 on host port 6060
config.vm.network "forwarded_port", guest: 6060, host: 6060
config.vm.network "forwarded_port", guest: 8080, host: 7070
config.vm.provider "virtualbox" do |vb|
# # Don't boot with headless mode. Use for debugging
# vb.gui = true
# # Use VBoxManage to customize the VM. For example to change memory:
vb.customize ["modifyvm", :id, "--memory", "1024"]
end
# Provision the box using a shell script
# This script is copied into the box and then run
config.vm.provision :shell, :privileged => true, :path => "provision.sh"
end
@zhiyinglei
Copy link
Author

http://automation.better-than.tv/info.html#downloads

What is this?
This is a “pre-packaged” build server. The Gist below contains configuration files that specifies a virtual box with Subversion, the Jenkins continuous integration server, Ant and Tomcat on it. The box is managed using the Vagrant tool, which essentially allows you to easily setup and run a virtual box like a server in headless mode. It also allows you to provision it with required project software and configuration on the first startup.

Using Vagrant and this configuration, a project can quickly turn any computer able to run VirtualBox into a build server in an easy and consistent way.

Prerequisites
The server requires VirtualBox and Vagrant to run.

  • VirtualBox
  • Vagrant
    Note: There is a problem with VirtualBox v4.2.14 and you need to upgrade to fix it. In that case you will get a warning like this:

Installation

  • Save the Vagrant config into a new folder (two files) Download config files here
  • Open a console window and navigate to the folder where the config files are saved
  • Type vagrant up
    On completion it should look like this:

Browsing to http://localhost:6060 will bring up Jenkins

Useful commands

  • Shell connect to VM vagrant ssh (for convenience ‘vagrant’ is a NOPASSWD sudo user)
  • Pause VM vagrant suspend
  • Start suspendend VM vagrant resume
  • Halt VM vagrant halt
  • Start halted VM vagrant up
  • Completely remove the VM vagrant destroy
  • Re-provision VM vagrant provision (useful when tweaking provisioning scripts)
    Next Steps
    These are some of the things you might consider as next steps

Add build jobs
This can be done using the Jenkins browser UI. Just press “new job”. There is also a REST API which you can access using curl for example. Subversion is pre-installed and supported by Jenkins out of the box, so adding a job that checks out and builds a project is pretty straight forward.

Additional software and configuration
Provisioning the box on first start is very easy, vagrant just executes the provided script as root. See the file “provision.sh” and the Vagrant config files.

Hint: The Vagrant file is a Ruby file, so switching your editor to Ruby mode will give you some extra help.

Tweak the VirtualBox config
Through the Vagrant configuration file, it is possible to configure the underlying box (memory, CPU, network card etc). I have added a section setting the memory as an example. See the online documentation for more details. You need to destroy and rebuild the machine for these changes to take effect.

Synced folders
Vagrant automatically mounts the host folder that it is run from into the box as a shared folder on /vagrant. This is a great way to exchange data with the machine and it is possible to configure additional shares in the Vagrant file. See online documentation for more info.

SECURITY!
At the heart of it, this is a default Debian 7.4 dist bundled up as a Vagrant box. This means it is NOT suitable for public Internet access. Use it on the LAN in the office, but do not make it available online unless you take extra measures to ensure general security and add access control in Jenkins.

Anders Weijnitz, Jacada 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment