Class: GoGreen

Inherits:
Object
  • Object
show all
Defined in:
lib/gogreen.rb

Instance Method Summary collapse

Constructor Details

#initialize(alias_file, site = nil, subdomain = nil, shell_execute: true, rsc: nil, keepalive: false) ⇒ GoGreen

Returns a new instance of GoGreen.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gogreen.rb', line 31

def initialize(alias_file, site=nil, subdomain=nil, shell_execute: true,
               rsc: nil, keepalive: false)
  super()

  @shell_execute, @rsc, @alias_file = shell_execute, rsc, alias_file
  @keepalive = keepalive

  buffer = RXFReader.read(alias_file).first

  name = case buffer
  when /^<\?dynarex/
    @records = dxread(buffer)
  when /^<\?polyrex/
    @records, @conf = pxread(buffer, site, subdomain)
  else
    raise GoGreenException, 'alias file ' + alias_file + ' not recognised'
  end


end

Instance Method Details

#execute(alias_name, job_args = []) ⇒ Object



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
# File 'lib/gogreen.rb', line 52

def execute(alias_name, job_args=[])

  alias_found = @records[alias_name]
  return 'job not found' unless alias_found

  cmd = alias_found[:body][:command]
  cmd += ' ' + @conf if @conf

  if cmd[/^rcscript/] then

    a = cmd.sub(/^rcscript /,'').split + job_args.map do |x|
      x.sub(/^["'](.*)["']$/,'\1')
    end

    raw_code, args = RScript.new.read a

    begin

      if @shell_execute then

        a = raw_code.strip.lines
        a.unshift 'args = ' + args.inspect + "\n\n"
        lastline = a.pop
        a.push('puts ' + lastline)

        var = '$alias_file = ' + @alias_file.inspect
        code2 = "$0 = 'gogreen'\n" + var +"\n" + a.join.gsub('\"','"')\
            .gsub('\#','#')

        file = Tempfile.new('gogreen')
        code3 = @keepalive ? apply_handler(code2) : code2
        file.write(code3)
        file.close
        puts 'before ruby'
        r = `ruby #{file.path}`
        r.strip
        #pipe = IO.popen("ruby", "w")
        #pipe.write code2
        #pipe.close

      else

        code3 = @keepalive ? apply_handler(raw_code) : raw_code
        result = eval(code3)

      end

    rescue
      ($!).to_s[/^[^\n]+/].to_s
    end

  elsif cmd[/^rsc/] and @rsc

    package, job, args = cmd.sub(/^rsc/,'').split + job_args
    @rsc.send(package.to_sym).send(job.to_sym, *args)

  else
    `#{cmd} #{job_args.join(' ')}`
  end

end