Class: Simp::Cli::Config::Item::PuppetConf

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

#initializePuppetConf

Returns a new instance of PuppetConf.



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

def initialize
  super
  @key         = 'puppet::conf'
  @description = 'silent item; configures /etc/puppet/puppet.conf'
  # FIXME: this path will change with Puppet Enterprise; should this autodetect?
  @file        = '/etc/puppet/puppet.conf'
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



9
10
11
# File 'lib/simp/cli/config/item/puppet_conf.rb', line 9

def file
  @file
end

Instance Method Details

#applyObject

NOTE: This is (mostly) lifted straight from the old simp config TODO: refactor sed statements to pure ruby,

consider using IO handles instead of File.open (easier to test in memory)?
or use Puppet::Settings ( https://github.com/puppetlabs/puppet/blob/master/lib/puppet/settings.rb )?


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
# File 'lib/simp/cli/config/item/puppet_conf.rb', line 23

def apply
  say_green "Updating #{@file}..." if !@silent
  if @skip_apply
    say_yellow "WARNING: directed to skip Puppet configuration of #{file}" if !@silent
    return
  end

  backup_file = "#{@file}.pre_simpconfig"
  FileUtils.cp("#{@file}", backup_file)
  `sed -i '/^\s*server.*/d'          #{@file}`
  `sed -i '/.*trusted_node_data.*/d' #{@file}`
  `sed -i '/.*digest_algorithm.*/d'  #{@file}`
  `sed -i '/.*stringify_facts.*/d'   #{@file}`
  `sed -i '/.*environment_path.*/d'  #{@file}`
  `sed -i '/^.main./ a \\    trusted_node_data = true\'   #{@file}`
  `sed -i '/^.main./ a \\    digest_algorithm  = sha256\' #{@file}`
  `sed -i '/^.main./ a \\    stringify_facts   = false\'  #{@file}`
  `sed -i '/^.main./ a \\    environmentpath   = /etc/puppet/environments\' #{@file}`
  `sed -i '/trusted_node_data/ a \\    server            = #{@config_items.fetch( 'puppet::server' ).value}\' #{@file}`
  keylength = @config_items.fetch( 'use_fips', nil )? '2048' : '4096'
  `sed -i '/^.main./ a \\    keylength         = #{keylength}\' #{@file}`

  # do not die if config items aren't found
  puppet_server  = 'puppet.change.me'
  puppet_ca      = 'puppetca.change.me'
  puppet_ca_port = '8141'
  if item = @config_items.fetch( 'puppet::server', nil )
    puppet_server  = item.value
  end
  if item = @config_items.fetch( 'puppet::ca', nil )
    puppet_ca      = item.value
  end
  if item = @config_items.fetch( 'puppet::ca_port', nil )
    puppet_ca_port = item.value
  end

  puppet_conf = File.readlines(@file)
  File.open("#{@file}", 'w') do |out_file|
    line_check = {
      'server'    => false,
      'ca_server' => false,
      'ca_port'   => false
    }
    puppet_conf.each do |line|
      if line !~ /^\s*(#{line_check.keys.join('|')})(\s*=\s*)/
        out_file.puts line
      else
        $1.chomp
        line_check[$1] = true
        case $1
          when 'server' then
            out_file.puts "    #{$1}#{$2}#{puppet_server}"
          when 'ca_server' then
            out_file.puts "    #{$1}#{$2}#{puppet_ca}"
          when 'ca_port' then
            out_file.puts "    #{$1}#{$2}#{puppet_ca_port}"
        end
      end
    end
    line_check.keys.each do |key|
      if not line_check[key] then
        case key
          when 'server' then
            out_file.puts "    server    = #{puppet_server}"
          when 'ca_server' then
            out_file.puts "    ca_server = #{puppet_ca}"
          when 'ca_port' then
            out_file.puts "    ca_port   = #{puppet_ca_port}"
        end
      end
    end
  end

end