Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / puppet_facts.rb @ 561a8ac9

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 CsvCommand
32
    class PuppetFactsCommand < BaseCommand
33
      command_name 'puppet-facts'
34
      desc         'import or export puppet facts'
35

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

    
48
          @api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
49
            line = [host['name'], 1]
50
            facts = @api.resource(:puppetfactss).call(:index, {'host_id' => host['name'], 'per_page' => 999999})[host['name']]
51
            facts ||= {}
52
            headers[2..-1].each do |fact_name|
53
              line << facts[fact_name] || ''
54
            end
55
            csv << line
56
          end
57
        end
58
      end
59

    
60
      def import
61
        @headers = nil
62

    
63
        thread_import(true) do |line|
64
          create_puppetfacts_from_csv(line)
65
        end
66
      end
67

    
68
      def create_puppetfacts_from_csv(line)
69
        if @headers.nil?
70
          @headers = line
71
          return
72
        end
73

    
74
        line[COUNT].to_i.times do |number|
75
          name = namify(line[NAME], number)
76
          print "Updating puppetfacts '#{name}'..." if option_verbose?
77
          facts = line.to_hash
78
          facts.delete(NAME)
79
          facts.delete(COUNT)
80

    
81
          # Namify the values if the host name was namified
82
          if name != line[NAME]
83
            facts.each do |fact, value|
84
              facts[fact] = namify(value, number) unless value.nil? || value.empty?
85
            end
86
          end
87

    
88
          @api.resource(:hosts).call(:facts, {
89
                              'name' => name,
90
                              'facts' => facts
91
                            })
92
          print "done\n" if option_verbose?
93
        end
94
      rescue RuntimeError => e
95
        raise "#{e}\n       #{line}"
96
      end
97
    end
98
  end
99
end