Add a publisher to package up the generated box

Enabling this post-build step means the wrapper will not destroy the machine,
but the publisher will take over responsibility for that instead.

Related to #10
This commit is contained in:
R. Tyler Croy 2012-03-16 14:57:59 -07:00
parent 6e93b99690
commit 52c5222dd9
4 changed files with 68 additions and 2 deletions

View File

@ -25,6 +25,7 @@ module Vagrant
end
unless @vagrant.nil?
build.env[:vagrant_dirty] = true
code = @vagrant.primary_vm.channel.send(vagrant_method, @command) do |type, data|
# type is one of [:stdout, :stderr, :exit_status]
# # data is a string for stdout/stderr and an int for exit status
@ -93,6 +94,7 @@ module Vagrant
end
listener.info('Provisioning the Vagrant VM.. (this may take a while)')
build.env[:vagrant_dirty] = true
@vagrant.cli('provision')
end
end

View File

@ -0,0 +1,46 @@
require 'rubygems'
require 'vagrant'
module Vagrant
class PackagePublisher < Jenkins::Tasks::Publisher
display_name "Package Vagrant box"
attr_accessor :boxname
def initialize(attrs)
@vagrant = nil
@boxname = attrs['boxname']
end
def prebuild(build, listener)
# To run packaging, we need to own the destroy of the box
build.env[:vagrant_disable_destroy] = true
end
def perform(build, launcher, listener)
@vagrant = build.env[:vagrant]
if @vagrant.nil?
built.halt "OH CRAP! I don't seem to have a Vagrant instance in the publisher"
end
unless build.env[:vagrant_dirty]
listener.info("It doesn't appear any changes were made inside the Vagrant VM")
listener.info("I'm going to save us all a lot of grief and skip packaging it up then")
return
end
name = @boxname
if name.nil? or name.empty?
name = 'package.box'
end
unless name.end_with? '.box'
name = "#{name}.box"
end
listener.info("Preparing to export the current Vagrant box to a file named: #{name}")
output = File.expand_path(File.join(build.workspace.to_s, name))
@vagrant.cli('package', '--output', output)
@vagrant.cli('destroy', '-f')
end
end
end

View File

@ -68,12 +68,20 @@ module Vagrant
listener.info "Vagrant box is online, continuing with the build"
build.env[:vagrant] = @vagrant
# We use this variable to determine if we have changes worth packaging,
# i.e. if we have actually done anything with the box, we will mark it
# dirty and can then take further action based on that
build.env[:vagrant_dirty] = false
end
# Called some time when the build is finished.
def teardown(build, listener)
listener.info "Build finished, destroying the Vagrant box"
unless @vagrant.nil?
if @vagrant.nil?
return
end
unless build.env[:vagrant_disable_destroy]
listener.info "Build finished, destroying the Vagrant box"
@vagrant.cli('destroy', '-f')
end
end

View File

@ -0,0 +1,10 @@
<%
f = taglib("/lib/form")
f.block do
f.entry(:title => 'Name of .box file (optional)',
:field => 'boxname',
:description => 'Name to give the .box file packaged by Vagrant') do
f.textbox
end
end
%>