module Convox module Api class Client include HTTMultiParty format :json base_uri 'https://console.convox.com' def initialize(api_key, debug = false) raise 'api_key must be set' if api_key.nil? @api_key = api_key self.class.debug_output $stdout if debug end def apps(rack: nil) parsed_response!(self.class.get('/apps', headers: headers(rack: rack))) end def create_app(name, rack: nil) query = {name: name} parsed_response!(self.class.post('/apps', query: query, headers: headers(rack: rack))) end def delete_app(name, rack: nil) parsed_response!(self.class.delete("/apps/#{name}", headers: headers(rack: rack))) end def app(app, rack: nil) parsed_response!(self.class.get("/apps/#{app}", headers: headers(rack: rack))) end def app_builds(app, rack: nil) parsed_response!(self.class.get("/apps/#{app}/builds", headers: headers(rack: rack))) end def create_app_build(app, rack: nil, cache: nil, description: nil, manifest: 'docker-compose.yml', repo: nil, source: nil) query = {} query[:cache] = cache if cache query[:description] = description if description query[:manifest] = manifest if manifest query[:repo] = repo if repo query[:source] = source if source parsed_response!(self.class.post("/apps/#{app}/builds", query: query, headers: headers(rack: rack))) end # API not functioning def delete_app_build(app, build, rack: nil) parsed_response!(self.class.delete("/apps/#{app}/builds/#{build}", headers: headers(rack: rack))) end def app_build(app, build, rack: nil) parsed_response!(self.class.get("/apps/#{app}/builds/#{build}", headers: headers(rack: rack))) end def update_app_build(app, build, rack: nil) raise 'Not implemented' end def app_environment(app, rack: nil) parsed_response!(self.class.get("/apps/#{app}/environment", headers: headers(rack: rack))) end def set_app_environment(app, environment, rack: nil) environment = environment.to_a.collect{|e| e.join "="}.join "\n" parsed_response!(self.class.post("/apps/#{app}/environment", body: environment, headers: headers(rack: rack))) end def delete_app_environment_key(app, key, rack: nil) parsed_response!(self.class.delete("/apps/#{app}/environment/#{key}", query: query, headers: headers(rack: rack))) end def app_formation(app, rack: nil) raise 'Not implemented' end def create_app_formation_process(app, process, rack: nil, count: nil, memory: nil) raise 'Not implemented' end def app_parameters(app, rack: nil) raise 'Not implemented' end def set_app_parameters(app, parameters, rack: nil) raise 'Not implemented' end def app_processes(app, rack: nil, stats: nil) raise 'Not implemented' end def delete_app_process(app, process, rack: nil) raise 'Not implemented' end def app_process(app, process, rack: nil) raise 'Not implemented' end def run_app_process(app, process, rack: nil, command: nil) raise 'Not implemented' end def app_releases(app, rack: nil) parsed_response!(self.class.get("/apps/#{app}/releases", headers: headers(rack: rack))) end def app_release(app, release, rack: nil) parsed_response!(self.class.get("/apps/#{app}/releases/#{release}", headers: headers(rack: rack))) end def promote_app_release(app, release, rack: nil) parsed_response!(self.class.post("/apps/#{app}/releases/#{release}/promote", headers: headers(rack: rack))) end def app_ssls(app, rack: nil) parsed_response!(self.class.get("/apps/#{app}/ssl", headers: headers(rack: rack))) end def set_app_ssl(app, process, port, rack: nil) raise 'Not implemented' end def certificates raise 'Not implemented' end def add_certificate(public, private, chain: nil) raise 'Not implemented' end def generate_certificate(domains: nil) raise 'Not implemented' end def delete_certificate(id: nil) raise 'Not implemented' end def instances raise 'Not implemented' end def instances_keyroll raise 'Not implemented' end def delete_instance(id: nil) raise 'Not implemented' end def delete_registries raise 'Not implemented' end def registries raise 'Not implemented' end def create_registry(server: nil, username: nil, password: nil) raise 'Not implemented' end def services raise 'Not implemented' end def create_service(name, type) raise 'Not implemented' end def delete_service(name) raise 'Not implemented' end def service(name) raise 'Not implemented' end def create_service_link(service, app) raise 'Not implemented' end def delete_service_link(service, app) raise 'Not implemented' end def system(rack: nil) raise 'Not implemented' end def update_system(count: nil, type: nil, version: nil) raise 'Not implemented' end def system_capacity raise 'Not implemented' end # Not documented in Swagger def app_logs(app, rack: nil) logs = [] EM.run { ws_headers = authorization_headers ws_headers["Rack"] = rack if rack ws = Faye::WebSocket::Client.new("wss://console.convox.com/apps/#{app}/logs", nil, {headers: ws_headers}) ws.on :open do |event| EventMachine.add_timer 2, proc { ws.close } end ws.on :message do |event| logs << event.data end ws.on :close do |event| p [:close, event.code, event.reason] ws = nil EM.stop_event_loop end } logs end private def parsed_response!(response) raise "Error: #{response.code} #{response.body}" unless response.code == 200 response.parsed_response end def headers(rack: nil) headers = {} headers[:rack] = rack if rack headers.merge authorization_headers end def authorization_headers auth_b64 = Base64.encode64("convox:#{@api_key}") {'Authorization' => "Basic #{auth_b64}"} end end end end