Class: Wakame::MasterManagers::CommandQueue::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/wakame/master_managers/command_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(command_queue) ⇒ Adapter

Returns a new instance of Adapter.



71
72
73
# File 'lib/wakame/master_managers/command_queue.rb', line 71

def initialize(command_queue)
  @command_queue = command_queue
end

Instance Method Details

#authentication(path, query) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/wakame/master_managers/command_queue.rb', line 111

def authentication(path, query)
  key = Wakame.config.private_key
  req = query.split(/\&signature\=/)
  hash = OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, key, req[0])
  hmac = Base64.encode64(hash).gsub(/\+/, "").gsub(/\n/, "").to_s
  if hmac != path["signature"]
    raise "Authentication failed"
  end
end

#call(env) ⇒ Object



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
# File 'lib/wakame/master_managers/command_queue.rb', line 75

def call(env)
  req = Rack::Request.new(env)
  begin
    unless req.get?().to_s == "true"
      raise "No Support Response"
    end
    query = req.query_string()
    params = req.params()
    if Wakame.config.enable_authentication == "true"
      auth = authentication(params, query)
    end
    cname = params["action"].split("_")
    begin
      cmd = eval("Command::#{(cname.collect{|c| c.capitalize}).join}").new 
      cmd.options = params
      command = @command_queue.send_cmd(cmd)

      if command.is_a?(Exception)
        status = 500
        body = json_encode(status, command.message)
      else
        status = 200
        body = json_encode(status, "OK", command)
      end
    rescue => e
      status = 404
      body = json_encode(status, e)
    end
  rescue => e
    status = 403
    body = json_encode(status, e)
    Wakame.log.error(e)
  end
  [ status, {'Content-Type' => 'text/javascript+json'}, body]
end

#json_encode(status, message, data = nil) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/wakame/master_managers/command_queue.rb', line 121

def json_encode(status, message, data=nil)
  if status == 200 && data.is_a?(Hash)
    body = [{:status=>status, :message=>message}, {:data=>data}].to_json
  else
    body = [{:status=>status, :message=>message}].to_json
  end
  body
end