Module: BeakerPuppetHelpers::DSL
- Defined in:
- lib/beaker_puppet_helpers/dsl.rb
Overview
The DSL methods for beaker. These are included in Beaker.
Instance Method Summary collapse
-
#apply_manifest(manifest, opts = {}, &block) ⇒ Array<Result>, ...
Runs ‘puppet apply’ on default host.
-
#apply_manifest_on(hosts, manifest, opts = {}, &block) ⇒ Array<Result>, ...
Runs ‘puppet apply’ on a remote host, piping manifest through stdin.
-
#fact(name, opts = {}) ⇒ Object
Get a facter fact from the default host.
-
#fact_on(host, name, opts = {}) ⇒ Object
Get a facter fact from a provided host.
Instance Method Details
#apply_manifest(manifest, opts = {}, &block) ⇒ Array<Result>, ...
Runs ‘puppet apply’ on default host
192 193 194 |
# File 'lib/beaker_puppet_helpers/dsl.rb', line 192 def apply_manifest(manifest, opts = {}, &block) apply_manifest_on(default, manifest, opts, &block) end |
#apply_manifest_on(hosts, manifest, opts = {}, &block) ⇒ Array<Result>, ...
Runs ‘puppet apply’ on a remote host, piping manifest through stdin
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/beaker_puppet_helpers/dsl.rb', line 109 def apply_manifest_on(hosts, manifest, opts = {}, &block) block_on hosts, opts do |host| = {} [:acceptable_exit_codes] = Array(opts[:acceptable_exit_codes]) puppet_apply_opts = {} if opts[:debug] puppet_apply_opts[:debug] = nil else puppet_apply_opts[:verbose] = nil end puppet_apply_opts[:parseonly] = nil if opts[:parseonly] puppet_apply_opts[:trace] = nil if opts[:trace] puppet_apply_opts[:parser] = 'future' if opts[:future_parser] puppet_apply_opts[:modulepath] = opts[:modulepath] if opts[:modulepath] puppet_apply_opts[:hiera_config] = opts[:hiera_config] if opts[:hiera_config] puppet_apply_opts[:noop] = nil if opts[:noop] puppet_apply_opts[:show_diff] = nil if opts[:show_diff] # From puppet help: # "... an exit code of '2' means there were changes, an exit code of # '4' means there were failures during the transaction, and an exit # code of '6' means there were both changes and failures." if [opts[:catch_changes], opts[:catch_failures], opts[:expect_failures], opts[:expect_changes]].compact.length > 1 raise(ArgumentError, 'Cannot specify more than one of `catch_failures`, ' \ '`catch_changes`, `expect_failures`, or `expect_changes` ' \ 'for a single manifest') end if opts[:catch_changes] puppet_apply_opts['detailed-exitcodes'] = nil # We're after idempotency so allow exit code 0 only. [:acceptable_exit_codes] |= [0] elsif opts[:catch_failures] puppet_apply_opts['detailed-exitcodes'] = nil # We're after only complete success so allow exit codes 0 and 2 only. [:acceptable_exit_codes] |= [0, 2] elsif opts[:expect_failures] puppet_apply_opts['detailed-exitcodes'] = nil # We're after failures specifically so allow exit codes 1, 4, and 6 only. [:acceptable_exit_codes] |= [1, 4, 6] elsif opts[:expect_changes] puppet_apply_opts['detailed-exitcodes'] = nil # We're after changes specifically so allow exit code 2 only. [:acceptable_exit_codes] |= [2] else # Either use the provided acceptable_exit_codes or default to [0] [:acceptable_exit_codes] |= [0] end # Not really thrilled with this implementation, might want to improve it # later. Basically, there is a magic trick in the constructor of # PuppetCommand which allows you to pass in a Hash for the last value in # the *args Array; if you do so, it will be treated specially. So, here # we check to see if our caller passed us a hash of environment variables # that they want to set for the puppet command. If so, we set the final # value of *args to a new hash with just one entry (the value of which # is our environment variables hash) puppet_apply_opts['ENV'] = opts[:environment] if opts.key?(:environment) puppet_apply_opts = host[:default_apply_opts].merge(puppet_apply_opts) if host[:default_apply_opts].respond_to? :merge file_path = host.tmpfile(%(apply_manifest_#{Time.now.strftime('%H%M%S%L')}.pp)) begin create_remote_file(host, file_path, "#{manifest}\n") on(host, Beaker::PuppetCommand.new('apply', file_path, puppet_apply_opts), **, &block) ensure host.rm_rf(file_path) end end end |
#fact(name, opts = {}) ⇒ Object
Get a facter fact from the default host
224 225 226 |
# File 'lib/beaker_puppet_helpers/dsl.rb', line 224 def fact(name, opts = {}) fact_on(default, name, opts) end |
#fact_on(host, name, opts = {}) ⇒ Object
Get a facter fact from a provided host
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/beaker_puppet_helpers/dsl.rb', line 205 def fact_on(host, name, opts = {}) raise(ArgumentError, "fact_on's `name` option must be a String. You provided a #{name.class}: '#{name}'") unless name.is_a?(String) if opts.is_a?(Hash) opts['json'] = nil else opts << ' --json' end result = on host, Beaker::Command.new('facter', [name], opts) if result.is_a?(Array) result.map { |res| JSON.parse(res.stdout)[name] } else JSON.parse(result.stdout)[name] end end |