Class: PuppetBox::Driver::Vagrant

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

Constant Summary collapse

PUPPET_CODE_MOUNT =

fixme - seems abandoned, might need to make my own :( DEFAULT_VAGRANT_BOX = “puppetlabs/centos-7.2-64-puppet”

"/etc/puppetlabs/code/environments/production"

Instance Method Summary collapse

Constructor Details

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

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppetbox/driver/vagrant.rb', line 18

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

  @name         = name
  @keep_vm      = keep_vm
  @working_dir  = working_dir || File.join(Dir.home, '.puppetbox')
  @config       = config
  @result       = Result.new
  @logger       = Logger.new(logger).logger

  if ! @config.has_key?("box")
    raise "Node #{node_name} must specify box"
  end

  # Add the code dir to the config has so that it will automatically become
  # a shared folder when the VM boots

  # can't use dig() might not be ruby 2.3
  if @config.has_key?("folders")
    @config["folders"] = Array(@config["folders"])

    # all paths must be fully qualified.  If we were asked to do a relative path, change
    # it to the current directory since that's probably what the user wanted.  Not right?
    # user supply correct path!
    @config["folders"] = @config["folders"].map { |folder|
      if ! folder.start_with? '/'
        folder = "#{Dir.pwd}/#{folder}"
      end
      folder
    }
  else
    @config["folders"] = []
  end
  @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…)



95
96
97
98
99
100
# File 'lib/puppetbox/driver/vagrant.rb', line 95

def close()
  if ! @keep_vm
    @logger.info("Closing #{@node_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)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/puppetbox/driver/vagrant.rb', line 76

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

  vom = Vagrantomatic::Vagrantomatic.new(vagrant_vm_dir: @working_dir, logger: @logger)

  @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")
  started = @vm.start
end

#resetObject



102
103
104
# File 'lib/puppetbox/driver/vagrant.rb', line 102

def reset()
  @vm.reset
end

#resultObject



54
55
56
# File 'lib/puppetbox/driver/vagrant.rb', line 54

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.


67
68
69
70
71
72
73
# File 'lib/puppetbox/driver/vagrant.rb', line 67

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



118
119
120
121
122
123
124
125
126
# File 'lib/puppetbox/driver/vagrant.rb', line 118

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



107
108
109
110
111
112
113
114
115
116
# File 'lib/puppetbox/driver/vagrant.rb', line 107

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