Class: Pupistry::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/pupistry/config.rb

Overview

Pupistry::Config

Provides loading of configuration.

Class Method Summary collapse

Class Method Details

.find_and_loadObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pupistry/config.rb', line 80

def self.find_and_load
  $logger.debug 'Looking for configuration file in common locations'

  # If the HOME environmental hasn't been set (which can happen when
  # running via some cloud user-data/init systems) the app will die
  # horribly, we should set a HOME path default.
  unless ENV['HOME']
    $logger.warn 'No HOME environmental set, defaulting to /tmp'
    ENV['HOME'] = '/tmp'
  end

  # Locations in order of preference:
  # settings.yaml (current dir)
  # ~/.pupistry/settings.yaml
  # /etc/pupistry/settings.yaml

  config    = ''
  local_dir = Dir.pwd

  if File.exist?("#{local_dir}/settings.yaml")
    config = "#{local_dir}/settings.yaml"

  elsif File.exist?(File.expand_path '~/.pupistry/settings.yaml')
    config = File.expand_path '~/.pupistry/settings.yaml'

  elsif File.exist?('/usr/local/etc/pupistry/settings.yaml')
    config = '/usr/local/etc/pupistry/settings.yaml'

  elsif File.exist?('/etc/pupistry/settings.yaml')
    config = '/etc/pupistry/settings.yaml'

  else
    $logger.error 'No configuration file provided.'
    $logger.error 'See pupistry help for information on configuration'
    exit 0
  end

  load(config)
end

.load(file) ⇒ Object



16
17
18
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
# File 'lib/pupistry/config.rb', line 16

def self.load(file)
  $logger.debug "Loading configuration file #{file}"
 
  # Load YAML file with minimum safety/basic checks
  unless File.exist?(file)
    $logger.fatal 'The configuration file provided does not exist, or cannot be accessed'
    exit 0
  end

  begin
    $config = YAML.load(File.open(file), safe: true, raise_on_unknown_tag: true)
  rescue => ex
    $logger.fatal 'The supplied file is not a valid YAML configuration file'
    $logger.debug ex.message
    exit 0
  end


  # Run checks for minimum configuration parameters
  # TODO: Is there a smarter way of doing this? Maybe a better config parser?
  begin
    fail 'Missing general:app_cache'         unless defined? $config['general']['app_cache']
    fail 'Missing general:s3_bucket'         unless defined? $config['general']['s3_bucket']
    fail 'Missing general:gpg_disable'       unless defined? $config['general']['gpg_disable']
    fail 'Missing agent:puppetcode'          unless defined? $config['agent']['puppetcode']
  rescue => ex
    $logger.fatal 'The supplied configuration files doesn\'t include the minimum expect configuration parameters'
    $logger.debug ex.message
    exit 0
  end

  

  # Make sure cache directory exists, create it otherwise
  $config['general']['app_cache'] = File.expand_path($config['general']['app_cache']).chomp('/')

  unless Dir.exist?($config['general']['app_cache'])
    begin
      FileUtils.mkdir_p($config['general']['app_cache'])
      FileUtils.chmod(0700, $config['general']['app_cache']) # Generally only the user running Pupistry should have access
    rescue StandardError => e
      $logger.fatal "Unable to create cache directory at \"#{$config['general']['app_cache']}\"."
      raise e
    end
  end

  # Write test file to confirm writability
  begin
    FileUtils.touch($config['general']['app_cache'] + '/testfile')
    FileUtils.rm($config['general']['app_cache'] + '/testfile')
  rescue StandardError => e
    $logger.fatal "Unexpected exception when creating testfile in cache directory at \"#{$config['general']['app_cache']}\", is the directory writable?"
    raise e
  end


  # Check if Puppet is available
  unless system('puppet --version 2>&1 > /dev/null')
    $logger.fatal "Unable to find an installation of Puppet - please make sure Puppet is installed from either OS package or Gem"
    exit 0
  end

end

.which_tarObject

Return which tar binary to use.



121
122
123
124
125
126
127
# File 'lib/pupistry/config.rb', line 121

def self.which_tar
    # Try to use GNU tar if present to work around weird issues with some
    # versions of BSD tar when using the tar files with GNU tar subsequently.
    tar = RubyWhich.new.which('gtar').first || RubyWhich.new.which('gnutar').first || 'tar'

    return tar
end