Class: QB::Ansible::Module
- Inherits:
-
Object
- Object
- QB::Ansible::Module
- Defined in:
- lib/qb/ansible/module.rb
Overview
Definitions
Constant Summary collapse
- @@arg_types =
Class Variables
{}
Class Method Summary collapse
- .arg(name, type) ⇒ Object
-
.stringify_keys(hash) ⇒ Object
Class Methods =====================================================================.
Instance Method Summary collapse
- #changed!(facts = {}) ⇒ Object
-
#debug(*args) ⇒ Object
Forward args to QB.debug if we are connected to a QB STDERR stream (write to STDERR).
- #done ⇒ Object
- #exit_json(hash) ⇒ Object
- #fail(msg) ⇒ Object
- #info(msg) ⇒ Object
-
#initialize ⇒ Module
constructor
Construction =====================================================================.
- #run ⇒ Object
-
#warn(msg) ⇒ Object
Append a warning message to @warnings.
Constructor Details
#initialize ⇒ Module
Construction
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/qb/ansible/module.rb', line 46 def initialize @changed = false # @input_file = ARGV[0] # @input = File.read @input_file # @args = JSON.load @input init_set_args! @facts = {} @warnings = [] @qb_stdio_out = nil @qb_stdio_err = nil @qb_stdio_in = nil # debug "HERE!" # debug ENV # if QB_STDIO_ env vars are set send stdout and stderr # to those sockets to print in the parent process if ENV['QB_STDIO_ERR'] @qb_stdio_err = $stderr = UNIXSocket.new ENV['QB_STDIO_ERR'] debug "Connected to QB stderr stream at #{ ENV['QB_STDIO_ERR'] } #{ @qb_stdio_err.path }." end if ENV['QB_STDIO_OUT'] @qb_stdio_out = $stdout = UNIXSocket.new ENV['QB_STDIO_OUT'] debug "Connected to QB stdout stream at #{ ENV['QB_STDIO_OUT'] }." end if ENV['QB_STDIO_IN'] @qb_stdio_in = UNIXSocket.new ENV['QB_STDIO_IN'] debug "Connected to QB stdin stream at #{ ENV['QB_STDIO_IN'] }." end @@arg_types.each {|key, type| var_name = "@#{ key.to_s }" unless instance_variable_get(var_name).nil? raise ArgumentError.new NRSER.squish <<-END an instance variable named #{ var_name } exists with value #{ instance_variable_get(var_name).inspect } END end value = type.check( @args[key.to_s] ) do |type:, value:| all_args = @args binding.erb <<-END Value <%= value.pretty_inspect %> for argument <%= key.inspect %> is not valid for type <%= type %> Arguments: <%= all_args.pretty_inspect %> END end instance_variable_set var_name, value } end |
Class Method Details
.arg(name, type) ⇒ Object
38 39 40 |
# File 'lib/qb/ansible/module.rb', line 38 def self.arg name, type @@arg_types[name.to_sym] = type end |
.stringify_keys(hash) ⇒ Object
Class Methods
33 34 35 |
# File 'lib/qb/ansible/module.rb', line 33 def self.stringify_keys hash hash.map {|k, v| [k.to_s, v]}.to_h end |
Instance Method Details
#changed!(facts = {}) ⇒ Object
228 229 230 231 232 |
# File 'lib/qb/ansible/module.rb', line 228 def changed! facts = {} @changed = true @facts.merge! facts done end |
#debug(*args) ⇒ Object
Forward args to QB.debug if we are connected to a QB STDERR stream (write to STDERR).
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/qb/ansible/module.rb', line 189 def debug *args if @qb_stdio_err header = "<QB::Ansible::Module #{ self.class.name }>" if args[0].is_a? String header += " " + args.shift end QB.debug header, *args end end |
#done ⇒ Object
234 235 236 237 238 |
# File 'lib/qb/ansible/module.rb', line 234 def done exit_json changed: @changed, ansible_facts: self.class.stringify_keys(@facts), warnings: @warnings end |
#exit_json(hash) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/qb/ansible/module.rb', line 240 def exit_json hash # print JSON response to process' actual STDOUT (instead of $stdout, # which may be pointing to the qb parent process) STDOUT.print JSON.pretty_generate(self.class.stringify_keys(hash)) [ [:stdin, @qb_stdio_in], [:stdout, @qb_stdio_out], [:stderr, @qb_stdio_err], ].each do |name, socket| if socket debug "Flushing socket #{ name }." socket.flush debug "Closing #{ name } socket at #{ socket.path.to_s }." socket.close end end exit 0 end |
#fail(msg) ⇒ Object
261 262 263 |
# File 'lib/qb/ansible/module.rb', line 261 def fail msg exit_json failed: true, msg: msg, warnings: @warnings end |
#info(msg) ⇒ Object
201 202 203 204 205 |
# File 'lib/qb/ansible/module.rb', line 201 def info msg if @qb_stdio_err $stderr.puts msg end end |
#run ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/qb/ansible/module.rb', line 213 def run result = main case result when nil # pass when Hash @facts.merge! result else raise "result of #main should be nil or Hash, found #{ result.inspect }" end done end |
#warn(msg) ⇒ Object
Append a warning message to @warnings.
208 209 210 |
# File 'lib/qb/ansible/module.rb', line 208 def warn msg @warnings << msg end |