require 'json' require 'logger' require "base64" require "prepare-environment" require "trace-runner" class HelperRun def run (solution, action, parameters) log = Logger.new(Canzea::config[:logging_root] + '/helpers.log') # Type can be either: ruby or shell # type = ARGV[0] type = "ruby" params = JSON.parse(parameters); # If there is context information, then merge them into the parameters before calling the helper if (ENV.has_key?('WORK_DIR') and File.exists?ENV['WORK_DIR'] + "/context.json") context = JSON.parse(File.read(ENV['WORK_DIR'] + "/context.json")) context.keys.each do |key| params[key] = context[key] end end # Make sure the parameters are valid JSON parameters = JSON.generate(params) envPush = PrepareEnvironment.new begin envScript = ENV['CANZEA_PLUGINS'] + "ike-environments/environment/production/helpers/#{solution}/environment.json" if File.exist?(envScript) log.info("Adding environment variables...") envPush.addToEnv "#{envScript}" end r = RunnerWorker.new if (type == "ruby") cmd = "ruby #{ENV['CANZEA_PLUGINS']}ike-environments/environment/production/helpers/#{solution}/#{action}.rb '#{parameters}'" else cmd = "#{ENV['CANZEA_PLUGINS']}ike-environments/environment/production/helpers/#{solution}/#{action}.sh \"#{parameters}\"" end r.run cmd, 1, 1, false rescue => error log.error(error.backtrace) log.error("Error processing #{error.message}") abort("Problem running plan #{error.message}") end end end