-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·210 lines (160 loc) · 5.7 KB
/
main.py
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
#!/usr/bin/env python3
from gi.repository import Gtk, GLib, Gio, Gdk
from http.client import HTTPSConnection
from base64 import b64encode
import json
import confwindow
import conn
import params
import infowindow
class GaneAdmin(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Ganeti Admin")
self.set_border_width(10)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_default_size(500,500)
self.set_wmclass("Ganeti Admin", "Ganeti Admin")
self.connect("delete-event", Gtk.main_quit)
hb = Gtk.HeaderBar()
hb.set_show_close_button(True)
hb.props.title = "Ganeti Admin"
self.set_titlebar(hb)
button = Gtk.Button()
icon = Gio.ThemedIcon(name="view-refresh-symbolic")
image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON)
button.add(image)
button.connect("clicked", self.update_clicked)
hb.pack_end(button)
self.box = Gtk.Box(spacing=6)
self.add(self.box)
sorted_model = Gtk.TreeModelSort(model=params.store)
sorted_model.set_sort_column_id(0, Gtk.SortType.ASCENDING)
self.tree = Gtk.TreeView(sorted_model)
self.tree.connect("row-activated", self.row_clicked)
self.tree.connect("button_release_event", self.mouse_click)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("Machine", renderer, text=0)
column.set_sort_column_id(0)
self.tree.append_column(column)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("Status", renderer, text=1)
column.set_sort_column_id(1)
self.tree.append_column(column)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("Port", renderer, text=2)
column.set_sort_column_id(2)
self.tree.append_column(column)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("Primary node", renderer, text=3)
column.set_sort_column_id(3)
self.tree.append_column(column)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("Memory", renderer, text=4)
column.set_sort_column_id(4)
self.tree.append_column(column)
swH = Gtk.ScrolledWindow()
swH.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
swH.add(self.tree)
self.box.pack_start(swH, True, True, 0)
self.menu()
def row_clicked(self, widget, row, col):
self.show_info_window()
def show_info_window(self):
instance_name = self.get_selected_instance()
data = params.conn.instance_info(instance_name)
info = infowindow.InfoWindow(instance_name)
info.show_all()
def info_window_cb(self, widget):
self.show_info_window()
def reboot_cb(self, widget):
instance_name = self.get_selected_instance()
dialog = ConfirmDialog(self)
dialog.set_label("Would you like to reboot instance " + instance_name + " ?")
response = dialog.run()
if response == Gtk.ResponseType.OK:
params.conn.reboot(instance_name)
dialog.destroy()
def start_cb(self, widget):
instance_name = self.get_selected_instance()
params.conn.start(instance_name)
def shutdown_cb(self, widget):
instance_name = self.get_selected_instance()
dialog = ConfirmDialog(self)
dialog.set_label("Would you like to shutdown instance " + instance_name + " ?")
response = dialog.run()
if response == Gtk.ResponseType.OK:
params.conn.shutdown(instance_name)
dialog.destroy()
def connect_clipboard_cb(self, widget):
instance_name = self.get_selected_instance()
info = params.machines[instance_name]
data = "vncviewer 127.0.0.1::" + str(info['network_port']) + " -via " + info['pnode']
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(data, -1)
clipboard.store()
def update_clicked(self, widget):
params.conn.update()
def menu(self):
self.treeview_menu = Gtk.Menu()
menu_item = Gtk.MenuItem("Copy connect command to clipboard")
menu_item.connect("activate", self.connect_clipboard_cb)
self.treeview_menu.append(menu_item)
menu_item = Gtk.MenuItem("Information")
menu_item.connect("activate", self.info_window_cb)
self.treeview_menu.append(menu_item)
menu_item = Gtk.MenuItem("Reboot")
menu_item.connect("activate", self.reboot_cb)
self.treeview_menu.append(menu_item)
menu_item = Gtk.MenuItem("Start")
menu_item.connect("activate", self.start_cb)
self.treeview_menu.append(menu_item)
menu_item = Gtk.MenuItem("Shutdown")
menu_item.connect("activate", self.shutdown_cb)
self.treeview_menu.append(menu_item)
def mouse_click(self, tv, event):
if event.button == 3:
instance_name = self.get_selected_instance()
info = params.machines[instance_name]
for menuitem in self.treeview_menu.get_children():
if menuitem.get_label()=="Reboot":
if info['status']!="running":
menuitem.set_sensitive(False)
else:
menuitem.set_sensitive(True)
if menuitem.get_label()=="Start":
if info['status']!="running":
menuitem.set_sensitive(True)
else:
menuitem.set_sensitive(False)
if menuitem.get_label()=="Shutdown":
if info['status']=="running":
menuitem.set_sensitive(True)
else:
menuitem.set_sensitive(False)
self.treeview_menu.show_all()
self.treeview_menu.popup(None, None, None, None, 1, 0)
def get_selected_instance(self):
selection = self.tree.get_selection()
(model, iter) = selection.get_selected()
if iter != None:
return model[iter][0]
else:
return None
class ConfirmDialog(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "Confirmation", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.set_default_size(150, 100)
def set_label(self, text):
label = Gtk.Label(text)
box = self.get_content_area()
box.add(label)
self.show_all()
def showall():
global win
win.show_all()
conf = confwindow.ConfWindow(showall)
conf.connect("delete-event", Gtk.main_quit)
conf.show_all()
win = GaneAdmin()
Gtk.main()