Class: Y2Network::Widgets::BootProtocol

Inherits:
CWM::CustomWidget
  • Object
show all
Defined in:
src/lib/y2network/widgets/boot_protocol.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ BootProtocol

Returns a new instance of BootProtocol.



38
39
40
41
42
# File 'src/lib/y2network/widgets/boot_protocol.rb', line 38

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

Instance Method Details

#contentsObject



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

def contents
  RadioButtonGroup(
    Id(:bootproto),
    VBox(
      Left(
        HBox(
          RadioButton(
            Id(:bootproto_none),
            Opt(:notify),
            _("No Link and IP Setup (Bond Ports)")
          )
        )
      ),
      Left(
        HBox(
          RadioButton(Id(:bootproto_dynamic), Opt(:notify), _("Dynamic Address")),
          HSpacing(2),
          ComboBox(
            Id(:bootproto_dyn),
            Opt(:notify),
            "",
            [
              Item(Id(:bootproto_dhcp), "DHCP"),
              Item(Id(:bootproto_dhcp_auto), "DHCP+Zeroconf"),
              Item(Id(:bootproto_auto), "Zeroconf")
            ]
          ),
          HSpacing(2),
          ComboBox(
            Id(:bootproto_dhcp_mode),
            "",
            [
              Item(Id(:bootproto_dhcp_both), _("DHCP both version 4 and 6")),
              Item(Id(:bootproto_dhcp_v4), _("DHCP version 4 only")),
              Item(Id(:bootproto_dhcp_v6), _("DHCP version 6 only"))
            ]
          )
        )
      ),
      VBox(
        Left(
          RadioButton(
            Id(:bootproto_static),
            Opt(:notify),
            _("Statically Assigned IP Address")
          )
        ),
        HBox(
          # TODO: When object CWM top level is used, then use here IPAddress object
          InputField(Id(:bootproto_ipaddr), Opt(:hstretch), _("&IP Address")),
          HSpacing(1),
          InputField(Id(:bootproto_netmask), Opt(:hstretch), _("&Subnet Mask")),
          HSpacing(1),
          InputField(Id(:bootproto_hostname), Opt(:hstretch), _("&Hostname")),
          HStretch()
        )
      )
    )
  )
end

#dynamic_enabled(value) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'src/lib/y2network/widgets/boot_protocol.rb', line 304

def dynamic_enabled(value)
  Yast::UI.ChangeWidget(Id(:bootproto_dyn), :Enabled, value)
  if value
    # dhcp mode works only with plain dhcp
    if :bootproto_dhcp == Yast::UI.QueryWidget(Id(:bootproto_dyn), :Value)
      Yast::UI.ChangeWidget(Id(:bootproto_dhcp_mode), :Enabled, value)
    else
      Yast::UI.ChangeWidget(Id(:bootproto_dhcp_mode), :Enabled, false)
    end
  else
    Yast::UI.ChangeWidget(Id(:bootproto_dhcp_mode), :Enabled, value)
  end
end

#handleObject



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

def handle
  case value
  when :bootproto_static
    static_enabled(true)
    dynamic_enabled(false)
    one_ip = Yast::UI.QueryWidget(Id(:bootproto_ipaddr), :Value)
    if one_ip.empty?
      current_hostname = Yast::Hostname.MergeFQ(Yast::DNS.hostname, Yast::DNS.domain)
      if ![nil, "", "localhost"].include?(current_hostname)
        log.info "Presetting global hostname"
        Yast::UI.ChangeWidget(Id(:bootproto_hostname), :Value, current_hostname)
      end
    end
  when :bootproto_dynamic
    static_enabled(false)
    dynamic_enabled(true)
  when :bootproto_none
    static_enabled(false)
    dynamic_enabled(false)
  else
    raise "Unexpected value for boot protocol #{value.inspect}"
  end

  nil
end

#helpObject



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

def help
  res = _(
    "<p><b><big>Address Setup</big></b></p>\n" \
    "<p>Select <b>No Address Setup</b> if you do not want " \
    "to assign an IP address to this device.\n" \
    "This is particularly useful for bonding ethernet devices.</p>\n"
  ) +
    # Address dialog help 2/8
    _(
      "<p>Select <b>Dynamic Address</b> if you do not have a static IP address \n" \
      "assigned by the system administrator or your Internet provider.</p>\n"
    ) +
    # Address dialog help 3/8
    _(
      "<p>Choose one of the dynamic address assignment methods. Select <b>DHCP</b>\n" \
      "if you have a DHCP server running on your local network. Network addresses \n" \
      "are then automatically obtained from the server.</p>\n"
    ) +
    # Address dialog help 4/8
    _(
      "<p>To search for an IP address and assign it statically, select \n" \
      "<b>Zeroconf</b>. To use DHCP and fall back to zeroconf, " \
      "select <b>DHCP + Zeroconf\n" \
      "</b>. Otherwise, the network addresses must be assigned <b>Statically</b>.</p>\n"
    )

  if Yast::ProductFeatures.GetBooleanFeature("network", "force_static_ip")
    res += _(
      "<p>DHCP configuration is not recommended for this product.\n" \
      "Components of this product might not work with DHCP.</p>"
    )
  end

  res
end

#initObject



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

def init
  case @settings.boot_protocol
  when Y2Network::BootProtocol::STATIC
    Yast::UI.ChangeWidget(Id(:bootproto), :CurrentButton, :bootproto_static)
    Yast::UI.ChangeWidget(
      Id(:bootproto_ipaddr),
      :Value,
      @settings.ip_address
    )
    Yast::UI.ChangeWidget(
      Id(:bootproto_netmask),
      :Value,
      @settings.subnet_prefix
    )
    Yast::UI.ChangeWidget(
      Id(:bootproto_hostname),
      :Value,
      @settings.hostname
    )
  when Y2Network::BootProtocol::DHCP
    Yast::UI.ChangeWidget(Id(:bootproto), :CurrentButton, :bootproto_dynamic)
    Yast::UI.ChangeWidget(Id(:bootproto_dhcp_mode), :Value, :bootproto_dhcp_both)
    Yast::UI.ChangeWidget(Id(:bootproto_dyn), :Value, :bootproto_dhcp)
  when Y2Network::BootProtocol::DHCP4
    Yast::UI.ChangeWidget(Id(:bootproto), :CurrentButton, :bootproto_dynamic)
    Yast::UI.ChangeWidget(Id(:bootproto_dhcp_mode), :Value, :bootproto_dhcp_v4)
    Yast::UI.ChangeWidget(Id(:bootproto_dyn), :Value, :bootproto_dhcp)
  when Y2Network::BootProtocol::DHCP6
    Yast::UI.ChangeWidget(Id(:bootproto), :CurrentButton, :bootproto_dynamic)
    Yast::UI.ChangeWidget(Id(:bootproto_dhcp_mode), :Value, :bootproto_dhcp_v6)
    Yast::UI.ChangeWidget(Id(:bootproto_dyn), :Value, :bootproto_dhcp)
  when Y2Network::BootProtocol::DHCP_AUTOIP
    Yast::UI.ChangeWidget(Id(:bootproto), :CurrentButton, :bootproto_dynamic)
    Yast::UI.ChangeWidget(Id(:bootproto_dyn), :Value, :bootproto_dhcp_auto)
  when Y2Network::BootProtocol::AUTOIP
    Yast::UI.ChangeWidget(Id(:bootproto), :CurrentButton, :bootproto_dynamic)
    Yast::UI.ChangeWidget(Id(:bootproto_dyn), :Value, :bootproto_auto)
  when Y2Network::BootProtocol::NONE, Y2Network::BootProtocol::IBFT
    Yast::UI.ChangeWidget(Id(:bootproto), :CurrentButton, :bootproto_none)
  end

  handle
end

#static_enabled(value) ⇒ Object



318
319
320
321
322
# File 'src/lib/y2network/widgets/boot_protocol.rb', line 318

def static_enabled(value)
  Yast::UI.ChangeWidget(Id(:bootproto_ipaddr), :Enabled, value)
  Yast::UI.ChangeWidget(Id(:bootproto_netmask), :Enabled, value)
  Yast::UI.ChangeWidget(Id(:bootproto_hostname), :Enabled, value)
end

#storeObject



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

def store
  # FIXME: this value reset should be in backend in general not Yast::UI responsibility
  @settings.ip_address = ""
  @settings.subnet_prefix = ""
  case value
  when :bootproto_none
    bootproto = "none"
    @settings.boot_protocol = bootproto
  when :bootproto_static
    @settings.boot_protocol = "static"
    @settings.ip_address = Yast::UI.QueryWidget(:bootproto_ipaddr, :Value)
    @settings.subnet_prefix = Yast::UI.QueryWidget(:bootproto_netmask, :Value)
    @settings.hostname = Yast::UI.QueryWidget(:bootproto_hostname, :Value)
  when :bootproto_dynamic
    case Yast::UI.QueryWidget(:bootproto_dyn, :Value)
    when :bootproto_dhcp
      case Yast::UI.QueryWidget(:bootproto_dhcp_mode, :Value)
      when :bootproto_dhcp_both
        @settings.boot_protocol = "dhcp"
      when :bootproto_dhcp_v4
        @settings.boot_protocol = "dhcp4"
      when :bootproto_dhcp_v6
        @settings.boot_protocol = "dhcp6"
      else
        raise "Unexpected dhcp mode value " \
              "#{Yast::UI.QueryWidget(:bootproto_dhcp_mode, :Value).inspect}"
      end
    when :bootproto_dhcp_auto
      @settings.boot_protocol = "dhcp+autoip"
    when :bootproto_auto
      @settings.boot_protocol = "autoip"
    else
      raise "Unexpected dynamic mode value " \
            "#{Yast::UI.QueryWidget(:bootproto_dyn, :Value).inspect}"
    end
  else
    raise "Unexpected boot protocol value #{Yast::UI.QueryWidget(:bootproto, :Value).inspect}"
  end
end

#valid_netmask(ip, mask) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
# File 'src/lib/y2network/widgets/boot_protocol.rb', line 328

def valid_netmask(ip, mask)
  mask = mask[1..-1] if mask.start_with?("/")

  if Yast::IP.Check4(ip) && (Yast::Netmask.Check4(mask) || Yast::Netmask.CheckPrefix4(mask))
    return true
  end

  return true if Yast::IP.Check6(ip) && Yast::Netmask.Check6(mask)

  log.warn "IP address #{ip} is not valid"
  false
end

#validateObject



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

def validate
  return true if value != :bootproto_static

  ipa = Yast::UI.QueryWidget(:bootproto_ipaddr, :Value)
  if !Yast::IP.Check(ipa)
    Yast::Popup.Error(_("No valid IP address."))
    Yast::UI.SetFocus(:bootproto_ipaddr)
    return false
  end

  mask = Yast::UI.QueryWidget(:bootproto_netmask, :Value)
  if mask != "" && !valid_netmask(ipa, mask)
    Yast::Popup.Error(_("No valid netmask or prefix length."))
    Yast::UI.SetFocus(:bootproto_netmask)
    return false
  end

  hname = Yast::UI.QueryWidget(:bootproto_hostname, :Value)
  if !hname.empty?
    if !Yast::Hostname.CheckFQ(hname)
      Yast::Popup.Error(_("Invalid hostname."))
      Yast::UI.SetFocus(:bootproto_hostname)
      return false
    end
  # There'll be no 127.0.0.2 -> remind user to define some hostname
  elsif !Yast::Popup.YesNo(
    _(
      "No hostname has been specified. We recommend to associate \n" \
      "a hostname with a static IP, otherwise the machine name will \n" \
      "not be resolvable without an active network connection.\n" \
      "\n" \
      "Really leave the hostname blank?\n"
    )
  )
    Yast::UI.SetFocus(:bootproto_hostname)
    return false
  end

  # validate duplication
  if Yast::NetHwDetection.DuplicateIP(ipa)
    Yast::UI.SetFocus(:bootproto_ipaddr)
    # Popup text
    if !Yast::Popup.YesNoHeadline(
      Yast::Label.WarningMsg,
      _("Duplicate IP address detected.\nReally continue?\n")
    )
      return false
    end
  end

  true
end

#valueObject



324
325
326
# File 'src/lib/y2network/widgets/boot_protocol.rb', line 324

def value
  Yast::UI.QueryWidget(Id(:bootproto), :CurrentButton)
end