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
Expand Up @@ -2,76 +2,97 @@
require 'vagrant'


class BaseVagrantBuilder < Jenkins::Tasks::Builder
attr_accessor :command

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

def prebuild(build, listener)
end
module Vagrant
module BaseBuilder
def prebuild(build, listener)
end

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

if @vagrant.nil?
build.halt "OH CRAP! I don't seem to have a Vagrant instance!"
end
if @vagrant.nil?
build.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
unless @vagrant.primary_vm.state == :running
build.halt 'Vagrant VM doesn\'t appear to be running!'
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?
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
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
unless code == 0
build.halt 'Command failed!'
end
end
end
end

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

include BaseBuilder

attr_accessor :command

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

def vagrant_method
:execute
end
end
end

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

include BaseBuilder

attr_accessor :command

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

def vagrant_method
:sudo
def vagrant_method
:sudo
end
end
end

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

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"
def initialize(attrs)
end

unless @vagrant.primary_vm.state == :running
build.halt 'Vagrant VM doesn\'t appear to be running!'
def prebuild(build, listener)
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
70 changes: 36 additions & 34 deletions models/vagrant_wrapper.rb
@@ -1,48 +1,50 @@
require 'rubygems'
require 'vagrant'

class VagrantWrapper < Jenkins::Tasks::BuildWrapper
display_name "Boot Vagrant box"
module Vagrant
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 initialize(attrs)
@vagrant = nil
@vagrantfile = attrs['vagrantfile']
end
def path_to_vagrantfile(build)
if @vagrantfile.nil?
return build.workspace.to_s
end

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

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

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

unless File.exists? path
listener.info("There is no Vagrantfile in your workspace!")
listener.info("We looked in: #{path}")
build.native.setResult(Java.hudson.model.Result::NOT_BUILT)
build.halt
end
listener.info("Running Vagrant with version: #{Vagrant::VERSION}")
@vagrant = Vagrant::Environment.new(:cwd => path)
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"

listener.info("Running Vagrant with version: #{Vagrant::VERSION}")
@vagrant = Vagrant::Environment.new(:cwd => path)
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
build.env[:vagrant] = @vagrant
end

# Called some time when the build is finished.
def teardown(build, listener)
listener.info "Build finished, destroying the Vagrant box"
unless @vagrant.nil?
@vagrant.cli('destroy', '-f')
# Called some time when the build is finished.
def teardown(build, listener)
listener.info "Build finished, destroying the Vagrant box"
unless @vagrant.nil?
@vagrant.cli('destroy', '-f')
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.