Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / partition_tables.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
# -= Partition Tables CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Partition table 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
#
23

    
24
require 'hammer_cli'
25
require 'json'
26
require 'csv'
27

    
28
module HammerCLICsv
29
  class PartitionTablesCommand < BaseCommand
30

    
31
    OSFAMILY = 'OS Family'
32
    OPERATINGSYSTEMS = 'Operating Systems'
33
    LAYOUT = 'Layout'
34

    
35
    def export
36
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
37
        csv << [NAME, COUNT, OSFAMILY, OPERATINGSYSTEMS, LAYOUT]
38
        @api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable|
39
          ptable = @api.resource(:ptables).call(:show, {'id' => ptable['id']})
40
          name = ptable['name']
41
          count = 1
42
          osfamily = ptable['os_family']
43
          layout = ptable['layout']
44
          raise "TODO: operating systems"
45
          csv << [name, count, osfamily, layout]
46
        end
47
      end
48
    end
49

    
50
    def import
51
      @existing = {}
52
      @api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable|
53
        @existing[ptable['name']] = ptable['id'] if ptable
54
      end
55

    
56
      thread_import do |line|
57
        create_ptables_from_csv(line)
58
      end
59
    end
60

    
61
    def create_ptables_from_csv(line)
62
      line[COUNT].to_i.times do |number|
63
        name = namify(line[NAME], number)
64
        operatingsystem_ids = CSV.parse_line(line[OPERATINGSYSTEMS]).collect do |operatingsystem_name|
65
          foreman_operatingsystem(:name => operatingsystem_name)
66
        end if line[OPERATINGSYSTEMS]
67
        if !@existing.include? name
68
          print "Creating ptable '#{name}'... " if option_verbose?
69
          @api.resource(:ptables).call(:create, {
70
                                         'ptable' => {
71
                                           'name' => name,
72
                                           'os_family' => line[OSFAMILY],
73
                                           'operatingsystem_ids' => operatingsystem_ids,
74
                                           'layout' => line[LAYOUT]
75
                                         }
76
                                       })
77
        else
78
          print "Updating ptable '#{name}'..." if option_verbose?
79
          @api.resource(:ptables).call(:update, {
80
                                         'id' => @existing[name],
81
                                         'ptable' => {
82
                                           'name' => name,
83
                                           'os_family' => line[OSFAMILY],
84
                                           'operatingsystem_ids' => operatingsystem_ids,
85
                                           'layout' => line[LAYOUT]
86
                                 }
87
                           })
88
        end
89
        print "done\n" if option_verbose?
90
      end
91
    rescue RuntimeError => e
92
      raise "#{e}\n       #{line}"
93
    end
94
  end
95

    
96
  HammerCLICsv::CsvCommand.subcommand("partition-tables",
97
                                      "import or export partition tables",
98
                                      HammerCLICsv::PartitionTablesCommand)
99
end