Class: Jackal::Cfn::OrchestrationUnit
- Defined in:
- lib/jackal-cfn/resource/orchestration_unit.rb
Overview
Execute arbitrary actions
Expected resource:
{
"Type": "Custom::OrchestrationUnit",
"Properties": {
"Parameters": {
"OnCreate": {
"Exec": "SHELL_COMMAND",
"ExecZip": "DOWNLOAD_URI",
"RawResult": true,
"Env": {
},
OnlyIf": "SHELL_COMMAND",
NotIf": "SHELL_COMMAND"
},
"OnUpdate": {
"Exec": "SHELL_COMMAND",
"ExecZip": "DOWNLOAD_URI",
"RawResult": true,
"Env": {
},
},
OnlyIf": "SHELL_COMMAND",
NotIf": "SHELL_COMMAND"
},
"OnDelete": {
"Exec": "SHELL_COMMAND",
"ExecZip": "DOWNLOAD_URI",
"RawResult": true,
"Env": {
},
OnlyIf": "SHELL_COMMAND",
NotIf": "SHELL_COMMAND"
},
"Exec": "SHELL_COMMAND",
"ExecZip": "DOWNLOAD_URI",
"Env": {
},
"RawResult": true,
"OnlyIf": "SHELL_COMMAND",
"NotIf": "SHELL_COMMAND"
}
}
}
Constant Summary collapse
- MAX_RESULT_SIZE =
Max result size
2048
Constants inherited from Resource
Resource::VALID_RESOURCE_STATUS
Instance Method Summary collapse
-
#create_working_directory(uuid) ⇒ String
Create a working directory for command execution.
-
#execute(message) ⇒ Object
Execute orchestration unit.
-
#fetch_and_unpack_exec(unit, working_directory) ⇒ Hash
Fetch compressed zip file from remote location and unpack into provided working directory.
-
#run_unit(unit, working_directory, response) ⇒ Hash
Run the unit and set result information into response.
-
#unit_for(request_type, parameters) ⇒ Hash
Extract unit information based on received request type.
-
#unit_runnable?(unit) ⇒ TrueClass, FalseClass
Determine if unit should be run based on conditional fields.
Methods inherited from Resource
#build_response, #failure_wrap, inherited, #physical_resource_id, #respond_to_stack, #setup, #unpack, #valid?
Methods included from Utils::Http
Methods included from Utils
#snakecase, #transform_parameters
Instance Method Details
#create_working_directory(uuid) ⇒ String
Create a working directory for command execution
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/jackal-cfn/resource/orchestration_unit.rb', line 87 def create_working_directory(uuid) dir_path = File.join( config.fetch( :working_directory, '/tmp/jackal-cfn' ), uuid ) FileUtils.mkdir_p(dir_path) dir_path end |
#execute(message) ⇒ Object
Execute orchestration unit
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jackal-cfn/resource/orchestration_unit.rb', line 59 def execute() failure_wrap() do |payload| cfn_resource = rekey_hash(payload.get(:data, :cfn_resource)) properties = rekey_hash(cfn_resource[:resource_properties]) parameters = rekey_hash(properties[:parameters]) cfn_response = build_response(cfn_resource) unit = unit_for(cfn_resource[:request_type], parameters) if(unit_runnable?(unit)) working_dir = create_working_directory(payload[:id]) keepalive = every(10){ .touch! } begin run_unit(unit, working_dir, cfn_response) ensure keepalive.cancel end FileUtils.rm_rf(working_dir) else debug "Received unit for #{} is not runnable due to conditions! #{unit}" end respond_to_stack(cfn_response, cfn_resource[:response_url]) job_completed(:jackal_cfn, payload, ) end end |
#fetch_and_unpack_exec(unit, working_directory) ⇒ Hash
will automatically set ‘unit = ’./run.sh’‘
Fetch compressed zip file from remote location and unpack into provided working directory
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/jackal-cfn/resource/orchestration_unit.rb', line 157 def fetch_and_unpack_exec(unit, working_directory) result = HTTP.get(unit[:exec_zip]) file = Tempfile.new('orchestration-unit') while(data = result.body.readpartial(2048)) file.write data end file.flush file.rewind asset_store.unpack(file, working_directory) unit[:exec] = File.join(working_directory, 'run.sh') unit end |
#run_unit(unit, working_directory, response) ⇒ Hash
Run the unit and set result information into response
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/jackal-cfn/resource/orchestration_unit.rb', line 176 def run_unit(unit, working_directory, response) if(unit[:exec_zip]) debug "Unit provided is compressed zip: #{unit[:exec_zip]}" fetch_and_unpack_exec(unit, working_directory) end if(unit[:exec]) debug "Unit to execute: #{unit[:exec]}" result = Smash.new stdout = process_manager.create_io_tmp(Carnivore.uuid, 'stdout') stderr = process_manager.create_io_tmp(Carnivore.uuid, 'stderr') result[:start_time] = Time.now.to_i [unit[:exec]].flatten.each do |exec_command| debug "Command to execute: #{exec_command}" process_manager.process(unit.hash, exec_command) do |process| process.io.stdout = stdout process.io.stderr = stderr process.cwd = working_directory if(unit[:env]) debug "Custom environment defined: #{unit[:env]}" process.environment.replace(unit[:env]) end process.leader = true process.start begin process.poll_for_exit(config.fetch(:max_execution_time, 500)) result[:exit_code] = process.exit_code debug "Execution of command successful - #{exec_command}" rescue ChildProcess::TimeoutError process.stop result[:timed_out] = true result[:exit_code] = process.exit_code end end break if result[:exit_code] != 0 end result[:stop_time] = Time.now.to_i stdout.rewind if(stdout.size > MAX_RESULT_SIZE) warn "Command result greater than allowed size: #{stdout.size} > #{MAX_RESULT_SIZE}" end result[:content] = stdout.size > 0 ? stdout.readpartial(MAX_RESULT_SIZE) : '' if(result[:exit_code] != 0) debug "Execution of unit failed - #{unit}" stderr.rewind result[:error_message] = stderr.size > 0 ? stderr.readpartial(MAX_RESULT_SIZE) : '' stderr.rewind stdout.rewind debug "Failed unit STDOUT: #{stdout.read}" debug "Failed unit STDERR: #{stderr.read}" end if(result[:exit_code] == 0) response['Data']['OrchestrationUnitResult'] = result[:content] begin j_result = MultiJson.load(result[:content]) response['Data'] = j_result.merge(response['Data']) unless(unit[:raw_result]) response['Data'].delete('OrchestrationUnitResult') end rescue MultiJson::ParseError => e debug 'Command result not JSON data' end response else raise "Execution failed! Exit code: #{result[:exit_code]} Reason: #{result[:error_message]}" end else response['Data']['OrchestrationUnitResult'] = 'No command executed!' response end end |
#unit_for(request_type, parameters) ⇒ Hash
Extract unit information based on received request type
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/jackal-cfn/resource/orchestration_unit.rb', line 252 def unit_for(request_type, parameters) base_key = "on_#{request_type.to_s.downcase}" result = Smash.new if(direct_unit = parameters[base_key]) direct_unit = rekey_hash(direct_unit) [:exec, :exec_zip, :env, :raw_result, :only_if, :not_if].each do |p_key| if(direct_unit[p_key]) result[p_key] = direct_unit[p_key] end end end unless(result[:exec] || result[:exec_zip]) if(parameters[:exec]) result[:exec] = parameters[:exec] elsif(parameters[:exec_zip]) result[:exec_zip] = parameters[:exec_zip] end end if(parameters[:env]) if(result[:env]) result[:env] = parameters[:env].merge(result[:env]) else result[:env] = parameters[:env] end end unless(result.key?('raw_result')) result[:raw_result] = parameters.fetch('raw_result', true) end [:only_if, :not_if].each do |conditional_key| if(!result.key?(conditional_key) && parameters.key?(conditional_key)) result[conditional_key] = parameters[conditional_key] end end result[:env] ||= Smash.new result[:env]['CFN_REQUEST_TYPE'] = request_type.to_s.upcase result end |
#unit_runnable?(unit) ⇒ TrueClass, FalseClass
Determine if unit should be run based on conditional fields
104 105 106 107 108 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 |
# File 'lib/jackal-cfn/resource/orchestration_unit.rb', line 104 def unit_runnable?(unit) result = true conditionals = Smash[ [:only_if, :not_if].map do |conditional_key| stdout = process_manager.create_io_tmp(Carnivore.uuid, 'stdout') stderr = process_manager.create_io_tmp(Carnivore.uuid, 'stderr') if(unit[conditional_key]) debug "Executing conditional `#{conditional_key}`: #{unit[conditional_key]}" conditional_result = false process_manager.process(unit.hash, unit[conditional_key]) do |process| process.io.stdout = stdout process.io.stderr = stderr process.cwd = '/tmp' if(unit[:env]) debug "Custom environment defined: #{unit[:env]}" process.environment.replace(unit[:env]) end process.leader = true process.start begin process.poll_for_exit(config.fetch(:max_execution_time, 60)) debug "Result of conditional `#{conditional_key}`: #{process.exit_code}" stdout.rewind stderr.rewind debug "Result of conditional `#{conditional_key}` output STDOUT: #{stdout.read}" debug "Result of conditional `#{conditional_key}` output STDERR: #{stderr.read}" conditional_result = process.exit_code == 0 rescue ChildProcess::TimeoutError process.stop conditional_result = false end end [conditional_key, conditional_result] end end.compact ] if(conditionals.key?(:not_if) && conditionals.key?(:only_if)) result = !conditionals[:not_if] && conditionals[:only_if] elsif(conditionals.key?(:not_if)) result = !conditionals.key?(:not_if) elsif(conditionals.key?(:only_if)) result = conditionals[:only_if] end result end |