| 1 | #!/usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # -*- python -*- |
|---|
| 4 | # |
|---|
| 5 | # Copyright (C) 2006 Christopher R. Gabriel <cgabriel@truelite.it> |
|---|
| 6 | # |
|---|
| 7 | # This program is free software; you can redistribute it and/or modify |
|---|
| 8 | # it under the terms of the GNU General Public License as published by |
|---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | # (at your option) any later version. |
|---|
| 11 | # |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | import gtk,gobject |
|---|
| 15 | import os,math,time |
|---|
| 16 | from gettext import gettext as _ |
|---|
| 17 | from octofuss.desktop import control as desktopcontrol |
|---|
| 18 | from octofuss.utils import hostup |
|---|
| 19 | from octofuss import art,config,log,firewall,utils,utils |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | class Host: |
|---|
| 24 | def __init__(self,name): |
|---|
| 25 | self.name = name |
|---|
| 26 | self.isup = hostup.host_is_up(self.name) |
|---|
| 27 | |
|---|
| 28 | def check_up(self): |
|---|
| 29 | self.isup = hostup.host_is_up(self.name) |
|---|
| 30 | |
|---|
| 31 | # Basic cluster definition |
|---|
| 32 | |
|---|
| 33 | class Cluster(list): |
|---|
| 34 | def __init__(self): |
|---|
| 35 | list.__init__(self) |
|---|
| 36 | |
|---|
| 37 | def add_host(self,host): |
|---|
| 38 | if not self.has_host(host): |
|---|
| 39 | self.append(host) |
|---|
| 40 | |
|---|
| 41 | def del_host(self,host): |
|---|
| 42 | for hostname in self: |
|---|
| 43 | if host == hostname.name: |
|---|
| 44 | index = self.index(hostname) |
|---|
| 45 | del(self[index]) |
|---|
| 46 | |
|---|
| 47 | def load_from_list(self,host_list): |
|---|
| 48 | self.__add__(host_list) |
|---|
| 49 | |
|---|
| 50 | def has_host(self,host): |
|---|
| 51 | if host in self: |
|---|
| 52 | return True |
|---|
| 53 | else: |
|---|
| 54 | return False |
|---|
| 55 | |
|---|
| 56 | #cluster's cluster |
|---|
| 57 | |
|---|
| 58 | class Network(dict): |
|---|
| 59 | def __init__(self): |
|---|
| 60 | dict.__init__(self) |
|---|
| 61 | self.conf = config |
|---|
| 62 | self.loaded = False |
|---|
| 63 | |
|---|
| 64 | def load(self): |
|---|
| 65 | """ |
|---|
| 66 | |
|---|
| 67 | """ |
|---|
| 68 | if self.loaded is False: |
|---|
| 69 | # load clusters from cssh's main cluster file |
|---|
| 70 | try: |
|---|
| 71 | self.load_from_file("/etc/clusters") |
|---|
| 72 | except IOError: |
|---|
| 73 | pass |
|---|
| 74 | |
|---|
| 75 | # load clusters from custom cluster file |
|---|
| 76 | try: |
|---|
| 77 | self.load_from_file(os.path.join(os.getenv("HOME"),".clusters")) |
|---|
| 78 | except IOError: |
|---|
| 79 | pass |
|---|
| 80 | self.loaded = True |
|---|
| 81 | |
|---|
| 82 | def get_from_tuple(self,path): |
|---|
| 83 | |
|---|
| 84 | k = self.keys() |
|---|
| 85 | |
|---|
| 86 | #k.sort() |
|---|
| 87 | |
|---|
| 88 | if len(path) is 1: |
|---|
| 89 | cluster = path[0] |
|---|
| 90 | return (k[cluster],) |
|---|
| 91 | |
|---|
| 92 | if len(path) is 2: |
|---|
| 93 | cluster = path[0] |
|---|
| 94 | host = path[1] |
|---|
| 95 | return (k[cluster],self[k[cluster]][host].name) |
|---|
| 96 | |
|---|
| 97 | def load_from_file(self,file): |
|---|
| 98 | f = open(file).readlines() |
|---|
| 99 | for cluster in f: |
|---|
| 100 | d = cluster.split() |
|---|
| 101 | |
|---|
| 102 | if len(d) > 0: |
|---|
| 103 | cname = d[0] |
|---|
| 104 | del(d[0]) |
|---|
| 105 | c = Cluster() |
|---|
| 106 | for h in d: |
|---|
| 107 | # avoid duplicates with host's name |
|---|
| 108 | if h not in c: |
|---|
| 109 | host = Host(h) |
|---|
| 110 | c.append(host) |
|---|
| 111 | self.__setitem__(cname,c) |
|---|
| 112 | |
|---|
| 113 | def load_from_data(self,data): |
|---|
| 114 | for cluster in data: |
|---|
| 115 | cname = cluster[0] |
|---|
| 116 | c = Cluster() |
|---|
| 117 | hosts = cluster[1].split() |
|---|
| 118 | for host in hosts: |
|---|
| 119 | if host not in c: |
|---|
| 120 | h = Host(host) |
|---|
| 121 | c.append(h) |
|---|
| 122 | self.__setitem__(cname,c) |
|---|
| 123 | |
|---|
| 124 | def save(self): |
|---|
| 125 | f = open(os.path.join(os.getenv("HOME"),".clusters"),"w") |
|---|
| 126 | for c in self.keys(): |
|---|
| 127 | f.write("%s\t\t" % c) |
|---|
| 128 | for host in self[c]: |
|---|
| 129 | f.write(" %s" % host.name) |
|---|
| 130 | f.write("\n") |
|---|
| 131 | f.close() |
|---|
| 132 | |
|---|
| 133 | def find_host(self,host): |
|---|
| 134 | for i in self.keys(): |
|---|
| 135 | if host in self[i]: |
|---|
| 136 | return i |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | ### UI STUFF |
|---|
| 140 | |
|---|
| 141 | class ClusterTreeModel(gtk.TreeStore): |
|---|
| 142 | def __init__(self,data): |
|---|
| 143 | gtk.TreeStore.__init__(self,gobject.TYPE_STRING) |
|---|
| 144 | |
|---|
| 145 | self.network = data |
|---|
| 146 | self.network.load() |
|---|
| 147 | clusters = self.network.keys() |
|---|
| 148 | #clusters.sort() |
|---|
| 149 | for cluster in clusters: |
|---|
| 150 | iter = self.append(None) |
|---|
| 151 | self.set(iter, 0, cluster) |
|---|
| 152 | for host in self.network[cluster]: |
|---|
| 153 | child_iter = self.append(iter) |
|---|
| 154 | self.set(child_iter, |
|---|
| 155 | 0, host.name) |
|---|
| 156 | |
|---|
| 157 | def add_cluster(self,cname): |
|---|
| 158 | c = Cluster() |
|---|
| 159 | self.network[cname] = c |
|---|
| 160 | iter = self.append(None) |
|---|
| 161 | self.set(iter,0,cname) |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | def add_host(self,path,cluster,host): |
|---|
| 166 | """ Add an host to cluster identified by cname """ |
|---|
| 167 | iter = self.get_iter(path) |
|---|
| 168 | child_iter = self.append(iter) |
|---|
| 169 | self.set(child_iter,0,host) |
|---|
| 170 | self.network[cluster].append(Host(host)) |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | def has_host(self, clusterName, hostname): |
|---|
| 174 | """ Returns true if cluster has hostname """ |
|---|
| 175 | return self.network[cname].has_host(hostname) |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | class HostWindow(gtk.HBox): |
|---|
| 184 | def __init__(self): |
|---|
| 185 | gtk.HBox.__init__(self) |
|---|
| 186 | self.octofuss_name = _("Cluster/host management") |
|---|
| 187 | self.hpaned = gtk.HPaned() |
|---|
| 188 | self.hpaned.set_border_width(5) |
|---|
| 189 | self.pack_start(self.hpaned, True, True) |
|---|
| 190 | self.network = Network() |
|---|
| 191 | |
|---|
| 192 | self.selected_cluster = None |
|---|
| 193 | |
|---|
| 194 | #Check when cannot import some modules |
|---|
| 195 | self.restartRequired = False |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | self.model = ClusterTreeModel(self.network) |
|---|
| 199 | #Host view |
|---|
| 200 | self.view = gtk.IconView() |
|---|
| 201 | #self.model = self.model.filter_new() |
|---|
| 202 | self.cluster_tree = gtk.TreeView(self.model) |
|---|
| 203 | self.cluster_tree.columns_autosize() |
|---|
| 204 | self.cluster_tree.connect("button_press_event", self.item_event) |
|---|
| 205 | cell = gtk.CellRendererText() |
|---|
| 206 | |
|---|
| 207 | column = gtk.TreeViewColumn(_("Clusters"), cell, text=0) |
|---|
| 208 | column.set_clickable(True) |
|---|
| 209 | column.connect("clicked",self.show_cluster_icon_list) |
|---|
| 210 | |
|---|
| 211 | self.cluster_tree.append_column(column) |
|---|
| 212 | |
|---|
| 213 | v = gtk.VBox() |
|---|
| 214 | self.hpaned.pack1(v,resize=True, shrink=False) |
|---|
| 215 | |
|---|
| 216 | frame = gtk.Frame() |
|---|
| 217 | frame.set_shadow_type(gtk.SHADOW_IN) |
|---|
| 218 | v.pack_start(frame) |
|---|
| 219 | |
|---|
| 220 | sw = gtk.ScrolledWindow() |
|---|
| 221 | sw.set_shadow_type(gtk.SHADOW_ETCHED_IN) |
|---|
| 222 | sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
|---|
| 223 | sw.add(self.cluster_tree) |
|---|
| 224 | |
|---|
| 225 | frame.add(sw) |
|---|
| 226 | |
|---|
| 227 | b = gtk.Button(stock='gtk-add') |
|---|
| 228 | b.connect("clicked", self.on_cluster_add,sw ) |
|---|
| 229 | v.pack_start(b,False,False, 10) |
|---|
| 230 | bb = gtk.Button(stock='gtk-remove') |
|---|
| 231 | bb.connect("clicked", self.on_cluster_del,self.cluster_tree ,sw) |
|---|
| 232 | v.pack_start(bb,False,False, 10) |
|---|
| 233 | |
|---|
| 234 | self.app_container = gtk.Frame() |
|---|
| 235 | self.app_container.set_shadow_type(gtk.SHADOW_IN) |
|---|
| 236 | self.hpaned.add2(self.app_container) |
|---|
| 237 | |
|---|
| 238 | self.app_container.add(self.get_cluster_icon_list()) |
|---|
| 239 | |
|---|
| 240 | def change_app_content(self,obj): |
|---|
| 241 | self.app_container.get_child().destroy() |
|---|
| 242 | self.app_container.add(obj) |
|---|
| 243 | obj.show_all() |
|---|
| 244 | |
|---|
| 245 | def get_possible_button_event(self, event): |
|---|
| 246 | '''mouse or keyboard caused the event?''' |
|---|
| 247 | if event.type == gtk.gdk.KEY_PRESS: |
|---|
| 248 | return 0 # no event.button so pass 0 |
|---|
| 249 | return event.button |
|---|
| 250 | |
|---|
| 251 | def on_cluster_add(self,button,sw): |
|---|
| 252 | dialog = gtk.Dialog("Add new cluster", None, 0, |
|---|
| 253 | (gtk.STOCK_OK, gtk.RESPONSE_OK, |
|---|
| 254 | gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)) |
|---|
| 255 | dialog.vbox.pack_start(gtk.Label("Cluster name:")) |
|---|
| 256 | entry = gtk.Entry() |
|---|
| 257 | dialog.vbox.pack_start(entry) |
|---|
| 258 | dialog.vbox.show_all() |
|---|
| 259 | response = dialog.run() |
|---|
| 260 | if response == gtk.RESPONSE_OK: |
|---|
| 261 | cname = entry.get_text() |
|---|
| 262 | if cname: |
|---|
| 263 | if not self.network.has_key(cname): |
|---|
| 264 | self.model.add_cluster(cname) |
|---|
| 265 | self.network.save() |
|---|
| 266 | self.model = ClusterTreeModel(self.network) |
|---|
| 267 | self.cluster_tree.set_model(self.model) |
|---|
| 268 | sw.show() |
|---|
| 269 | dialog.destroy() |
|---|
| 270 | |
|---|
| 271 | def on_cluster_del(self,button,treeView,sw): |
|---|
| 272 | """ Delete cluster """ |
|---|
| 273 | model = treeView.get_model() |
|---|
| 274 | treeSelection = treeView.get_selection() |
|---|
| 275 | selection = treeSelection.get_selected() |
|---|
| 276 | iter = selection[1] |
|---|
| 277 | try: |
|---|
| 278 | value = model.get_value(iter,0) |
|---|
| 279 | # Delete cluster from network |
|---|
| 280 | del(self.network[value]) |
|---|
| 281 | |
|---|
| 282 | # Save |
|---|
| 283 | self.network.save() |
|---|
| 284 | # Rebuild model |
|---|
| 285 | self.model = ClusterTreeModel(self.network) |
|---|
| 286 | self.cluster_tree.set_model(self.model) |
|---|
| 287 | sw.show() |
|---|
| 288 | |
|---|
| 289 | except: |
|---|
| 290 | dialog = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, message_format="You must select a cluster!", buttons=gtk.BUTTONS_OK) |
|---|
| 291 | dialog.show_all() |
|---|
| 292 | response = dialog.run() |
|---|
| 293 | dialog.destroy() |
|---|
| 294 | return |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | def on_host_select(self,icon_view, clusterPath): |
|---|
| 299 | selected = icon_view.get_selected_items() |
|---|
| 300 | if selected: |
|---|
| 301 | self.show_context_menu_host(None, (clusterPath,selected[0][0])) |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | def get_host_icon_list(self,cluster, clusterPath): |
|---|
| 305 | hbox = gtk.HBox() |
|---|
| 306 | swin = gtk.ScrolledWindow() |
|---|
| 307 | swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
|---|
| 308 | swin.set_shadow_type(gtk.SHADOW_ETCHED_IN) |
|---|
| 309 | hbox.pack_start(swin) |
|---|
| 310 | self.view = gtk.IconView() |
|---|
| 311 | self.view.set_text_column(0) |
|---|
| 312 | self.view.set_pixbuf_column(1) |
|---|
| 313 | self.view.set_selection_mode(gtk.SELECTION_SINGLE) |
|---|
| 314 | self.view.set_size_request(200, 150) |
|---|
| 315 | model = gtk.ListStore(str, gtk.gdk.Pixbuf) |
|---|
| 316 | for host in cluster: |
|---|
| 317 | logged_user = None |
|---|
| 318 | #statusList = (LoggedUser, screenLocked) |
|---|
| 319 | |
|---|
| 320 | statusList = None |
|---|
| 321 | |
|---|
| 322 | if host.isup: |
|---|
| 323 | statusList = desktopcontrol.machine_status(host.name) |
|---|
| 324 | |
|---|
| 325 | if statusList is None: |
|---|
| 326 | statusList = ["",False] |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | if host.isup: |
|---|
| 330 | pix = art.get_pixbuf_from_name("system-up") |
|---|
| 331 | logged_user = statusList[0] |
|---|
| 332 | else: |
|---|
| 333 | pix = art.get_pixbuf_from_name("system-down") |
|---|
| 334 | |
|---|
| 335 | if firewall.host_is_listed_no_wan(host.name): |
|---|
| 336 | pix = art.get_blocked_pixbuf(pix) |
|---|
| 337 | |
|---|
| 338 | if statusList[1]: |
|---|
| 339 | pix = art.get_screen_lock_pixbuf(pix) |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | if logged_user: |
|---|
| 343 | model.append([host.name+"\n"+logged_user,pix]) |
|---|
| 344 | else: |
|---|
| 345 | model.append([host.name,pix]) |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | self.view.set_model(model) |
|---|
| 349 | #view.connect('selection-changed', self.on_host_select, clusterPath) |
|---|
| 350 | |
|---|
| 351 | self.view.connect("button_press_event", self.host_item_event, cluster, clusterPath) |
|---|
| 352 | self.view.unparent() |
|---|
| 353 | swin.add(self.view) |
|---|
| 354 | hbox.show_all() |
|---|
| 355 | return hbox |
|---|
| 356 | |
|---|
| 357 | def show_cluster_icon_list(self,obj): |
|---|
| 358 | s = self.get_cluster_icon_list() |
|---|
| 359 | self.change_app_content(s) |
|---|
| 360 | |
|---|
| 361 | def show_host_icon_list(self,event,path): |
|---|
| 362 | res = self.network.get_from_tuple(path) |
|---|
| 363 | hostNumber = len(self.network[res[0]]) |
|---|
| 364 | if len(res) is 1: |
|---|
| 365 | #self.selected_cluster = path[0] |
|---|
| 366 | s = self.get_host_icon_list(self.network[res[0]],path[0]) |
|---|
| 367 | self.change_app_content(s) |
|---|
| 368 | if hostNumber == 0: |
|---|
| 369 | hboxEmpty = gtk.HBox() |
|---|
| 370 | label = gtk.Label("No hosts in selected cluster!") |
|---|
| 371 | hboxEmpty.pack_start(label) |
|---|
| 372 | hboxEmpty.show_all() |
|---|
| 373 | self.change_app_content(hboxEmpty) |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | def on_cluster_select(self,icon_view,model=None): |
|---|
| 378 | selected = icon_view.get_selected_items() |
|---|
| 379 | |
|---|
| 380 | self.selected_cluster = selected[0][0] |
|---|
| 381 | |
|---|
| 382 | if len(selected) == 0: return |
|---|
| 383 | if len(selected) == 1: |
|---|
| 384 | self.show_host_icon_list(None,selected[0]) |
|---|
| 385 | |
|---|
| 386 | def get_cluster_icon_list(self): |
|---|
| 387 | hbox = gtk.HBox() |
|---|
| 388 | swin = gtk.ScrolledWindow() |
|---|
| 389 | swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
|---|
| 390 | swin.set_shadow_type(gtk.SHADOW_ETCHED_IN) |
|---|
| 391 | hbox.pack_start(swin) |
|---|
| 392 | view = gtk.IconView() |
|---|
| 393 | |
|---|
| 394 | view.set_text_column(0) |
|---|
| 395 | view.set_pixbuf_column(1) |
|---|
| 396 | view.set_selection_mode(gtk.SELECTION_SINGLE) |
|---|
| 397 | view.set_size_request(200, 150) |
|---|
| 398 | model = gtk.ListStore(str, gtk.gdk.Pixbuf) |
|---|
| 399 | cList = self.network.keys() |
|---|
| 400 | #cList.sort() |
|---|
| 401 | for cluster in cList: |
|---|
| 402 | pix = art.get_pixbuf_from_name("cluster-new") |
|---|
| 403 | model.append([cluster,pix]) |
|---|
| 404 | view.set_model(model) |
|---|
| 405 | view.connect('selection-changed', self.on_cluster_select, model) |
|---|
| 406 | swin.add(view) |
|---|
| 407 | hbox.show_all() |
|---|
| 408 | return hbox |
|---|
| 409 | |
|---|
| 410 | def remote_shell(self,obj,path): |
|---|
| 411 | # check if we'got a cluster or a host |
|---|
| 412 | |
|---|
| 413 | if len(path) is 1: |
|---|
| 414 | from octofuss.sshterm import SshMultiTerm |
|---|
| 415 | hosts = [] |
|---|
| 416 | for host in self.network[path[0]]: |
|---|
| 417 | hosts.append(host.name) |
|---|
| 418 | if len(hosts) > 0: |
|---|
| 419 | s = SshMultiTerm(hosts) |
|---|
| 420 | self.change_app_content(s) |
|---|
| 421 | |
|---|
| 422 | elif len(path) is 2: |
|---|
| 423 | from octofuss.sshterm import SshTerm |
|---|
| 424 | s = SshTerm(path[1]) |
|---|
| 425 | self.change_app_content(s) |
|---|
| 426 | |
|---|
| 427 | def install_software(self,obj,path): |
|---|
| 428 | try: |
|---|
| 429 | if self.restartRequired: |
|---|
| 430 | raise Exception("You need to check some problems!\nRestart needed.") |
|---|
| 431 | from octofuss.packages import PackagesAdmin |
|---|
| 432 | if len(path) is 1: |
|---|
| 433 | s = PackagesAdmin(self.network[path[0]]) |
|---|
| 434 | elif len(path) is 2: |
|---|
| 435 | s = PackagesAdmin(Host(path[1])) |
|---|
| 436 | self.change_app_content(s) |
|---|
| 437 | except Exception, e: |
|---|
| 438 | log.debug(str(e)) |
|---|
| 439 | self.restartRequired = True |
|---|
| 440 | self.__error_dialog(str(e)) |
|---|
| 441 | |
|---|
| 442 | def dialogYesNo(self, message): |
|---|
| 443 | dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO, message ) |
|---|
| 444 | dialog.show() |
|---|
| 445 | # Close dialog on user response |
|---|
| 446 | resp = dialog.run() |
|---|
| 447 | dialog.destroy() |
|---|
| 448 | return resp |
|---|
| 449 | |
|---|
| 450 | def shutdown_now(self, obj, path, res): |
|---|
| 451 | shutdownHostList = [] |
|---|
| 452 | if len(res) > 1: |
|---|
| 453 | if self.dialogYesNo("Are you sure to shutdown the host?") == gtk.RESPONSE_NO: |
|---|
| 454 | return |
|---|
| 455 | # add a single to host list to be shutted down |
|---|
| 456 | shutdownHostList.append(res[1]) |
|---|
| 457 | host = self.network[res[0]][path[1]].isup = False |
|---|
| 458 | elif len(res) == 1: |
|---|
| 459 | # add a list to host list to be shutted down |
|---|
| 460 | if self.dialogYesNo("Are you sure to shutdown the cluster?") == gtk.RESPONSE_NO: |
|---|
| 461 | return |
|---|
| 462 | |
|---|
| 463 | cluster = self.network[res[0]] |
|---|
| 464 | for host in cluster: |
|---|
| 465 | shutdownHostList.append(host.name) |
|---|
| 466 | host.isup = False |
|---|
| 467 | from octofuss.utils import remotecmd |
|---|
| 468 | remotecmd.shutdown(shutdownHostList) |
|---|
| 469 | |
|---|
| 470 | def unlock_desktop(self, obj, path, res): |
|---|
| 471 | lockdesktoplist = [] |
|---|
| 472 | if len(res) > 1: |
|---|
| 473 | if self.dialogYesNo("Are you sure to unlock desktop for this host?") == gtk.RESPONSE_NO: |
|---|
| 474 | return |
|---|
| 475 | # add a single to host list to be shutted down |
|---|
| 476 | lockdesktoplist.append(res[1]) |
|---|
| 477 | elif len(res) == 1: |
|---|
| 478 | # add a list to host list to be shutted down |
|---|
| 479 | if self.dialogYesNo("Are you sure to lock desktop to this cluster?") == gtk.RESPONSE_NO: |
|---|
| 480 | return |
|---|
| 481 | |
|---|
| 482 | cluster = self.network[res[0]] |
|---|
| 483 | for host in cluster: |
|---|
| 484 | lockdesktoplist.append(host.name) |
|---|
| 485 | for host in lockdesktoplist: |
|---|
| 486 | desktopcontrol.screen_release(host) |
|---|
| 487 | #Update view |
|---|
| 488 | self.show_host_icon_list(obj,(path[0],)) |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | def lock_desktop(self, obj, path, res): |
|---|
| 492 | lockdesktoplist = [] |
|---|
| 493 | if len(res) > 1: |
|---|
| 494 | if self.dialogYesNo("Are you sure to lock desktop for this host?") == gtk.RESPONSE_NO: |
|---|
| 495 | return |
|---|
| 496 | # add a single to host list to be shutted down |
|---|
| 497 | lockdesktoplist.append(res[1]) |
|---|
| 498 | elif len(res) == 1: |
|---|
| 499 | # add a list to host list to be shutted down |
|---|
| 500 | if self.dialogYesNo("Are you sure to lock desktop to this cluster?") == gtk.RESPONSE_NO: |
|---|
| 501 | return |
|---|
| 502 | |
|---|
| 503 | cluster = self.network[res[0]] |
|---|
| 504 | for host in cluster: |
|---|
| 505 | lockdesktoplist.append(host.name) |
|---|
| 506 | for host in lockdesktoplist: |
|---|
| 507 | desktopcontrol.screen_lock(host, "Lock from network administrator") |
|---|
| 508 | #Update view |
|---|
| 509 | self.show_host_icon_list(obj,(path[0],)) |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | def popup_message(self, obj, path, res): |
|---|
| 513 | PopupMessageHostList = [] |
|---|
| 514 | if len(res) > 1: |
|---|
| 515 | # add a single to host list to be shutted down |
|---|
| 516 | PopupMessageHostList.append(res[1]) |
|---|
| 517 | elif len(res) == 1: |
|---|
| 518 | cluster = self.network[res[0]] |
|---|
| 519 | for host in cluster: |
|---|
| 520 | PopupMessageHostList.append(host.name) |
|---|
| 521 | |
|---|
| 522 | desktopcontrol.PopupMessageWindow(PopupMessageHostList) |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | def add_host(self, obj,path, res): |
|---|
| 526 | """ Add host to cluster """ |
|---|
| 527 | dialog = gtk.Dialog("Add new host", None, 0, |
|---|
| 528 | (gtk.STOCK_OK, gtk.RESPONSE_OK, |
|---|
| 529 | gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)) |
|---|
| 530 | dialog.vbox.pack_start(gtk.Label("Host name:")) |
|---|
| 531 | entry = gtk.Entry() |
|---|
| 532 | dialog.vbox.pack_start(entry) |
|---|
| 533 | dialog.vbox.show_all() |
|---|
| 534 | response = dialog.run() |
|---|
| 535 | if response == gtk.RESPONSE_OK: |
|---|
| 536 | hostname = entry.get_text() |
|---|
| 537 | clustername = res[0] |
|---|
| 538 | if not self.network[clustername].has_host(hostname): |
|---|
| 539 | self.model.add_host(path,clustername,hostname) |
|---|
| 540 | self.show_host_icon_list(obj,path) |
|---|
| 541 | self.network.save() |
|---|
| 542 | dialog.destroy() |
|---|
| 543 | |
|---|
| 544 | def remove_host(self, obj,path, res, treeView): |
|---|
| 545 | """ Delete host from cluster """ |
|---|
| 546 | cluster = res[0] |
|---|
| 547 | host = res[1] |
|---|
| 548 | log.debug("Removing host "+host+" in cluster "+cluster) |
|---|
| 549 | self.network[cluster].del_host(host) |
|---|
| 550 | self.network.save() |
|---|
| 551 | # Update the model |
|---|
| 552 | self.model = ClusterTreeModel(self.network) |
|---|
| 553 | self.cluster_tree.set_model(self.model) |
|---|
| 554 | self.cluster_tree.show() |
|---|
| 555 | #TODO: update the icon view model |
|---|
| 556 | self.show_host_icon_list(obj,(path[0],)) |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | def show_context_menu(self,event,path): |
|---|
| 561 | menu = gtk.Menu() |
|---|
| 562 | res = self.network.get_from_tuple(path) |
|---|
| 563 | if len(res) is 2: |
|---|
| 564 | menufor = "%s: %s" % (res[0],res[1]) |
|---|
| 565 | else: |
|---|
| 566 | self.selected_cluster = res[0] |
|---|
| 567 | menufor = res[0] |
|---|
| 568 | item = gtk.MenuItem(menufor) |
|---|
| 569 | item.set_sensitive(False) |
|---|
| 570 | menu.append(item) |
|---|
| 571 | |
|---|
| 572 | # remote shell |
|---|
| 573 | item = gtk.MenuItem(_("Remote shell")) |
|---|
| 574 | item.connect("activate", self.remote_shell, res) |
|---|
| 575 | menu.append(item) |
|---|
| 576 | # install software |
|---|
| 577 | item = gtk.MenuItem(_("Install software")) |
|---|
| 578 | item.connect("activate", self.install_software,res) |
|---|
| 579 | menu.append(item) |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | # message |
|---|
| 583 | item = gtk.MenuItem(_("Send message")) |
|---|
| 584 | item.connect("activate", self.popup_message,path,res) |
|---|
| 585 | menu.append(item) |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | # separator |
|---|
| 589 | item = gtk.SeparatorMenuItem() |
|---|
| 590 | menu.append(item) |
|---|
| 591 | |
|---|
| 592 | # lockdown |
|---|
| 593 | item = gtk.MenuItem(_("Lock desktop")) |
|---|
| 594 | item.connect("activate", self.lock_desktop,path,res) |
|---|
| 595 | menu.append(item) |
|---|
| 596 | |
|---|
| 597 | # shutdown pcs |
|---|
| 598 | item = gtk.MenuItem(_("Shutdown now")) |
|---|
| 599 | item.connect("activate", self.shutdown_now,path,res) |
|---|
| 600 | menu.append(item) |
|---|
| 601 | |
|---|
| 602 | # separator |
|---|
| 603 | item = gtk.SeparatorMenuItem() |
|---|
| 604 | menu.append(item) |
|---|
| 605 | |
|---|
| 606 | # new host |
|---|
| 607 | item = gtk.MenuItem(_("Add host")) |
|---|
| 608 | item.connect("activate", self.add_host,path,res) |
|---|
| 609 | menu.append(item) |
|---|
| 610 | |
|---|
| 611 | #event_button = self.get_possible_button_event(event) |
|---|
| 612 | menu.attach_to_widget(self.cluster_tree, None) |
|---|
| 613 | |
|---|
| 614 | #menu.popup(None,None,None,3,0) |
|---|
| 615 | menu.popup(None, None, None, 3,0) |
|---|
| 616 | menu.show_all() |
|---|
| 617 | |
|---|
| 618 | def show_context_menu_host(self,event,path): |
|---|
| 619 | menu = gtk.Menu() |
|---|
| 620 | res = self.network.get_from_tuple(path) |
|---|
| 621 | host = self.network[res[0]][path[1]] |
|---|
| 622 | |
|---|
| 623 | #host = Host(res[1]) |
|---|
| 624 | |
|---|
| 625 | if len(res) is 2: |
|---|
| 626 | menufor = "%s: %s" % (res[0],res[1]) |
|---|
| 627 | else: |
|---|
| 628 | self.selected_cluster = res[0] |
|---|
| 629 | menufor = res[0] |
|---|
| 630 | |
|---|
| 631 | item = gtk.MenuItem(menufor) |
|---|
| 632 | item.set_sensitive(False) |
|---|
| 633 | menu.append(item) |
|---|
| 634 | |
|---|
| 635 | # separator |
|---|
| 636 | item = gtk.SeparatorMenuItem() |
|---|
| 637 | menu.append(item) |
|---|
| 638 | |
|---|
| 639 | ### HOST OPERATIONS |
|---|
| 640 | |
|---|
| 641 | # remote shell |
|---|
| 642 | item = gtk.MenuItem(_("Remote shell")) |
|---|
| 643 | item.set_sensitive(host.isup) |
|---|
| 644 | item.connect("activate", self.remote_shell, res) |
|---|
| 645 | menu.append(item) |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | # install software |
|---|
| 649 | item = gtk.MenuItem(_("Install software")) |
|---|
| 650 | item.connect("activate", self.install_software,res) |
|---|
| 651 | menu.append(item) |
|---|
| 652 | |
|---|
| 653 | #Block internet |
|---|
| 654 | if firewall.host_is_listed_no_wan(res[1]): |
|---|
| 655 | #Host blocked |
|---|
| 656 | item = gtk.MenuItem(_("Enable Internet")) |
|---|
| 657 | item.connect("activate", self.enable_wan_services,res[1],path) |
|---|
| 658 | menu.append(item) |
|---|
| 659 | else: |
|---|
| 660 | #Host no blocked |
|---|
| 661 | item = gtk.MenuItem(_("Disable Internet")) |
|---|
| 662 | item.connect("activate", self.disable_wan_services,res[1],path) |
|---|
| 663 | menu.append(item) |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | # remote desktop |
|---|
| 667 | rdesktop_client = "vncviewer" |
|---|
| 668 | self.command = utils.which(rdesktop_client) |
|---|
| 669 | item = gtk.MenuItem(_("Show Desktop")) |
|---|
| 670 | item.connect("activate", self.execute_command,rdesktop_client,res[1]) |
|---|
| 671 | item.set_sensitive(False) |
|---|
| 672 | if self.command and host.isup: |
|---|
| 673 | item.set_sensitive(True) |
|---|
| 674 | menu.append(item) |
|---|
| 675 | |
|---|
| 676 | # message |
|---|
| 677 | item = gtk.MenuItem(_("Send message")) |
|---|
| 678 | item.connect("activate", self.popup_message,path,res) |
|---|
| 679 | if not host.isup: |
|---|
| 680 | item.set_sensitive(False) |
|---|
| 681 | menu.append(item) |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | # separator |
|---|
| 685 | item = gtk.SeparatorMenuItem() |
|---|
| 686 | menu.append(item) |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | #lockdown |
|---|
| 690 | if host.isup: |
|---|
| 691 | screen_locked = desktopcontrol.screen_is_locked(host.name) |
|---|
| 692 | else: |
|---|
| 693 | screen_locked = None |
|---|
| 694 | |
|---|
| 695 | if screen_locked == True: |
|---|
| 696 | item = gtk.MenuItem(_("Unlock desktop")) |
|---|
| 697 | item.connect("activate", self.unlock_desktop, path, res) |
|---|
| 698 | |
|---|
| 699 | elif screen_locked == False: |
|---|
| 700 | item = gtk.MenuItem(_("Lock desktop")) |
|---|
| 701 | item.connect("activate", self.lock_desktop, path, res) |
|---|
| 702 | else: |
|---|
| 703 | item = gtk.MenuItem(_("Lock desktop")) |
|---|
| 704 | item.set_sensitive(False) |
|---|
| 705 | |
|---|
| 706 | menu.append(item) |
|---|
| 707 | |
|---|
| 708 | # shutdown host |
|---|
| 709 | item = gtk.MenuItem(_("Shutdown now")) |
|---|
| 710 | item.connect("activate", self.shutdown_now,path,res) |
|---|
| 711 | if not host.isup: |
|---|
| 712 | item.set_sensitive(False) |
|---|
| 713 | menu.append(item) |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | # separator |
|---|
| 717 | item = gtk.SeparatorMenuItem() |
|---|
| 718 | menu.append(item) |
|---|
| 719 | |
|---|
| 720 | # delete host |
|---|
| 721 | item = gtk.MenuItem(_("Delete host")) |
|---|
| 722 | item.connect("activate", self.remove_host,path,res, self.cluster_tree) |
|---|
| 723 | menu.append(item) |
|---|
| 724 | |
|---|
| 725 | |
|---|
| 726 | |
|---|
| 727 | menu.attach_to_widget(self.cluster_tree, None) |
|---|
| 728 | |
|---|
| 729 | #menu.popup(None,None,None,3,0) |
|---|
| 730 | menu.popup(None, None, None, 3,0) |
|---|
| 731 | menu.show_all() |
|---|
| 732 | |
|---|
| 733 | def execute_command(self,a,command, host): |
|---|
| 734 | """ Execute a command """ |
|---|
| 735 | import os |
|---|
| 736 | os.system(command + " " + host + "&") |
|---|
| 737 | |
|---|
| 738 | def enable_wan_services(self, a,host,path): |
|---|
| 739 | """ Enable wan services """ |
|---|
| 740 | |
|---|
| 741 | if not firewall.host_remove_no_wan(host): |
|---|
| 742 | log.debug("Problem enabling wan services for host "+host) |
|---|
| 743 | else: |
|---|
| 744 | #Update view |
|---|
| 745 | self.show_host_icon_list(a,(path[0],)) |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | def disable_wan_services(self,a,host,path): |
|---|
| 750 | """ Disable wan services """ |
|---|
| 751 | if not firewall.host_no_wan(host): |
|---|
| 752 | log.debug("Problem disabling wan services for host "+host) |
|---|
| 753 | else: |
|---|
| 754 | #Update view |
|---|
| 755 | self.show_host_icon_list(a,(path[0],)) |
|---|
| 756 | |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | def item_event(self,obj,event): |
|---|
| 762 | try: |
|---|
| 763 | path, column, x, y = self.cluster_tree.get_path_at_pos(int(event.x), int(event.y)) |
|---|
| 764 | if len(path) == 1: |
|---|
| 765 | |
|---|
| 766 | if event.button == 1: |
|---|
| 767 | self.show_host_icon_list(event,path) |
|---|
| 768 | |
|---|
| 769 | |
|---|
| 770 | if event.button == 3: |
|---|
| 771 | self.show_context_menu(event,path) |
|---|
| 772 | else: |
|---|
| 773 | if len(path) == 2: |
|---|
| 774 | if event.button == 3: |
|---|
| 775 | self.show_context_menu_host(event,path) |
|---|
| 776 | |
|---|
| 777 | except TypeError,e: |
|---|
| 778 | log.debug(str(e)) |
|---|
| 779 | except Exception, e: |
|---|
| 780 | log.debug(str(e)) |
|---|
| 781 | |
|---|
| 782 | def host_item_event(self,obj,event, cluster, clusterPath): |
|---|
| 783 | try: |
|---|
| 784 | try: |
|---|
| 785 | subpath = self.view.get_path_at_pos(int(event.x), int(event.y)) |
|---|
| 786 | path = (clusterPath, subpath[0]) |
|---|
| 787 | if event.button == 3: |
|---|
| 788 | self.show_context_menu_host(event,path) |
|---|
| 789 | except Exception, e: |
|---|
| 790 | log.debug(str(e)) |
|---|
| 791 | |
|---|
| 792 | except TypeError,e: |
|---|
| 793 | log.debug(str(e)) |
|---|
| 794 | except Exception, e: |
|---|
| 795 | log.debug(str(e)) |
|---|
| 796 | |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | |
|---|
| 800 | def __error_dialog(self, error): |
|---|
| 801 | dialog = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, message_format=error, buttons=gtk.BUTTONS_OK) |
|---|
| 802 | dialog.show_all() |
|---|
| 803 | response = dialog.run() |
|---|
| 804 | dialog.destroy() |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | # routine test |
|---|
| 812 | if __name__ == "__main__": |
|---|
| 813 | w = gtk.Window() |
|---|
| 814 | w.set_title("Cluster window test") |
|---|
| 815 | w.connect("delete_event", gtk.main_quit) |
|---|
| 816 | w.connect("destroy_event", gtk.main_quit) |
|---|
| 817 | s = HostWindow() |
|---|
| 818 | w.add(s) |
|---|
| 819 | w.show_all() |
|---|
| 820 | gtk.main() |
|---|
| 821 | |
|---|
| 822 | |
|---|