Skip to content

Commit

Permalink
Various packaging issues and some error handling improved
Browse files Browse the repository at this point in the history
  • Loading branch information
ripienaar committed Jul 5, 2010
1 parent f48e46d commit 0828eea
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 14 deletions.
14 changes: 14 additions & 0 deletions COPYING
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright 2009 R.I.Pienaar

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

88 changes: 88 additions & 0 deletions Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,88 @@
# Rakefile to build a project using HUDSON

require 'rake/rdoctask'
require 'rake/packagetask'
require 'rake/clean'
require 'find'

PROJ_DOC_TITLE = "EC2 Bootstrap System"
PROJ_VERSION = "0.0.1"
PROJ_RELEASE = "1"
PROJ_NAME = "ec2-boot-init"
PROJ_RPM_NAMES = [PROJ_NAME]
PROJ_FILES = ["#{PROJ_NAME}.spec", "#{PROJ_NAME}.rb", "#{PROJ_NAME}.init", "COPYING", "motd.provisioned", "motd.unprovisioned"]
PROJ_SUBDIRS = ["lib", "actions"]

Find.find("actions", "lib") do |f|
if FileTest.directory?(f) and f =~ /\.svn/
Find.prune
else
PROJ_FILES << f
end
end

ENV["RPM_VERSION"] ? CURRENT_VERSION = ENV["RPM_VERSION"] : CURRENT_VERSION = PROJ_VERSION
ENV["BUILD_NUMBER"] ? CURRENT_RELEASE = ENV["BUILD_NUMBER"] : CURRENT_RELEASE = PROJ_RELEASE

CLEAN.include("build")

def announce(msg='')
STDERR.puts "================"
STDERR.puts msg
STDERR.puts "================"
end

def init
FileUtils.mkdir("build") unless File.exist?("build")
end

desc "Build documentation, tar balls and rpms"
task :default => [:clean, :doc, :package, :rpm]

# task for building docs
rd = Rake::RDocTask.new(:doc) { |rdoc|
announce "Building documentation for #{CURRENT_VERSION}"

rdoc.rdoc_dir = 'doc'
rdoc.template = 'html'
rdoc.title = "#{PROJ_DOC_TITLE} version #{CURRENT_VERSION}"
rdoc.options << '--line-numbers' << '--inline-source' << '--main' << 'MCollective'
}

desc "Create a tarball for this release"
task :package => [:clean, :doc] do
announce "Creating #{PROJ_NAME}-#{CURRENT_VERSION}.tgz"

FileUtils.mkdir_p("build/#{PROJ_NAME}-#{CURRENT_VERSION}")
system("cp -R #{PROJ_FILES.join(' ')} build/#{PROJ_NAME}-#{CURRENT_VERSION}")
system("cd build && tar --exclude .svn -cvzf #{PROJ_NAME}-#{CURRENT_VERSION}.tgz #{PROJ_NAME}-#{CURRENT_VERSION}")
end

desc "Creates a RPM"
task :rpm => [:clean, :doc, :package] do
announce("Building RPM for #{PROJ_NAME}-#{CURRENT_VERSION}-#{CURRENT_RELEASE}")

sourcedir = `rpm --eval '%_sourcedir'`.chomp
specsdir = `rpm --eval '%_specdir'`.chomp
srpmsdir = `rpm --eval '%_srcrpmdir'`.chomp
rpmdir = `rpm --eval '%_rpmdir'`.chomp
lsbdistrel = `lsb_release -r -s | cut -d . -f1`.chomp
lsbdistro = `lsb_release -i -s`.chomp

case lsbdistro
when 'CentOS'
rpmdist = ".el#{lsbdistrel}"
else
rpmdist = ""
end

system %{cp build/#{PROJ_NAME}-#{CURRENT_VERSION}.tgz #{sourcedir}}
system %{cat #{PROJ_NAME}.spec|sed -e s/%{rpm_release}/#{CURRENT_RELEASE}/g | sed -e s/%{version}/#{CURRENT_VERSION}/g > #{specsdir}/#{PROJ_NAME}.spec}
system %{cd #{specsdir} && rpmbuild -D 'version #{CURRENT_VERSION}' -D 'rpm_release #{CURRENT_RELEASE}' -D 'dist #{rpmdist}' -ba #{PROJ_NAME}.spec}

system %{cp #{srpmsdir}/#{PROJ_NAME}-#{CURRENT_VERSION}-#{CURRENT_RELEASE}#{rpmdist}.src.rpm build/}

system %{cp #{rpmdir}/*/#{PROJ_NAME}*-#{CURRENT_VERSION}-#{CURRENT_RELEASE}#{rpmdist}.*.rpm build/}
end

# vi:tabstop=4:expandtab:ai
96 changes: 96 additions & 0 deletions ec2-boot-init.init
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/sh
#
# ec2-boot-init Bootstrapper for EC2
#
# chkconfig: 345 24 76
#
# description: Bootstraps EC2 instances in a way that enables you to
# customise a standard image into different configurations
# via structured user data
#
### BEGIN INIT INFO
# Provides: ec2-boot-init
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO

prog="/usr/sbin/ec2-boot-init"

# Lockfile
if [ -d /var/lock/subsys ]; then
# RedHat/CentOS/etc who use subsys
lock="/var/lock/subsys/ec2-boot-init"
else
# The rest of them
lock="/var/lock/ec2-boot-init"
fi


# Source function library.
. /lib/lsb/init-functions

# Check that binary exists
if ! [ -f $prog ]
then
echo "ec2-boot-init binary not found"
exit 0
fi

# See how we were called.
case "$1" in
start)
echo -n "Starting ec2-boot-init: "

cp /etc/ec2-boot-init/motd.unprovisioned /etc/motd

${prog}

if [ $? = 0 ]; then
cp /etc/ec2-boot-init/motd.provisioned /etc/motd
log_success_msg
touch $lock
exit 0
else
log_failure_msg
exit 1
fi
;;
stop)
log_success_msg
rm -f $lock
;;
restart)
$0 stop
sleep 2
$0 start
;;
condrestart)
if [ -f $lock ]; then
$0 stop
# avoid race
sleep 2
$0 start
fi
;;
status)
if [ -f ${lock} ]; then
echo "ec2-boot-init ran"
exit 0
else
echo "ec2-boot-init: service not started"
exit 1
fi
;;
force-reload)
echo "not implemented"
;;
*)
echo "Usage: ec2-boot-init {start|stop|restart|condrestart|status}"
exit 1
;;
esac
exit 0
12 changes: 12 additions & 0 deletions ec2-boot-init.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/ruby

require 'ec2boot'

config = EC2Boot::Config.new
ud = EC2Boot::UserData.new(config)
md = EC2Boot::MetaData.new(config)

EC2Boot::Util.write_facts(ud, md, config)

config.actions.run_actions(ud, md, config)

68 changes: 68 additions & 0 deletions ec2-boot-init.spec
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,68 @@
%define ruby_sitelib %(ruby -rrbconfig -e "puts Config::CONFIG['sitelibdir']")
%define release %{rpm_release}%{?dist}

Summary: EC2 Bootstrap System
Name: ec2-boot-init
Version: %{version}
Release: %{release}
Group: System Tools
License: Apache License, Version 2
URL: http://www.devco.net/
Source0: %{name}-%{version}.tgz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
Requires: ruby
Packager: R.I.Pienaar <rip@devco.net>
BuildArch: noarch

%description
Bootstrap system for EC2 based systems.

%prep
%setup -q

%build

%install
rm -rf %{buildroot}
%{__install} -d -m0755 %{buildroot}/%{ruby_sitelib}/ec2boot
%{__install} -d -m0755 %{buildroot}/usr/sbin
%{__install} -d -m0755 %{buildroot}/etc/init.d
%{__install} -d -m0755 %{buildroot}/etc/ec2-boot-init
%{__install} -d -m0755 %{buildroot}/etc/ec2-boot-init/actions
%{__install} -m0755 ec2-boot-init.rb %{buildroot}/usr/sbin/ec2-boot-init
%{__install} -m0755 ec2-boot-init.init %{buildroot}/etc/init.d/ec2-boot-init
%{__install} -m0644 motd.provisioned %{buildroot}/etc/ec2-boot-init/motd.provisioned
%{__install} -m0644 motd.unprovisioned %{buildroot}/etc/ec2-boot-init/motd.unprovisioned


cp -R lib/* %{buildroot}/%{ruby_sitelib}/
cp -R actions/* %{buildroot}/etc/ec2-boot-init/actions/

%clean
rm -rf %{buildroot}

%post
cp /etc/ec2-boot-init/motd.unprovisioned /etc/motd
/sbin/chkconfig --add ec2-boot-init || :

%postun
if [ "$1" -ge 1 ]; then
/sbin/service ec2-boot-init condrestart &>/dev/null || :
fi

%preun
if [ "$1" = 0 ] ; then
/sbin/chkconfig --del mcollective || :
fi

%files
%doc COPYING
/usr/sbin/ec2-boot-init
/etc/ec2-boot-init
/etc/init.d/ec2-boot-init
%{ruby_sitelib}/ec2boot.rb
%{ruby_sitelib}/ec2boot

%changelog
* Tue Nov 03 2009 R.I.Pienaar <rip@devco.net>
- First release
26 changes: 15 additions & 11 deletions lib/ec2boot/actions.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ def load_actions
end end


def run_actions(ud, md, config) def run_actions(ud, md, config)
if ud.user_data.include?(:actions) if ud.user_data.is_a?(Hash)
ud.user_data[:actions].each do |action| if ud.user_data.include?(:actions)
if action.include?(:type) ud.user_data[:actions].each do |action|
type = action[:type].to_s if action.include?(:type)
meth = "#{type}_action" type = action[:type].to_s
meth = "#{type}_action"


if respond_to?(meth) if respond_to?(meth)
send(meth, action, ud, md, config) send(meth, action, ud, md, config)
else
# no such method
end
else else
# no such method # no type
end end
else
# no type
end end
else
# no :actions
end end
else else
# no :actions # not a hash
end end
end end
end end
Expand Down
3 changes: 3 additions & 0 deletions lib/ec2boot/config.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def initialize
@facts_file = "/etc/facts.txt" @facts_file = "/etc/facts.txt"


@actions = Actions.new(self) @actions = Actions.new(self)


FileUtils.mkdir_p(@cache_dir)
end end
end end
end end
8 changes: 5 additions & 3 deletions lib/ec2boot/util.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ def self.write_facts(ud, md, config)
File.open(config.facts_file, "w") do |facts| File.open(config.facts_file, "w") do |facts|


if ud.fetched? if ud.fetched?
if ud.user_data.include?(:facts) if ud.user_data.is_a?(Hash)
ud.user_data[:facts].each_pair do |k,v| if ud.user_data.include?(:facts)
facts.puts("#{k}=#{v}") ud.user_data[:facts].each_pair do |k,v|
facts.puts("#{k}=#{v}")
end
end end
end end
end end
Expand Down
9 changes: 9 additions & 0 deletions motd.provisioned
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
___ _ _ _
| _ \_ _ _____ _(_)__(_)___ _ _ ___ __| |
| _/ '_/ _ \ V / (_-< / _ \ ' \/ -_) _` |
|_| |_| \___/\_/|_/__/_\___/_||_\___\__,_|

Successfully provisioned Amazon instance.

System facts are in /etc/facts.txt

9 changes: 9 additions & 0 deletions motd.unprovisioned
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
_ _ _ _ _
| | | |_ _ _ __ _ _ _____ _(_)__(_)___ _ _ ___ __| |
| |_| | ' \| '_ \ '_/ _ \ V / (_-< / _ \ ' \/ -_) _` |
\___/|_||_| .__/_| \___/\_/|_/__/_\___/_||_\___\__,_|
|_|

Unprovisioned Amazon instance. The ec2-boot-init
system failed to run or has not yet been run

0 comments on commit 0828eea

Please sign in to comment.