Class: Simp::Cli::Config::Item::NTPServers

Inherits:
ListItem show all
Defined in:
lib/simp/cli/config/item/ntp_servers.rb

Instance Attribute Summary

Attributes inherited from ListItem

#allow_empty_list

Attributes inherited from Simp::Cli::Config::Item

#allow_user_apply, #config_items, #die_on_apply_fail, #fact, #fail_on_missing_answer, #key, #next_items_tree, #silent, #skip_apply, #skip_query, #skip_yaml, #value

Instance Method Summary collapse

Methods inherited from ListItem

#highline_question_type, #not_valid_message, #query_extras

Methods inherited from Simp::Cli::Config::Item

#apply, #default_value, #highline_question_type, #next_items, #not_valid_message, #print_banner, #print_summary, #puppet_value, #query, #query_ask, #query_extras, #query_status, #safe_apply, #say_blue, #say_green, #say_red, #say_yellow, #to_yaml_s

Constructor Details

#initializeNTPServers

Returns a new instance of NTPServers.



10
11
12
13
14
15
16
17
18
19
# File 'lib/simp/cli/config/item/ntp_servers.rb', line 10

def initialize
  super
  @key              = 'ntpd::servers'
  @warnings         = {
    :no_ntp            => "A consistent time source is critical to your systems' security.",
    :warning_hw_clocks => "DO NOT run multiple production systems using individual hardware clocks!",
  }
  @description      =  "Your network's NTP time servers.\n\n#{@warnings.values.join("\n")}"
  @allow_empty_list = true
end

Instance Method Details

#descriptionObject



21
22
23
24
25
26
27
28
# File 'lib/simp/cli/config/item/ntp_servers.rb', line 21

def description
  extra = ''
  if @config_items.key? 'gateway'
    gateway  = @config_items.fetch('gateway').value
    extra = "\nFor many networks, the default gateway (#{gateway}) provides an NTP server."
  end
  "#{@description}#{extra}"
end

#os_value(file = '/etc/ntp/ntpservers') ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/simp/cli/config/item/ntp_servers.rb', line 30

def os_value( file='/etc/ntp/ntpservers' )
  # TODO: make this a custom fact?
  # TODO: is /etc/ntp/ntpservers being used in recent versions of SIMP?
  servers = []
  if File.readable? file
    File.readlines( file ).map do |line|
      line.strip!
      if line !~ /^#/
        servers << line
      else
        nil
      end
    end.compact
  end
  servers
end


47
48
49
50
51
52
53
# File 'lib/simp/cli/config/item/ntp_servers.rb', line 47

def recommended_value
  if (!os_value.empty?) && (os_value.first !~ /^127\./)
    os_value
  else
    nil
  end
end

#validate(list) ⇒ Object

allow empty NTP servers, but reiterate warning because it’s important.



56
57
58
59
60
61
62
# File 'lib/simp/cli/config/item/ntp_servers.rb', line 56

def validate list
  if !@silent && (list.is_a?(Array) || list.is_a?(String)) && list.empty?
    say_red( "IMPORTANT: #{@warnings.fetch(:no_ntp)}" )
    sleep 3  # TODO: should there be a standard timeout for Item delays?
  end
  super
end

#validate_item(item) ⇒ Object



64
65
66
67
# File 'lib/simp/cli/config/item/ntp_servers.rb', line 64

def validate_item item
  ( Simp::Cli::Config::Utils.validate_ip( item ) ||
    Simp::Cli::Config::Utils.validate_fqdn( item ) )
end