Class: Talos

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/talos.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepare_config(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/talos.rb', line 29

def self.prepare_config(path)
  set :talos, YAML.load_file(path)
  settings.talos['ssl'] = true if settings.talos['ssl'].nil?
  settings.talos['scopes'].each do |scope_config|
    begin
      scope_config['regexp'] = Regexp.new(scope_config['match'])
    rescue
      fail "Invalid regexp: #{scope_config['match']}"
    end
  end
end

Instance Method Details

#absolute_datadirObject



56
57
58
59
# File 'lib/talos.rb', line 56

def absolute_datadir
  datadir = settings.hiera[:yaml][:datadir]
  Pathname.new(datadir).relative? ? File.join(File.dirname(__FILE__), '..', datadir) : datadir
end

#compress_files(files) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/talos.rb', line 90

def compress_files(files)
  output = StringIO.new
  begin
    sgz = Zlib::GzipWriter.new(output)
    tar = Minitar::Output.new(sgz)
    Dir.chdir(absolute_datadir) { files.each { |f| Minitar.pack_file(f, tar) } }
  ensure
    tar.close
  end
  output
end

#files_in_scope(scope) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/talos.rb', line 79

def files_in_scope(scope)
  files = []
  Hiera::Backend.datasources(scope, nil) do |source, yamlfile|
    yamlfile = Hiera::Backend.datafile(:yaml, scope, source, 'yaml') || next
    next unless File.exist?(yamlfile)
    # Strip path from filename
    files << yamlfile.gsub(settings.hiera[:yaml][:datadir] + '/', '')
  end
  files
end

#get_scope(fqdn) ⇒ Object

Extracts scopes from FQDN using regexp with named captures Falls back to insecure arguments passed by the puppet agent (needed for the hosts not following naming convention)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/talos.rb', line 64

def get_scope(fqdn)
  scope = {'fqdn' => fqdn}
  settings.talos['scopes'].each do | scope_config|
    if m = fqdn.match(scope_config['regexp'])
      scope.update(Hash[ m.names.zip( m.captures ) ])
      scope.update(scope_config['facts'])
    end
  end

  unsafe_scope = settings.talos['unsafe_scopes'] ? request.env['rack.request.query_hash'] : {}
  scope.update(unsafe_scope)
  # scope = {"pod"=>"lon3", "site"=>"lon", "role"=>"puppet", "pool"=>"a"}
  scope
end