1
|
require 'uri'
|
2
|
|
3
|
module ForemanDocker
|
4
|
class Docker < ::ComputeResource
|
5
|
|
6
|
validates :url, :format => { :with => URI.regexp }
|
7
|
|
8
|
def self.model_name
|
9
|
ComputeResource.model_name
|
10
|
end
|
11
|
|
12
|
def capabilities
|
13
|
[:image]
|
14
|
end
|
15
|
|
16
|
def supports_update?
|
17
|
false
|
18
|
end
|
19
|
|
20
|
def provided_attributes
|
21
|
super.merge({:mac => :mac})
|
22
|
end
|
23
|
|
24
|
|
25
|
def max_cpu_count
|
26
|
8
|
27
|
end
|
28
|
|
29
|
def max_memory
|
30
|
16*1024*1024*1024
|
31
|
end
|
32
|
|
33
|
def available_images
|
34
|
client.images
|
35
|
end
|
36
|
|
37
|
def image(id)
|
38
|
client.image.get(id) || raise(ActiveRecord::RecordNotFound)
|
39
|
end
|
40
|
|
41
|
def provider_friendly_name
|
42
|
"Docker"
|
43
|
end
|
44
|
|
45
|
def create_vm args = {}
|
46
|
args['cmd'] = Array.wrap( args.delete('cmd') )
|
47
|
options = vm_instance_defaults.merge(args)
|
48
|
logger.debug("creating Docker with the following options: #{options.inspect}")
|
49
|
client.servers.create options
|
50
|
rescue Fog::Errors::Error => e
|
51
|
logger.debug "Fog error: #{e.message}\n " + e.backtrace.join("\n ")
|
52
|
errors.add(:base, e.message.to_s)
|
53
|
false
|
54
|
end
|
55
|
|
56
|
def vm_instance_defaults
|
57
|
ActiveSupport::HashWithIndifferentAccess.new('name' => "foreman_#{Time.now.to_i}", 'cmd' => ['/bin/bash'])
|
58
|
end
|
59
|
|
60
|
protected
|
61
|
|
62
|
def bootstrap(args)
|
63
|
client.servers.bootstrap vm_instance_defaults.merge(args.to_hash)
|
64
|
rescue Fog::Errors::Error => e
|
65
|
errors.add(:base, e.to_s)
|
66
|
false
|
67
|
end
|
68
|
|
69
|
|
70
|
def client
|
71
|
@client ||= ::Fog::Compute.new(
|
72
|
:provider => "fogdocker",
|
73
|
:docker_username => user,
|
74
|
:docker_password => password,
|
75
|
:docker_url => url
|
76
|
)
|
77
|
end
|
78
|
|
79
|
def api_version
|
80
|
@api_version ||= client.send(:client).api_version
|
81
|
end
|
82
|
|
83
|
end
|
84
|
end
|