Class: Sensu::Translator::Runner

Inherits:
Object
  • Object
show all
Includes:
Translations
Defined in:
lib/sensu/translator.rb

Instance Method Summary collapse

Methods included from Translations

#go_spec, #translate_check, #translate_extension, #translate_filter, #translate_handler, #translate_mutator

Constructor Details

#initializeRunner

Returns a new instance of Runner.



15
16
17
# File 'lib/sensu/translator.rb', line 15

def initialize
  @options = CLI.read
end

Instance Method Details

#create_go_output_files!(go_resources) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sensu/translator.rb', line 40

def create_go_output_files!(go_resources)
  output_dir = @options[:output_dir]
  Sensu::Settings::CATEGORIES.each do |category|
    category_dir = File.join(output_dir, category.to_s)
    FileUtils.mkdir_p(category_dir)
  end
  go_resources.each do |go_resource|
    category = "#{go_resource[:type].downcase}s"
    file_name = "#{go_resource[:metadata][:name]}.json"
    output_file = File.join(output_dir, category, file_name)
    content = Sensu::JSON.dump(go_resource, :pretty => true)
    File.open(output_file, "w") do |file|
      file.write(content)
    end
  end
end

#load_v1_settingsObject



19
20
21
22
23
24
25
26
27
# File 'lib/sensu/translator.rb', line 19

def load_v1_settings
  settings = Sensu::Settings.load(@options)
  unless settings.errors.empty?
    puts "Sensu 1.x configuration is invalid!"
    puts Sensu::JSON.dump({:errors => settings.errors}, :pretty => true)
    exit 2
  end
  settings.to_hash
end

#runObject



57
58
59
60
61
62
# File 'lib/sensu/translator.rb', line 57

def run
  v1_settings = load_v1_settings
  go_resources = translate(v1_settings)
  create_go_output_files!(go_resources)
  puts "DONE!"
end

#translate(v1_settings) ⇒ Object



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

def translate(v1_settings)
  go_resources = []
  Sensu::Settings::CATEGORIES.each do |category|
    method_name = "translate_#{category.to_s.chop}"
    v1_settings[category].each do |name, object|
      go_resources << send(method_name, object, @options[:namespace], name)
    end
  end
  go_resources.compact
end