Class: PuppetBox::Driver::Vagrant
- Inherits:
-
Object
- Object
- PuppetBox::Driver::Vagrant
- Defined in:
- lib/puppetbox/driver/vagrant.rb
Constant Summary collapse
- WORKING_DIR_VAGRANT =
"vagrant"- PUPPET_CODE_MOUNT =
"/etc/puppetlabs/code/environments/production"
Instance Method Summary collapse
-
#close ⇒ Object
Close a connection to a box (eg stop a vm, probaly doesn’t need to do anything on SSH…).
-
#initialize(name, codedir, config, keep_vm: false, working_dir: nil, logger: nil) ⇒ Vagrant
constructor
A new instance of Vagrant.
- #node_name ⇒ Object
-
#open ⇒ Object
Open a connection to a box (eg start a vm, ssh to a host etc).
- #reset ⇒ Object
- #result ⇒ Object
-
#run_puppet(puppet_class) ⇒ Object
convert a derelelict (vagrant library used by vagrantomatic) exectutor to a result object as used by puppetbox.
- #run_puppet_x2(puppet_class) ⇒ Object
-
#self_test ⇒ Object
Test that a VM is operational and able to run puppet.
- #validate_config ⇒ Object
Constructor Details
#initialize(name, codedir, config, keep_vm: false, working_dir: nil, logger: nil) ⇒ Vagrant
Returns a new instance of Vagrant.
16 17 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 53 54 55 56 57 |
# File 'lib/puppetbox/driver/vagrant.rb', line 16 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) @vm.add_shared_folder("#{codedir}:#{PUPPET_CODE_MOUNT}") # 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
#close ⇒ Object
Close a connection to a box (eg stop a vm, probaly doesn’t need to do anything on SSH…)
102 103 104 105 106 107 |
# File 'lib/puppetbox/driver/vagrant.rb', line 102 def close() if ! @keep_vm @logger.info("Closing #{@node_name}") @vm.purge end end |
#node_name ⇒ Object
12 13 14 |
# File 'lib/puppetbox/driver/vagrant.rb', line 12 def node_name @name end |
#open ⇒ Object
Open a connection to a box (eg start a vm, ssh to a host etc)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/puppetbox/driver/vagrant.rb', line 81 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("creating instance metadata for #{@name}") # @vm = vom.instance(@name, config:@config) # obtain 'fixed' metadata from instance # @config = @vm.config # add in our mandatory shared folder # @vm.add_shared_folder("#{codedir}:#{PUPPET_CODE_MOUNT}") @vm.save @logger.debug("Instance saved and ready for starting") started = @vm.start end |
#reset ⇒ Object
109 110 111 |
# File 'lib/puppetbox/driver/vagrant.rb', line 109 def reset() @vm.reset end |
#result ⇒ Object
59 60 61 |
# File 'lib/puppetbox/driver/vagrant.rb', line 59 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.
72 73 74 75 76 77 78 |
# File 'lib/puppetbox/driver/vagrant.rb', line 72 def run_puppet(puppet_class) status_code, = @vm.run( "sudo -i puppet apply --detailed-exitcodes -e 'include #{puppet_class}'" ) @result.save(status_code, ) @result.passed? end |
#run_puppet_x2(puppet_class) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/puppetbox/driver/vagrant.rb', line 129 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_test ⇒ Object
Test that a VM is operational and able to run puppet
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/puppetbox/driver/vagrant.rb', line 118 def self_test() status_code, = @vm.run("sudo -i puppet --version") self_test = (status_code == 0) if self_test @logger.info("Running under Puppet version: #{[0].strip}") else @logger.error("Error #{status_code} running puppet: #{}") end self_test end |
#validate_config ⇒ Object
113 114 115 |
# File 'lib/puppetbox/driver/vagrant.rb', line 113 def validate_config @vm.validate_config end |