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
Constructor =====================================================================.
- #run ⇒ Object
-
#warn(msg) ⇒ Object
Append a warning message to @warnings.
Constructor Details
#initialize ⇒ Module
Constructor
39 40 41 42 43 44 45 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 |
# File 'lib/qb/ansible/module.rb', line 39 def initialize @changed = false @input_file = ARGV[0] @input = File.read @input_file @args = JSON.load @input @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 " an instance variable named \#{ var_name } exists\n with value \#{ instance_variable_get(var_name).inspect }\n END\n end\n \n instance_variable_set var_name,\n type.check(@args.fetch(key.to_s))\n }\nend\n" |
Class Method Details
.arg(name, type) ⇒ Object
31 32 33 |
# File 'lib/qb/ansible/module.rb', line 31 def self.arg name, type @@arg_types[name.to_sym] = type end |
.stringify_keys(hash) ⇒ Object
Class Methods
26 27 28 |
# File 'lib/qb/ansible/module.rb', line 26 def self.stringify_keys hash hash.map {|k, v| [k.to_s, v]}.to_h end |
Instance Method Details
#changed!(facts = {}) ⇒ Object
158 159 160 161 162 |
# File 'lib/qb/ansible/module.rb', line 158 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).
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/qb/ansible/module.rb', line 119 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
164 165 166 167 168 |
# File 'lib/qb/ansible/module.rb', line 164 def done exit_json changed: @changed, ansible_facts: self.class.stringify_keys(@facts), warnings: @warnings end |
#exit_json(hash) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/qb/ansible/module.rb', line 170 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.dump(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
191 192 193 |
# File 'lib/qb/ansible/module.rb', line 191 def fail msg exit_json failed: true, msg: msg, warnings: @warnings end |
#info(msg) ⇒ Object
131 132 133 134 135 |
# File 'lib/qb/ansible/module.rb', line 131 def info msg if @qb_stdio_err $stderr.puts msg end end |
#run ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/qb/ansible/module.rb', line 143 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.
138 139 140 |
# File 'lib/qb/ansible/module.rb', line 138 def warn msg @warnings << msg end |