Project

General

Profile

Download (2.62 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / puppet_facts.rb @ f92e695a

1 42d8c0ad Tom McKay
module HammerCLICsv
2 7bea883f Grant Gainey
  class CsvCommand
3
    class PuppetFactsCommand < BaseCommand
4 26b7d467 Tom McKay
      command_name 'puppet-facts'
5
      desc         'import or export puppet facts'
6 42d8c0ad Tom McKay
7 6f221b51 Tom McKay
      ORGANIZATION = 'Organization'
8
      FACTS = 'Puppet Facts'
9
10
      SEPARATOR = ' = '
11
12 c06f1783 Tom McKay
      def export(csv)
13
        csv << [NAME, ORGANIZATION, FACTS]
14
15
        search_options = {:per_page => 999999}
16
        search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
17
        @api.resource(:hosts).call(:index, search_options)['results'].each do |host|
18
          facts = @api.resource(:fact_values).call(:index, {
19
                                                             'search' => "host = #{host['name']}",
20
                                                             'per_page' => 999999
21
                                                           })['results']
22
          facts = @api.resource(:fact_values).call(:index, {
23
                                                             'search' => "host = #{host['name']}",
24
                                                             'per_page' => 999999
25
                                                           })['results'][host['name']]
26
          facts ||= {}
27
28
          values = CSV.generate do |column|
29
            column << facts.collect do |fact_name, fact_value|
30
              "#{fact_name}#{SEPARATOR}#{fact_value}"
31 7bea883f Grant Gainey
            end
32 42d8c0ad Tom McKay
          end
33 c06f1783 Tom McKay
          values.delete!("\n")
34
35
          csv << [host['name'], host['organization_name'], values]
36 42d8c0ad Tom McKay
        end
37
      end
38
39 7bea883f Grant Gainey
      def import
40
        thread_import(true) do |line|
41
          create_puppetfacts_from_csv(line)
42
        end
43 42d8c0ad Tom McKay
      end
44
45 7bea883f Grant Gainey
      def create_puppetfacts_from_csv(line)
46 6f221b51 Tom McKay
        return if option_organization && line[ORGANIZATION] != option_organization
47 42d8c0ad Tom McKay
48 02387fb5 Tom McKay
        count(line[COUNT]).times do |number|
49 7bea883f Grant Gainey
          name = namify(line[NAME], number)
50
          print "Updating puppetfacts '#{name}'..." if option_verbose?
51 6f221b51 Tom McKay
          facts = {}
52
          collect_column(line[FACTS]) do |fact|
53
            (fact_name, fact_value) = fact.split(SEPARATOR)
54
            facts[fact_name] = fact_value
55
          end
56 42d8c0ad Tom McKay
57 7bea883f Grant Gainey
          # Namify the values if the host name was namified
58
          if name != line[NAME]
59
            facts.each do |fact, value|
60
              facts[fact] = namify(value, number) unless value.nil? || value.empty?
61
            end
62 42d8c0ad Tom McKay
          end
63
64 7bea883f Grant Gainey
          @api.resource(:hosts).call(:facts, {
65 6f221b51 Tom McKay
                                               'name' => name,
66
                                               'facts' => facts
67
                                             })
68 7bea883f Grant Gainey
          print "done\n" if option_verbose?
69
        end
70 42d8c0ad Tom McKay
      end
71
    end
72
  end
73
end