class Haile::Client

Constants

EDITABLE_APP_ATTRIBUTES

Public Class Methods

new(url = nil, user = nil, pass = nil, proxy = nil) click to toggle source
# 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

Public Instance Methods

docker_deploy(id, image) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/haile/client.rb, line 109
def kill(id)
  wrap_request(:delete, "/v2/apps/#{id}")
end
kill_tasks(appId, params = {}) click to toggle source
# 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
list() click to toggle source
# File lib/haile/client.rb, line 38
def list
  wrap_request(:get, '/v2/apps')
end
list_tasks(id) click to toggle source
# 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) click to toggle source
# 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
upstart(config_file) click to toggle source
# 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

Private Instance Methods

query_params(hash) click to toggle source
# File lib/haile/client.rb, line 135
def query_params(hash)
  hash = hash.select { |k,v| !v.nil? }
  URI.escape(hash.map { |k,v| "#{k}=#{v}" }.join('&'))
end
wrap_request(method, url, options = {}) click to toggle source
# File lib/haile/client.rb, line 127
def wrap_request(method, url, options = {})
  options = @default_options.merge(options)
  http = self.class.send(method, @host + url, options)
  Haile::Response.new(http)
rescue => e
  Haile::Response.error(e.message)
end