1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #2658 from abenari/master

Add docker support
This commit is contained in:
Ohad Levy 2014-02-23 17:48:07 +02:00
commit ab79fb8c58
32 changed files with 832 additions and 1 deletions

View file

@ -27,6 +27,7 @@ require 'fog/clodo'
require 'fog/digitalocean'
require 'fog/dnsimple'
require 'fog/dnsmadeeasy'
require 'fog/fogdocker'
require 'fog/dreamhost'
require 'fog/dynect'
require 'fog/ecloud'

View file

@ -65,6 +65,7 @@ require 'fog/bin/clodo'
require 'fog/bin/digitalocean'
require 'fog/bin/dnsimple'
require 'fog/bin/dnsmadeeasy'
require 'fog/bin/fogdocker'
require 'fog/bin/dreamhost'
require 'fog/bin/dynect'
require 'fog/bin/ecloud'

30
lib/fog/bin/fogdocker.rb Normal file
View file

@ -0,0 +1,30 @@
class Fogdocker < Fog::Bin
class << self
def class_for(key)
case key
when :compute
Fog::Compute::Fogdocker
else
raise ArgumentError, "Unrecognized service: #{key}"
end
end
def [](service)
@@connections ||= Hash.new do |hash, key|
hash[key] = case key
when :compute
Fog::Compute.new(:provider => 'Fogdocker')
else
raise ArgumentError, "Unrecognized service: #{key.inspect}"
end
end
@@connections[service]
end
def services
Fog::Fogdocker.services
end
end
end

1
lib/fog/fogdocker.rb Normal file
View file

@ -0,0 +1 @@
require 'fog/fogdocker/compute'

View file

@ -0,0 +1,55 @@
require 'fog/fogdocker/core'
module Fog
module Compute
class Fogdocker < Fog::Service
requires :docker_url
recognizes :docker_username, :docker_password, :docker_email
model_path 'fog/fogdocker/models/compute'
model :server
collection :servers
model :image
collection :images
request_path 'fog/fogdocker/requests/compute'
request :api_version
request :container_all
request :container_create
request :container_delete
request :container_get
request :container_action
request :image_all
request :image_create
request :image_delete
request :image_get
class Mock
def initialize(options={})
end
end
class Real
def initialize(options={})
require 'docker'
username = options[:docker_username]
password = options[:docker_password]
email = options[:docker_email]
url = options[:docker_url]
Docker.url = url
Docker.authenticate!('username' => username, 'password' => password, 'email' => email) unless username. nil? || username.empty?
end
def downcase_hash_keys(hash, k = [])
return {k.join('_').gsub(/([a-z])([A-Z])/,'\1_\2').downcase => hash} unless hash.is_a?(Hash)
hash.inject({}){ |h, v| h.merge! downcase_hash_keys(v[-1], k + [v[0]]) }
end
end
end
end
end

17
lib/fog/fogdocker/core.rb Normal file
View file

@ -0,0 +1,17 @@
require 'fog/core'
module Fog
module Fogdocker
extend Fog::Provider
module Errors
class ServiceError < Fog::Errors::Error; end
class SecurityError < ServiceError; end
class NotFound < ServiceError; end
end
service(:compute, 'Compute')
end
end

View file

@ -0,0 +1,37 @@
module Fog
module Compute
class Fogdocker
class Image < Fog::Model
identity :id
attr_accessor :info
attribute :repotags
attribute :created
attribute :size
attribute :virtual_size
def ready?
!(status =~ /down/i)
end
def destroy(options = {})
service.image_delete(id)
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
service.image_create(attributes)
end
def to_s
name
end
end
end
end
end

View file

@ -0,0 +1,23 @@
require 'fog/core/collection'
require 'fog/fogdocker/models/compute/image'
module Fog
module Compute
class Fogdocker
class Images < Fog::Collection
model Fog::Compute::Fogdocker::Image
def all(filters = {})
load service.image_all(filters)
end
def get(id)
new service.image_get(id)
end
end
end
end
end

View file

@ -0,0 +1,126 @@
require 'fog/compute/models/server'
module Fog
module Compute
class Fogdocker
# fog server is a docker container
class Server < Fog::Compute::Server
identity :id
attr_accessor :info
attribute :name
attribute :created
attribute :network_settings_ipaddress, :aliases => 'ipaddress'
attribute :network_settings_bridge, :aliases => 'bridge'
attribute :state_running
attribute :state_pid
attribute :config_cpu_shares, :aliases => 'cpus'
attribute :config_memory, :aliases => 'memory'
attribute :config_hostname, :aliases => 'hostname'
attribute :host
attribute :image
attribute :config_exposed_ports, :aliases => 'exposed_ports'
attribute :volumes
#raw = {"ID"=>"2ce79789656e4f7474624be6496dc6d988899af30d556574389a19aade2f9650",
# "Created"=>"2014-01-16T12:42:38.081665295Z",
# "Path"=>"/bin/bash",
# "Args"=>[],
# "Config"=>{
# "Hostname"=>"2ce79789656e",
# "Domainname"=>"",
# "User"=>"",
# "Memory"=>0,
# "MemorySwap"=>0,
# "CpuShares"=>0,
# "AttachStdin"=>true,
# "AttachStdout"=>true,
# "AttachStderr"=>true,
# "PortSpecs"=>nil,
# "ExposedPorts"=>{},
# "State"=>{
# "Running"=>true,
# "Pid"=>1505,
# "ExitCode"=>0,
# "StartedAt"=>"2014-01-16T15:50:36.304626413Z",
# "FinishedAt"=>"2014-01-16T15:50:36.238743161Z",
# "Ghost"=>false},
# "Image"=>"7c8cf65e1efa9b55f9ba8c60a970fe41595e56b894c7fdb19871bd9b276ca9d3",
# "NetworkSettings"=>{
# "IPAddress"=>"172.17.0.2",
# "IPPrefixLen"=>16,
# "Gateway"=>"172.17.42.1",
# "Bridge"=>"docker0",
# "PortMapping"=>nil,
# "Ports"=>{}},
# "SysInitPath"=>"/var/lib/docker/init/dockerinit-0.7.2",
# "ResolvConfPath"=>"/etc/resolv.conf",
# "HostnamePath"=>"/var/lib/docker/containers/2ce79789656e4f7474624be6496dc6d988899af30d556574389a19aade2f9650/hostname",
# "HostsPath"=>"/var/lib/docker/containers/2ce79789656e4f7474624be6496dc6d988899af30d556574389a19aade2f9650/hosts",
# "Name"=>"/boring_engelbart",
# "Driver"=>"devicemapper",
# "Volumes"=>{},
# "VolumesRW"=>{},
# "HostConfig"=>{
# "Binds"=>nil,
# "ContainerIDFile"=>"",
# "LxcConf"=>[],
# "Privileged"=>false,
# "PortBindings"=>{},
# "Links"=>nil,
# "PublishAllPorts"=>false}
# }
def ready?
state_running == true
end
def stopped?
state_running == false
end
def mac
# TODO
end
def start(options = {})
service.container_action(:id =>id, :action => :start)
reload
end
def stop(options = {})
action = options['force'] ? :kill : :stop
service.container_action(:id =>id, :action => action)
reload
end
def restart(options = {})
service.container_action(:id =>id, :action => :restart)
reload
end
def destroy(options = {})
service.container_action(:id =>id, :action => :kill)
service.container_delete(:id => id)
end
def save
if persisted?
service.container_update(attributes)
else
self.id = service.container_create(attributes)['id']
end
reload
end
def to_s
name
end
end
end
end
end

View file

@ -0,0 +1,28 @@
require 'fog/core/collection'
require 'fog/fogdocker/models/compute/server'
module Fog
module Compute
class Fogdocker
class Servers < Fog::Collection
model Fog::Compute::Fogdocker::Server
def all(filters = {})
load service.container_all(filters)
end
def get(id)
new service.container_get(id)
end
def bootstrap(new_attributes = {})
server = create(new_attributes)
server
end
end
end
end
end

View file

@ -0,0 +1,16 @@
module Fog
module Compute
class Fogdocker
class Real
def api_version
Docker.version
end
end
class Mock
def api_version
{'Version' => '1.6'}
end
end
end
end
end

View file

@ -0,0 +1,26 @@
module Fog
module Compute
class Fogdocker
class Real
def container_action(options = {})
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
raise ArgumentError, "action is a required parameter" unless options.has_key? :action
container = Docker::Container.get(options[:id])
container.send(options[:action], options[:id]).info
end
end
class Mock
def container_action(options = {})
raise ArgumentError, "id is a required parameter" unless options.has_key? :id
raise ArgumentError, "action is a required parameter" unless options.has_key? :action
{'id' => 'a6b02c7ca29a22619f7d0e59062323247739bc0cd375d619f305f0b519af4ef3','State' => {'Running' => false}}
end
end
end
end
end

View file

@ -0,0 +1,38 @@
module Fog
module Compute
class Fogdocker
class Real
# 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
def container_all(filters = {})
Docker::Container.all(filters.merge(:all => true)).map do |container|
downcase_hash_keys(container.info)
end
end
end
class Mock
def container_all(filters = {})
[
{'id' => '2ce79789656e4f7474624be6496dc6d988899af30d556574389a19aade2f9650',
'image' => 'mattdm/fedora:f19',
'command' => '/bin/bash',
'created' => '1389876158',
'status' => 'Up 45 hours',
'state_running' => true,
'ports' => nil,
'sizerw' => 0,
'sizerootfs' => 0,
'name' => '123123123',
'names' => ['/boring_engelbert']
}
]
end
end
end
end
end

View file

@ -0,0 +1,53 @@
module Fog
module Compute
class Fogdocker
# 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' => {}
#}
class Real
def container_create(attrs)
downcase_hash_keys Docker::Container.create(attrs).info
end
end
class Mock
def container_create(attrs)
{'id' => '2ce79789656e4f7474624be6496dc6d988899af30d556574389a19aade2f9650',
'image' => 'mattdm/fedora:f19',
'command' => '/bin/bash',
'created' => '1389876158',
'status' => 'Up 45 hours',
'state' => {'running' => 'true'},
'ports' => nil,
'sizerw' => 0,
'sizerootfs' => 0,
'name' => '123123123',
'names' => ['/boring_engelbert']
}
end
end
end
end
end

View file

@ -0,0 +1,24 @@
module Fog
module Compute
class Fogdocker
class Real
def container_delete(options = {})
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
container = Docker::Container.get(options[:id])
container.delete()
true
end
end
class Mock
def container_delete(options = {})
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
true
end
end
end
end
end

View file

@ -0,0 +1,31 @@
module Fog
module Compute
class Fogdocker
class Real
def container_get(id)
downcase_hash_keys Docker::Container.get(id).info
end
end
class Mock
def container_get(id)
{'id' => '2ce79789656e4f7474624be6496dc6d988899af30d556574389a19aade2f9650',
'image' => 'mattdm/fedora:f19',
'command' => '/bin/bash',
'created' => '1389876158',
'status' => 'Up 45 hours',
'state_running' => true,
'config_cpu_shares' => '1',
'network_settings_ipaddress' => '172.17.0.2',
'config_memory' => '1024',
'config_hostname' => '21341234',
'ports' => nil,
'sizerw' => 0,
'sizerootfs' => 0,
'name' => '123123123',
'names' => ['/boring_engelbert']}
end
end
end
end
end

View file

@ -0,0 +1,27 @@
module Fog
module Compute
class Fogdocker
class Real
def image_all(filters = {})
Docker::Image.all.map do |image|
downcase_hash_keys(image.info)
end
end
end
class Mock
def image_all(filters = {})
[
{'id'=>'a6b02c7ca29a22619f7d0e59062323247739bc0cd375d619f305f0b519af4ef2',
'repotags' => ['repo/one'],
'created' => 1389877693,
'size' => 3265536},
{'id'=>'a6b02c7ca29a22619f7d0e59062323247739bc0cd375d619f305f0b519af4ef3',
'repotags' => ['repo/other'],
'created' => 1389877693,
'size' => 3265536}
]
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Compute
class Fogdocker
class Real
def image_create(attrs)
downcase_hash_keys Docker::Image.create(attrs).info
end
end
class Mock
def image_create(attrs)
{'id'=>'a6b02c7ca29a22619f7d0e59062323247739bc0cd375d619f305f0b519af4ef2'}
end
end
end
end
end

View file

@ -0,0 +1,23 @@
module Fog
module Compute
class Fogdocker
class Real
def image_delete(options = {})
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
image = Docker::Image.get(options[:id])
image.remove()
end
end
class Mock
def image_delete(options = {})
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
true
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Fogdocker
class Real
def image_get(id)
downcase_hash_keys Docker::Image.get(id).info
end
end
class Mock
def image_get(id)
{'id'=>'a6b02c7ca29a22619f7d0e59062323247739bc0cd375d619f305f0b519af4ef3',
'repotags' => ['repo/other'],
'created' => 1389877693,
'size' => 3265536}
end
end
end
end
end

View file

@ -0,0 +1,17 @@
Shindo.tests('Fog::Compute[:fogdocker]', ['fogdocker']) do
compute = Fog::Compute[:fogdocker]
tests("Compute collections") do
%w{ servers images }.each do |collection|
test("it should respond to #{collection}") { compute.respond_to? collection }
end
end
tests("Compute requests") do
%w{ api_version container_all container_create container_delete container_get
container_action image_all image_create image_delete image_get }.each do |collection|
test("it should respond to #{collection}") { compute.respond_to? collection }
end
end
end

View file

@ -0,0 +1,31 @@
Shindo.tests('Fog::Compute[:fogdocker] | image model', ['fogdocker']) do
images = Fog::Compute[:fogdocker].images
image = images.last.reload
tests('The image model should') do
tests('have the action') do
test('reload') { image.respond_to? 'reload' }
end
tests('have attributes') do
model_attribute_hash = image.attributes
attributes = [ :id,
:repotags,
:created,
:size
]
tests("The image model should respond to") do
attributes.each do |attribute|
test("#{attribute}") { image.respond_to? attribute }
end
end
tests("The attributes hash should have key") do
(attributes-[:repotags]).each do |attribute|
test("#{attribute}") { model_attribute_hash.has_key? attribute }
end
end
end
test('be a kind of Fog::Compute::Fogdocker::image') { image.kind_of? Fog::Compute::Fogdocker::Image }
end
end

View file

@ -0,0 +1,9 @@
Shindo.tests('Fog::Compute[:fogdocker] | images collection', ['fogdocker']) do
images = Fog::Compute[:fogdocker].images
tests('The images collection') do
test('should be a kind of Fog::Compute::Fogdocker::images') { images.kind_of? Fog::Compute::Fogdocker::Images }
end
end

View file

@ -0,0 +1,46 @@
Shindo.tests('Fog::Compute[:fogdocker] | server model', ['fogdocker']) do
compute = Fog::Compute[:fogdocker]
server = compute.servers.create(:name => "fog-#{Time.now.to_i}", 'image' => 'mattdm/fedora:f19','Cmd' => ['date'])
tests('The server model should') do
tests('have the action') do
test('reload') { server.respond_to? 'reload' }
%w{ start restart stop destroy }.each do |action|
test(action) { server.respond_to? action }
end
%w{ start restart stop destroy}.each do |action|
test("#{action} returns successfully") {
server.send(action.to_sym) ? true : false
}
end
end
tests('have attributes') do
model_attribute_hash = server.attributes
attributes = [ :id,
:name,
:created,
:network_settings_ipaddress,
:state_running,
:config_cpu_shares,
:config_memory,
:config_hostname,
:image,
#:config => exposed_ports,
#:config => volumes
]
tests("The server model should respond to") do
attributes.each do |attribute|
test("#{attribute}") { server.respond_to? attribute }
end
end
tests("The attributes hash should have key") do
attributes.each do |attribute|
test("#{attribute}") { model_attribute_hash.has_key? attribute }
end
end
end
test('be a kind of Fog::Compute::Fogdocker::Server') { server.kind_of? Fog::Compute::Fogdocker::Server }
end
end

View file

@ -0,0 +1,14 @@
Shindo.tests('Fog::Compute[:fogdocker] | servers collection', ['fogdocker']) do
servers = Fog::Compute[:fogdocker].servers
tests('The servers collection') do
test('should not be empty') { not servers.empty? }
test('should be a kind of Fog::Compute::Fogdocker::Servers') { servers.kind_of? Fog::Compute::Fogdocker::Servers }
tests('should be able to reload itself').succeeds { servers.reload }
tests('should be able to get a model') do
tests('by instance uuid').succeeds { servers.get(servers.first.id) }
end
end
end

View file

@ -0,0 +1,10 @@
Shindo.tests('Fog::Compute[:fogdocker] | api_version request', ['fogdocker']) do
compute = Fog::Compute[:fogdocker]
tests('The response should') do
response = compute.api_version()
test('be a Hash') { response.kind_of? Hash}
end
end

View file

@ -0,0 +1,25 @@
Shindo.tests("Fog::Compute[:fogdocker] | container_action request", 'fogdocker') do
compute = Fog::Compute[:fogdocker]
name = "fog-#{Time.now.to_i}"
response = compute.container_create(:name => name, 'image' => 'mattdm/fedora:f19','Cmd' => ['date'] )
id = response['id']
tests("Run Container") do
response = compute.container_action(:id => id, :action => 'run' )
test("should be a kind of Hash") { response.kind_of? Hash}
end
tests("Stop Container") do
response = compute.container_action(:id => id, :action => 'stop' )
test("should be a kind of Hash") { response.kind_of? Hash}
test("should be stopped") { response['State']['Running'] == false}
end
tests("Kill Container") do
response = compute.container_action(:id => id, :action => 'kill' )
test("should be a kind of Hash") { response.kind_of? Hash}
end
end

View file

@ -0,0 +1,21 @@
Shindo.tests("Fog::Compute[:fogdocker] | container_create request", 'fogdocker') do
compute = Fog::Compute[:fogdocker]
name_base = Time.now.to_i
tests("Create Container") do
response = compute.container_create(:name => 'fog-'+name_base.to_s, 'image' => 'mattdm/fedora:f19','Cmd' => ['date'] )
test("should be a kind of Hash") { response.kind_of? Hash}
end
tests("Fail Creating Container") do
begin
response = compute.container_create(:name => 'fog-'+name_base.to_s, 'image' => 'mattdm/fedora:f19')
test("should be a kind of Hash") { response.kind_of? Hash} #mock never raise exceptions
rescue => e
#should raise missing command in the create attributes.
test("error should be a kind of Docker::Error") { e.kind_of? Docker::Error::ServerError}
end
end
end

View file

@ -0,0 +1,17 @@
Shindo.tests('Fog::Compute[:fogdocker] | container_delete request', ['fogdocker']) do
compute = Fog::Compute[:fogdocker]
container = compute.servers.create('name' => 'fog-'+Time.now.to_i.to_s,
'Image' => 'mattdm/fedora:f19',
'Cmd' => ['/bin/bash'])
tests('The response should') do
response = compute.container_delete(:id => container.id)
test('be a success') { response ? true: false }
end
tests('The expected options') do
raises(ArgumentError, 'raises ArgumentError when id option is missing') { compute.container_delete }
end
end

View file

@ -0,0 +1,21 @@
Shindo.tests("Fog::Compute[:fogdocker] | image_create request", 'fogdocker') do
compute = Fog::Compute[:fogdocker]
tests("Create image") do
response = compute.image_create({'fromImage' => 'mattdm/fedora', 'repo'=>'test', 'tag'=>'create_image'})
test("should be a kind of Hash") { response.kind_of? Hash}
test("should have an id") { !response['id'].nil? && !response['id'].empty? }
end
tests("Fail Creating image") do
begin
response = compute.image_create('fromImage' => 'not/exists')
test("should be a kind of Hash") { response.kind_of? Hash} #mock never raise exceptions
rescue => e
#should raise missing command in the create attributes.
test("error should be a kind of Docker::Error") { e.kind_of? Docker::Error::ServerError}
end
end
end

View file

@ -0,0 +1,20 @@
Shindo.tests("Fog::Compute[:fogdocker] | image_delete request", 'fogdocker') do
compute = Fog::Compute[:fogdocker]
image_hash = compute.image_create({'fromImage' => 'mattdm/fedora', 'repo'=>'test', 'tag'=>'delete_image'})
tests("Delete image") do
begin
response = compute.image_delete(:id => image_hash['id'])
test("should be success") { response ? true : false } #mock doesn't throw errors
rescue => e
#should raise image not found
test("error should be a kind of Docker::Error::NotFoundError") { e.kind_of? Docker::Error::NotFoundError}
end
end
tests('The expected options') do
raises(ArgumentError, 'raises ArgumentError when id option is missing') { compute.container_delete }
end
end

View file

@ -100,6 +100,10 @@ if Fog.mock?
:libvirt_username => 'root',
:libvirt_password => 'password',
:cloudsigma_username => 'csuname',
:cloudsigma_password => 'cspass'
:cloudsigma_password => 'cspass',
:docker_username => 'docker-fan',
:docker_password => 'i<3docker',
:docker_email => 'dockerfan@gmail.com',
:docker_url => 'unix://var/run/docker.sock'
}.merge(Fog.credentials)
end