2015-09-09 19:18:52 +00:00
|
|
|
# Copyright 2015 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-10-02 00:03:54 +00:00
|
|
|
from urwid import Text, Columns, Pile, ListBox, CheckBox
|
2016-06-30 18:17:01 +00:00
|
|
|
from subiquitycore.models.filesystem import _humanize_size
|
|
|
|
from subiquitycore.view import ViewPolicy
|
|
|
|
from subiquitycore.ui.buttons import cancel_btn, done_btn
|
|
|
|
from subiquitycore.ui.interactive import (StringEditor, IntegerEditor,
|
|
|
|
Selector)
|
|
|
|
from subiquitycore.ui.utils import Color, Padding
|
2015-09-09 19:18:52 +00:00
|
|
|
import logging
|
|
|
|
|
2016-06-30 18:17:01 +00:00
|
|
|
log = logging.getLogger('subiquitycore.ui.raid')
|
2015-09-09 19:18:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RaidView(ViewPolicy):
|
|
|
|
def __init__(self, model, signal):
|
|
|
|
self.model = model
|
|
|
|
self.signal = signal
|
|
|
|
self.raid_level = Selector(self.model.raid_levels)
|
|
|
|
self.hot_spares = IntegerEditor(caption="")
|
2015-10-02 00:03:54 +00:00
|
|
|
self.chunk_size = StringEditor(edit_text="4K", caption="")
|
|
|
|
self.selected_disks = []
|
2015-09-09 19:18:52 +00:00
|
|
|
body = [
|
|
|
|
Padding.center_50(self._build_disk_selection()),
|
|
|
|
Padding.line_break(""),
|
|
|
|
Padding.center_50(self._build_raid_configuration()),
|
|
|
|
Padding.line_break(""),
|
2015-11-19 21:24:40 +00:00
|
|
|
Padding.fixed_10(self._build_buttons())
|
2015-09-09 19:18:52 +00:00
|
|
|
]
|
|
|
|
super().__init__(ListBox(body))
|
|
|
|
|
|
|
|
def _build_disk_selection(self):
|
2015-10-02 00:03:54 +00:00
|
|
|
log.debug('raid: _build_disk_selection')
|
2015-09-09 19:18:52 +00:00
|
|
|
items = [
|
|
|
|
Text("DISK SELECTION")
|
|
|
|
]
|
2015-10-05 15:56:59 +00:00
|
|
|
|
|
|
|
# raid can use empty whole disks, or empty partitions
|
|
|
|
avail_disks = self.model.get_empty_disk_names()
|
|
|
|
avail_parts = self.model.get_empty_partition_names()
|
|
|
|
avail_devs = sorted(avail_disks + avail_parts)
|
|
|
|
if len(avail_devs) == 0:
|
2015-10-02 00:03:54 +00:00
|
|
|
return items.append(
|
|
|
|
[Color.info_minor(Text("No available disks."))])
|
|
|
|
|
2015-10-05 15:56:59 +00:00
|
|
|
for dname in avail_devs:
|
|
|
|
device = self.model.get_disk(dname)
|
|
|
|
if device.path != dname:
|
|
|
|
# we've got a partition
|
|
|
|
raiddev = device.get_partition(dname)
|
|
|
|
else:
|
|
|
|
raiddev = device
|
|
|
|
|
|
|
|
disk_sz = _humanize_size(raiddev.size)
|
|
|
|
disk_string = "{} {}, {}".format(dname,
|
2015-10-02 00:03:54 +00:00
|
|
|
disk_sz,
|
2015-10-05 15:56:59 +00:00
|
|
|
device.model)
|
2015-10-02 00:03:54 +00:00
|
|
|
log.debug('raid: disk_string={}'.format(disk_string))
|
|
|
|
self.selected_disks.append(CheckBox(disk_string))
|
|
|
|
|
|
|
|
items += self.selected_disks
|
|
|
|
|
2015-09-09 19:18:52 +00:00
|
|
|
return Pile(items)
|
|
|
|
|
|
|
|
def _build_raid_configuration(self):
|
2015-10-02 00:03:54 +00:00
|
|
|
log.debug('raid: _build_raid_config')
|
2015-09-09 19:18:52 +00:00
|
|
|
items = [
|
|
|
|
Text("RAID CONFIGURATION"),
|
|
|
|
Columns(
|
|
|
|
[
|
|
|
|
("weight", 0.2, Text("RAID Level", align="right")),
|
|
|
|
("weight", 0.3,
|
|
|
|
Color.string_input(Pile(self.raid_level.group),
|
|
|
|
focus_map="string_input focus"))
|
|
|
|
],
|
|
|
|
dividechars=4
|
|
|
|
),
|
|
|
|
Columns(
|
|
|
|
[
|
|
|
|
("weight", 0.2, Text("Hot spares",
|
|
|
|
align="right")),
|
|
|
|
("weight", 0.3,
|
|
|
|
Color.string_input(self.hot_spares,
|
|
|
|
focus_map="string_input focus"))
|
|
|
|
],
|
|
|
|
dividechars=4
|
|
|
|
),
|
|
|
|
Columns(
|
|
|
|
[
|
|
|
|
("weight", 0.2, Text("Chunk size", align="right")),
|
|
|
|
("weight", 0.3,
|
|
|
|
Color.string_input(self.chunk_size,
|
|
|
|
focus_map="string_input focus"))
|
|
|
|
],
|
|
|
|
dividechars=4
|
|
|
|
)
|
|
|
|
]
|
|
|
|
return Pile(items)
|
|
|
|
|
|
|
|
def _build_buttons(self):
|
2015-10-02 00:03:54 +00:00
|
|
|
log.debug('raid: _build_buttons')
|
2015-09-09 19:18:52 +00:00
|
|
|
cancel = cancel_btn(on_press=self.cancel)
|
|
|
|
done = done_btn(on_press=self.done)
|
|
|
|
|
|
|
|
buttons = [
|
|
|
|
Color.button(done, focus_map='button focus'),
|
|
|
|
Color.button(cancel, focus_map='button focus')
|
|
|
|
]
|
|
|
|
return Pile(buttons)
|
|
|
|
|
|
|
|
def done(self, result):
|
2015-10-02 00:03:54 +00:00
|
|
|
result = {
|
|
|
|
'devices': [x.get_label() for x in self.selected_disks if x.state],
|
|
|
|
'raid_level': self.raid_level.value,
|
|
|
|
'hot_spares': self.hot_spares.value,
|
|
|
|
'chunk_size': self.chunk_size.value,
|
|
|
|
}
|
|
|
|
log.debug('raid_done: result = {}'.format(result))
|
|
|
|
self.signal.emit_signal('filesystem:add-raid-dev', result)
|
2015-09-09 19:18:52 +00:00
|
|
|
|
|
|
|
def cancel(self, button):
|
2015-10-02 00:03:54 +00:00
|
|
|
log.debug('raid: button_cancel')
|
2015-10-29 20:58:51 +00:00
|
|
|
self.signal.prev_signal()
|