Class: Superhosting::DockerApi
Constant Summary
collapse
- AVAILABLE_DOCKER_OPTIONS =
[:user, :cpu_period, :cpu_quota, :cpu_shares, :memory, :memory_swap]
Instance Method Summary
collapse
#__debug, #__dry_run, #__dry_run=, #__logger, #__logger=, #debug, #debug_block, #debug_operation, #indent, #indent=, #indent_reset, #indent_step, #indent_step_back, #info, #storage, #t, #with_dry_run, #with_indent, #with_logger
#_command, #_command_without_debug, #command, #command!
Constructor Details
#initialize(**kwargs) ⇒ DockerApi
Returns a new instance of DockerApi.
8
9
10
|
# File 'lib/superhosting/docker_api.rb', line 8
def initialize(**kwargs)
@socket = kwargs[:socket] || '/var/run/docker.sock'
end
|
Instance Method Details
#container_dead?(name) ⇒ Boolean
146
147
148
|
# File 'lib/superhosting/docker_api.rb', line 146
def container_dead?(name)
container_status?(name, 'dead')
end
|
#container_exists?(name) ⇒ Boolean
154
155
156
157
158
159
|
# File 'lib/superhosting/docker_api.rb', line 154
def container_exists?(name)
self.with_dry_run do |dry_run|
return true if dry_run and self.storage.key? name
container_info(name).nil? ? false : true
end
end
|
#container_exited?(name) ⇒ Boolean
142
143
144
|
# File 'lib/superhosting/docker_api.rb', line 142
def container_exited?(name)
container_status?(name, 'exited')
end
|
#container_image?(name, image) ⇒ Boolean
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/superhosting/docker_api.rb', line 161
def container_image?(name, image)
container = container_info(name)
image = image_info(image)
if container.nil? or image.nil?
false
else
container['Image'] == image['Id']
end
end
|
#container_info(name) ⇒ Object
24
25
26
|
# File 'lib/superhosting/docker_api.rb', line 24
def container_info(name)
resp_if_success raw_connection.request(method: :get, path: "/containers/#{name}/json")
end
|
#container_kill!(name) ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/superhosting/docker_api.rb', line 32
def container_kill!(name)
self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
self.with_dry_run do |dry_run|
self.storage.delete(name) if dry_run
resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/kill") unless dry_run
end
blk.call(code: :killed)
end
end
|
#container_list ⇒ Object
28
29
30
|
# File 'lib/superhosting/docker_api.rb', line 28
def container_list
resp_if_success raw_connection.request(method: :get, path: '/containers/json')
end
|
#container_not_exists?(name) ⇒ Boolean
150
151
152
|
# File 'lib/superhosting/docker_api.rb', line 150
def container_not_exists?(name)
!container_exists?(name)
end
|
#container_not_running?(name) ⇒ Boolean
130
131
132
|
# File 'lib/superhosting/docker_api.rb', line 130
def container_not_running?(name)
!container_running?(name)
end
|
#container_pause!(name) ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/superhosting/docker_api.rb', line 72
def container_pause!(name)
self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
self.with_dry_run do |dry_run|
self.storage[name] = 'paused' if dry_run
resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/pause") unless dry_run
end
blk.call(code: :paused)
end
end
|
#container_paused?(name) ⇒ Boolean
138
139
140
|
# File 'lib/superhosting/docker_api.rb', line 138
def container_paused?(name)
container_status?(name, 'paused')
end
|
#container_restart!(name) ⇒ Object
92
93
94
95
96
97
98
99
100
|
# File 'lib/superhosting/docker_api.rb', line 92
def container_restart!(name)
self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
self.with_dry_run do |dry_run|
resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/restart") unless dry_run
self.storage[name] = 'running' if dry_run
end
blk.call(code: :restarted)
end
end
|
#container_restarting?(name) ⇒ Boolean
134
135
136
|
# File 'lib/superhosting/docker_api.rb', line 134
def container_restarting?(name)
container_status?(name, 'restarting')
end
|
#container_rm!(name) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/superhosting/docker_api.rb', line 42
def container_rm!(name)
self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
self.with_dry_run do |dry_run|
self.storage.delete(name) if dry_run
resp_if_success raw_connection.request(method: :delete, path: "/containers/#{name}") unless dry_run
end
blk.call(code: :removed)
end
end
|
#container_rm_inactive!(name) ⇒ Object
102
103
104
|
# File 'lib/superhosting/docker_api.rb', line 102
def container_rm_inactive!(name)
self.container_rm!(name) if self.container_exists?(name) and !self.container_running?(name)
end
|
#container_run(name, options, image, command) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/superhosting/docker_api.rb', line 172
def container_run(name, options, image, command)
cmd = "docker run --detach --name #{name} #{options.join(' ')} #{image} #{command}"
self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
self.with_dry_run do |dry_run|
self.storage[name] = 'running' if dry_run
end
self.command!(cmd).tap do
blk.call(code: :added)
end
end
end
|
#container_running?(name) ⇒ Boolean
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/superhosting/docker_api.rb', line 118
def container_running?(name)
self.with_dry_run do |dry_run|
return true if dry_run and self.storage[name] == 'running'
resp = container_info(name)
if resp.nil?
false
else
resp['State']['Running'] and %w(Restarting Paused OOMKilled Dead).all? {|c| !resp['State'][c] }
end
end
end
|
#container_start!(name) ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/superhosting/docker_api.rb', line 62
def container_start!(name)
self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
self.with_dry_run do |dry_run|
self.storage[name] = 'running' if dry_run
resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/start") unless dry_run
end
blk.call(code: :started)
end
end
|
#container_status?(name, status) ⇒ Boolean
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/superhosting/docker_api.rb', line 106
def container_status?(name, status)
self.with_dry_run do |dry_run|
return true if dry_run and self.storage[name] == status
resp = container_info(name)
if resp.nil?
false
else
resp['State'][status.capitalize]
end
end
end
|
#container_stop!(name) ⇒ Object
52
53
54
55
56
57
58
59
60
|
# File 'lib/superhosting/docker_api.rb', line 52
def container_stop!(name)
self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
self.with_dry_run do |dry_run|
self.storage[name] = 'exited' if dry_run
resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/stop") unless dry_run
end
blk.call(code: :stopped)
end
end
|
#container_unpause!(name) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/superhosting/docker_api.rb', line 82
def container_unpause!(name)
self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
self.with_dry_run do |dry_run|
self.storage[name] = 'running' if dry_run
resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/unpause") unless dry_run
end
blk.call(code: :unpaused)
end
end
|
#grab_container_options(command_options) ⇒ Object
185
186
187
188
189
190
191
192
193
|
# File 'lib/superhosting/docker_api.rb', line 185
def grab_container_options(command_options)
options = []
AVAILABLE_DOCKER_OPTIONS.map do |k|
unless (value = command_options[k]).nil?
value.lines.each {|val| options << "--#{k.to_s.sub('_', '-')} #{val}" }
end
end
options
end
|
#image_info(name) ⇒ Object
20
21
22
|
# File 'lib/superhosting/docker_api.rb', line 20
def image_info(name)
resp_if_success raw_connection.request(method: :get, path: "/images/#{name}/json")
end
|
#raw_connection ⇒ Object
12
13
14
|
# File 'lib/superhosting/docker_api.rb', line 12
def raw_connection
Excon.new('unix:///', socket: @socket)
end
|
#resp_if_success(resp) ⇒ Object
16
17
18
|
# File 'lib/superhosting/docker_api.rb', line 16
def resp_if_success(resp)
JSON.load(resp.body) if resp.status == 200
end
|