virt-manager/virtManager/createvol.py

390 lines
12 KiB
Python
Raw Normal View History

# Copyright (C) 2008, 2013, 2014 Red Hat, Inc.
2008-08-15 19:24:24 +04:00
# Copyright (C) 2008 Cole Robinson <crobinso@redhat.com>
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
2008-08-15 19:24:24 +04:00
Convert to use GTK3 and GObject Introspection bindings Switch over to use GObject introspection bindings for all python modules related to GObject/GTK3/etc. It is not possible to mix and match old pyggtk/pygobject manual bindings with new introspection based bindings so it must be all changed in one go. Imports like import gtk Change to from gi.repository import Gtk The vmmGObject class is changed to always inherit from GObject.GObject There is no compelling reason to avoid a GObject dep for the virt-manager TUI & it horribly messed up the code. Signal declarations are changed from vmmChooseCD.signal_new(vmmChooseCD, "cdrom-chosen", [object, str]) To __gsignals__ = { "cdrom-chosen": (GObject.SignalFlags.RUN_FIRST, None, [object, str]) } which is required by new GObject bindings Most of the rest of the change is simply dealing with renamed constants / classes. Alot of legacy compat code was removed - ie helpers which check to see if certain GTK2 methods are available are no longer required since we're mandating GTK3 only. The event loop is replaced with LibvirtGLib's event loop. Still todo - Rip out all DBus stuff & make vmmEngine class inherit GtkApplication which provides unique support & DBus method handling - Switch to use LibvirtGConfig & LibvirtGObject for libvirt interaction - Possibly switch to Python 3 too ? - Figure out why GNOME keyring is missing Introspection support My suggestion is that the standalone GIT repo for virt-install only live on as a support branch for legacy platforms. A stable-0.9 branch of virt-manager can be kept for legacy PyGtk2 based virt-manager releases. The virt-manager master branch should exclusively use GObject inspection and ideally Python3 and contain both the virt-manager and virt-install codebases in one since they are intimately related to each other & using separate GIT repos has needlessly complicated life for everyone. crobinso: Some locking fixes Misc cleanups and dropping now-useless code Fix dbus usage Fix graph cell renderer regression Fix a couple tooltip issues
2012-05-14 17:24:56 +04:00
from gi.repository import Gtk
from gi.repository import Gdk
from virtinst import log
from virtinst import StorageVolume
from .lib import uiutil
from .asyncjob import vmmAsyncJob
2019-05-04 22:42:54 +03:00
from .baseclass import vmmGObjectUI
from .xmleditor import vmmXMLEditor
2008-08-15 19:24:24 +04:00
2010-12-09 01:26:19 +03:00
class vmmCreateVolume(vmmGObjectUI):
Convert to use GTK3 and GObject Introspection bindings Switch over to use GObject introspection bindings for all python modules related to GObject/GTK3/etc. It is not possible to mix and match old pyggtk/pygobject manual bindings with new introspection based bindings so it must be all changed in one go. Imports like import gtk Change to from gi.repository import Gtk The vmmGObject class is changed to always inherit from GObject.GObject There is no compelling reason to avoid a GObject dep for the virt-manager TUI & it horribly messed up the code. Signal declarations are changed from vmmChooseCD.signal_new(vmmChooseCD, "cdrom-chosen", [object, str]) To __gsignals__ = { "cdrom-chosen": (GObject.SignalFlags.RUN_FIRST, None, [object, str]) } which is required by new GObject bindings Most of the rest of the change is simply dealing with renamed constants / classes. Alot of legacy compat code was removed - ie helpers which check to see if certain GTK2 methods are available are no longer required since we're mandating GTK3 only. The event loop is replaced with LibvirtGLib's event loop. Still todo - Rip out all DBus stuff & make vmmEngine class inherit GtkApplication which provides unique support & DBus method handling - Switch to use LibvirtGConfig & LibvirtGObject for libvirt interaction - Possibly switch to Python 3 too ? - Figure out why GNOME keyring is missing Introspection support My suggestion is that the standalone GIT repo for virt-install only live on as a support branch for legacy platforms. A stable-0.9 branch of virt-manager can be kept for legacy PyGtk2 based virt-manager releases. The virt-manager master branch should exclusively use GObject inspection and ideally Python3 and contain both the virt-manager and virt-install codebases in one since they are intimately related to each other & using separate GIT repos has needlessly complicated life for everyone. crobinso: Some locking fixes Misc cleanups and dropping now-useless code Fix dbus usage Fix graph cell renderer regression Fix a couple tooltip issues
2012-05-14 17:24:56 +04:00
__gsignals__ = {
"vol-created": (vmmGObjectUI.RUN_FIRST, None, [str, str]),
Convert to use GTK3 and GObject Introspection bindings Switch over to use GObject introspection bindings for all python modules related to GObject/GTK3/etc. It is not possible to mix and match old pyggtk/pygobject manual bindings with new introspection based bindings so it must be all changed in one go. Imports like import gtk Change to from gi.repository import Gtk The vmmGObject class is changed to always inherit from GObject.GObject There is no compelling reason to avoid a GObject dep for the virt-manager TUI & it horribly messed up the code. Signal declarations are changed from vmmChooseCD.signal_new(vmmChooseCD, "cdrom-chosen", [object, str]) To __gsignals__ = { "cdrom-chosen": (GObject.SignalFlags.RUN_FIRST, None, [object, str]) } which is required by new GObject bindings Most of the rest of the change is simply dealing with renamed constants / classes. Alot of legacy compat code was removed - ie helpers which check to see if certain GTK2 methods are available are no longer required since we're mandating GTK3 only. The event loop is replaced with LibvirtGLib's event loop. Still todo - Rip out all DBus stuff & make vmmEngine class inherit GtkApplication which provides unique support & DBus method handling - Switch to use LibvirtGConfig & LibvirtGObject for libvirt interaction - Possibly switch to Python 3 too ? - Figure out why GNOME keyring is missing Introspection support My suggestion is that the standalone GIT repo for virt-install only live on as a support branch for legacy platforms. A stable-0.9 branch of virt-manager can be kept for legacy PyGtk2 based virt-manager releases. The virt-manager master branch should exclusively use GObject inspection and ideally Python3 and contain both the virt-manager and virt-install codebases in one since they are intimately related to each other & using separate GIT repos has needlessly complicated life for everyone. crobinso: Some locking fixes Misc cleanups and dropping now-useless code Fix dbus usage Fix graph cell renderer regression Fix a couple tooltip issues
2012-05-14 17:24:56 +04:00
}
2010-12-09 01:26:19 +03:00
def __init__(self, conn, parent_pool):
vmmGObjectUI.__init__(self, "createvol.ui", "vmm-create-vol")
2008-08-15 19:24:24 +04:00
self.conn = conn
self._parent_pool = parent_pool
self._name_hint = None
self._storage_browser = None
2008-08-15 19:24:24 +04:00
2019-05-04 22:42:54 +03:00
self._xmleditor = vmmXMLEditor(self.builder, self.topwin,
self.widget("details-box-align"),
self.widget("details-box"))
self._xmleditor.connect("xml-requested",
self._xmleditor_xml_requested_cb)
self.builder.connect_signals({
"on_vmm_create_vol_delete_event": self.close,
"on_vol_cancel_clicked": self.close,
"on_vol_create_clicked": self._create_clicked_cb,
"on_vol_name_changed": self._vol_name_changed_cb,
"on_vol_format_changed": self._vol_format_changed_cb,
"on_backing_store_changed": self._backing_storage_changed_cb,
"on_vol_allocation_value_changed": self._vol_allocation_changed_cb,
"on_vol_capacity_value_changed": self._vol_capacity_changed_cb,
"on_backing_browse_clicked": self._browse_backing_clicked_cb,
2008-08-15 19:24:24 +04:00
})
self.bind_escape_key_close()
2008-08-15 19:24:24 +04:00
self._init_state()
2008-08-15 19:24:24 +04:00
#######################
# Standard UI methods #
#######################
2011-04-14 16:47:42 +04:00
def show(self, parent):
try:
parent_xml = self._parent_pool.xmlobj.get_xml()
except Exception:
log.debug("Error getting parent_pool xml", exc_info=True)
parent_xml = None
log.debug("Showing new volume wizard for parent_pool=\n%s",
parent_xml)
self._reset_state()
2011-04-14 16:47:42 +04:00
self.topwin.set_transient_for(parent)
2008-08-15 19:24:24 +04:00
self.topwin.present()
def close(self, ignore1=None, ignore2=None):
log.debug("Closing new volume wizard")
2008-08-15 19:24:24 +04:00
self.topwin.hide()
if self._storage_browser:
self._storage_browser.close()
self.set_modal(False)
2008-08-15 19:24:24 +04:00
return 1
def _cleanup(self):
self.conn = None
self._parent_pool = None
2019-05-04 22:42:54 +03:00
self._xmleditor.cleanup()
self._xmleditor = None
if self._storage_browser:
self._storage_browser.cleanup()
self._storage_browser = None
##############
# Public API #
##############
def set_name_hint(self, hint):
self._name_hint = hint
def set_modal(self, modal):
self.topwin.set_modal(bool(modal))
def set_parent_pool(self, conn, pool):
self.conn = conn
self._parent_pool = pool
###########
# UI init #
###########
def _init_state(self):
blue = Gdk.color_parse("#0072A8")
self.widget("header").modify_bg(Gtk.StateType.NORMAL, blue)
format_list = self.widget("vol-format")
format_model = Gtk.ListStore(str, str)
format_list.set_model(format_model)
uiutil.init_combo_text_column(format_list, 1)
for fmt in ["raw", "qcow2"]:
format_model.append([fmt, fmt])
def _reset_state(self):
2019-05-04 22:42:54 +03:00
self._xmleditor.reset_state()
vol = self._make_stub_vol()
hasformat = vol.supports_format()
uiutil.set_grid_row_visible(self.widget("vol-format"), hasformat)
uiutil.set_list_selection(self.widget("vol-format"),
self.conn.get_default_storage_format())
2008-08-15 19:24:24 +04:00
self.widget("vol-name").set_text(self._default_vol_name() or "")
self.widget("vol-name").grab_focus()
self.widget("vol-name").emit("changed")
default_alloc = 0
default_cap = 20
self.widget("backing-store").set_text("")
alloc = default_alloc
if not self._can_alloc():
alloc = default_cap
self._show_alloc()
self._show_backing()
self.widget("backing-expander").set_expanded(False)
self.widget("vol-allocation").set_range(0,
int(self._parent_pool.get_available() / 1024 / 1024 / 1024))
self.widget("vol-allocation").set_value(alloc)
self.widget("vol-capacity").set_range(0.1, 1000000)
self.widget("vol-capacity").set_value(default_cap)
2008-08-15 19:24:24 +04:00
self.widget("vol-parent-name").set_markup(
"<b>" + self._parent_pool.get_name() + "'s</b>")
self.widget("vol-parent-space").set_text(
self._parent_pool.get_pretty_available())
2008-08-15 19:24:24 +04:00
###################
# Helper routines #
###################
def _get_config_format(self):
if not self.widget("vol-format").get_visible():
return None
return uiutil.get_list_selection(self.widget("vol-format"))
2008-08-15 19:24:24 +04:00
def _default_vol_name(self):
hint = self._name_hint or "vol"
suffix = self._default_suffix()
ret = ""
try:
ret = StorageVolume.find_free_name(
self.conn.get_backend(),
self._parent_pool.get_backend(),
hint, suffix=suffix)
if ret and suffix:
ret = ret.rsplit(".", 1)[0]
except Exception:
log.exception("Error finding a default vol name")
return ret
def _default_suffix(self):
vol = self._make_stub_vol()
if vol.file_type != vol.TYPE_FILE:
return ""
return StorageVolume.get_file_extension_for_format(
self._get_config_format())
def _can_only_sparse(self):
if self._get_config_format() == "qcow2":
return True
if (self.widget("backing-store").is_visible() and
self.widget("backing-store").get_text()):
return True
return False
def _can_alloc(self):
if self._can_only_sparse():
return False
if self._parent_pool.get_type() == "logical":
# Sparse LVM volumes don't auto grow, so alloc=0 is useless
return False
return True
def _show_alloc(self):
uiutil.set_grid_row_visible(
self.widget("vol-allocation"), self._can_alloc())
def _can_backing(self):
if self._parent_pool.get_type() == "logical":
return True
if self._get_config_format() == "qcow2":
return True
return False
def _show_backing(self):
uiutil.set_grid_row_visible(
self.widget("backing-expander"), self._can_backing())
def _browse_file(self):
if (self._storage_browser and
self._storage_browser.conn != self.conn):
self._storage_browser.cleanup()
self._storage_browser = None
if self._storage_browser is None:
def cb(src, text):
ignore = src
self.widget("backing-store").set_text(text)
from .storagebrowse import vmmStorageBrowser
self._storage_browser = vmmStorageBrowser(self.conn)
self._storage_browser.set_finish_cb(cb)
self._storage_browser.topwin.set_modal(self.topwin.get_modal())
self._storage_browser.set_browse_reason(
self.config.CONFIG_DIR_IMAGE)
self._storage_browser.show(self.topwin)
def _show_err(self, info, details=None):
self.err.show_err(info, details, modal=self.topwin.get_modal())
###################
# Object building #
###################
2019-05-04 22:42:54 +03:00
def _make_stub_vol(self, xml=None):
vol = StorageVolume(self.conn.get_backend(), parsexml=xml)
vol.pool = self._parent_pool.get_backend()
return vol
2019-05-04 22:42:54 +03:00
def _build_xmlobj_from_xmleditor(self):
xml = self._xmleditor.get_xml()
log.debug("Using XML from xmleditor:\n%s", xml)
2019-05-04 22:42:54 +03:00
return self._make_stub_vol(xml=xml)
def _build_xmlobj_from_ui(self):
name = self.widget("vol-name").get_text()
suffix = self.widget("vol-name-suffix").get_text()
volname = name + suffix
fmt = self._get_config_format()
alloc = self.widget("vol-allocation").get_value()
cap = self.widget("vol-capacity").get_value()
backing = self.widget("backing-store").get_text()
if not self.widget("vol-allocation").get_visible():
alloc = cap
if self._can_only_sparse():
alloc = 0
2019-05-04 22:42:54 +03:00
vol = self._make_stub_vol()
vol.name = volname
vol.capacity = (cap * 1024 * 1024 * 1024)
vol.allocation = (alloc * 1024 * 1024 * 1024)
if backing:
vol.backing_store = backing
if fmt:
vol.format = fmt
return vol
2019-05-04 22:42:54 +03:00
def _build_xmlobj(self, check_xmleditor):
try:
xmlobj = self._build_xmlobj_from_ui()
if check_xmleditor and self._xmleditor.is_xml_selected():
xmlobj = self._build_xmlobj_from_xmleditor()
return xmlobj
except Exception as e:
self.err.show_err(_("Error building XML: %s") % str(e))
##################
# Object install #
##################
def _pool_refreshed_cb(self, pool, volname):
self.emit("vol-created", pool.get_connkey(), volname)
def _finish_cb(self, error, details, vol):
self.reset_finish_cursor()
if error:
error = _("Error creating vol: %s") % error
self._show_err(error, details=details)
else:
self._parent_pool.connect("refreshed",
self._pool_refreshed_cb, vol.name)
self.idle_add(self._parent_pool.refresh)
self.close()
def _finish(self):
2019-05-04 22:42:54 +03:00
vol = self._build_xmlobj(check_xmleditor=True)
if not vol:
return
2008-08-15 19:24:24 +04:00
try:
vol.validate()
except Exception as e:
return self._show_err(_("Error validating volume: %s") % str(e))
2008-08-15 19:24:24 +04:00
self.set_finish_cursor()
progWin = vmmAsyncJob(self._async_vol_create, [vol],
self._finish_cb, [vol],
2010-12-10 17:57:42 +03:00
_("Creating storage volume..."),
_("Creating the storage volume may take a "
2011-04-14 16:47:42 +04:00
"while..."),
self.topwin)
progWin.run()
2008-08-15 19:24:24 +04:00
def _async_vol_create(self, asyncjob, vol):
conn = self.conn.get_backend()
2008-08-15 19:24:24 +04:00
2010-12-10 17:57:42 +03:00
# Lookup different pool obj
newpool = conn.storagePoolLookupByName(self._parent_pool.get_name())
vol.pool = newpool
2008-08-15 19:24:24 +04:00
meter = asyncjob.get_meter()
log.debug("Starting background vol creation.")
vol.install(meter=meter)
log.debug("vol creation complete.")
2008-08-15 19:24:24 +04:00
################
# UI listeners #
################
2019-05-04 22:42:54 +03:00
def _xmleditor_xml_requested_cb(self, src):
xmlobj = self._build_xmlobj(check_xmleditor=False)
self._xmleditor.set_xml(xmlobj and xmlobj.get_xml() or "")
def _vol_format_changed_cb(self, src):
self._show_alloc()
self._show_backing()
self.widget("vol-name").emit("changed")
def _vol_capacity_changed_cb(self, src):
alloc_widget = self.widget("vol-allocation")
cap = src.get_value()
alloc = self.widget("vol-allocation").get_value()
2008-08-15 19:24:24 +04:00
if cap < alloc:
alloc_widget.set_value(cap)
2008-08-15 19:24:24 +04:00
def _vol_allocation_changed_cb(self, src):
cap_widget = self.widget("vol-capacity")
alloc = src.get_value()
cap = cap_widget.get_value()
if alloc > cap:
cap_widget.set_value(alloc)
def _vol_name_changed_cb(self, src):
text = src.get_text()
suffix = self._default_suffix()
if "." in text:
suffix = ""
self.widget("vol-name-suffix").set_text(suffix)
def _browse_backing_clicked_cb(self, src):
self._browse_file()
def _backing_storage_changed_cb(self, src):
self._show_alloc()
def _create_clicked_cb(self, src):
self._finish()