Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / puppet_facts.rb @ 0c7641a0

1
# Copyright (c) 2013 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24
#
25
# -= Puppet Facts CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Host name
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "os%d" -> "os1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34
#   <Fact Key names>
35
#     - May contain '%d' which will be replaced with current iteration number of Count
36
#
37

    
38
require 'hammer_cli'
39
require 'json'
40
require 'csv'
41

    
42
module HammerCLICsv
43
  class PuppetFactsCommand < BaseCommand
44

    
45
    def execute
46
      super
47
      csv_export? ? export : import
48
      HammerCLI::EX_OK
49
    end
50

    
51
    def export
52
      CSV.open(csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
53
        headers = [NAME, COUNT]
54
        # Extracted facts are always based upon the first host found, otherwise this would be an intensive
55
        # method to gather all the possible column names
56
        host = @f_host_api.index({:per_page => 1}, HEADERS)[0][0]
57
        host = host['host'] # TODO: old style
58
        headers += @f_puppetfacts_api.index({'host_id' => host['name'], 'per_page' => 999999}, HEADERS)[0][host['name']].keys
59
        csv << headers
60

    
61
        @f_host_api.index({:per_page => 999999}, HEADERS)[0].each do |host|
62
          host = host['host'] # TODO: old style
63
          line = [host['name'], 1]
64
          facts = @f_puppetfacts_api.index({'host_id' => host['name'], 'per_page' => 999999}, HEADERS)[0][host['name']]
65
          facts ||= {}
66
          headers[2..-1].each do |fact_name|
67
            line << facts[fact_name] || ''
68
          end
69
          csv << line
70
        end
71
      end
72
    end
73

    
74
    def import
75
      @headers = nil
76

    
77
      thread_import(true) do |line|
78
        create_puppetfacts_from_csv(line)
79
      end
80
    end
81

    
82
    def create_puppetfacts_from_csv(line)
83
      if @headers.nil?
84
        @headers = line
85
        return
86
      end
87

    
88
      line[COUNT].to_i.times do |number|
89
        name = namify(line[NAME], number)
90
        print "Updating puppetfacts '#{name}'..." if verbose?
91
        facts = line.to_hash
92
        facts.delete(NAME)
93
        facts.delete(COUNT)
94

    
95
        # Namify the values if the host name was namified
96
        if name != line[NAME]
97
          facts.each do |fact, value|
98
            facts[fact] = namify(value, number) unless value.nil? || value.empty?
99
          end
100
        end
101

    
102
        @f_host_api.facts({
103
                            'name' => name,
104
                            'facts' => facts
105
                          }, HEADERS)
106
        print "done\n" if verbose?
107
      end
108
    rescue RuntimeError => e
109
      raise RuntimeError.new("#{e}\n       #{line}")
110
    end
111
  end
112

    
113
  HammerCLI::MainCommand.subcommand("csv:puppetfacts", "ping the katello server", HammerCLICsv::PuppetFactsCommand)
114
end