Class: PuppetBox::Driver::Vagrant

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetbox/driver/vagrant.rb

Constant Summary collapse

DEFAULT_VAGRANT_BOX =

fixme - seems abandoned, might need to make my own :(

"puppetlabs/centos-7.2-64-puppet"
PUPPET_CODE_MOUNT =
"/etc/puppetlabs/code/environments/production"

Instance Method Summary collapse

Constructor Details

#initialize(name, codedir, keep_vm: true, working_dir: nil, config: {'box'=> DEFAULT_VAGRANT_BOX}, logger: nil) ⇒ Vagrant

Returns a new instance of Vagrant.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/puppetbox/driver/vagrant.rb', line 13

def initialize(name, codedir, keep_vm:true, working_dir:nil, config:{'box'=> DEFAULT_VAGRANT_BOX}, logger: nil)
  @name         = name
  @keep_vm      = keep_vm
  @working_dir  = working_dir || File.join(Dir.home, '.puppetbox')
  @config       = config
  @result       = PuppetBox::Result.new
  @logger       = PuppetBox::Logger.new(logger).logger

  # Add the code dir to the config has so that it will automatically become
  # a shared folder when the VM boots
  @config["folders"] = "#{codedir}:#{PUPPET_CODE_MOUNT}"
  @logger.debug "instance #{name} initialised"
end

Instance Method Details

#closeObject

Close a connection to a box (eg stop a vm, probaly doesn’t need to do anything on SSH…)



64
65
66
67
68
# File 'lib/puppetbox/driver/vagrant.rb', line 64

def close()
  if ! @keep_vm
    @vm.purge
  end
end

#openObject

Open a connection to a box (eg start a vm, ssh to a host etc)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppetbox/driver/vagrant.rb', line 46

def open()
  # make sure working dir exists...
  FileUtils.mkdir_p(@working_dir)
  vom = Vagrantomatic::Vagrantomatic.new(vagrant_vm_dir: @working_dir)

  @logger.debug("reading instance metadata for #{@name}")
  @vm = vom.instance(@name)

  @logger.debug("...setting instance config and saving")

  @vm.config=(@config)
  @vm.save
  @logger.debug("Instance saved and ready for starting")
  @vm.start
end

#resultObject



27
28
29
# File 'lib/puppetbox/driver/vagrant.rb', line 27

def result
  @result
end

#run_puppet(puppet_class) ⇒ Object

convert a derelelict (vagrant library used by vagrantomatic) exectutor to a result object as used by puppetbox

Puppet exit status codes:

0: The run succeeded with no changes or failures; the system was already in the desired state.
1: The run failed, or wasn't attempted due to another run already in progress.
2: The run succeeded, and some resources were changed.
4: The run succeeded, and some resources failed.
6: The run succeeded, and included both changes and failures.


40
41
42
43
# File 'lib/puppetbox/driver/vagrant.rb', line 40

def run_puppet(puppet_class)
  run_hash = @vm.run("sudo -i puppet apply --detailed-exitcodes -e 'include #{puppet_class}'")
  @result.report(run_hash[:status], run_hash[:messages])
end

#run_puppet_x2(puppet_class) ⇒ Object



75
76
77
78
79
# File 'lib/puppetbox/driver/vagrant.rb', line 75

def run_puppet_x2(puppet_class)
  # fixme - link module
  run_puppet(puppet_class)
  run_puppet(puppet_class)
end

#self_testObject

Test that a VM is operational and able to run puppet



71
72
73
# File 'lib/puppetbox/driver/vagrant.rb', line 71

def self_test()
  @vm.run("sudo -i puppet --version")[:status] == 0
end