Class: Fog::Compute::Fogdocker::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/fogdocker/requests/compute/container_create.rb,
lib/fog/fogdocker/compute.rb,
lib/fog/fogdocker/requests/compute/image_all.rb,
lib/fog/fogdocker/requests/compute/image_get.rb,
lib/fog/fogdocker/requests/compute/api_version.rb,
lib/fog/fogdocker/requests/compute/image_create.rb,
lib/fog/fogdocker/requests/compute/image_delete.rb,
lib/fog/fogdocker/requests/compute/image_search.rb,
lib/fog/fogdocker/requests/compute/container_all.rb,
lib/fog/fogdocker/requests/compute/container_get.rb,
lib/fog/fogdocker/requests/compute/container_action.rb,
lib/fog/fogdocker/requests/compute/container_commit.rb,
lib/fog/fogdocker/requests/compute/container_delete.rb

Overview

Create attributes ‘Hostname’ => ”, ‘User’ => ”, ‘Memory’ => 0, ‘MemorySwap’ => 0, ‘AttachStdin’ => false, ‘AttachStdout’ => true, ‘AttachStderr’ => true, ‘PortSpecs’ => nil, ‘Tty’ => false, ‘OpenStdin’ => false, ‘StdinOnce’ => false, ‘Env’ => nil, ‘Cmd’ => [‘date’], ‘Dns’ => nil, ‘Image’ => ‘base’, ‘Volumes’ =>

'/tmp' =>  {

}, ‘VolumesFrom’ => ”, ‘WorkingDir’ => ”, ‘ExposedPorts’ => {

'22/tcp' => {}

}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/fogdocker/compute.rb', line 36

def initialize(options={})
  require 'docker'
  username = options[:docker_username]
  password = options[:docker_password]
  email    = options[:docker_email]
  url      = options[:docker_url]

  connection_options = {:username => username, :password => password, :email => email}
  @connection = Docker::Connection.new(url, connection_options)
  Docker.authenticate!(connection_options, @connection) if username || email || password
rescue Docker::Error::AuthenticationError => e
  raise Fog::Errors::Fogdocker::AuthenticationError.new(e.message)
end

Instance Method Details

#api_versionObject



5
6
7
# File 'lib/fog/fogdocker/requests/compute/api_version.rb', line 5

def api_version
  Docker.version(@connection)
end

#camelize_hash_keys(hash) ⇒ Object



55
56
57
# File 'lib/fog/fogdocker/compute.rb', line 55

def camelize_hash_keys(hash)
  Hash[ hash.map {|k, v| [k.to_s.split('_').map {|w| w.capitalize}.join, v] }]
end

#container_action(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/fogdocker/requests/compute/container_action.rb', line 5

def container_action(options = {})
  raise ArgumentError, "instance id is a required parameter" unless options.key? :id
  raise ArgumentError, "action is a required parameter" unless options.key? :action
  result = Docker::Container.get(options[:id], {}, @connection).send(options[:action], options[:options] || {})

  if result.is_a?(Hash)
    downcase_hash_keys(result)
  else
    result
  end
rescue Docker::Error::NotFoundError => e
  raise Fog::Errors::NotFound.new(e.message)
rescue Docker::Error::TimeoutError => e
  raise Fog::Errors::TimeoutError.new(e.message)
rescue Docker::Error::UnauthorizedError => e
  raise Fog::Errors::Fogdocker::AuthenticationError.new(e.message)
rescue Docker::Error::DockerError => e
  raise Fog::Errors::Fogdocker::ServiceError.new(e.message)
end

#container_all(filters = {}) ⇒ Object

filter options all – true or false, Show all containers. Only running containers are shown by default limit – Show limit last created containers, include non-running ones. since – Show only containers created since Id, include non-running ones. before – Show only containers created before Id, include non-running ones. size – true or false, Show the containers sizes



11
12
13
14
15
# File 'lib/fog/fogdocker/requests/compute/container_all.rb', line 11

def container_all(filters = {})
  Docker::Container.all(filters.merge(:all => true), @connection).map do |container|
    downcase_hash_keys(container.json)
  end
end

#container_commit(options) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
# File 'lib/fog/fogdocker/requests/compute/container_commit.rb', line 5

def container_commit(options)
  raise ArgumentError, "instance id is a required parameter" unless options.key? :id
  container = Docker::Container.get(options[:id], {}, @connection)
  downcase_hash_keys container.commit(camelize_hash_keys(options)).json
end

#container_create(attrs) ⇒ Object



29
30
31
# File 'lib/fog/fogdocker/requests/compute/container_create.rb', line 29

def container_create(attrs)
  downcase_hash_keys Docker::Container.create(camelize_hash_keys(attrs), @connection).json
end

#container_delete(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
# File 'lib/fog/fogdocker/requests/compute/container_delete.rb', line 5

def container_delete(options = {})
  raise ArgumentError, "instance id is a required parameter" unless options.key? :id
  container = Docker::Container.get(options[:id], {}, @connection)
  container.delete()
  true
end

#container_get(id) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/fogdocker/requests/compute/container_get.rb', line 5

def container_get(id)
  raw_container = Docker::Container.get(id, {}, @connection).json
  processed_container = downcase_hash_keys(raw_container)
  processed_container['hostconfig_port_bindings'] = raw_container['HostConfig']['PortBindings']
  processed_container['hostconfig_links']         = raw_container['HostConfig']['Links']
  processed_container['config_exposed_ports']     = raw_container['Config']['ExposedPorts']
  processed_container
end

#downcase_hash_keys(hash, k = []) ⇒ Object



50
51
52
53
# File 'lib/fog/fogdocker/compute.rb', line 50

def downcase_hash_keys(hash, k = [])
  return {k.join('_').gsub(/([a-z])([A-Z])/,'\1_\2').downcase => hash} unless hash.is_a?(Hash)
  hash.reduce({}){ |h, v| h.merge! downcase_hash_keys(v[-1], k + [v[0]]) }
end

#image_all(filters = {}) ⇒ Object



5
6
7
8
9
# File 'lib/fog/fogdocker/requests/compute/image_all.rb', line 5

def image_all(filters = {})
  Docker::Image.all({}, @connection).map do |image|
    downcase_hash_keys(image.json)
  end
end

#image_create(attrs) ⇒ Object



5
6
7
# File 'lib/fog/fogdocker/requests/compute/image_create.rb', line 5

def image_create(attrs)
  downcase_hash_keys Docker::Image.create(attrs, nil, @connection).json
end

#image_delete(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
# File 'lib/fog/fogdocker/requests/compute/image_delete.rb', line 5

def image_delete(options = {})
  raise ArgumentError, "instance id is a required parameter" unless options.key? :id
  image = Docker::Image.get(options[:id], {}, @connection)
  image.remove()
end

#image_get(id) ⇒ Object



5
6
7
# File 'lib/fog/fogdocker/requests/compute/image_get.rb', line 5

def image_get(id)
  downcase_hash_keys Docker::Image.get(id, {}, @connection).json
end

#image_search(query = {}) ⇒ Object



5
6
7
8
9
# File 'lib/fog/fogdocker/requests/compute/image_search.rb', line 5

def image_search(query = {})
  Docker::Util.parse_json(@connection.get('/images/search', query)).map do |image|
    downcase_hash_keys(image)
  end
end