Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Packer templates for AWS OpsWorks
=================================

This repository contains a [Packer](https://www.packer.io) templates for Ubuntu
This repository contains a [Packer](https://www.packer.io) templates for Ubuntu and CentOS 7
pre-loaded with the `opsworks-agent` software utilized by Amazon Web Services,
allowing [OpsWorks](http://aws.amazon.com/opsworks/) stacks to be virtualized
for local testing and development.
Expand All @@ -26,19 +26,21 @@ $ rake vmware:build
```

Rake will build a Ubuntu 14.04 LTS "*Trusty Tahr*" box by default, but you can
also specify Ubuntu 12.04 LTS "*Precise Pangolin*" like so:
also specify Ubuntu 12.04 LTS "*Precise Pangolin*" or CentOS 7 LTS mini like so:

```bash
$ rake build[ubuntu1204] install[ubuntu1204]
or
$ rake build[centos7mini] install[centos7mini]
```

_**Note:** Amazon Linux is not supported as it cannot be run outside of
Amazon EC2._
Amazon EC2. But the CentOS 7 build is very close to a Amazon Linux._


## Using the Box

The compiled box will be named `ubuntu1404-opsworks` or `ubuntu1204-opsworks`.
The compiled box will be named `ubuntu1404-opsworks` or `ubuntu1204-opsworks` or `centos7mini-opsworks`.
To utilize this box in your project, create a new Vagrantfile and include
the following:

Expand Down
19 changes: 15 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# first is default
BOXES = %w( ubuntu1404 ubuntu1204 ).freeze
BOXES = %w( ubuntu1404 ubuntu1204 centos7mini ).freeze

# namespace for each provider
provider_builder = lambda do |provider|
Expand Down Expand Up @@ -30,10 +30,18 @@ provider_builder.call(:vmware)

desc "Remove compiled assets and cached files"
task :clean do
sh 'rm -f build/*.box'
if ENV['OS'] == 'Windows_NT'
sh "rm 'build/*.box' -Recurse -Force -ErrorAction SilentlyContinue"
else
sh 'rm -f build/*.box'
end
end
task :clear do
sh 'rm -rf packer_cache'
if ENV['OS'] == 'Windows_NT'
sh 'rm packer_cache -Force -Recurse -ErrorAction SilentlyContinue'
else
sh 'rm -rf packer_cache'
end
end

# shortcuts to virtualbox tasks with no namespace
Expand All @@ -44,7 +52,10 @@ task :remove, [:box] => 'virtualbox:remove'
# build a box for the specified provider
def build_box(box, provider)
log "Building #{box} for #{provider}"
sh "rm -f build/#{box}-opsworks-#{provider}.box && packer build -only=#{provider}-iso template/#{box}.json"
file = "build/#{box}-opsworks-#{provider}.box"
# sh "rm -f build/#{box}-opsworks-#{provider}.box && packer build -only=#{provider}-iso template/#{box}.json"
File.delete(file) if File.exist?(file)
sh "packer build -only=#{provider}-iso template/#{box}.json"
end

# build a box with vagrant
Expand Down
1 change: 1 addition & 0 deletions iso/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.iso
15 changes: 13 additions & 2 deletions opsworks/opsworks
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ if [[ $# -eq 0 ]]; then
exit
fi

# yum or apt-get
OS=red
if [ -f /etc/lsb-release ]; then
OS=ubuntu
fi

# install opsworks agent if not already available
if [[ ! -f /opt/aws/opsworks/current/VERSION ]]; then

Expand All @@ -16,8 +22,13 @@ if [[ ! -f /opt/aws/opsworks/current/VERSION ]]; then

# install dependencies
echo "Installing OpsWorks agent dependencies"
apt-get -yq update
apt-get -yq install curl libxml2-dev libxslt-dev libyaml-dev
if [ "x$OS" = "xred" ]; then
yum -q -y update
yum -q -y install curl libxml2-devel libxslt-devel libyaml-devel
else
apt-get -yq update
apt-get -yq install curl libxml2-dev libxslt-dev libyaml-dev
fi

# create expected directories and populate them with expected config files
mkdir -p /{etc,opt,var/{log,lib}}/aws/opsworks/ /var/lib/cloud/
Expand Down
8 changes: 6 additions & 2 deletions opsworks/opsworks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OpsWorksError < StandardError; end
}
},
"dependencies" => {
"gem_binary" => "/usr/local/bin/gem",
"gem_binary" => "gem",
"gems" => {},
"debs" => {}
},
Expand Down Expand Up @@ -213,7 +213,11 @@ def self.log(msg)
end

def self.check_dependencies
`apt-get -yq install git 2>&1` if `which git`.empty?
if `which yum`.empty?
`apt-get -yq install git 2>&1` if `which git`.empty?
else
`yum -q -y install git 2>&1` if `which git`.empty?
end
end

def self.set_hostname(hostname)
Expand Down
106 changes: 106 additions & 0 deletions preseed/ks.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Perform the installation in a completely non-interactive command line mode. Any prompts for interaction halts the install.
cmdline

# Install OS instead of upgrade
install

# System authorization information
auth --useshadow --enablemd5
# System bootloader configuration
bootloader --location=mbr --append="net.ifnames=0"
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
#Root password - required cmd - remove pw in post
rootpw vagrant
# Firewall
# firewall --enabled --ssh --trust=eth0 --trust=eth1 --service=ssh,http,https
firewall --disabled

authconfig --enableshadow --passalgo=sha512
# first boot disabled
firstboot --disabled
# System keyboard
keyboard us
# System language
lang en_US.UTF-8
# Installation logging level
logging --level=info
# Reboot after installation
reboot
# SELinux configuration
selinux --permissive
# Do not configure the X Window System
skipx
# System timezone
timezone --isUtc UTC
# Disk partitioning information
part /boot --fstype ext3 --size 250
part pv.01 --size 1 --grow
volgroup vg pv.01
logvol / --vgname=vg --size=1 --grow --fstype ext4 --name=root
logvol /tmp --vgname=vg --size=1024 --fstype ext4 --name=tmp
logvol swap --vgname=vg --recommended --name=swap
# Setup network interfaces via DHCP
network --device=eth0 --bootproto=dhcp --onboot=yes --activate --noipv6
# add our vagrant user
user --name=vagrant --groups=wheel --password=vagrant
# agree to the EULA
eula --agreed

%packages --excludedocs --nobase
@Core
openssh-server
vim-minimal
curl
%end

%pre --log=/var/log/ks_pre.log
echo 'enter ks pre stage...'
date +%Y-%m-%dT%H:%m:%S
%end

%post --log=/var/log/ks_post.log
#!/bin/bash
# this bash will improve our syntax here, empty lines allowed ;)
echo 'enter ks post stage...'
date +%Y-%m-%dT%H:%m:%S

yum -y -q install sudo

# Make sure our vagrant can access via ssh without the need of tty, otherwise=>vagrant will fail
/bin/cat >> /etc/sudoers.d/wheel << EOF_sudoers_wheel
Defaults:%wheel env_keep += "SSH_AUTH_SOCK"
Defaults:%wheel !requiretty
%wheel ALL=(ALL) ALL
%wheel ALL=(ALL) NOPASSWD: ALL
EOF_sudoers_wheel

/bin/chmod 0440 /etc/sudoers.d/wheel

# opsworks-agent will execute some commands as user 'root' and fail with missing tty error
# So add root to our wheel group which does not require tty
usermod -a -G wheel root

# In case we wanna use ssh keys for vagrant
#/bin/mkdir /home/vagrant/.ssh
#/bin/chmod 700 /home/vagrant/.ssh
#/usr/bin/curl -L -o /home/vagrant/.ssh/id_rsa https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant
#/usr/bin/curl -L -o /home/vagrant/.ssh/authorized_keys https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
#/bin/chown -R vagrant:vagrant /home/vagrant/.ssh
#/bin/chmod 0400 /home/vagrant/.ssh/*

yum -y clean all

/sbin/swapoff -a
/sbin/mkswap /dev/mapper/vg_vagrantcentos-lv_swap

/bin/dd if=/dev/zero of=/boot/EMPTY bs=1M
/bin/rm -f /boot/EMPTY
/bin/dd if=/dev/zero of=/EMPTY bs=1M
/bin/rm -f /EMPTY

chkconfig sshd on

%end
41 changes: 41 additions & 0 deletions provision/centos/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash -eux

# Force grub to boot without user interation
echo "GRUB_RECORDFAIL_TIMEOUT=10" >> /etc/default/grub
grub2-mkconfig -o "$(readlink /etc/grub2.cfg)"

echo "==> Cleaning up tmp"
rm -rf /tmp/*

# Remove Bash history
unset HISTFILE
rm -f /root/.bash_history
rm -f /home/vagrant/.bash_history

# Clean up log files
find /var/log -type f | while read f; do echo -ne '' > $f; done;

echo "==> Clearing last login information"
>/var/log/lastlog
>/var/log/wtmp
>/var/log/btmp

# Whiteout root
count=$(df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}')
let count--
dd if=/dev/zero of=/tmp/whitespace bs=1024 count=$count
rm /tmp/whitespace

# Whiteout /boot
count=$(df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}')
let count--
dd if=/dev/zero of=/boot/whitespace bs=1024 count=$count
rm /boot/whitespace

# Zero out the free space to save space in the final image
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY

# Make sure we wait until all the data is written to disk, otherwise
# Packer might quite too early before the large files are deleted
sync
41 changes: 41 additions & 0 deletions provision/centos/minimize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash -eux

###############################
# @TODO: adopt this to CentOS 7
###############################

echo "==> Installed packages before cleanup"
rpm -qa | less

# Remove some packages to get a minimal install
#echo "==> Removing all linux kernels except the currrent one"
#dpkg --list | awk '{ print $2 }' | grep 'linux-image-3.*-generic' | grep -v $(uname -r) | xargs apt-get -y purge
#echo "==> Removing linux source"
#dpkg --list | awk '{ print $2 }' | grep linux-source | xargs apt-get -y purge
# echo "==> Removing development packages"
# dpkg --list | awk '{ print $2 }' | grep -- '-dev$' | xargs apt-get -y purge
#echo "==> Removing documentation"
#dpkg --list | awk '{ print $2 }' | grep -- '-doc$' | xargs apt-get -y purge
#echo "==> Removing X11 libraries"
#apt-get -y purge libx11-data xauth libxmuu1 libxcb1 libx11-6 libxext6
#echo "==> Removing obsolete networking components"
#apt-get -y purge ppp pppconfig pppoeconf
#echo "==> Removing other oddities"
#apt-get -y purge popularity-contest installation-report landscape-common wireless-tools wpasupplicant ubuntu-serverguide

# Clean up the yum cache
yum clean all

# Clean up orphaned packages with yum utils' package-cleanup
package-cleanup --quiet --leaves

# echo "==> Removing man pages"
# rm -rf /usr/share/man/*
# echo "==> Removing APT files"
# find /var/lib/apt -type f | xargs rm -f
echo "==> Removing anything in /usr/src"
rm -rf /usr/src/*
echo "==> Removing any docs"
rm -rf /usr/share/doc/*
echo "==> Removing caches"
find /var/cache -type f -exec rm -rf {} \;
14 changes: 14 additions & 0 deletions provision/centos/network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -eux

# Make sure udev does not block our network
echo "==> Cleaning up udev rules"
rm /etc/udev/rules.d/70-persistent-ipoib.rules

if [ -d "/var/lib/dhclient" ]; then
echo "==> Cleaning up leftover dhcp leases"
dhclient -r
rm /var/lib/dhclient/*
fi

# Disable DNS reverse lookup
echo "UseDNS no" >> /etc/ssh/sshd_config
Loading