module Hyperb::Containers

wrapper for containers api

Public Instance Methods

container_logs(params = {}) click to toggle source

container logs

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/logs.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::NotFound] raised when the container can't be found. @raise [Hyperb::Error::BadRequest] raised when request is invalid. @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned.

@return [HTTP::Response::Body] a streamable http response body object

@param params [Hash] A customizable set of params. @option params [String] :id container's name or id @option params [String] :follow stream output @option params [String] :stderr stream stderr @option params [String] :stdout stream stdout @option params [String] :since stream outputs since id @option params [String] :timestamps include timestamps on stdouts, default false @option params [String] :tail tail number

# File lib/hyperb/containers/containers.rb, line 190
def container_logs(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
  path = '/containers/' + params[:id] + '/logs'
  query = {}
  params.delete(:id)
  query.merge!(params)
  Hyperb::Request.new(self, path, query, 'get').perform
end
container_stats(params = {}) click to toggle source

container stats

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/logs.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::NotFound] raised when the container can't be found. @raise [Hyperb::Error::BadRequest] raised when request is invalid. @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned.

@return [HTTP::Response::Body] a streamable http response body object

@param params [Hash] A customizable set of params. @option params [String] :id container's name or id @option params [String] :stream stream output

# File lib/hyperb/containers/containers.rb, line 213
def container_stats(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
  path = '/containers/' + params[:id] + '/stats'
  query = {}
  query[:stream] = params[:stream] if params.key?(:stream)
  Hyperb::Request.new(self, path, query, 'get').perform
end
containers(params = {}) click to toggle source

list existing containers

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/list.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned from hyper.

@return [Hyperb::Container] Array of Hyperb::Container.

@param params [Hash] A customizable set of params.

@option params [Boolean] :all show all containers, false by default @option params [Boolean] :size show containers size @option params [String] :limit show `limit` last created containers. @option params [String] :since show only containers created since Id. @option params [String] :before only containers created before Id. TODO: @option params [Hash] :filters JSON encoded value of the filters.

# File lib/hyperb/containers/containers.rb, line 31
def containers(params = {})
  path = '/containers/json'
  query = {}
  query.merge!(params)
  response = JSON.parse(Hyperb::Request.new(self, path, query, 'get').perform)
  response.map { |container| Hyperb::Container.new(container) }
end
create_container(params = {}) click to toggle source

create a container

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/create.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::Conflict] raised container with the same name is already created. @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned

@return [Hash] hash containing downcased symbolized json response.

@param params [Hash] A customizable set of params.

@option params [String] :name container name @option params [String] :image image to be used @option params [String] :hostname container hostname @option params [String] :entrypoint container entrypoint @option params [String] :cmd container command @option params [String] :user add user to container @option params [String] :workingdir working directory for commands to run in. @option params [Array] :mounts array of strings with mount directories. @option params [String] :networkmode network mode, ie 'bridge'. @option params [Hash] :exposedports ports to expose.

@option params [Hash] :exposedports ports to expose.

@option params [Hash] :labels hash containing key: value @option params labels [String] :sh_hyper_instancetype container size: s1, s2, s3 …

# File lib/hyperb/containers/containers.rb, line 111
def create_container(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'image')
  path = '/containers/create'
  query = {}
  # set default size, otherwise container can't be started
  body = { labels: { sh_hyper_instancetype: 's1' } }

  # parse host_config options
  if params.key?(:host_config)
    body[camelize(:host_config)] = setup_host_config(params)
    params.delete(:host_config)
  end

  query[:name] = params[:name] if params.key?(:name)
  params.delete(:name)
  body.merge!(params)

  downcase_symbolize(JSON.parse(Hyperb::Request.new(self, path, query, 'post', body).perform))
end
inspect_container(params = {}) click to toggle source

inspect a container

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/inspect.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::NotFound] raised when the container can't be found. @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned.

@return [Hash] Array of downcased symbolized json response.

@param params [Hash] A customizable set of params.

@option params [String] :id container's name or id @option params [String] :size include container's size on response

# File lib/hyperb/containers/containers.rb, line 145
def inspect_container(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
  path = '/containers/' + params[:id] + '/json'
  query = {}
  query[:size] = params[:size] if params.key?(:size)
  response = JSON.parse(Hyperb::Request.new(self, path, query, 'get').perform)
  downcase_symbolize(response)
end
kill_container(params = {}) click to toggle source

kill a container

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/kill.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::NotFound] raised when the container can't be found. @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned.

@param params [Hash] A customizable set of params. @option params [String] :id container's name or id @option params [String] :signal stream output

# File lib/hyperb/containers/containers.rb, line 232
def kill_container(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
  path = '/containers/' + params[:id] + '/kill'
  query = {}
  query[:signal] = params[:signal] if params.key?(:signal)
  Hyperb::Request.new(self, path, query, 'post').perform
end
remove_container(params = {}) click to toggle source

remove the container id

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/remove.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::NotFound] raised when container can't be found. @raise [Hyperb::Error::Conflict] raised when container is running and can't be removed. @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned.

@return [Hash] downcased symbolized json response.

@param params [Hash] A customizable set of params.

@option params [Boolean] :v remove volumes attached. default false @option params [Boolean] :force force remove. default false

# File lib/hyperb/containers/containers.rb, line 74
def remove_container(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
  path = '/containers/' + params[:id]
  query = {}
  query[:v] = params[:v] if params.key?(:v)
  query[:force] = params[:force] if params.key?(:force)
  response = JSON.parse(Hyperb::Request.new(self, path, query, 'delete').perform)
  downcase_symbolize(response)
end
rename_container(params = {}) click to toggle source

rename a container

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/rename.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::NotFound] raised when the container can't be found. @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned.

@param params [Hash] A customizable set of params. @option params [String] :id new name @option params [String] :name new name

# File lib/hyperb/containers/containers.rb, line 251
def rename_container(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'name', 'id')
  path = '/containers/' + params[:id] + '/rename'
  query = {}
  query[:name] = params[:name] if params.key?(:name)
  Hyperb::Request.new(self, path, query, 'post').perform
end
start_container(params = {}) click to toggle source

start a container

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/start.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::NotFound] raised when the container can't be found. @raise [Hyperb::Error::BadRequest] raised when request is invalid. @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned.

@param params [Hash] A customizable set of params. @option params [String] :id container's name or id

# File lib/hyperb/containers/containers.rb, line 165
def start_container(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
  path = '/containers/' + params[:id] + '/start'
  Hyperb::Request.new(self, path, {}, 'post').perform
end
stop_container(params = {}) click to toggle source

stop the container id

@see docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/stop.html

@raise [Hyperb::Error::Unauthorized] raised when credentials are not valid. @raise [Hyperb::Error::NotFound] raised when container can't be found. @raise [Hyperb::Error::Conflict] raised when container is running and can't be removed. @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned.

@param params [Hash] A customizable set of params.

@option params [Boolean] :t number of seconds to wait before killing the container.

# File lib/hyperb/containers/containers.rb, line 51
def stop_container(params = {})
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
  path = '/containers/' + params[:id] + '/stop'
  query = {}
  query[:t] = params[:t] if params.key?(:t)
  Hyperb::Request.new(self, path, query, 'post').perform
end

Private Instance Methods

setup_host_config(params) click to toggle source
# File lib/hyperb/containers/containers.rb, line 261
def setup_host_config(params)
  if params[:host_config].is_a?(Hash)
    HostConfig.new(params[:host_config]).fmt
  elsif params[:host_config].is_a?(HostConfig)
    params[:host_config].fmt
  end
end