Module: VagrantBolt::Util::Bolt
- Defined in:
- lib/vagrant-bolt/util/bolt.rb
Class Method Summary collapse
-
.generate_bolt_command(config, inventory_path = nil) ⇒ String
Create a bolt command from the config.
-
.generate_inventory_hash(env) ⇒ Hash
Generate a bolt inventory hash for the environment.
-
.generate_node_hash(machine) ⇒ Hash
Generate a bolt inventory node hash from the VM config.
-
.inventory_file(env) ⇒ String
Return the path to the inventory file.
-
.machine_transport_hash(machine, machine_config = {}, ssh_info = nil) ⇒ Object
Return a transport config hash for a node.
-
.update_inventory_file(env, inventory_file = nil) ⇒ Object
Update and write the inventory file for the current running machines.
Class Method Details
.generate_bolt_command(config, inventory_path = nil) ⇒ String
Create a bolt command from the config
13 14 15 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 |
# File 'lib/vagrant-bolt/util/bolt.rb', line 13 def self.generate_bolt_command(config, inventory_path = nil) command = [] command << config.bolt_exe command << "#{config.command} run \'#{config.name}\'" config.instance_variables_hash.each do |key, value| next if key.to_s.start_with?('__') next if config.blacklist.include?(key) next if value.nil? key = key.tr('_', '-') case value when TrueClass, FalseClass # Verbose and debug do not have --no flags so exclude them next if ['verbose', 'debug', 'noop'].include?(key) && !value arg = value ? "--#{key}" : "--no-#{key}" command << arg when String command << "--#{key} \'#{value}\'" when Hash command << "--#{key} \'#{value.to_json}\'" unless value.empty? end end command << "--inventoryfile \'#{inventory_path}\'" unless inventory_path.nil? command << "--targets \'#{config.target_list}\'" unless config.target_list.nil? command << config.args unless config.args.nil? command.flatten.join(" ") end |
.generate_inventory_hash(env) ⇒ Hash
Generate a bolt inventory hash for the environment
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vagrant-bolt/util/bolt.rb', line 47 def self.generate_inventory_hash(env) inventory = { 'version' => 2, 'targets' => [] } inventory.merge!(env.vagrantfile.config.bolt.inventory_config.compact) VagrantBolt::Util::Machine.machines_in_environment(env).each do |vm| next unless VagrantBolt::Util::Machine.running?(vm) inventory['targets'] << generate_node_hash(vm) end inventory.compact end |
.generate_node_hash(machine) ⇒ Hash
Generate a bolt inventory node hash from the VM config
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/vagrant-bolt/util/bolt.rb', line 61 def self.generate_node_hash(machine) # Only call ssh_info once node_hash = {} ssh_info = machine.ssh_info return node_hash if ssh_info.nil? node_hash['alias'] = machine.name.to_s machine_config = machine.config.bolt.inventory_config node_hash['config'] = {} transport = VagrantBolt::Util::Machine.windows?(machine) ? 'winrm' : 'ssh' node_hash['config'][transport] = machine_transport_hash(machine, machine_config, ssh_info).compact node_hash['config']['transport'] = transport node_hash['uri'] = "#{transport}://#{ssh_info[:host]}:#{node_hash['config'][transport]['port']}" machine_config.each do |key, value| next if key == 'config' || value.nil? || value.empty? node_hash[key] = value end node_hash.compact end |
.inventory_file(env) ⇒ String
Return the path to the inventory file
110 111 112 |
# File 'lib/vagrant-bolt/util/bolt.rb', line 110 def self.inventory_file(env) VagrantBolt::Util::Config.relative_path('bolt_inventory.yaml', env.local_data_path) end |
.machine_transport_hash(machine, machine_config = {}, ssh_info = nil) ⇒ Object
Return a transport config hash for a node
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/vagrant-bolt/util/bolt.rb', line 86 def self.machine_transport_hash(machine, machine_config = {}, ssh_info = nil) config = {} if VagrantBolt::Util::Machine.windows?(machine) transport = 'winrm' config['ssl'] = (machine.config.winrm.transport == :ssl) config['ssl_verify'] = machine.config.winrm.ssl_peer_verification config['port'] = machine.config.winrm.port config['user'] = machine.config.winrm.username config['password'] = machine.config.winrm.password else transport = 'ssh' config['private-key'] = ssh_info[:private_key_path][0] unless ssh_info[:private_key_path].nil? config['host-key-check'] = (ssh_info[:verify_host_key] == true) config['port'] = ssh_info[:port] config['user'] = ssh_info[:username] config['password'] = ssh_info[:password] end config.merge!(machine_config['config'][transport]) if machine_config.dig('config', transport) config end |
.update_inventory_file(env, inventory_file = nil) ⇒ Object
Update and write the inventory file for the current running machines
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/vagrant-bolt/util/bolt.rb', line 118 def self.update_inventory_file(env, inventory_file = nil) inventory = generate_inventory_hash(env).to_yaml inventory_file ||= Pathname.new(inventory_file(env)) # TODO: This lock should be global lock = Mutex.new lock.synchronize do if !File.exist?(inventory_file) || (inventory != File.read(inventory_file)) begin inventory_tmpfile = Tempfile.new('.vagrant_bolt_inventory', env.local_data_path) inventory_tmpfile.write(inventory) inventory_tmpfile.close File.rename(inventory_tmpfile.path, inventory_file) ensure inventory_tmpfile.close inventory_tmpfile.unlink end end end inventory_file end |