Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / partition_tables.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
# -= Partition Tables CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Partition table 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
#
35

    
36
require 'hammer_cli'
37
require 'json'
38
require 'csv'
39

    
40
module HammerCLICsv
41
  class PartitionTablesCommand < BaseCommand
42

    
43
    OSFAMILY = 'OS Family'
44
    LAYOUT = 'Layout'
45

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

    
52
    def export
53
      CSV.open(csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
54
        csv << [NAME, COUNT, OSFAMILY, LAYOUT, OPERATINGSYSTEMS]
55
        @f_ptable_api.index({:per_page => 999999}, HEADERS)[0].each do |ptable|
56
          name = ptable['name']
57
          count = 1
58
          osfamily = ptable['os_family']
59
          layout = ptable['layout']
60
          puts ptable
61
          csv << [name, count, osfamily, layout]
62
        end
63
      end
64
    end
65

    
66
    def import
67
      @existing = {}
68
      @f_partitiontable_api.index({:per_page => 999999}, HEADERS)[0].each do |ptable|
69
        @existing[ptable['name']] = ptable['id']
70
      end
71

    
72
      thread_import do |line|
73
        create_ptables_from_csv(line)
74
      end
75
    end
76

    
77
    def create_ptables_from_csv(line)
78
      line[COUNT].to_i.times do |number|
79
        name = namify(line[NAME], number)
80
        if !@existing.include? name
81
          print "Creating ptable '#{name}'... " if verbose?
82
          @f_partitiontable_api.create({
83
                                 'ptable' => {
84
                                   'name' => name,
85
                                   'os_family' => line[OSFAMILY],
86
                                   'layout' => line[LAYOUT]
87
                             }
88
                           }, HEADERS)
89
        else
90
          print "Updating ptable '#{name}'..." if verbose?
91
          @f_partitiontable_api.update({
92
                                 'id' => @existing[name],
93
                                 'ptable' => {
94
                                   'name' => name,
95
                                   'os_family' => line[OSFAMILY],
96
                                   'layout' => line[LAYOUT]
97
                                 }
98
                           }, HEADERS)
99
        end
100
        print "done\n" if verbose?
101
      end
102
    rescue RuntimeError => e
103
      raise RuntimeError.new("#{e}\n       #{line}")
104
    end
105
  end
106

    
107
  HammerCLI::MainCommand.subcommand("csv:partitiontables", "ping the katello server", HammerCLICsv::PartitionTablesCommand)
108
end