Skip to content

Commit

Permalink
Rename the crap out of all these classes
Browse files Browse the repository at this point in the history
Part of this is to make things a bit cleaner, the other part is to fix the
BaseVagrantBuilder showing up as a build step, derp
  • Loading branch information
R. Tyler Croy committed Mar 13, 2012
1 parent d3f90a8 commit 2bba59f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 84 deletions.
121 changes: 71 additions & 50 deletions models/vagrant_builder.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,76 +2,97 @@
require 'vagrant' require 'vagrant'




class BaseVagrantBuilder < Jenkins::Tasks::Builder module Vagrant
attr_accessor :command module BaseBuilder

def prebuild(build, listener)
def initialize(attrs) end
@command = attrs["command"]
end

def prebuild(build, listener)
end


def perform(build, launcher, listener) def perform(build, launcher, listener)
# This should be set by the VagrantWrapper # This should be set by the VagrantWrapper
@vagrant = build.env[:vagrant] @vagrant = build.env[:vagrant]


if @vagrant.nil? if @vagrant.nil?
build.halt "OH CRAP! I don't seem to have a Vagrant instance!" build.halt "OH CRAP! I don't seem to have a Vagrant instance!"
end end


unless @vagrant.primary_vm.state == :running unless @vagrant.primary_vm.state == :running
build.halt 'Vagrant VM doesn\'t appear to be running!' build.halt 'Vagrant VM doesn\'t appear to be running!'
end end


listener.info "Running the command in Vagrant with \"#{vagrant_method.to_s}\": #{@command}" listener.info("Running the command in Vagrant with \"#{vagrant_method.to_s}\":")
@command.split("\n").each do |line|
listener.info("+ #{line}")
end


unless @vagrant.nil? unless @vagrant.nil?
code = @vagrant.primary_vm.channel.send(vagrant_method, @command) do |type, data| code = @vagrant.primary_vm.channel.send(vagrant_method, @command) do |type, data|
# type is one of [:stdout, :stderr, :exit_status] # type is one of [:stdout, :stderr, :exit_status]
# # data is a string for stdout/stderr and an int for exit status # # data is a string for stdout/stderr and an int for exit status
if type == :stdout if type == :stdout
listener.info data listener.info data
elsif type == :stderr elsif type == :stderr
listener.error data listener.error data
end
end
unless code == 0
build.halt 'Command failed!'
end end
end
unless code == 0
build.halt 'Command failed!'
end end
end end
end end
end


class VagrantUserBuilder < BaseVagrantBuilder class UserBuilder < Jenkins::Tasks::Builder
display_name "Execute shell script in Vagrant" display_name "Execute shell script in Vagrant"

include BaseBuilder

attr_accessor :command


def vagrant_method def initialize(attrs)
:execute @command = attrs["command"]
end

def vagrant_method
:execute
end
end end
end


class VagrantSudoBuilder < BaseVagrantBuilder class SudoBuilder < Jenkins::Tasks::Builder
display_name "Execute shell script in Vagrant as admin" display_name "Execute shell script in Vagrant as admin"

include BaseBuilder

attr_accessor :command

def initialize(attrs)
@command = attrs["command"]
end


def vagrant_method def vagrant_method
:sudo :sudo
end
end end
end


class VagrantProvisionBuilder < Jenkins::Tasks::Builder class ProvisionBuilder < Jenkins::Tasks::Builder
display_name 'Provision the Vagrant machine' display_name 'Provision the Vagrant machine'


def perform(build, launcher, listener) def initialize(attrs)
@vagrant = build.env[:vagrant]
if @vagrant.nil?
built.halt "OH CRAP! I don't seem to have a Vagrant instance"
end end


unless @vagrant.primary_vm.state == :running def prebuild(build, listener)
build.halt 'Vagrant VM doesn\'t appear to be running!'
end end


@vagrant.cli('provision') 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"
end

unless @vagrant.primary_vm.state == :running
build.halt 'Vagrant VM doesn\'t appear to be running!'
end

@vagrant.cli('provision')
end
end end
end end
70 changes: 36 additions & 34 deletions models/vagrant_wrapper.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,48 +1,50 @@
require 'rubygems' require 'rubygems'
require 'vagrant' require 'vagrant'


class VagrantWrapper < Jenkins::Tasks::BuildWrapper module Vagrant
display_name "Boot Vagrant box" class BasicWrapper < Jenkins::Tasks::BuildWrapper
display_name "Boot Vagrant box"

attr_accessor :vagrantfile
def initialize(attrs)
@vagrant = nil
@vagrantfile = attrs['vagrantfile']
end


attr_accessor :vagrantfile def path_to_vagrantfile(build)
def initialize(attrs) if @vagrantfile.nil?
@vagrant = nil return build.workspace.to_s
@vagrantfile = attrs['vagrantfile'] end
end


def path_to_vagrantfile(build) return File.expand_path(File.join(build.workspace.to_s, @vagrantfile))
if @vagrantfile.nil?
return build.workspace.to_s
end end


return File.expand_path(File.join(build.workspace.to_s, @vagrantfile)) # Called some time before the build is to start.
end def setup(build, launcher, listener)
path = File.join(path_to_vagrantfile(build), 'Vagrantfile')


# Called some time before the build is to start. unless File.exists? path
def setup(build, launcher, listener) listener.info("There is no Vagrantfile in your workspace!")
path = File.join(path_to_vagrantfile(build), 'Vagrantfile') listener.info("We looked in: #{path}")
build.native.setResult(Java.hudson.model.Result::NOT_BUILT)
build.halt
end


unless File.exists? path listener.info("Running Vagrant with version: #{Vagrant::VERSION}")
listener.info("There is no Vagrantfile in your workspace!") @vagrant = Vagrant::Environment.new(:cwd => path)
listener.info("We looked in: #{path}") listener.info "Vagrantfile loaded, bringing Vagrant box up for the build"
build.native.setResult(Java.hudson.model.Result::NOT_BUILT) @vagrant.cli('up', '--no-provision')
build.halt listener.info "Vagrant box is online, continuing with the build"
end


listener.info("Running Vagrant with version: #{Vagrant::VERSION}") build.env[:vagrant] = @vagrant
@vagrant = Vagrant::Environment.new(:cwd => path) end
listener.info "Vagrantfile loaded, bringing Vagrant box up for the build"
@vagrant.cli('up', '--no-provision')
listener.info "Vagrant box is online, continuing with the build"

build.env[:vagrant] = @vagrant
end


# Called some time when the build is finished. # Called some time when the build is finished.
def teardown(build, listener) def teardown(build, listener)
listener.info "Build finished, destroying the Vagrant box" listener.info "Build finished, destroying the Vagrant box"
unless @vagrant.nil? unless @vagrant.nil?
@vagrant.cli('destroy', '-f') @vagrant.cli('destroy', '-f')
end
end end
end end
end end
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2bba59f

Please sign in to comment.