Class: Y2Network::Widgets::BondSlave

Inherits:
CWM::CustomWidget
  • Object
show all
Includes:
UI::TextHelpers, SlaveItems
Defined in:
src/lib/y2network/widgets/bond_slave.rb

Instance Method Summary collapse

Methods included from SlaveItems

#physical_port_id, #physical_port_id?, #slave_items_from

Constructor Details

#initialize(settings) ⇒ BondSlave


36
37
38
39
# File 'src/lib/y2network/widgets/bond_slave.rb', line 36

def initialize(settings)
  textdomain "network"
  @settings = settings
end

Instance Method Details

#contentsObject


41
42
43
44
45
46
47
48
49
50
51
52
# File 'src/lib/y2network/widgets/bond_slave.rb', line 41

def contents
  Frame(
    _("Bond Slaves and Order"),
    VBox(
      MultiSelectionBox(Id(:bond_slaves_items), Opt(:notify), "", []),
      HBox(
        PushButton(Id(:bond_slaves_up), Opt(:disabled), _("Up")),
        PushButton(Id(:bond_slaves_down), Opt(:disabled), _("Down"))
      )
    )
  )
end

#continue_with_duplicates?(physical_ports) ⇒ Boolean

Given a map of duplicated port ids with device names, aks the user if he would like to continue or not.

mapping to an array of device names


221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'src/lib/y2network/widgets/bond_slave.rb', line 221

def continue_with_duplicates?(physical_ports)
  message = physical_ports.map do |port, slave|
    label = "PhysicalPortID (#{port}): "
    wrap_text(slave.join(", "), 76, prepend_text: label)
  end.join("\n")

  Yast::Popup.YesNoHeadline(
    Yast::Label.WarningMsg,
    # Translators: Warn the user about not desired effect
    _("The interfaces selected share the same physical port and bonding " \
      "them \nmay not have the desired effect of redundancy.\n\n%s\n\n" \
      "Really continue?\n") % message
  )
end

#enable_slave_buttonsObject


161
162
163
164
165
166
167
168
169
# File 'src/lib/y2network/widgets/bond_slave.rb', line 161

def enable_slave_buttons
  if value_index
    Yast::UI.ChangeWidget(:bond_slaves_up, :Enabled, value_index > 0)
    Yast::UI.ChangeWidget(:bond_slaves_down, :Enabled, value_index < ui_items.size - 1)
  else
    Yast::UI.ChangeWidget(:bond_slaves_up, :Enabled, false)
    Yast::UI.ChangeWidget(:bond_slaves_down, :Enabled, false)
  end
end

#handle(event) ⇒ Object


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
# File 'src/lib/y2network/widgets/bond_slave.rb', line 54

def handle(event)
  if event["EventReason"] == "SelectionChanged"
    enable_slave_buttons
  elsif event["EventReason"] == "Activated" && event["WidgetClass"] == :PushButton
    items = ui_items || []
    current = value.to_s
    index = value_index
    case event["ID"]
    when :up
      items[index], items[index - 1] = items[index - 1], items[index]
    when :down
      items[index], items[index + 1] = items[index + 1], items[index]
    else
      log.warn("unknown action #{event["ID"]}")
      return nil
    end
    Yast::UI.ChangeWidget(:bond_slaves_items, :Items, items)
    Yast::UI.ChangeWidget(:bond_slaves_items, :CurrentItem, current)
    enable_slave_buttons
  else
    log.debug("event:#{event}")
  end

  nil
end

#helpObject


80
81
82
83
84
85
86
87
# File 'src/lib/y2network/widgets/bond_slave.rb', line 80

def help
  # TODO: write it
  _(
    "<p>Select the slave devices for the bond device.\n" \
      "Only devices with the device activation set to <b>Never</b> " \
      "and with <b>No Address Setup</b> are available.</p>"
  )
end

#initObject

Default function to init the value of slave devices box for bonding.


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
# File 'src/lib/y2network/widgets/bond_slave.rb', line 90

def init
  slaves = @settings.slaves
  # TODO: use def items, but problem now is that slave_items returns term and not array
  items = slave_items_from(
    @settings.bondable_interfaces.map(&:name),
    slaves,
    Yast::Lan.yast_config # ideally get it from builder?
  )

  # reorder the items
  l1, l2 = items.partition { |t| slaves.include? t[0][0] }

  items = l1 + l2.sort_by { |t| justify_dev_name(t[0][0]) }

  Yast::UI.ChangeWidget(:bond_slaves_items, :Items, items)

  Yast::UI.ChangeWidget(
    :bond_slaves_items,
    :SelectedItems,
    slaves
  )

  enable_slave_buttons

  nil
end

#justify_dev_name(name) ⇒ String

A helper for sort devices by name. It justify at right with 0's numeric parts of given device name until 5 digits.

TODO: should not be in CWM ==== Examples

justify_dev_name("eth0") # => "eth00000" justify_dev_name("eth111") # => "eth00111" justify_dev_name("enp0s25") # => "enp00000s00025"


183
184
185
186
187
188
189
190
191
192
# File 'src/lib/y2network/widgets/bond_slave.rb', line 183

def justify_dev_name(name)
  splited_dev_name = name.scan(/\p{Alpha}+|\p{Digit}+/)
  splited_dev_name.map! do |d|
    if d =~ /\p{Digit}+/
      d.rjust(5, "0")
    else
      d
    end
  end.join
end

#repeated_physical_port_ids(slaves) ⇒ Hash{String => Array<String>}

Given a list of device names returns a hash of physical port ids mapping device names if at least two devices shared the same physical port id TODO: backend method


200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'src/lib/y2network/widgets/bond_slave.rb', line 200

def repeated_physical_port_ids(slaves)
  physical_port_ids = {}

  slaves.each do |slave|
    if physical_port_id?(slave)
      p = physical_port_ids[physical_port_id(slave)] ||= []
      p << slave
    end
  end

  physical_port_ids.select! { |_k, v| v.size > 1 }

  physical_port_ids
end

#selected_itemsObject


149
150
151
# File 'src/lib/y2network/widgets/bond_slave.rb', line 149

def selected_items
  Yast::UI.QueryWidget(:bond_slaves_items, :SelectedItems) || []
end

#storeObject

Default function to store the value of slave devices box.


118
119
120
# File 'src/lib/y2network/widgets/bond_slave.rb', line 118

def store
  @settings.slaves = selected_items
end

#ui_itemsObject


153
154
155
# File 'src/lib/y2network/widgets/bond_slave.rb', line 153

def ui_items
  Yast::UI.QueryWidget(:bond_slaves_items, :Items) || []
end

#validateObject

Validates created bonding. Currently just prevent the user to create a bond with more than one interface sharing the same physical port id


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'src/lib/y2network/widgets/bond_slave.rb', line 126

def validate
  physical_ports = repeated_physical_port_ids(selected_items)

  if !physical_ports.empty?
    return false unless continue_with_duplicates?(physical_ports)
  end

  if @settings.already_configured?(selected_items || [])
    return Yast::Popup.ContinueCancel(
      _(
        "At least one selected device is already configured.\nAdapt the configuration for bonding?\n"
      )
    )
  else
    true
  end
end

#valueObject


144
145
146
147
# File 'src/lib/y2network/widgets/bond_slave.rb', line 144

def value
  # TODO: it is multiselection, so does it make sense?
  Yast::UI.QueryWidget(:bond_slaves_items, :CurrentItem)
end

#value_indexObject


157
158
159
# File 'src/lib/y2network/widgets/bond_slave.rb', line 157

def value_index
  ui_items.index { |i| i[0] == Id(value) }
end