-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqubes-template-upgrade-gui
283 lines (233 loc) · 9.99 KB
/
qubes-template-upgrade-gui
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
#!/usr/bin/env python3
#
# GUI for Qubes OS Template Upgrade Script
# Supports Fedora and Debian Templates
# https://www.kennethrrosen.cloud
#
# Copyright (C) 2024 by Kenneth R. Rosen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License;
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import gi
import re
import subprocess
import threading
import os
import signal
import qubesadmin
from qubes_config.widgets.gtk_utils import load_icon
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib, GdkPixbuf
class QubesTemplateUpgradeGUI(Gtk.Window):
def __init__(self):
super().__init__(title="Qubes Template Upgrade")
self.set_border_width(10)
self.set_default_size(500, 500)
self.set_position(Gtk.WindowPosition.CENTER)
self.process = None
self.selected_template = None
main_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(main_vbox)
header = Gtk.Label()
header.set_markup("<big><b>Qubes Template Upgrade</b></big>")
main_vbox.pack_start(header, False, False, 10)
templates_frame = Gtk.Frame(label="Available Templates and StandaloneVMs")
main_vbox.pack_start(templates_frame, True, True, 10)
self.templates_liststore = Gtk.ListStore(GdkPixbuf.Pixbuf, str)
self.templates_treeview = Gtk.TreeView(model=self.templates_liststore)
self.templates_treeview.connect(
"row-activated", self.on_template_double_clicked
)
renderer_pixbuf = Gtk.CellRendererPixbuf()
column_pixbuf = Gtk.TreeViewColumn("", renderer_pixbuf, pixbuf=0)
self.templates_treeview.append_column(column_pixbuf)
renderer_text = Gtk.CellRendererText()
column_text = Gtk.TreeViewColumn("Template", renderer_text, text=1)
self.templates_treeview.append_column(column_text)
templates_scrolled_window = Gtk.ScrolledWindow()
templates_scrolled_window.set_policy(
Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC
)
templates_scrolled_window.add(self.templates_treeview)
templates_frame.add(templates_scrolled_window)
self.populate_templates()
clone_hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
main_vbox.pack_start(clone_hbox, False, False, 0)
self.clone_checkbox = Gtk.CheckButton(label="Clone and Rename Template")
self.clone_checkbox.connect("toggled", self.on_clone_checkbox_toggled)
clone_hbox.pack_start(self.clone_checkbox, False, False, 0)
self.clone_entry = Gtk.Entry()
self.clone_entry.set_sensitive(False)
clone_hbox.pack_start(self.clone_entry, True, True, 0)
button_hbox = Gtk.Box(spacing=6)
main_vbox.pack_start(button_hbox, False, False, 0)
self.start_button = Gtk.Button(label="Start Upgrade")
self.start_button.connect("clicked", self.on_start_button_clicked)
self.start_button.set_sensitive(False)
button_hbox.pack_start(self.start_button, True, True, 0)
self.close_button = Gtk.Button(label="Close")
self.close_button.connect("clicked", self.on_close_button_clicked)
button_hbox.pack_start(self.close_button, True, True, 0)
self.progressbar = Gtk.ProgressBar()
main_vbox.pack_start(self.progressbar, False, False, 10)
log_frame = Gtk.Frame(label="Output Log")
main_vbox.pack_start(log_frame, True, True, 0)
self.log_textview = Gtk.TextView()
self.log_textview.set_editable(False)
self.log_textbuffer = self.log_textview.get_buffer()
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
scrolled_window.add(self.log_textview)
log_frame.add(scrolled_window)
self.connect("delete-event", self.confirm_exit)
def confirm_exit(self, widget, event):
dialog = Gtk.MessageDialog(
transient_for=self,
flags=0,
message_type=Gtk.MessageType.QUESTION,
buttons=Gtk.ButtonsType.YES_NO,
text="Are you sure you want to exit?",
)
dialog.format_secondary_text(
"An upgrade process is running. Exiting now may interrupt the upgrade."
)
response = dialog.run()
dialog.destroy()
if response == Gtk.ResponseType.YES:
self.on_close_button_clicked(widget)
return True # Prevents the window from closing
def on_close_button_clicked(self, widget):
if self.process and self.process.poll() is None:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
subprocess.run(["qvm-shutdown", self.selected_template])
Gtk.main_quit()
def populate_templates(self):
try:
app = qubesadmin.Qubes() # Create a Qubes admin object
for vm in app.domains:
if vm.klass in ["TemplateVM", "StandaloneVM"]:
self.add_vm_item(vm)
except Exception as e:
print("Error fetching Qubes VMs:", e)
def add_vm_item(self, vm):
pixbuf = self.get_qube_icon(vm)
self.templates_liststore.append([pixbuf, vm.name])
def get_qube_icon(self, vm):
label_name = vm.label.name
icon = load_icon(vm.label.icon, 16, 16)
return icon
def on_template_double_clicked(self, treeview, path, column):
model = treeview.get_model()
iter = model.get_iter(path)
template_name = model.get_value(iter, 1)
self.selected_template = template_name
self.clone_checkbox.set_sensitive(True)
self.start_button.set_sensitive(True)
def on_clone_checkbox_toggled(self, widget):
if self.clone_checkbox.get_active():
self.clone_entry.set_sensitive(True)
else:
self.clone_entry.set_sensitive(False)
def on_start_button_clicked(self, widget):
self.start_button.set_sensitive(False)
self.progressbar.set_fraction(0.0)
self.log_textbuffer.set_text("")
if self.selected_template:
if self.clone_checkbox.get_active():
new_template = self.clone_entry.get_text()
threading.Thread(
target=self.run_upgrade, args=(self.selected_template, new_template)
).start()
else:
threading.Thread(
target=self.run_upgrade, args=(self.selected_template, None)
).start()
def run_upgrade(self, template, new_template):
if new_template:
self.process = subprocess.Popen(
[
"./qvm-template-upgrade",
template,
"--clone",
"--new-template",
new_template,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
preexec_fn=os.setsid,
)
else:
self.process = subprocess.Popen(
["./qvm-template-upgrade", template],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
preexec_fn=os.setsid,
)
GLib.io_add_watch(
self.process.stdout, GLib.IO_IN | GLib.IO_HUP, self.on_stdout_ready
)
GLib.io_add_watch(
self.process.stderr, GLib.IO_IN | GLib.IO_HUP, self.on_stderr_ready
)
threading.Thread(target=self.monitor_process, daemon=True).start()
def monitor_process(self):
self.process.wait()
GLib.idle_add(self.on_upgrade_finished)
def on_stdout_ready(self, source, condition):
if condition == GLib.IO_IN:
line = source.readline()
if line:
GLib.idle_add(self.update_log, line.strip())
GLib.idle_add(self.update_progress, line.strip())
return True
return False
def on_stderr_ready(self, source, condition):
if condition == GLib.IO_IN:
line = source.readline()
if line:
GLib.idle_add(self.update_log, line.strip())
GLib.idle_add(self.update_progress, line.strip())
return True
return False
def update_log(self, output):
ansi_escape = re.compile(r"(\x1B[@-_][0-?]*[ -/]*[@-~]|[\x80-\xFF])")
output_clean = ansi_escape.sub("", output)
end_iter = self.log_textbuffer.get_end_iter()
self.log_textbuffer.insert(end_iter, output_clean + "\n")
self.progressbar.pulse()
self.log_textview.scroll_to_iter(end_iter, 0.0, True, 0.0, 1.0)
return False
def update_progress(self, output):
package_progress_regex = re.compile(r"\((\d+)/(\d+)\)")
match = package_progress_regex.search(output)
if match:
current, total = map(int, match.groups())
if total > 0:
fraction = current / total
self.progressbar.set_fraction(fraction)
return False
def on_upgrade_finished(self):
self.start_button.set_sensitive(True)
self.progressbar.set_fraction(1.0)
def on_close_button_clicked(self, widget):
if self.process and self.process.poll() is None:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
subprocess.run(["qvm-shutdown", self.selected_template])
Gtk.main_quit()
win = QubesTemplateUpgradeGUI()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()