Class: PuppetBox::Driver::Vagrant

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

Constant Summary collapse

WORKING_DIR_VAGRANT =
"vagrant"
PUPPET_CODE_MOUNT =
"/etc/puppetlabs/code/environments/production"
SPEC_ACCEPTANCE_MOUNT =
"spec/acceptance:/acceptance"

Instance Method Summary collapse

Constructor Details

#initialize(name, codedir, config, keep_vm: false, working_dir: nil, logger: nil) ⇒ Vagrant

Returns a new instance of Vagrant.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppetbox/driver/vagrant.rb', line 17

def initialize(name, codedir, config, keep_vm:false, working_dir:nil, logger: nil)

  @name         = name
  @keep_vm      = keep_vm
  @working_dir  = File.join((working_dir || PuppetBox::WORKING_DIR), WORKING_DIR_VAGRANT)
  @config       = config
  @result       = Result.new
  @logger       = Logger.new(logger).logger

  # setup the instance
  @vom = Vagrantomatic::Vagrantomatic.new(vagrant_vm_dir:@working_dir, logger:@logger)
  @logger.debug("creating instance metadata for #{@name}")
  @vm = @vom.instance(@name, config:@config)

  # the code under test
  @vm.add_shared_folder("#{codedir}:#{PUPPET_CODE_MOUNT}")

  # ./spec/acceptance directory
  @vm.add_shared_folder(SPEC_ACCEPTANCE_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…)



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

def close()
  if ! @keep_vm
    @logger.info("Closing #{@name}")
    @vm.purge
  end
end

#node_nameObject



13
14
15
# File 'lib/puppetbox/driver/vagrant.rb', line 13

def node_name
  @name
end

#openObject

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



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

def open()
  # make sure working dir exists...
  FileUtils.mkdir_p(@working_dir)
  @vm.save

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

#resetObject



80
81
82
# File 'lib/puppetbox/driver/vagrant.rb', line 80

def reset()
  @vm.reset
end

#resultObject



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

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.


53
54
55
56
57
58
59
# File 'lib/puppetbox/driver/vagrant.rb', line 53

def run_puppet(puppet_class)
  status_code, messages = @vm.run(
    "sudo -i puppet apply --detailed-exitcodes -e 'include #{puppet_class}'"
  )
  @result.save(status_code, messages)
  @result.passed?
end

#run_puppet_x2(puppet_class) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/puppetbox/driver/vagrant.rb', line 100

def run_puppet_x2(puppet_class)
  # if you need to link a module into puppet's modulepath either do it
  # before running puppet (yet to be supported) or use the @config hash
  # for vagrant to mount what you need as a shared folder
  if run_puppet(puppet_class)
    # Only do the second run if the first run passes
    run_puppet(puppet_class)
  end
end

#self_testObject

Test that a VM is operational and able to run puppet



89
90
91
92
93
94
95
96
97
98
# File 'lib/puppetbox/driver/vagrant.rb', line 89

def self_test()
  status_code, messages = @vm.run("sudo -i puppet --version")
  self_test = (status_code == 0)
  if self_test
    @logger.info("Running under Puppet version: #{messages[0].strip}")
  else
    @logger.error("Error #{status_code} running puppet: #{messages}")
  end
  self_test
end

#validate_configObject



84
85
86
# File 'lib/puppetbox/driver/vagrant.rb', line 84

def validate_config
  @vm.validate_config
end