Class: Kafo::PuppetCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/puppet_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options = [], puppet_config = nil, configuration = KafoConfigure.config) ⇒ PuppetCommand

Returns a new instance of PuppetCommand.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kafo/puppet_command.rb', line 4

def initialize(command, options = [], puppet_config = nil, configuration = KafoConfigure.config)
  @configuration = configuration
  @command = command
  @puppet_config = puppet_config

  if puppet_config
    puppet_config['basemodulepath'] = modules_path.join(':')
    @options = options.push("--config=#{puppet_config.config_path}")
  else
    @options = options.push("--modulepath #{modules_path.join(':')}")
  end
  @logger  = KafoConfigure.logger
  @puppet_version_check = !configuration.app[:skip_puppet_version_check]
  @suffix = nil
end

Class Method Details

.aio_puppet?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/kafo/puppet_command.rb', line 46

def self.aio_puppet?
  puppet_command = search_puppet_path('puppet')
  File.realpath(puppet_command).start_with?('/opt/puppetlabs')
rescue Errno::ENOENT
  false
end

.clean_env_varsObject



61
62
63
64
65
66
67
68
69
# File 'lib/kafo/puppet_command.rb', line 61

def self.clean_env_vars
  # Cleaning ENV vars and keeping required vars only because,
  # When using SCL it adds GEM_HOME and GEM_PATH ENV vars.
  whitelisted_vars = %w[HOME USER LANG]

  cleaned_env = ::ENV.select { |var| whitelisted_vars.include?(var) || var.start_with?('LC_') }
  cleaned_env['PATH'] = '/sbin:/bin:/usr/sbin:/usr/bin:/opt/puppetlabs/bin'
  cleaned_env
end

.format_command(command) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/kafo/puppet_command.rb', line 53

def self.format_command(command)
  if aio_puppet?
    [clean_env_vars, command, :unsetenv_others => true]
  else
    [::ENV, command, :unsetenv_others => false]
  end
end

.search_puppet_path(bin_name) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/kafo/puppet_command.rb', line 37

def self.search_puppet_path(bin_name)
  # Find the location of the puppet executable and use that to
  # determine the path of all executables
  bin_path = (::ENV['PATH'].split(File::PATH_SEPARATOR) + ['/opt/puppetlabs/bin']).find do |path|
    File.executable?(File.join(path, 'puppet')) && File.executable?(File.join(path, bin_name))
  end
  File.join([bin_path, bin_name].compact)
end

Instance Method Details

#append(suffix) ⇒ Object



32
33
34
35
# File 'lib/kafo/puppet_command.rb', line 32

def append(suffix)
  @suffix = suffix
  self
end

#commandObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kafo/puppet_command.rb', line 20

def command
  @puppet_config.write_config if @puppet_config
  result = [
      manifest,
      '|',
      "RUBYLIB=#{[@configuration.kafo_modules_dir, ::ENV['RUBYLIB']].join(File::PATH_SEPARATOR)}",
      "#{puppet_path} apply #{@options.join(' ')} #{@suffix}",
  ].join(' ')
  @logger.debug result
  result
end