Class: Installation::Widgets::NtpServer

Inherits:
CWM::InputField
  • Object
show all
Extended by:
Yast::I18n
Defined in:
src/lib/installation/widgets/ntp_server.rb

Overview

This widget is responsible of validating and storing the NTP server to use.

Constant Summary collapse

NOT_VALID_SERVERS_MESSAGE =
N_("Not valid location for the NTP servers:\n%{servers}" \
"\n\nPlease, enter a valid IP or Hostname").freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_servers = []) ⇒ NtpServer

Constructor

Parameters:

  • default_servers (Array<String>) (defaults to: [])

    List of servers



43
44
45
46
47
48
# File 'src/lib/installation/widgets/ntp_server.rb', line 43

def initialize(default_servers = [])
  super()
  textdomain "installation"

  @default_servers = default_servers
end

Instance Attribute Details

#default_serversArray<String> (readonly)

Returns List of default servers.

Returns:

  • (Array<String>)

    List of default servers



38
39
40
# File 'src/lib/installation/widgets/ntp_server.rb', line 38

def default_servers
  @default_servers
end

Instance Method Details

#helpObject



102
103
104
105
106
107
108
109
110
# File 'src/lib/installation/widgets/ntp_server.rb', line 102

def help
  # TRANSLATORS: a help text for the NTP server input field
  _("<h3>NTP Servers</h3>") +
    # TRANSLATORS: a help text for the NTP server input field
    _("<p>Enter the host name or the IP address of the NTP server which will be used for " \
      "synchronizing the time on this machine</p>") +
    # TRANSLATORS: a help text for the NTP server input field
    _("<p>Use comma (,) or space to separate multiple values.</p>")
end

#initObject

Initializes the widget's value



76
77
78
79
# File 'src/lib/installation/widgets/ntp_server.rb', line 76

def init
  saved_servers = (role && role["ntp_servers"]) || default_servers
  self.value = saved_servers.join(" ")
end

#labelString

Returns Widget's label.

Returns:

  • (String)

    Widget's label



51
52
53
54
# File 'src/lib/installation/widgets/ntp_server.rb', line 51

def label
  # TRANSLATORS: input field label
  _("N&TP Servers (comma or space separated)")
end

#storeObject

Store the value of the input field if validates



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'src/lib/installation/widgets/ntp_server.rb', line 57

def store
  if servers.empty?
    # we need to reset the previous settings after going back
    Yast::NtpClient.ntp_selected = false
    Yast::NtpClient.modified = false

    return
  end

  Yast::NtpClient.ntp_selected = true
  Yast::NtpClient.modified = true
  Yast::NtpClient.ntp_conf.clear_pools
  servers.each { |server| Yast::NtpClient.ntp_conf.add_pool(server) }
  # run NTP as a service (not via cron)
  Yast::NtpClient.run_service = true
  Yast::NtpClient.synchronize_time = false
end

#validateBoolean

Validate input

  • All specified IPs or hostnames should be valid
  • If no server is specified, ask the user whether proceed with installation or not

Returns:

  • (Boolean)

    true if value is valid; false otherwise.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'src/lib/installation/widgets/ntp_server.rb', line 89

def validate
  return skip_ntp_server? if servers.empty?

  invalid_servers = servers.reject { |v| Yast::IP.Check(v) || Yast::Hostname.CheckFQ(v) }
  return true if invalid_servers.empty?

  Yast::Popup.Error(
    format(_(NOT_VALID_SERVERS_MESSAGE), servers: invalid_servers.join(", "))
  )

  false
end