Make the Vagrant plugin work

Check this fancy screenshot out! <http://strongspace.com/rtyler/public/vagrant-plugin-yay.png>
This commit is contained in:
R. Tyler Croy 2012-03-07 16:03:48 -08:00
parent 35bc3b5312
commit 50df225690
7 changed files with 101 additions and 82 deletions

17
Gemfile
View File

@ -1,12 +1,9 @@
source :rubygems
source :gemcutter
# Pulling jorgenpt@'s jenkins-plugin-runtime which has some useful buildwrapper
# additions
gem "jenkins-plugin-runtime",
:git => 'git://github.com/jorgenpt/jenkins-plugin-runtime.git',
:branch => 'bump-version'
gem "jenkins-plugin-runtime"
gem "jenkins-plugin", '~> 0.2.0'
gem "vagrant", '~> 1.0'
gem "jenkins-plugin"
gem "vagrant"
#gem "virtualbox", :git => "git://github.com/mitchellh/virtualbox.git"
group :development do
gem 'jpi', '~> 0.3.3'
end

View File

@ -1,46 +1,48 @@
GIT
remote: git://github.com/jorgenpt/jenkins-plugin-runtime.git
revision: 4d44d4875006ef38400c4df78b7aaeb5ae1a9aec
branch: bump-version
specs:
jenkins-plugin-runtime (0.1.9)
json
GEM
remote: http://rubygems.org/
specs:
archive-tar-minitar (0.5.2)
childprocess (0.3.1)
ffi (~> 1.0.6)
erubis (2.7.0)
ffi (0.6.3-java)
jenkins-plugin (0.1.13)
bundler
jenkins-plugin-runtime (~> 0.1.6)
ffi (1.0.11)
ffi (1.0.11-java)
i18n (0.6.0)
jenkins-plugin (0.2.0)
jenkins-plugin-runtime (0.1.26)
json
slop (~> 3.0.2)
jenkins-war (1.447)
jpi (0.3.3)
bundler (~> 1.1.rc2)
jenkins-plugin-runtime (~> 0.1.13)
jenkins-war (>= 1.427)
rubyzip
thor
jenkins-war (1.434)
json (1.6.1-java)
mario (0.0.6)
json (1.5.4)
json (1.5.4-java)
log4r (1.1.10)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
net-ssh (2.2.1)
rubyzip (0.9.4)
net-ssh (2.2.2)
rubyzip (0.9.6.1)
slop (3.0.4)
thor (0.14.6)
vagrant (0.5.4)
vagrant (1.0.0)
archive-tar-minitar (= 0.5.2)
erubis (>= 2.6.6)
json (>= 1.4.3)
mario (~> 0.0.6)
net-scp (>= 1.0.2)
net-ssh (>= 2.0.19)
virtualbox (~> 0.7.3)
virtualbox (0.7.9)
ffi (~> 0.6.3)
childprocess (~> 0.3.1)
erubis (~> 2.7.0)
i18n (~> 0.6.0)
json (~> 1.5.1)
log4r (~> 1.1.9)
net-scp (~> 1.0.4)
net-ssh (~> 2.2.2)
PLATFORMS
java
DEPENDENCIES
jenkins-plugin
jenkins-plugin-runtime!
vagrant
jenkins-plugin (~> 0.2.0)
jenkins-plugin-runtime
jpi (~> 0.3.3)
vagrant (~> 1.0)

View File

@ -2,44 +2,54 @@ require 'rubygems'
require 'vagrant'
class VagrantBuilder < Jenkins::Tasks::Builder
display_name "Execute shell script in Vagrant box"
class BaseVagrantBuilder < Jenkins::Tasks::Builder
attr_accessor :command
def initialize(attrs)
puts "Initialize VagrantBuilder"
p attrs
@command = attrs["command"]
@vagrant = nil
end
def prebuild(build, listener)
build.env.each do |k, v|
listener.info("#{k} = #{v}")
end
listener.info("BUILD VARS")
build.build_var.each do |k, v|
listener.info("#{k} = #{v}")
end
end
def perform(build, launcher, listener)
t = Tempfile.new("vagrant-shell")
@command.split("\n").each do |line|
t.write("#{line}\n")
@vagrant = Vagrant::Environment.new(:cwd => build.workspace.to_s)
unless @vagrant.primary_vm.state == :running
build.halt 'Vagrant VM doesn\'t appear to be running!'
end
t.flush
listener.info("Checking my build variables: #{build.build_var.inspect}")
listener.info("I should be running the following command: #{@command}")
listener.info "Running the command in Vagrant with \"#{vagrant_method.to_s}\": #{@command}"
begin
listener.info("Running shell script locally for now")
launcher.execute("sh -xe #{t.path}", :chdir => "/", :out => listener)
rescue
raise
ensure
t.delete
unless @vagrant.nil?
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
if type == :stdout
listener.info data
elsif type == :stderr
listener.error data
end
end
unless code == 0
build.halt 'Command failed!'
end
end
end
end
class VagrantUserBuilder < BaseVagrantBuilder
display_name "Execute shell script in Vagrant"
def vagrant_method
:execute
end
end
class VagrantSudoBuilder < BaseVagrantBuilder
display_name "Execute shell script in Vagrant as admin"
def vagrant_method
:sudo
end
end

View File

@ -2,12 +2,17 @@ require 'rubygems'
require 'vagrant'
class VagrantWrapper < Jenkins::Tasks::BuildWrapper
display_name "Boot Vagrant box"
def initialize(*args)
@vagrant = nil
super(*args)
end
# Called some time before the build is to start.
def setup(build, launcher, listener, env)
def setup(build, launcher, listener)
vagrant_file = build.workspace.to_s + "/Vagrantfile"
unless File.exists? vagrant_file
listener.info("There is no Vagrantfile in your workspace!")
build.native.setResult(Java.hudson.model.Result::NOT_BUILT)
@ -15,21 +20,17 @@ class VagrantWrapper < Jenkins::Tasks::BuildWrapper
end
listener.info("Running Vagrant with version: #{Vagrant::VERSION}")
listener.info "Bringing Vagrant box up for build"
listener.info("Build object ID in builder: #{build.object_id}")
native = Jenkins::Plugin.instance.export(self)
# FFFFUUU
native.makeBuildVariables(build.instance_variable_get(:@native), {"foo" => "bar"})
#@vagrant = Vagrant::Environment.new.load!
#@vagrant.primary_vm.up
true
@vagrant = Vagrant::Environment.new(:cwd => build.workspace.to_s)
listener.info "Vagrantfile loaded, bringing Vagrant box up for the build"
@vagrant.cli('up')
listener.info "Vagrant box is online, continuing with the build"
end
# Called some time when the build is finished.
def teardown(build, listener, env)
listener.info "Build finished, bringing down Vagrant box"
#@vagrant.primary_vm.halt
true
def teardown(build, listener)
listener.info "Build finished, destroying the Vagrant box"
unless @vagrant.nil?
@vagrant.cli('destroy', '-f')
end
end
end

View File

@ -1,9 +1,8 @@
Jenkins::Plugin::Specification.new do |plugin|
plugin.name = 'vagrant-plugin'
plugin.version = '0.0.1'
plugin.version = '0.0.2'
plugin.description = 'Vagrant rocks'
plugin.depends_on 'ruby-runtime', '0.3'
plugin.depends_on 'git', '1.1.11'
plugin.depends_on 'ruby-runtime', '0.9'
end

View File

@ -0,0 +1,10 @@
<%
=begin
These taglib docs are kind of useful: <http://jenkins-ci.org/maven-site/hudson-core/jelly-taglib-ref.html>
=end
%>
<% f = taglib("/lib/form") %>
<% f.entry(:title => "Command", :field => "command") do %>
<% f.textarea %>
<% end %>