Class: HelperRun

Inherits:
Object
  • Object
show all
Defined in:
lib/helper-run-class.rb

Instance Method Summary collapse

Constructor Details

#initialize(_raw) ⇒ HelperRun

Returns a new instance of HelperRun.



9
10
11
12
13
# File 'lib/helper-run-class.rb', line 9

def initialize (_raw)
    @basePath = "#{Pathname.new(Canzea::config[:catalog_location]).realpath}"
    @log = Logger.new(Canzea::config[:logging_root] + '/plans.log')
    @raw = _raw
end

Instance Method Details

#enrich(params) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/helper-run-class.rb', line 22

def enrich (params)
    # 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"))
        result = JSON.parse(params).merge(context)
        return JSON.generate(result)
    end
    return params
end

#run(solution, action, parameters, status = false) ⇒ Object



32
33
34
35
36
37
38
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/helper-run-class.rb', line 32

def run (solution, action, parameters, status = false)

    type = "ruby"

    parameters = self.enrich(parameters)

    envPush = PrepareEnvironment.new @raw

    begin
        root = "#{@basePath}/helpers/#{solution}"
        if File.exist?("#{@basePath}/blocks/#{solution}/#{action}")
            root = "#{@basePath}/blocks/#{solution}/#{action}"
        end
        envScript = "#{root}/environment.json"
        if File.exist?(envScript)
            @log.info("Adding environment variables...")
            envPush.addToEnv "#{envScript}"
        end

        if File.exist?("#{root}/#{action}.sh")
            type = "shell"
        elsif File.exist?("#{root}/#{action}.py")
            type = "python"
        end

        r = RunnerWorker.new @raw

        ENV['CATALOG_LOCATION'] = "#{@basePath}";

        ENV['ES_SOLUTION'] = solution;
        ENV['ES_ACTION'] = action;

        parameters = Template.new.processString(parameters, {})

        if (type == "ruby")
            cmd = "ruby #{root}/#{action}.rb '#{parameters}'"
        elsif (type == "python")
            cmd = "python #{root}/#{action}.py '#{parameters}'"
        else
            argList = []
            args = JSON.parse(parameters)
            args.each do | k |
                argList.push('--' + k[0])
                argList.push('"' + k[1] + '"')
            end

            cmd = "#{root}/#{action}.sh #{argList.join(' ')}"
        end
        r.run cmd, 1, 1, status

    rescue => exception
        @log.error(cmd)
        @log.error(exception.to_s)
        @log.error(exception.backtrace)
        abort()
    end
end

#runCommand(cmd, status = false) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/helper-run-class.rb', line 90

def runCommand (cmd, status = false)

    begin

        r = RunnerWorker.new @raw

        r.run cmd, 1, 1, status

    rescue => exception
        @log.error(cmd)
        @log.error(exception.to_s)
        @log.error(exception.backtrace)
        abort()
    end
end

#runPlan(plans) ⇒ Object



15
16
17
18
19
20
# File 'lib/helper-run-class.rb', line 15

def runPlan (plans)
    plans.each { | plan |
        puts "Running #{plan[:action]}"
        self.run plan[:solution], plan[:action], JSON.generate(plan[:parameters])
    }
end