Class: Brick::Docker::DockerClient
Constant Summary
collapse
- @@default_client =
nil
- @@lock =
Monitor.new
- @@connection_pool =
Hash.new
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#create_config, #start_config
Constructor Details
#initialize(options = {}) ⇒ DockerClient
Returns a new instance of DockerClient.
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/brick/docker/docker_client.rb', line 45
def initialize(options={})
unless(options[:base_url].nil?)
@base_url = options[:base_url]
end
@base_url ||= 'unix:///var/run/docker.sock'
@connection= self.class.connection @base_url
puts "#{__method__} #{__LINE__} @connection=#{@connection}"
end
|
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
23
24
25
|
# File 'lib/brick/docker/docker_client.rb', line 23
def base_url
@base_url
end
|
#connection ⇒ Object
Returns the value of attribute connection.
23
24
25
|
# File 'lib/brick/docker/docker_client.rb', line 23
def connection
@connection
end
|
Class Method Details
.connection(base_url) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/brick/docker/docker_client.rb', line 29
def self.connection base_url
conn = nil
@@lock.synchronize do
conn ||= @@connection_pool[base_url.to_sym]
if(conn.nil?)
conn = ::Docker::Connection.new(base_url, {})
@@connection_pool[base_url.to_sym] = @connection
end
end
conn
end
|
.default ⇒ Object
60
61
62
63
64
|
# File 'lib/brick/docker/docker_client.rb', line 60
def self.default
@@default_client ||= DockerClient.new
return @@default_client
end
|
Instance Method Details
#build_from_dir(options = {}) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/brick/docker/docker_client.rb', line 132
def build_from_dir options={}
image_name = options[:image_name]
dockerfile_path = options[:build_dir]
project_dir = options[:project_dir]
no_cache = options[:no_cache]
dockerfile_path = determine_dockerfile_path(dockerfile_path, project_dir)
image = ::Docker::Image.build_from_dir(dockerfile_path, {"t"=>image_name, "nocache" =>no_cache }) {
|chunk| h1 = ::JSON.parse(chunk);
value = h1.values[0].to_s;
puts(::URI.unescape(value))
}
image
end
|
#create(config_hash, name = nil) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/brick/docker/docker_client.rb', line 66
def create config_hash, name=nil
docker_hash= create_config(config_hash)
docker_hash['name'] = name unless name.nil?
begin
container = ::Docker::Container.create(docker_hash.dup, connection)
container = ::Docker::Container.get(container.id,connection)
rescue ::Docker::Error::NotFoundError => exception
if exception.message.include? 'No such image'
::Docker::Image.create({'fromImage'=> config_hash['image']},{}, connection)
container = ::Docker::Container.create(docker_hash.dup, connection)
container = ::Docker::Container.get(container.id,connection)
else
raise exception
end
return container
end
end
|
#get_container_by_id(id) ⇒ Object
127
128
129
|
# File 'lib/brick/docker/docker_client.rb', line 127
def get_container_by_id id
::Docker::Container.get(id,connection)
end
|
#get_container_by_name(name = nil) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/brick/docker/docker_client.rb', line 115
def get_container_by_name name=nil
container = nil
unless name.nil?
container = ::Docker::Container.search_by_name name, connection
end
container.first
end
|
#run(config_hash, name = nil) ⇒ Object
if the container is already existed, reuse it if the container is not started, start it
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/brick/docker/docker_client.rb', line 92
def run config_hash, name=nil
container = get_container_by_name name
if container.nil?
container = create config_hash, name
else
Brick::CLI::logger.info "container #{name} has already existed."
end
Brick::CLI::logger.debug "container #{container}."
unless container.is_running?
container.start(start_config(config_hash))
else
Brick::CLI::logger.info "container #{name} is #{container.info["Status"]}"
end
container
end
|