class Shelly::Client

Public Instance Methods

add_ssh_key(ssh_key) click to toggle source
# File lib/shelly/client/ssh_keys.rb, line 2
def add_ssh_key(ssh_key)
  post("/ssh_keys", :ssh_key => ssh_key)
end
api_host() click to toggle source
# File lib/shelly/client.rb, line 28
def api_host
  URI.parse(api_url).host
end
api_url() click to toggle source
# File lib/shelly/client.rb, line 24
def api_url
  ENV["SHELLY_URL"] || "https://api.shellycloud.com/apiv2"
end
app(code_name) click to toggle source
# File lib/shelly/client/apps.rb, line 26
def app(code_name)
  get("/apps/#{code_name}")
end
app_config(cloud, path) click to toggle source
# File lib/shelly/client/configs.rb, line 6
def app_config(cloud, path)
  get("/apps/#{cloud}/configs/#{CGI.escape(path)}")
end
app_configs(cloud) click to toggle source
# File lib/shelly/client/configs.rb, line 2
def app_configs(cloud)
  get("/apps/#{cloud}/configs")
end
app_create_config(cloud, path, content) click to toggle source
# File lib/shelly/client/configs.rb, line 10
def app_create_config(cloud, path, content)
  post("/apps/#{cloud}/configs", :config => {:path => path, :content => content})
end
app_delete_config(cloud, path) click to toggle source
# File lib/shelly/client/configs.rb, line 18
def app_delete_config(cloud, path)
  delete("/apps/#{cloud}/configs/#{CGI.escape(path)}")
end
app_update_config(cloud, path, content) click to toggle source
# File lib/shelly/client/configs.rb, line 14
def app_update_config(cloud, path, content)
  put("/apps/#{cloud}/configs/#{CGI.escape(path)}", :config => {:content => content})
end
application_logs(cloud, options = {}) click to toggle source
# File lib/shelly/client/application_logs.rb, line 2
def application_logs(cloud, options = {})
  get("/apps/#{cloud}/application_logs#{query(options)}")
end
application_logs_tail(cloud) { |c| ... } click to toggle source
# File lib/shelly/client/application_logs.rb, line 6
def application_logs_tail(cloud)
  url = get("/apps/#{cloud}/application_logs/tail")["url"]
  options = {
    :url            => url,
    :method         => :get,
    :timeout        => 60 * 60 * 24,
    :block_response => Proc.new { |r| r.read_body { |c| yield(c) } }
  }.merge(http_basic_auth_options)
  RestClient::Request.execute(options)
end
apps() click to toggle source
# File lib/shelly/client/apps.rb, line 22
def apps
  get("/apps")
end
authorize!() click to toggle source
# File lib/shelly/client/auth.rb, line 13
def authorize!
  get_token
  true
end
authorize_with_email_and_password(email, password) click to toggle source
# File lib/shelly/client/auth.rb, line 2
def authorize_with_email_and_password(email, password)
  forget_authorization
  @email = email; @password = password
  api_key = get_token
  store_api_key_in_netrc(email, api_key)
end
basic_auth_from_netrc() click to toggle source
# File lib/shelly/client/auth.rb, line 26
def basic_auth_from_netrc
  if netrc
    user, password = netrc[api_host]
    {:user => user, :password => password}
  else
    {}
  end
end
command(cloud, body, type) click to toggle source
# File lib/shelly/client/apps.rb, line 34
def command(cloud, body, type)
  post("/apps/#{cloud}/command", {:body => body, :type => type})
end
configured_db_server(code_name, server = nil) click to toggle source
# File lib/shelly/client/tunnels.rb, line 6
def configured_db_server(code_name, server = nil)
  get("/apps/#{code_name}/configured_db_server",
    {:server => server, :service => "ssh"})
end
create_app(attributes) click to toggle source
# File lib/shelly/client/apps.rb, line 2
def create_app(attributes)
  organization = attributes.delete(:organization_name)
  region = attributes.delete(:region)
  zone = attributes.delete(:zone)
  post("/apps", :app => attributes, :organization_name => organization,
         :region => region, :zone => zone)
end
create_endpoint(cloud, certificate, key, sni) click to toggle source
# File lib/shelly/client/endpoint.rb, line 10
def create_endpoint(cloud, certificate, key, sni)
  endpoint = certificate && key ? {:certificate => certificate,
    :key => key} : {}

  post("/apps/#{cloud}/endpoints", :endpoint => endpoint.merge(:sni => sni))
end
create_organization(attributes, referral_code = nil) click to toggle source
# File lib/shelly/client/organizations.rb, line 10
def create_organization(attributes, referral_code = nil)
  post("/organizations", :organization => attributes,
    :referral_code => referral_code)
end
database_backup(code_name, handler) click to toggle source
# File lib/shelly/client/database_backups.rb, line 6
def database_backup(code_name, handler)
  get("/apps/#{code_name}/database_backups/#{handler}")
end
database_backups(code_name) click to toggle source
# File lib/shelly/client/database_backups.rb, line 2
def database_backups(code_name)
  get("/apps/#{code_name}/database_backups")
end
delete(path, params = {}) click to toggle source
# File lib/shelly/client.rb, line 49
def delete(path, params = {})
  request(path, :delete, params)
end
delete_app(code_name) click to toggle source
# File lib/shelly/client/apps.rb, line 10
def delete_app(code_name)
  delete("/apps/#{code_name}")
end
delete_endpoint(cloud, uuid) click to toggle source
# File lib/shelly/client/endpoint.rb, line 22
def delete_endpoint(cloud, uuid)
  delete("/apps/#{cloud}/endpoints/#{uuid}")
end
delete_member(name, email) click to toggle source
# File lib/shelly/client/organizations.rb, line 23
def delete_member(name, email)
  delete("/organizations/#{name}/memberships/#{email}")
end
delete_ssh_key(fingerprint) click to toggle source
# File lib/shelly/client/ssh_keys.rb, line 6
def delete_ssh_key(fingerprint)
  delete("/ssh_keys/#{fingerprint}")
end
deploy_log(cloud, log) click to toggle source
# File lib/shelly/client/deployment_logs.rb, line 6
def deploy_log(cloud, log)
  get("/apps/#{cloud}/deployment_logs/#{log}")
end
deploy_logs(cloud) click to toggle source
# File lib/shelly/client/deployment_logs.rb, line 2
def deploy_logs(cloud)
  get("/apps/#{cloud}/deployment_logs")
end
deployment(cloud, deployment_id) click to toggle source
# File lib/shelly/client/deploys.rb, line 6
def deployment(cloud, deployment_id)
  get("/apps/#{cloud}/deploys/#{deployment_id}")
end
download_application_logs_attributes(code_name, options) click to toggle source
# File lib/shelly/client/application_logs.rb, line 17
def download_application_logs_attributes(code_name, options)
  get("/apps/#{code_name}/application_logs/download#{query(options)}")
end
download_backup_url(code_name, filename) click to toggle source
# File lib/shelly/client/database_backups.rb, line 18
def download_backup_url(code_name, filename)
  get("/apps/#{code_name}/database_backups/#{filename}/download_url")["url"]
end
download_file(cloud, filename, url, progress_callback = nil) click to toggle source
# File lib/shelly/client.rb, line 53
def download_file(cloud, filename, url, progress_callback = nil)
  File.open(filename, "wb") do |out|
    process_response = lambda do |response|
      raise_error_for_400_to_599(response.code.to_i)

      total_size = response.to_hash['file-size'].first.to_i if response.to_hash['file-size']

      response.read_body do |chunk|
        out.write(chunk)

        progress_callback.call(chunk.size,
          total_size) if progress_callback
      end
    end

    options = {
      :url            => url,
      :method         => :get,
      :block_response => process_response,
      :headers => {:accept => "application/x-gzip"}
    }.merge(http_basic_auth_options)

    RestClient::Request.execute(options)
  end
end
email_from_netrc() click to toggle source
# File lib/shelly/client/auth.rb, line 52
def email_from_netrc
  netrc[api_host].first if netrc
end
endpoint(cloud, uuid) click to toggle source
# File lib/shelly/client/endpoint.rb, line 6
def endpoint(cloud, uuid)
  get("/apps/#{cloud}/endpoints/#{uuid}")
end
endpoints(cloud) click to toggle source
# File lib/shelly/client/endpoint.rb, line 2
def endpoints(cloud)
  get("/apps/#{cloud}/endpoints")
end
finish_maintenance(cloud) click to toggle source
# File lib/shelly/client/maintenance.rb, line 10
def finish_maintenance(cloud)
  put("/apps/#{cloud}/maintenances/last", :maintenance => {:finished => true})
end
forget_authorization() click to toggle source
# File lib/shelly/client/auth.rb, line 18
def forget_authorization
  remove_api_key_from_netrc
end
get(path, params = {}) click to toggle source
# File lib/shelly/client.rb, line 45
def get(path, params = {})
  request(path, :get, params)
end
get_token() click to toggle source
# File lib/shelly/client/auth.rb, line 22
def get_token
  get("/token")["token"]
end
headers() click to toggle source
# File lib/shelly/client.rb, line 86
def headers
  {:accept          => :json,
   :content_type    => :json,
   "shelly-version" => Shelly::VERSION}
end
http_basic_auth_options() click to toggle source
# File lib/shelly/client.rb, line 92
def http_basic_auth_options
  if @email && @password
    {:user => @email, :password => @password}
  else
    basic_auth_from_netrc
  end
end
maintenances(cloud) click to toggle source
# File lib/shelly/client/maintenance.rb, line 2
def maintenances(cloud)
  get("/apps/#{cloud}/maintenances")
end
members(name) click to toggle source
# File lib/shelly/client/organizations.rb, line 15
def members(name)
  get("/organizations/#{name}/memberships")
end
netrc() click to toggle source
# File lib/shelly/client/auth.rb, line 56
def netrc
  @netrc ||= File.exists?(netrc_path) && Netrc.read(netrc_path)
end
netrc_path() click to toggle source
# File lib/shelly/client/auth.rb, line 60
def netrc_path
  default = Netrc.default_path
  encrypted = default + ".gpg"
  if File.exists?(encrypted)
    encrypted
  else
    default
  end
end
organization(name) click to toggle source
# File lib/shelly/client/organizations.rb, line 6
def organization(name)
  get("/organizations/#{name}")
end
organizations() click to toggle source
# File lib/shelly/client/organizations.rb, line 2
def organizations
  get("/organizations")
end
post(path, params = {}) click to toggle source
# File lib/shelly/client.rb, line 37
def post(path, params = {})
  request(path, :post, params)
end
process_response(response) click to toggle source
# File lib/shelly/client.rb, line 112
def process_response(response)
  body = JSON.parse(response.body) rescue JSON::ParserError && {}
  code = response.code
  raise_error_for_400_to_599(code, body, response)
  response.return!
  body
end
put(path, params = {}) click to toggle source
# File lib/shelly/client.rb, line 41
def put(path, params = {})
  request(path, :put, params)
end
query(options = {}) click to toggle source
# File lib/shelly/client.rb, line 32
def query(options = {})
  "?" + options.map { |k, v|
    URI.escape(k.to_s) + "=" + URI.escape(v.to_s) }.join("&")
end
raise_error_for_400_to_599(code, body = nil, response = nil) click to toggle source
# File lib/shelly/client.rb, line 120
def raise_error_for_400_to_599(code, body = nil, response = nil)
  if (400..599).include?(code)
    exception_class = case code
    when 401; UnauthorizedException
    when 403; ForbiddenException
    when 404; NotFoundException
    when 409; ConflictException
    when 412; GemVersionException
    when 422; ValidationException
    when 423; LockedException
    when 504; GatewayTimeoutException
    else; APIException
    end
    request_id = response.headers[:x_request_id] if response.respond_to?(:headers)
    raise exception_class.new(body, code, request_id)
  end
end
redeploy(cloud) click to toggle source
# File lib/shelly/client/deploys.rb, line 2
def redeploy(cloud)
  post("/apps/#{cloud}/deploys")
end
register_user(email, password) click to toggle source
# File lib/shelly/client/users.rb, line 2
def register_user(email, password)
  post("/users", :user => {:email => email, :password => password})
end
remove_api_key_from_netrc() click to toggle source
# File lib/shelly/client/auth.rb, line 44
def remove_api_key_from_netrc
  if netrc
    netrc.delete(api_host)
    netrc.save
    true # must return a truthy value to print logout confirmation
  end
end
request(path, method, params = {}) click to toggle source
# File lib/shelly/client.rb, line 79
def request(path, method, params = {})
  options = request_parameters(path, method, params)
  RestClient::Request.execute(options) do |response, request|
    process_response(response)
  end
end
request_backup(code_name, kind = nil) click to toggle source
# File lib/shelly/client/database_backups.rb, line 14
def request_backup(code_name, kind = nil)
  post("/apps/#{code_name}/database_backups", :kind => kind)
end
request_parameters(path, method, params = {}) click to toggle source
# File lib/shelly/client.rb, line 100
def request_parameters(path, method, params = {})
  parameters = {
    :method   => method,
    :url      => "#{api_url}#{path}",
    :headers  => headers
  }.merge(http_basic_auth_options)
  unless [:get, :head].include?(method)
    parameters = parameters.merge(:payload => params.to_json)
  end
  parameters
end
restore_backup(code_name, filename) click to toggle source
# File lib/shelly/client/database_backups.rb, line 10
def restore_backup(code_name, filename)
  put("/apps/#{code_name}/database_backups/#{filename}/restore")
end
send_invitation(name, email, owner = false) click to toggle source
# File lib/shelly/client/organizations.rb, line 19
def send_invitation(name, email, owner = false)
  post("/organizations/#{name}/memberships", :email => email, :owner => owner)
end
shellyapp_url() click to toggle source
# File lib/shelly/client/shellyapp.rb, line 2
def shellyapp_url
  get("/shellyapp")["url"]
end
ssh_key(fingerprint) click to toggle source
# File lib/shelly/client/ssh_keys.rb, line 10
def ssh_key(fingerprint)
  get("/ssh_keys/#{fingerprint}")
end
start_cloud(cloud) click to toggle source
# File lib/shelly/client/apps.rb, line 14
def start_cloud(cloud)
  put("/apps/#{cloud}/start")
end
start_maintenance(cloud, attributes) click to toggle source
# File lib/shelly/client/maintenance.rb, line 6
def start_maintenance(cloud, attributes)
  post("/apps/#{cloud}/maintenances", :maintenance => attributes)
end
statistics(code_name) click to toggle source
# File lib/shelly/client/apps.rb, line 30
def statistics(code_name)
  get("/apps/#{code_name}/statistics")
end
stop_cloud(cloud) click to toggle source
# File lib/shelly/client/apps.rb, line 18
def stop_cloud(cloud)
  put("/apps/#{cloud}/stop")
end
store_api_key_in_netrc(email, api_key) click to toggle source
# File lib/shelly/client/auth.rb, line 35
def store_api_key_in_netrc(email, api_key)
  FileUtils.mkdir_p(File.dirname(netrc_path))
  FileUtils.touch(netrc_path)
  FileUtils.chmod(0600, netrc_path)

  netrc[api_host] = [email, api_key]
  netrc.save
end
tunnel(code_name, service, server = nil) click to toggle source
# File lib/shelly/client/tunnels.rb, line 2
def tunnel(code_name, service, server = nil)
  post("/apps/#{code_name}/tunnels", {:server => server, :service => service})
end
update_endpoint(cloud, uuid, certificate, key) click to toggle source
# File lib/shelly/client/endpoint.rb, line 17
def update_endpoint(cloud, uuid, certificate, key)
  put("/apps/#{cloud}/endpoints/#{uuid}",
    :endpoint => {:certificate => certificate, :key => key})
end
user_email() click to toggle source
# File lib/shelly/client/auth.rb, line 9
def user_email
  @email || email_from_netrc
end