Class: Simp::Cli::Config::Item::PuppetFileServer

Inherits:
ActionItem show all
Defined in:
lib/simp/cli/config/item/puppet_fileserver.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

#initializePuppetFileServer

Returns a new instance of PuppetFileServer.



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

def initialize
  super
  @key         = 'puppet::fileserver'
  @description = 'silent item; configures /etc/puppet/fileserver.conf'
  @file        = '/etc/puppet/fileserver.conf'
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/simp/cli/config/item/puppet_fileserver.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
43
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
# File 'lib/simp/cli/config/item/puppet_fileserver.rb', line 19

def apply
  say_green "  updating Puppet configurations in #{@file}..." if !@silent

  conf = @file

  require 'fileutils'
  FileUtils.cp(conf, "#{conf}.pre_simpconfig")

  hostname = @config_items.fetch( 'hostname' ) #FIXME: should this be hostname or puppet_server?
  domain = hostname.value.split('.')[1..-1].join('.')

  if !domain or domain.empty?
    raise "Could not determine domain from hostname '#{hostname}"
  end

  default_entries = ['facts','plugins','keydist','cacerts','mcollective']

  fileserver_default = <<-EOM
    [facts]
      path /etc/puppet/facts
      allow *.#{domain}

    [plugins]
      allow *.#{domain}

    [keydist]
      path /etc/puppet/keydist/%H
      allow *.#{domain}

    [cacerts]
      path /etc/puppet/keydist/cacerts
      allow *.#{domain}

    [mcollective]
      path /etc/puppet/keydist/mcollective
      allow *.#{domain}
  EOM

  # Complete crib from StackOverflow
  fileserver_default.gsub!(/^#{fileserver_default[/\A\s*/]}/,'')

  fileserver_new = []

  fileserver_old = File.read(conf).split("\n")

  # Preserve any beginning comments
  while fileserver_old[0] =~ /^\s*(#.*|\s*)$/ do
    fileserver_new << fileserver_old.shift
  end

  # Add in our defaults
  fileserver_new << fileserver_default

  # Read the rest of the file, ignoring any section that we're going to
  # replace.
  key = nil
  comments = []
  fileserver_old.each do |line|
    if line =~ /\[(.*)\]/ then
      key = $1.strip
      comments = []
    end

    next if default_entries.include?(key)

    fileserver_new << line
  end

  # If the last entry was a default entry key, preserve the trailing file
  # comments (if any)
  if default_entries.include?(key) then
    fileserver_new << "\n"
    fileserver_new += comments
  end

  # Smash duplicates
  fileserver_new.each_with_index do |x,i|
    fileserver_new.delete(i) unless fileserver_new[i] != fileserver_new[i + 1]
  end

  File.open(conf,'w'){|x| x.puts(fileserver_new.join("\n"))}

  true
end