Skip to content

Instantly share code, notes, and snippets.

@gregwork
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregwork/17205697c450eaafd3cb to your computer and use it in GitHub Desktop.
Save gregwork/17205697c450eaafd3cb to your computer and use it in GitHub Desktop.
Rakefile example

This example splits namespaces into different files, but that's likely overkill for most uses. Shoving everything in a Rakefile would work fine too.

To use this:

cd /tmp
mkdir raketest
cd raketest
touch Rakefile
mkdir rakelib
# save the two sample *.rake files in rakelib

In this example, a blank Rakefile is fine; it provides an 'anchor' so rake can find the root of the 'project'. Rake will then find and autoload rakelib/*.rake files to load tasks.

Invoke via rake packer:release[baseboxname]

In the tasks, the :basebox parameter is accessible as args.basebox. In the task's sh block, use normal hash+curlies interpolation: #{args.basebox}

require "pathname"
packer_binary_dir = Pathname.new("~/software/packer/current")
packer_template_basedir = Pathname.new("~/code/packer")
namespace :packer do
# this is the published/preferred task to use; it has a 'desc' description command
# and will appear in rake -T
desc "Build, upload and clean"
task :release, [:basebox] => [:build, :upload, :clean, "vagrant:basebox:remove"]
# this is an 'internal' task, not meant to be invoked directly as part of normal use.
# As such, there's no description and it won't appear in rake -T
task :build, [:basebox] do |t, args|
sh <<-CMD
set -o errexit
set -o nounset
PATH=#{packer_binary_dir}:$PATH
cd #{packer_template_basedir}/#{args.basebox}
packer build template.json
CMD
end
task :upload, [:basebox] do |t, args|
sh <<-CMD
set -o errexit
set -o nounset
cd #{packer_template_basedir}/#{args.basebox}
scp packer*.box user@host:/directory/
CMD
end
task :clean, [:basebox] do |t, args|
sh <<-CMD
set -o errexit
set -o nounset
cd #{packer_template_basedir}/#{args.basebox}
rm -r packer_cache packer*.box
CMD
end
end
namespace :vagrant do
namespace :basebox do
task :remove, [:basebox] do |t, args|
sh <<-CMD
vagrant box remove #{args.basebox} || true
CMD
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment