-
Notifications
You must be signed in to change notification settings - Fork 7
/
main_dialog.rb
354 lines (302 loc) · 9.13 KB
/
main_dialog.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Copyright (c) 2014 SUSE LLC.
# All Rights Reserved.
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 or 3 of the GNU General
# Public License as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact SUSE LLC.
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.suse.com
require "yast"
require "docker"
require "y2storage"
require "ydocker/changes_dialog"
require "ydocker/commit_dialog"
require "ydocker/run_image_dialog"
require "ydocker/inject_shell_dialog"
module YDocker
class MainDialog
include Yast::UIShortcuts
include Yast::I18n
include Yast::Logger
def self.run
Yast.import "UI"
Yast.import "Popup"
Yast.import "Service"
dialog = new
dialog.run
end
def initialize
textdomain "docker"
end
def run
return unless create_dialog
begin
controller_loop
ensure
close_dialog
end
end
private
DEFAULT_SIZE_OPT = Yast::Term.new(:opt, :defaultsize)
def create_dialog
return unless ensure_docker_run
Yast::UI.OpenDialog DEFAULT_SIZE_OPT, dialog_content
update_images_buttons
@current_tab = :images
end
def ensure_docker_run
return true if Yast::Service.active?("docker")
# Only root can start process
if Process::UID.rid == 0
return false unless Yast::Popup.YesNo(
_("Docker service does not run. Should YaST start docker? Otherwise YaST quits.")
)
Yast::Service.start("docker")
else
Yast::Popup.Error(_("Docker service does not run. \
Run this module as root or start docker service manually."))
false
end
end
def close_dialog
Yast::UI.CloseDialog
end
def controller_loop
loop do
input = Yast::UI.UserInput
handle_docker_exceptions do
case input
when :ok, :cancel
return :ok
when :container_stop
stop_container
when :container_kill
kill_container
when :containers_redraw
redraw_containers
when :images_redraw
redraw_images
when :container_inject
InjectShellDialog.new(selected_container).run
when :container_changes
ChangesDialog.new(selected_container).run
when :container_commit
CommitDialog.new(selected_container).run
when :images
switch_to_images
when :containers
switch_to_containers
when :image_delete
image_delete
when :images_table
update_images_buttons
when :image_run
RunImageDialog.new(selected_image[0]).run
else
raise "Unknown action #{input}"
end
end
end
end
def switch_to_images
Yast::UI::ReplaceWidget(:tabContent, images_page)
update_images_buttons
@current_tab = :images
end
def switch_to_containers
Yast::UI::ReplaceWidget(:tabContent, containers_page)
update_containers_buttons
@current_tab = :containers
end
def handle_docker_exceptions
yield
rescue Docker::Error::DockerError => e
log.error "Docker exception #{e.inspect}"
Yast::Popup.Error(
_("Communication with docker failed with error: %s. Please try again.") % e.to_s
)
@current_tab == :images ? redraw_images : redraw_containers
end
def selected_container
selected = Yast::UI.QueryWidget(:containers_table, :SelectedItems)
selected = selected.first if selected.is_a? Array
Docker::Container.get(selected)
end
def stop_container
return unless Yast::Popup.YesNo(_("Do you really want to stop the running container?"))
selected_container.stop!
return unless Yast::Popup.YesNo(_("Do you want to remove the container?"))
selected_container.delete
redraw_containers
end
def kill_container
return unless Yast::Popup.YesNo(_("Do you really want to kill the running container?"))
selected_container.kill!
return unless Yast::Popup.YesNo(_("Do you want to remove the container?"))
selected_container.delete
redraw_containers
end
def dialog_content
VBox(
DumbTab(
[
Item(Id(:images), _("&Images"), true),
Item(Id(:containers), _("&Containers"))
],
ReplacePoint(Id(:tabContent), images_page)
),
ending_buttons
)
end
def images_page
VBox(
Heading(_("Docker Images")),
HBox(
images_table,
action_buttons_images
)
)
end
def containers_page
VBox(
Heading(_("Running Docker Containers")),
HBox(
containers_table,
action_buttons_containers
)
)
end
def redraw_containers
Yast::UI.ChangeWidget(:containers_table, :Items, containers_items)
update_containers_buttons
end
def redraw_images
Yast::UI.ChangeWidget(:images_table, :Items, images_items)
update_images_buttons
end
def images_table
Table(
Id(:images_table),
Opt(:notify),
Header(
_("Repository"),
_("Tag"),
_("Image ID"),
_("Created"),
_("Virtual Size")
),
images_items
)
end
def containers_table
Table(
Id(:containers_table),
Header(
_("Container ID"),
_("Image"),
_("Command"),
_("Created"),
_("Status"),
_("Ports")
),
containers_items
)
end
def containers_items
containers = Docker::Container.all
containers.map do |container|
Item(
Id(container.id),
container.id.slice(0, 12),
container.info["Image"],
container.info["Command"],
DateTime.strptime(container.info["Created"].to_s, "%s").to_s,
container.info["Status"],
container.info["Ports"].map do |p|
"#{p["IP"]}:#{p["PublicPort"]}->#{p["PrivatePort"]}/#{p["Type"]}"
end.join(",")
)
end
end
def images_items
images = Docker::Image.all
ret = []
images.map do |image|
repotags = image.info["RepoTags"]
repotags = [":<none>"] if repotags.nil?
repotags.each do |repotag|
repository, tag = repotag.split(":", 2)
ret << Item(
Id(id: image.id, label: repotag),
repository,
tag,
image.id.slice(0, 12),
DateTime.strptime(image.info["Created"].to_s, "%s").to_s,
Y2Storage::DiskSize.new(image.info["Size"]).to_human_string
)
end
end
ret
end
def action_button(id, title)
Left(PushButton(Id(id), Opt(:hstretch), title))
end
def action_buttons_images
HSquash(
VBox(
action_button(:images_redraw, _("Re&fresh")),
action_button(:image_run, _("R&un")),
action_button(:image_delete, _("&Delete"))
)
)
end
def action_buttons_containers
HSquash(
VBox(
action_button(:containers_redraw, _("Re&fresh")),
action_button(:container_changes, _("S&how Changes")),
action_button(:container_inject, _("Inject &Terminal")),
action_button(:container_stop, _("&Stop Container")),
action_button(:container_kill, _("&Kill Container")),
action_button(:container_commit, _("&Commit"))
)
)
end
def ending_buttons
PushButton(Id(:ok), _("&Exit"))
end
def selected_image
selected = Yast::UI.QueryWidget(:images_table, :SelectedItems)
selected = selected.first if selected.is_a? Array
[Docker::Image.get(selected[:id]), selected[:label]]
end
def image_delete
image, label = selected_image
return unless Yast::Popup.YesNo(_("Do you really want to delete image \"%s\"?") % label)
if label == "<none>:<none>"
image.remove
else
image.connection.delete("/images/#{label}", {})
end
redraw_images
end
def update_images_buttons
is_something_selected = !Yast::UI.QueryWidget(:images_table, :SelectedItems).empty?
[:image_run, :image_delete].each do |item|
Yast::UI.ChangeWidget(item, :Enabled, is_something_selected)
end
end
def update_containers_buttons
is_something_selected = !Yast::UI.QueryWidget(:containers_table, :SelectedItems).empty?
[:container_inject, :container_changes, :container_stop, :container_kill,
:container_commit].each do |item|
Yast::UI.ChangeWidget(item, :Enabled, is_something_selected)
end
end
end
end