Module: MarathonDeploy::Utils

Defined in:
lib/marathon_deploy/utils.rb

Class Method Summary collapse

Class Method Details

.deep_symbolize(obj) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/marathon_deploy/utils.rb', line 10

def self.deep_symbolize(obj)
  return obj.reduce({}) do |memo, (k, v)|
    memo.tap { |m| m[k.to_sym] = deep_symbolize(v) }
  end if obj.is_a? Hash
  
  return obj.reduce([]) do |memo, v| 
    memo << deep_symbolize(v); memo
  end if obj.is_a? Array

  obj
end

.getValue(url, application, *keys) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/marathon_deploy/utils.rb', line 50

def self.getValue(url,application,*keys)
  value = nil
  5.times { |i|
    i+=1
    model = response_body(HttpUtil.get(url + MarathonDefaults::MARATHON_APPS_REST_PATH + application.id))
    value = lookup(model, *keys) if (!model.nil? and !model.empty?)
    break if (!value.nil? and !value.empty?)
    $LOG.info "Application #{application.id} is not yet registered with Marathon. Waiting #{i} seconds then retrying ..."
    sleep i
  }
  value
end

.lookup(model, key, *rest) ⇒ Object



75
76
77
78
79
# File 'lib/marathon_deploy/utils.rb', line 75

def self.lookup(model, key, *rest)
  v = model[key]
  return v if rest.empty?
  v && lookup(v, *rest)
end

.putJSON(url, application) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/marathon_deploy/utils.rb', line 63

def self.putJSON(url,application)
  response = nil
  5.times { |i|
    i+=1
    response = HttpUtil.put(url + MarathonDefaults::MARATHON_APPS_REST_PATH + application.id,application.json)
    break if (!response.nil?)
    $LOG.info "Did not receive anything from Marathon. Waiting #{i} seconds then retrying ..."
    sleep i
  }
  return response
end

.randomObject



22
23
24
25
# File 'lib/marathon_deploy/utils.rb', line 22

def self.random
  range = [*'0'..'9',*'A'..'Z',*'a'..'z']
  return Array.new(30){ range.sample }.join
end

.response_body(response) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/marathon_deploy/utils.rb', line 27

def self.response_body(response)
  if (!response.body.nil? && !response.body.empty? && response.kind_of?(Net::HTTPSuccess))
    begin
      return deep_symbolize((JSON.parse(response.body)))
    rescue Exception=>e
      $LOG.error "EXCEPTION: #{e} Cannot parse JSON"
    end
  end

  if response.body.nil?
    $LOG.error "No response.body"
  end
  if response.body.empty?
    $LOG.error "Response.body is empty"
  end
  if !response.kind_of?(Net::HTTPSuccess)
    $LOG.error "Answer is not 200, more info:"
    $LOG.error deep_symbolize((JSON.parse(response.body)))
  end

  return nil
end

.symbolize(data) ⇒ Object



6
7
8
# File 'lib/marathon_deploy/utils.rb', line 6

def self.symbolize(data) 
  data.inject({}){|h,(k,v)| h[k.to_sym] = v; h}
end