forked from sous-chefs/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_image.rb
189 lines (161 loc) · 5.3 KB
/
docker_image.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
module DockerCookbook
class DockerImage < DockerBase
resource_name :docker_image
# Modify the default of read_timeout from 60 to 120
property :read_timeout, default: 120, desired_state: false
# https://docs.docker.com/engine/api/v1.35/#tag/Image
property :destination, String
property :force, [TrueClass, FalseClass], default: false, desired_state: false
property :host, [String, nil], default: lazy { ENV['DOCKER_HOST'] }, desired_state: false
property :nocache, [TrueClass, FalseClass], default: false
property :noprune, [TrueClass, FalseClass], default: false
property :repo, String, name_property: true
property :rm, [TrueClass, FalseClass], default: true
property :source, String
property :tag, String, default: 'latest'
alias_method :image, :repo
alias_method :image_name, :repo
alias_method :no_cache, :nocache
alias_method :no_prune, :noprune
#########
# Actions
#########
default_action :pull
action :build do
converge_by "Build image #{image_identifier}" do
build_image
end
end
action :build_if_missing do
return if Docker::Image.exist?(image_identifier, {}, connection)
action_build
end
action :import do
return if Docker::Image.exist?(image_identifier, {}, connection)
converge_by "Import image #{image_identifier}" do
import_image
end
end
action :pull do
# We already did the work, but we need to report what we did!
converge_by "Pull image #{image_identifier}" do
end if pull_image
end
action :pull_if_missing do
return if Docker::Image.exist?(image_identifier, {}, connection)
action_pull
end
action :push do
converge_by "Push image #{image_identifier}" do
push_image
end
end
action :remove do
return unless Docker::Image.exist?(image_identifier, {}, connection)
converge_by "Remove image #{image_identifier}" do
remove_image
end
end
action :save do
converge_by "Save image #{image_identifier}" do
save_image
end
end
action :load do
converge_by "load image #{image_identifier}" do
load_image
end
end
declare_action_class.class_eval do
################
# Helper methods
################
def build_from_directory
i = Docker::Image.build_from_dir(
new_resource.source,
{
'nocache' => new_resource.nocache,
'rm' => new_resource.rm,
},
connection
)
i.tag('repo' => new_resource.repo, 'tag' => new_resource.tag, 'force' => new_resource.force)
end
def build_from_dockerfile
i = Docker::Image.build(
IO.read(new_resource.source),
{
'nocache' => new_resource.nocache,
'rm' => new_resource.rm,
},
connection
)
i.tag('repo' => new_resource.repo, 'tag' => new_resource.tag, 'force' => new_resource.force)
end
def build_from_tar
i = Docker::Image.build_from_tar(
::File.open(new_resource.source, 'r'),
{
'nocache' => new_resource.nocache,
'rm' => new_resource.rm,
},
connection
)
i.tag('repo' => new_resource.repo, 'tag' => new_resource.tag, 'force' => new_resource.force)
end
def build_image
if ::File.directory?(new_resource.source)
build_from_directory
elsif ::File.extname(new_resource.source) == '.tar'
build_from_tar
else
build_from_dockerfile
end
end
def image_identifier
"#{new_resource.repo}:#{new_resource.tag}"
end
def import_image
with_retries do
i = Docker::Image.import(new_resource.source, {}, connection)
i.tag('repo' => new_resource.repo, 'tag' => new_resource.tag, 'force' => new_resource.force)
end
end
def pull_image
with_retries do
creds = credentails
original_image = Docker::Image.get(image_identifier, {}, connection) if Docker::Image.exist?(image_identifier, {}, connection)
new_image = Docker::Image.create({ 'fromImage' => image_identifier }, creds, connection)
!(original_image && original_image.id.start_with?(new_image.id))
end
end
def push_image
with_retries do
creds = credentails
i = Docker::Image.get(image_identifier, {}, connection)
i.push(creds, repo_tag: image_identifier)
end
end
def remove_image
with_retries do
i = Docker::Image.get(image_identifier, {}, connection)
i.remove(force: new_resource.force, noprune: new_resource.noprune)
end
end
def save_image
with_retries do
Docker::Image.save(new_resource.repo, new_resource.destination, connection)
end
end
def load_image
with_retries do
Docker::Image.load(new_resource.source, {}, connection)
end
end
def credentails
registry_host = parse_registry_host(new_resource.repo)
node.run_state['docker_auth'] && node.run_state['docker_auth'][registry_host] || (node.run_state['docker_auth'] ||= {})['index.docker.io']
end
end
end
end