Project

General

Profile

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

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

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11

    
12
#
13
# -= Puppet Facts CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Host name
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "os%d" -> "os1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22
#   <Fact Key names>
23
#     - May contain '%d' which will be replaced with current iteration number of Count
24
#
25

    
26
require 'hammer_cli'
27
require 'json'
28
require 'csv'
29

    
30
module HammerCLICsv
31
  class PuppetFactsCommand < BaseCommand
32

    
33
    def export
34
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
35
        headers = [NAME, COUNT]
36
        # Extracted facts are always based upon the first host found, otherwise this would be an intensive
37
        # method to gather all the possible column names
38
        host = @api.resource(:hosts).call(:index, {:per_page => 1})['results'][0]
39
        headers += @api.resource(:puppetfactss).call(:index, {'host_id' => host['name'], 'per_page' => 999999})['results'][host['name']].keys
40
        csv << headers
41

    
42
        @api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
43
          line = [host['name'], 1]
44
          facts = @api.resource(:puppetfactss).call(:index, {'host_id' => host['name'], 'per_page' => 999999})[host['name']]
45
          facts ||= {}
46
          headers[2..-1].each do |fact_name|
47
            line << facts[fact_name] || ''
48
          end
49
          csv << line
50
        end
51
      end
52
    end
53

    
54
    def import
55
      @headers = nil
56

    
57
      thread_import(true) do |line|
58
        create_puppetfacts_from_csv(line)
59
      end
60
    end
61

    
62
    def create_puppetfacts_from_csv(line)
63
      if @headers.nil?
64
        @headers = line
65
        return
66
      end
67

    
68
      line[COUNT].to_i.times do |number|
69
        name = namify(line[NAME], number)
70
        print "Updating puppetfacts '#{name}'..." if option_verbose?
71
        facts = line.to_hash
72
        facts.delete(NAME)
73
        facts.delete(COUNT)
74

    
75
        # Namify the values if the host name was namified
76
        if name != line[NAME]
77
          facts.each do |fact, value|
78
            facts[fact] = namify(value, number) unless value.nil? || value.empty?
79
          end
80
        end
81

    
82
        @api.resource(:hosts).call(:facts, {
83
                            'name' => name,
84
                            'facts' => facts
85
                          })
86
        print "done\n" if option_verbose?
87
      end
88
    rescue RuntimeError => e
89
      raise "#{e}\n       #{line}"
90
    end
91
  end
92

    
93
  HammerCLICsv::CsvCommand.subcommand("puppet-facts",
94
                                      "import or export puppet facts",
95
                                      HammerCLICsv::PuppetFactsCommand)
96
end