Class: Haile::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/haile/client.rb

Constant Summary collapse

EDITABLE_APP_ATTRIBUTES =
%w(
  cmd constraints container cpus env executor id instances mem ports uris
)

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, user = nil, pass = nil, proxy = nil) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/haile/client.rb', line 22

def initialize(url = nil, user = nil, pass = nil, proxy = nil)
  @host = url || ENV['MARATHON_URL'] || 'http://localhost:8080'
  @default_options = {}

  if user && pass
    @default_options[:basic_auth] = {:username => user, :password => pass}
  end

  if proxy
    @default_options[:http_proxyaddr] = proxy[:addr]
    @default_options[:http_proxyport] = proxy[:port]
    @default_options[:http_proxyuser] = proxy[:user] if proxy[:user]
    @default_options[:http_proxypass] = proxy[:pass] if proxy[:pass]
  end
end

Instance Method Details

#docker_deploy(id, image) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/haile/client.rb', line 79

def docker_deploy(id, image)
  # Fetch current state and update only the 'container['docker']['image']'
  # attribute. Since the API only supports PUT, the full representation
  # of the app must be supplied to update even just a single attribute.

  app = wrap_request(:get, "/v2/apps/#{id}").parsed_response['app']
  app.select! {|k, v| EDITABLE_APP_ATTRIBUTES.include?(k)}

  begin
    app['container']['docker']['image'] = image
  rescue
    msg = "App doesn't have a docker image configured. Make sure " \
          "the ID is correct and that this app is already configured " \
          "with a docker image."
    return Haile::Response.error(msg)
  end
  wrap_request(:put, "/v2/apps/#{id}", :body => app)
end

#endpoints(id = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/haile/client.rb', line 54

def endpoints(id = nil)
  if id.nil?
    url = "/v2/tasks"
  else
    url = "/v2/apps/#{id}/tasks"
  end

  wrap_request(:get, url)
end

#kill(id) ⇒ Object



109
110
111
# File 'lib/haile/client.rb', line 109

def kill(id)
  wrap_request(:delete, "/v2/apps/#{id}")
end

#kill_tasks(appId, params = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/haile/client.rb', line 113

def kill_tasks(appId, params = {})
  if params[:task_id].nil?
    wrap_request(:delete, "/v2/apps/#{appId}/tasks?#{query_params(params)}")
  else
    query = params.clone
    task_id = query[:task_id]
    query.delete(:task_id)

    wrap_request(:delete, "/v2/apps/#{appId}/tasks/#{task_id}?#{query_params(query)}")
  end
end

#listObject



38
39
40
# File 'lib/haile/client.rb', line 38

def list
  wrap_request(:get, '/v2/apps')
end

#list_tasks(id) ⇒ Object



42
43
44
# File 'lib/haile/client.rb', line 42

def list_tasks(id)
  wrap_request(:get, URI.escape("/v2/apps/#{id}/tasks"))
end

#scale(id, num_instances) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/haile/client.rb', line 98

def scale(id, num_instances)
  # Fetch current state and update only the 'instances' attribute. Since the
  # API only supports PUT, the full representation of the app must be
  # supplied to update even just a single attribute.
  app = wrap_request(:get, "/v2/apps/#{id}").parsed_response['app']
  app.select! {|k, v| EDITABLE_APP_ATTRIBUTES.include?(k)}

  app['instances'] = num_instances
  wrap_request(:put, "/v2/apps/#{id}", :body => app)
end

#search(id = nil, cmd = nil) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/haile/client.rb', line 46

def search(id = nil, cmd = nil)
  params = {}
  params[:id] = id unless id.nil?
  params[:cmd] = cmd unless cmd.nil?

  wrap_request(:get, "/v2/apps?#{query_params(params)}")
end

#upstart(config_file) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/haile/client.rb', line 64

def upstart(config_file)
  body = JSON.parse(open(config_file).read)
  id = body['id']
  app = wrap_request(:get, "/v2/apps/#{id}").parsed_response['app']
  if !!app
    puts "Updating app #{id}...".green
    resp = wrap_request(:put, "/v2/apps/#{id}", :body => body)
  else
    puts "Starting app #{id}...".blue
    resp = wrap_request(:post, '/v2/apps/', :body => body)
  end

  puts "#{id}: #{resp.to_s}"
end