createvol: Modernize code style
* Have clear UI callbacks * Group functions * Privatize internal methods * Don't carry around _vol internally
This commit is contained in:
parent
3694f45939
commit
bad894ad8c
|
@ -105,12 +105,12 @@
|
|||
<property name="orientation">vertical</property>
|
||||
<property name="spacing">18</property>
|
||||
<child>
|
||||
<object class="GtkAlignment" id="alignment2">
|
||||
<object class="GtkAlignment" id="details-box-align">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="right_padding">1</property>
|
||||
<child>
|
||||
<object class="GtkBox" id="vbox2">
|
||||
<object class="GtkBox" id="details-box">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="orientation">vertical</property>
|
||||
|
|
|
@ -24,89 +24,80 @@ class vmmCreateVolume(vmmGObjectUI):
|
|||
def __init__(self, conn, parent_pool):
|
||||
vmmGObjectUI.__init__(self, "createvol.ui", "vmm-create-vol")
|
||||
self.conn = conn
|
||||
self.parent_pool = parent_pool
|
||||
|
||||
self.name_hint = None
|
||||
self.vol = None
|
||||
self.storage_browser = None
|
||||
self._parent_pool = parent_pool
|
||||
self._name_hint = None
|
||||
self._storage_browser = None
|
||||
|
||||
self.builder.connect_signals({
|
||||
"on_vmm_create_vol_delete_event": self.close,
|
||||
"on_vol_cancel_clicked": self.close,
|
||||
"on_vol_create_clicked": self.finish,
|
||||
"on_vol_create_clicked": self._create_clicked_cb,
|
||||
|
||||
"on_vol_name_changed": self.vol_name_changed,
|
||||
"on_vol_format_changed": self.vol_format_changed,
|
||||
"on_backing_store_changed": self._show_alloc,
|
||||
"on_vol_allocation_value_changed": self.vol_allocation_changed,
|
||||
"on_vol_capacity_value_changed": self.vol_capacity_changed,
|
||||
"on_backing_browse_clicked": self.browse_backing,
|
||||
"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,
|
||||
})
|
||||
self.bind_escape_key_close()
|
||||
|
||||
self._init_state()
|
||||
self.reset_state()
|
||||
|
||||
|
||||
#######################
|
||||
# Standard UI methods #
|
||||
#######################
|
||||
|
||||
def show(self, parent):
|
||||
try:
|
||||
parent_xml = self.parent_pool.xmlobj.get_xml()
|
||||
parent_xml = self._parent_pool.xmlobj.get_xml()
|
||||
except Exception:
|
||||
logging.debug("Error getting parent_pool xml", exc_info=True)
|
||||
parent_xml = None
|
||||
|
||||
logging.debug("Showing new volume wizard for parent_pool=\n%s",
|
||||
parent_xml)
|
||||
self.reset_state()
|
||||
self._reset_state()
|
||||
self.topwin.set_transient_for(parent)
|
||||
self.topwin.present()
|
||||
|
||||
def close(self, ignore1=None, ignore2=None):
|
||||
logging.debug("Closing new volume wizard")
|
||||
self.topwin.hide()
|
||||
if self.storage_browser:
|
||||
self.storage_browser.close()
|
||||
if self._storage_browser:
|
||||
self._storage_browser.close()
|
||||
self.set_modal(False)
|
||||
return 1
|
||||
|
||||
def _cleanup(self):
|
||||
self.conn = None
|
||||
self.parent_pool = None
|
||||
self._parent_pool = None
|
||||
|
||||
if self.storage_browser:
|
||||
self.storage_browser.cleanup()
|
||||
self.storage_browser = 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
|
||||
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
|
||||
self._parent_pool = pool
|
||||
|
||||
|
||||
def default_vol_name(self):
|
||||
if not self.name_hint:
|
||||
return ""
|
||||
|
||||
suffix = self.default_suffix()
|
||||
ret = ""
|
||||
try:
|
||||
ret = StorageVolume.find_free_name(
|
||||
self.parent_pool.get_backend(), self.name_hint, suffix=suffix)
|
||||
except Exception:
|
||||
logging.exception("Error finding a default vol name")
|
||||
|
||||
return ret
|
||||
|
||||
def default_suffix(self):
|
||||
if self.vol.file_type != self.vol.TYPE_FILE:
|
||||
return ""
|
||||
return StorageVolume.get_file_extension_for_format(
|
||||
self.get_config_format())
|
||||
###########
|
||||
# UI init #
|
||||
###########
|
||||
|
||||
def _init_state(self):
|
||||
blue = Gdk.color_parse("#0072A8")
|
||||
|
@ -119,49 +110,14 @@ class vmmCreateVolume(vmmGObjectUI):
|
|||
for fmt in ["raw", "qcow2"]:
|
||||
format_model.append([fmt, fmt])
|
||||
|
||||
def _reset_state(self):
|
||||
vol = self._make_stub_vol()
|
||||
|
||||
def _make_stub_vol(self):
|
||||
self.vol = StorageVolume(self.conn.get_backend())
|
||||
self.vol.pool = self.parent_pool.get_backend()
|
||||
|
||||
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, *args, **kwargs):
|
||||
ignore = args
|
||||
ignore = kwargs
|
||||
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 reset_state(self):
|
||||
self._make_stub_vol()
|
||||
|
||||
self.widget("vol-name").set_text(self.default_vol_name() or "")
|
||||
self.widget("vol-name").set_text(self._default_vol_name() or "")
|
||||
self.widget("vol-name").grab_focus()
|
||||
self.vol_name_changed(self.widget("vol-name"))
|
||||
self.widget("vol-name").emit("changed")
|
||||
|
||||
hasformat = self.vol.supports_property("format")
|
||||
hasformat = vol.supports_property("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())
|
||||
|
@ -178,108 +134,120 @@ class vmmCreateVolume(vmmGObjectUI):
|
|||
self.widget("backing-expander").set_expanded(False)
|
||||
|
||||
self.widget("vol-allocation").set_range(0,
|
||||
int(self.parent_pool.get_available() / 1024 / 1024 / 1024))
|
||||
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)
|
||||
|
||||
self.widget("vol-parent-name").set_markup(
|
||||
"<b>" + self.parent_pool.get_name() + "'s</b>")
|
||||
"<b>" + self._parent_pool.get_name() + "'s</b>")
|
||||
self.widget("vol-parent-space").set_text(
|
||||
self.parent_pool.get_pretty_available())
|
||||
self._parent_pool.get_pretty_available())
|
||||
|
||||
def get_config_format(self):
|
||||
|
||||
###################
|
||||
# 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"))
|
||||
|
||||
def vol_name_changed(self, src):
|
||||
text = src.get_text()
|
||||
def _default_vol_name(self):
|
||||
if not self._name_hint:
|
||||
return ""
|
||||
|
||||
suffix = self.default_suffix()
|
||||
if "." in text:
|
||||
suffix = ""
|
||||
self.widget("vol-name-suffix").set_text(suffix)
|
||||
self.widget("vol-create").set_sensitive(bool(text))
|
||||
|
||||
def vol_allocation_changed(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_capacity_changed(self, src):
|
||||
alloc_widget = self.widget("vol-allocation")
|
||||
|
||||
cap = src.get_value()
|
||||
alloc = self.widget("vol-allocation").get_value()
|
||||
|
||||
if cap < alloc:
|
||||
alloc_widget.set_value(cap)
|
||||
|
||||
def vol_format_changed(self, src):
|
||||
ignore = src
|
||||
self._show_alloc()
|
||||
self._show_backing()
|
||||
self.widget("vol-name").emit("changed")
|
||||
|
||||
def browse_backing(self, src):
|
||||
ignore = src
|
||||
self._browse_file()
|
||||
|
||||
def _signal_vol_created(self, pool, volname):
|
||||
self.emit("vol-created", pool.get_connkey(), volname)
|
||||
|
||||
def _finish_cb(self, error, details):
|
||||
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._signal_vol_created,
|
||||
self.vol.name)
|
||||
self.idle_add(self.parent_pool.refresh)
|
||||
self.close()
|
||||
|
||||
def finish(self, src_ignore):
|
||||
suffix = self._default_suffix()
|
||||
ret = ""
|
||||
try:
|
||||
if not self.validate():
|
||||
return
|
||||
except Exception as e:
|
||||
self.show_err(_("Uncaught error validating input: %s") % str(e))
|
||||
return
|
||||
ret = StorageVolume.find_free_name(
|
||||
self._parent_pool.get_backend(),
|
||||
self._name_hint, suffix=suffix)
|
||||
except Exception:
|
||||
logging.exception("Error finding a default vol name")
|
||||
|
||||
self.set_finish_cursor()
|
||||
progWin = vmmAsyncJob(self._async_vol_create, [],
|
||||
self._finish_cb, [],
|
||||
_("Creating storage volume..."),
|
||||
_("Creating the storage volume may take a "
|
||||
"while..."),
|
||||
self.topwin)
|
||||
progWin.run()
|
||||
return ret
|
||||
|
||||
def _async_vol_create(self, asyncjob):
|
||||
conn = self.conn.get_backend()
|
||||
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())
|
||||
|
||||
# Lookup different pool obj
|
||||
newpool = conn.storagePoolLookupByName(self.parent_pool.get_name())
|
||||
self.vol.pool = newpool
|
||||
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
|
||||
|
||||
meter = asyncjob.get_meter()
|
||||
logging.debug("Starting background vol creation.")
|
||||
self.vol.install(meter=meter)
|
||||
logging.debug("vol creation complete.")
|
||||
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 validate(self):
|
||||
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())
|
||||
|
||||
def _val_err(self, info, details):
|
||||
return self.err.val_err(info, details, modal=self.topwin.get_modal())
|
||||
|
||||
|
||||
###################
|
||||
# Object building #
|
||||
###################
|
||||
|
||||
def _make_stub_vol(self):
|
||||
vol = StorageVolume(self.conn.get_backend())
|
||||
vol.pool = self._parent_pool.get_backend()
|
||||
return vol
|
||||
|
||||
def _build_xmlobj(self):
|
||||
name = self.widget("vol-name").get_text()
|
||||
suffix = self.widget("vol-name-suffix").get_text()
|
||||
volname = name + suffix
|
||||
fmt = self.get_config_format()
|
||||
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()
|
||||
|
@ -289,40 +257,111 @@ class vmmCreateVolume(vmmGObjectUI):
|
|||
alloc = 0
|
||||
|
||||
try:
|
||||
self._make_stub_vol()
|
||||
self.vol.name = volname
|
||||
self.vol.capacity = (cap * 1024 * 1024 * 1024)
|
||||
self.vol.allocation = (alloc * 1024 * 1024 * 1024)
|
||||
vol = self._make_stub_vol()
|
||||
vol.name = volname
|
||||
vol.capacity = (cap * 1024 * 1024 * 1024)
|
||||
vol.allocation = (alloc * 1024 * 1024 * 1024)
|
||||
if backing:
|
||||
self.vol.backing_store = backing
|
||||
vol.backing_store = backing
|
||||
if fmt:
|
||||
self.vol.format = fmt
|
||||
self.vol.validate()
|
||||
vol.format = fmt
|
||||
except ValueError as e:
|
||||
return self.val_err(_("Volume Parameter Error"), e)
|
||||
return True
|
||||
return self._val_err(_("Volume Parameter Error"), e)
|
||||
return vol
|
||||
|
||||
def show_err(self, info, details=None):
|
||||
self.err.show_err(info, details, modal=self.topwin.get_modal())
|
||||
|
||||
def val_err(self, info, details):
|
||||
return self.err.val_err(info, details, modal=self.topwin.get_modal())
|
||||
##################
|
||||
# Object install #
|
||||
##################
|
||||
|
||||
def _browse_file(self):
|
||||
if self.storage_browser and self.storage_browser.conn != self.conn:
|
||||
self.storage_browser.cleanup()
|
||||
self.storage_browser = None
|
||||
def _pool_refreshed_cb(self, pool, volname):
|
||||
self.emit("vol-created", pool.get_connkey(), volname)
|
||||
|
||||
if self.storage_browser is None:
|
||||
def cb(src, text):
|
||||
ignore = src
|
||||
self.widget("backing-store").set_text(text)
|
||||
def _finish_cb(self, error, details, vol):
|
||||
self.reset_finish_cursor()
|
||||
|
||||
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)
|
||||
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()
|
||||
|
||||
self.storage_browser.show(self.topwin)
|
||||
def _finish(self):
|
||||
vol = self._build_xmlobj()
|
||||
if not vol:
|
||||
return
|
||||
|
||||
try:
|
||||
vol.validate()
|
||||
except Exception as e:
|
||||
return self._show_err(_("Error validating volume: %s") % str(e))
|
||||
|
||||
self.set_finish_cursor()
|
||||
progWin = vmmAsyncJob(self._async_vol_create, [vol],
|
||||
self._finish_cb, [vol],
|
||||
_("Creating storage volume..."),
|
||||
_("Creating the storage volume may take a "
|
||||
"while..."),
|
||||
self.topwin)
|
||||
progWin.run()
|
||||
|
||||
def _async_vol_create(self, asyncjob, vol):
|
||||
conn = self.conn.get_backend()
|
||||
|
||||
# Lookup different pool obj
|
||||
newpool = conn.storagePoolLookupByName(self._parent_pool.get_name())
|
||||
vol.pool = newpool
|
||||
|
||||
meter = asyncjob.get_meter()
|
||||
logging.debug("Starting background vol creation.")
|
||||
vol.install(meter=meter)
|
||||
logging.debug("vol creation complete.")
|
||||
|
||||
|
||||
################
|
||||
# UI listeners #
|
||||
################
|
||||
|
||||
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()
|
||||
|
||||
if cap < alloc:
|
||||
alloc_widget.set_value(cap)
|
||||
|
||||
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)
|
||||
self.widget("vol-create").set_sensitive(bool(text))
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue