Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / partition_tables.rb @ bfc065ce

1
# Copyright (c) 2013-2014 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
    OPERATINGSYSTEMS = 'Operating Systems'
45
    LAYOUT = 'Layout'
46

    
47
    def export
48
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
49
        csv << [NAME, COUNT, OSFAMILY, OPERATINGSYSTEMS, LAYOUT]
50
        @f_partitiontable_api.index({:per_page => 999999})[0]['results'].each do |ptable|
51
          ptable = @f_partitiontable_api.show({'id' => ptable['id']})[0]
52
          name = ptable['name']
53
          count = 1
54
          osfamily = ptable['os_family']
55
          layout = ptable['layout']
56
          raise "TODO: operating systems"
57
          csv << [name, count, osfamily, layout]
58
        end
59
      end
60
    end
61

    
62
    def import
63
      @existing = {}
64
      @f_partitiontable_api.index({:per_page => 999999})[0]['results'].each do |ptable|
65
        @existing[ptable['name']] = ptable['id'] if ptable
66
      end
67

    
68
      thread_import do |line|
69
        create_ptables_from_csv(line)
70
      end
71
    end
72

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

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