Class: Simp::Cli::Config::Item::PuppetHostsEntry

Inherits:
ActionItem show all
Defined in:
lib/simp/cli/config/item/puppet_hosts_entry.rb

Instance Attribute Summary collapse

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

#allow_user_apply, #config_items, #description, #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 ActionItem

#query, #to_yaml_s, #validate

Methods included from SafeApplying

#safe_apply

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

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

Constructor Details

#initializePuppetHostsEntry

Returns a new instance of PuppetHostsEntry.



12
13
14
15
16
17
# File 'lib/simp/cli/config/item/puppet_hosts_entry.rb', line 12

def initialize
  super
  @key         = 'puppet::hosts_entry'
  @description = %Q{Ensures an entry for the puppet server in /etc/hosts (apply-only; noop).}
  @file        = '/etc/hosts'
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/simp/cli/config/item/puppet_hosts_entry.rb', line 10

def file
  @file
end

Instance Method Details

#applyObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/simp/cli/config/item/puppet_hosts_entry.rb', line 19

def apply
  puppet_server    = @config_items.fetch( 'puppet::server' ).value
  puppet_server_ip = @config_items.fetch( 'puppet::server::ip' ).value
  status = false

  say_green "Updating #{@file}..." if !@silent

  values = Array.new
  File.readlines(@file).each do |line|
    next if line =~ /\s*#/
    next if line =~ /#{puppet_server}/ and @value.eql?(puppet_server)
    next if line =~ /localdomain/
    next if line =~ /localdomain6/
    next if line =~ /\spuppet(\s|$)/  # remove alias 'puppet'
    values.push(line)
  end
  File.open(@file,'w') {|fh|
    fh.puts('127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4')
    fh.puts('::1 localhost localhost.localdomain localhost6 localhost6.localdomain6')
    fh.puts("#{puppet_server_ip} #{puppet_server} #{puppet_server.split('.').first}")
    fh.puts(values)
  }
  true
end