summaryrefslogtreecommitdiff
path: root/virtManager/vmmenu.py
blob: a5b283d0fa3725cec9f7fbebf0e4afe99805264f (plain)
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
# Copyright (C) 2009, 2013, 2014 Red Hat, Inc.
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.com>
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.

import logging

from gi.repository import Gtk

from .asyncjob import vmmAsyncJob


####################################################################
# Build toolbar shutdown button menu (manager and details toolbar) #
####################################################################

class _VMMenu(Gtk.Menu):
    def __init__(self, src, current_vm_cb, show_open=True):
        Gtk.Menu.__init__(self)
        self._parent = src
        self._current_vm_cb = current_vm_cb
        self._show_open = show_open

        self._init_state()

    def _add_action(self, label, widgetname, cb,
                    iconname="system-shutdown"):
        if label.startswith("gtk-"):
            item = Gtk.ImageMenuItem.new_from_stock(label, None)
        else:
            item = Gtk.ImageMenuItem.new_with_mnemonic(label)

        if iconname:
            if iconname.startswith("gtk-"):
                icon = Gtk.Image.new_from_stock(iconname, Gtk.IconSize.MENU)
            else:
                icon = Gtk.Image.new_from_icon_name(iconname,
                                                    Gtk.IconSize.MENU)
            item.set_image(icon)

        item.vmm_widget_name = widgetname
        if cb:
            def _cb(_menuitem):
                _vm = self._current_vm_cb()
                if _vm:
                    return cb(self._parent, _vm)
            item.connect("activate", _cb)

        self.add(item)
        return item

    def _init_state(self):
        raise NotImplementedError()
    def update_widget_states(self, vm):
        raise NotImplementedError()


class VMShutdownMenu(_VMMenu):
    """
    Shutdown submenu for reboot, forceoff, reset, etc.
    """
    def _init_state(self):
        self._add_action(_("_Reboot"), "reboot", VMActionUI.reboot)
        self._add_action(_("_Shut Down"), "shutdown", VMActionUI.shutdown)
        self._add_action(_("F_orce Reset"), "reset", VMActionUI.reset)
        self._add_action(_("_Force Off"), "destroy", VMActionUI.destroy)
        self.add(Gtk.SeparatorMenuItem())
        self._add_action(_("Sa_ve"), "save", VMActionUI.save,
                iconname=Gtk.STOCK_SAVE)

        self.show_all()

    def update_widget_states(self, vm):
        statemap = {
            "reboot": bool(vm and vm.is_stoppable()),
            "shutdown": bool(vm and vm.is_stoppable()),
            "reset": bool(vm and vm.is_stoppable()),
            "destroy": bool(vm and vm.is_destroyable()),
            "save": bool(vm and vm.is_destroyable()),
        }

        for child in self.get_children():
            name = getattr(child, "vmm_widget_name", None)
            if name in statemap:
                child.set_sensitive(statemap[name])

            if name == "reset":
                child.set_tooltip_text(None)
                if vm and not vm.conn.support.conn_domain_reset():
                    child.set_tooltip_text(_("Hypervisor does not support "
                        "domain reset."))
                    child.set_sensitive(False)


class VMActionMenu(_VMMenu):
    """
    VM submenu for run, pause, shutdown, clone, etc
    """
    def _init_state(self):
        self._add_action(_("_Run"), "run", VMActionUI.run,
                iconname=Gtk.STOCK_MEDIA_PLAY)
        self._add_action(_("_Pause"), "suspend", VMActionUI.suspend,
                Gtk.STOCK_MEDIA_PAUSE)
        self._add_action(_("R_esume"), "resume", VMActionUI.resume,
                Gtk.STOCK_MEDIA_PAUSE)
        s = self._add_action(_("_Shut Down"), "shutdown", None)
        s.set_submenu(VMShutdownMenu(self._parent, self._current_vm_cb))

        self.add(Gtk.SeparatorMenuItem())
        self._add_action(_("Clone..."), "clone",
                VMActionUI.clone, iconname=None)
        self._add_action(_("Migrate..."), "migrate",
                VMActionUI.migrate, iconname=None)
        self._add_action(_("_Delete"), "delete",
                VMActionUI.delete, iconname=Gtk.STOCK_DELETE)

        if self._show_open:
            self.add(Gtk.SeparatorMenuItem())
            self._add_action(Gtk.STOCK_OPEN, "show",
                VMActionUI.show, iconname=None)

        self.show_all()

    def update_widget_states(self, vm):
        statemap = {
            "run": bool(vm and vm.is_runable()),
            "shutdown": bool(vm and vm.is_stoppable()),
            "suspend": bool(vm and vm.is_stoppable()),
            "resume": bool(vm and vm.is_paused()),
            "migrate": bool(vm and vm.is_stoppable()),
            "clone": bool(vm and vm.is_clonable()),
        }
        vismap = {
            "suspend": bool(vm and not vm.is_paused()),
            "resume": bool(vm and vm.is_paused()),
        }

        for child in self.get_children():
            name = getattr(child, "vmm_widget_name", None)
            if child.get_submenu():
                child.get_submenu().update_widget_states(vm)
            if name in statemap:
                child.set_sensitive(statemap[name])
            if name in vismap:
                child.set_visible(vismap[name])

    def change_run_text(self, text):
        for child in self.get_children():
            if getattr(child, "vmm_widget_name", None) == "run":
                child.get_child().set_label(text)


class VMActionUI(object):
    """
    Singleton object for handling VM actions, asking for confirmation,
    showing errors/progress dialogs, etc.
    """

    @staticmethod
    def save_cancel(asyncjob, vm):
        logging.debug("Cancelling save job")
        if not vm:
            return

        try:
            vm.abort_job()
        except Exception as e:
            logging.exception("Error cancelling save job")
            asyncjob.show_warning(_("Error cancelling save job: %s") % str(e))
            return

        asyncjob.job_canceled = True
        return

    @staticmethod
    def save(src, vm):
        if not src.err.chkbox_helper(src.config.get_confirm_poweroff,
                src.config.set_confirm_poweroff,
                text1=_("Are you sure you want to save '%s'?") % vm.get_name()):
            return

        _cancel_cb = None
        if vm.getjobinfo_supported:
            _cancel_cb = (VMActionUI.save_cancel, vm)

        def cb(asyncjob):
            vm.save(meter=asyncjob.get_meter())
        def finish_cb(error, details):
            if error is not None:
                error = _("Error saving domain: %s") % error
                src.err.show_err(error, details=details)

        progWin = vmmAsyncJob(cb, [],
                    finish_cb, [],
                    _("Saving Virtual Machine"),
                    _("Saving virtual machine memory to disk "),
                    src.topwin, cancel_cb=_cancel_cb)
        progWin.run()

    @staticmethod
    def destroy(src, vm):
        if not src.err.chkbox_helper(
            src.config.get_confirm_forcepoweroff,
            src.config.set_confirm_forcepoweroff,
            text1=_("Are you sure you want to force poweroff '%s'?" %
                    vm.get_name()),
            text2=_("This will immediately poweroff the VM without "
                    "shutting down the OS and may cause data loss.")):
            return

        logging.debug("Destroying vm '%s'", vm.get_name())
        vmmAsyncJob.simple_async_noshow(vm.destroy, [], src,
                                        _("Error shutting down domain"))

    @staticmethod
    def suspend(src, vm):
        if not src.err.chkbox_helper(src.config.get_confirm_pause,
            src.config.set_confirm_pause,
            text1=_("Are you sure you want to pause '%s'?" %
                    vm.get_name())):
            return

        logging.debug("Pausing vm '%s'", vm.get_name())
        vmmAsyncJob.simple_async_noshow(vm.suspend, [], src,
                                        _("Error pausing domain"))

    @staticmethod
    def resume(src, vm):
        logging.debug("Unpausing vm '%s'", vm.get_name())
        vmmAsyncJob.simple_async_noshow(vm.resume, [], src,
                                        _("Error unpausing domain"))

    @staticmethod
    def run(src, vm):
        logging.debug("Starting vm '%s'", vm.get_name())

        if vm.has_managed_save():
            def errorcb(error, details):
                # This is run from the main thread
                res = src.err.show_err(
                    _("Error restoring domain") + ": " + error,
                    details=details,
                    text2=_(
                        "The domain could not be restored. Would you like\n"
                        "to remove the saved state and perform a regular\n"
                        "start up?"),
                    dialog_type=Gtk.MessageType.WARNING,
                    buttons=Gtk.ButtonsType.YES_NO,
                    modal=True)

                if not res:
                    return

                try:
                    vm.remove_saved_image()
                    VMActionUI.run(src, vm)
                except Exception as e:
                    src.err.show_err(_("Error removing domain state: %s")
                                     % str(e))

            # VM will be restored, which can take some time, so show progress
            title = _("Restoring Virtual Machine")
            text = _("Restoring virtual machine memory from disk")
            vmmAsyncJob.simple_async(vm.startup, [], src,
                                     title, text, "", errorcb=errorcb)

        else:
            # Regular startup
            errorintro  = _("Error starting domain")
            vmmAsyncJob.simple_async_noshow(vm.startup, [], src, errorintro)

    @staticmethod
    def shutdown(src, vm):
        if not src.err.chkbox_helper(src.config.get_confirm_poweroff,
            src.config.set_confirm_poweroff,
            text1=_("Are you sure you want to poweroff '%s'?" %
                    vm.get_name())):
            return

        logging.debug("Shutting down vm '%s'", vm.get_name())
        vmmAsyncJob.simple_async_noshow(vm.shutdown, [], src,
                                        _("Error shutting down domain"))

    @staticmethod
    def reboot(src, vm):
        if not src.err.chkbox_helper(src.config.get_confirm_poweroff,
            src.config.set_confirm_poweroff,
            text1=_("Are you sure you want to reboot '%s'?" %
                    vm.get_name())):
            return

        logging.debug("Rebooting vm '%s'", vm.get_name())
        vmmAsyncJob.simple_async_noshow(vm.reboot, [], src,
            _("Error rebooting domain"))

    @staticmethod
    def reset(src, vm):
        if not src.err.chkbox_helper(
            src.config.get_confirm_forcepoweroff,
            src.config.set_confirm_forcepoweroff,
            text1=_("Are you sure you want to force reset '%s'?" %
                    vm.get_name()),
            text2=_("This will immediately reset the VM without "
                    "shutting down the OS and may cause data loss.")):
            return

        logging.debug("Resetting vm '%s'", vm.get_name())
        vmmAsyncJob.simple_async_noshow(vm.reset, [], src,
                                        _("Error resetting domain"))

    @staticmethod
    def delete(src, vm):
        from .delete import vmmDeleteDialog
        vmmDeleteDialog.show_instance(src, vm)

    @staticmethod
    def migrate(src, vm):
        from .migrate import vmmMigrateDialog
        vmmMigrateDialog.show_instance(src, vm)

    @staticmethod
    def clone(src, vm):
        from .clone import vmmCloneVM
        vmmCloneVM.show_instance(src, vm)

    @staticmethod
    def show(src, vm):
        from .vmwindow import vmmVMWindow
        vmmVMWindow.get_instance(src, vm).show()