2
2
require 'json'
3
3
require 'uri'
4
4
require 'securerandom'
5
+ require 'centurion/ssh'
5
6
6
7
module Centurion ; end
7
8
8
9
class Centurion ::DockerViaApi
9
- def initialize ( hostname , port , tls_args = { } , api_version = nil )
10
- @tls_args = default_tls_args ( tls_args [ :tls ] ) . merge ( tls_args . reject { |k , v | v . nil? } ) # Required by tls_enable?
11
- @base_uri = "http#{ 's' if tls_enable? } ://#{ hostname } :#{ port } "
10
+ def initialize ( hostname , port , connection_opts = { } , api_version = nil )
11
+ @tls_args = default_tls_args ( connection_opts [ :tls ] ) . merge ( connection_opts . reject { |k , v | v . nil? } ) # Required by tls_enable?
12
+ if connection_opts [ :ssh ]
13
+ @base_uri = hostname
14
+ @ssh = true
15
+ @ssh_user = connection_opts [ :ssh_user ]
16
+ @ssh_log_level = connection_opts [ :ssh_log_level ]
17
+ else
18
+ @base_uri = "http#{ 's' if tls_enable? } ://#{ hostname } :#{ port } "
19
+ end
12
20
api_version ||= "/v1.12"
13
21
@docker_api_version = api_version
14
22
configure_excon_globally
@@ -17,7 +25,7 @@ def initialize(hostname, port, tls_args = {}, api_version = nil)
17
25
def ps ( options = { } )
18
26
path = @docker_api_version + "/containers/json"
19
27
path += "?all=1" if options [ :all ]
20
- response = Excon . get ( @base_uri + path , tls_excon_arguments )
28
+ response = with_excon { | e | e . get ( path : path ) }
21
29
22
30
raise unless response . status == 200
23
31
JSON . load ( response . body )
@@ -27,61 +35,64 @@ def inspect_image(image, tag = "latest")
27
35
repository = "#{ image } :#{ tag } "
28
36
path = @docker_api_version + "/images/#{ repository } /json"
29
37
30
- response = Excon . get (
31
- @base_uri + path ,
32
- tls_excon_arguments . merge ( headers : { 'Accept' => 'application/json' } )
33
- )
38
+ response = with_excon do |e |
39
+ e . get (
40
+ path : path ,
41
+ headers : { 'Accept' => 'application/json' }
42
+ )
43
+ end
34
44
raise response . inspect unless response . status == 200
35
45
JSON . load ( response . body )
36
46
end
37
47
38
48
def remove_container ( container_id )
39
49
path = @docker_api_version + "/containers/#{ container_id } "
40
- response = Excon . delete (
41
- @base_uri + path ,
42
- tls_excon_arguments
43
- )
50
+ response = with_excon do |e |
51
+ e . delete (
52
+ path : path ,
53
+ )
54
+ end
44
55
raise response . inspect unless response . status == 204
45
56
true
46
57
end
47
58
48
59
def stop_container ( container_id , timeout = 30 )
49
60
path = @docker_api_version + "/containers/#{ container_id } /stop?t=#{ timeout } "
50
- response = Excon . post (
51
- @base_uri + path ,
52
- tls_excon_arguments . merge (
61
+ response = with_excon do | e |
62
+ e . post (
63
+ path : path ,
53
64
# Wait for both the docker stop timeout AND the kill AND
54
65
# potentially a very slow HTTP server.
55
66
read_timeout : timeout + 120
56
67
)
57
- )
68
+ end
58
69
raise response . inspect unless response . status == 204
59
70
true
60
71
end
61
72
62
73
def create_container ( configuration , name = nil )
63
74
path = @docker_api_version + "/containers/create"
64
- response = Excon . post (
65
- @base_uri + path ,
66
- tls_excon_arguments . merge (
67
- query : name ? { name : " #{ name } -#{ SecureRandom . hex ( 7 ) } "} : nil ,
75
+ response = with_excon do | e |
76
+ e . post (
77
+ path : path ,
78
+ query : name ? "name= #{ name } -#{ SecureRandom . hex ( 7 ) } " : nil ,
68
79
body : configuration . to_json ,
69
80
headers : { "Content-Type" => "application/json" }
70
81
)
71
- )
82
+ end
72
83
raise response . inspect unless response . status == 201
73
84
JSON . load ( response . body )
74
85
end
75
86
76
87
def start_container ( container_id , configuration )
77
88
path = @docker_api_version + "/containers/#{ container_id } /start"
78
- response = Excon . post (
79
- @base_uri + path ,
80
- tls_excon_arguments . merge (
89
+ response = with_excon do | e |
90
+ e . post (
91
+ path : path ,
81
92
body : configuration . to_json ,
82
93
headers : { "Content-Type" => "application/json" }
83
94
)
84
- )
95
+ end
85
96
case response . status
86
97
when 204
87
98
true
@@ -94,14 +105,14 @@ def start_container(container_id, configuration)
94
105
95
106
def restart_container ( container_id , timeout = 30 )
96
107
path = @docker_api_version + "/containers/#{ container_id } /restart?t=#{ timeout } "
97
- response = Excon . post (
98
- @base_uri + path ,
99
- tls_excon_arguments . merge (
108
+ response = with_excon do | e |
109
+ e . post (
110
+ path : path ,
100
111
# Wait for both the docker stop timeout AND the kill AND
101
112
# potentially a very slow HTTP server.
102
113
read_timeout : timeout + 120
103
114
)
104
- )
115
+ end
105
116
case response . status
106
117
when 204
107
118
true
@@ -116,10 +127,11 @@ def restart_container(container_id, timeout = 30)
116
127
117
128
def inspect_container ( container_id )
118
129
path = @docker_api_version + "/containers/#{ container_id } /json"
119
- response = Excon . get (
120
- @base_uri + path ,
121
- tls_excon_arguments
122
- )
130
+ response = with_excon do |e |
131
+ e . get (
132
+ path : path ,
133
+ )
134
+ end
123
135
raise response . inspect unless response . status == 200
124
136
JSON . load ( response . body )
125
137
end
@@ -172,4 +184,19 @@ def default_tls_args(tls_enabled)
172
184
{ }
173
185
end
174
186
end
187
+
188
+ def with_excon ( &block )
189
+ if @ssh
190
+ with_excon_via_ssh ( &block )
191
+ else
192
+ yield Excon . new ( @base_uri , tls_excon_arguments )
193
+ end
194
+ end
195
+
196
+ def with_excon_via_ssh
197
+ Centurion ::SSH . with_docker_socket ( @base_uri , @ssh_user , @ssh_log_level ) do |socket |
198
+ conn = Excon . new ( 'unix:///' , socket : socket )
199
+ yield conn
200
+ end
201
+ end
175
202
end
0 commit comments