Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / operating_systems.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
# -= Operating Systems CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Operating system 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
#   Major
35
#   Minor
36
#   Family
37
#
38

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

    
43
module HammerCLICsv
44
  class OperatingSystemsCommand < BaseCommand
45

    
46
    FAMILY = 'Family'
47
    ARCHITECTURES = 'Architectures'
48
    PARTITIONTABLES = 'Partition Tables'
49

    
50
    def execute
51
      super
52
      csv_export? ? export : import
53
      HammerCLI::EX_OK
54
    end
55

    
56
    def export
57
      CSV.open(csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
58
        csv << [NAME, COUNT, FAMILY, ARCHITECTURES, PARTITIONTABLES]
59
        @f_operatingsystem_api.index({:per_page => 999999}, HEADERS)[0].each do |operatingsystem|
60
          name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])
61
          count = 1
62
          family = operatingsystem['family']
63
          architectures = ""
64
          operatingsystem['architectures'].each do |architecture|
65
            architectures += "," unless architectures == ""
66
            architectures += architecture['name']
67
          end
68
          partitiontables = ""
69
          operatingsystem['ptables'].each do |partitiontable|
70
            partitiontables += "," unless partitiontables == ""
71
            partitiontables += partitiontable['name']
72
          end
73
          csv << [name, count, family, architectures, partitiontables]
74
        end
75
      end
76
    end
77

    
78
    def import
79
      @existing = {}
80
      @f_operatingsystem_api.index({:per_page => 999999}, HEADERS)[0].each do |operatingsystem|
81
        @existing[build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])] = operatingsystem['id']
82
      end
83

    
84
      thread_import do |line|
85
        create_operatingsystems_from_csv(line)
86
      end
87
    end
88

    
89
    def create_operatingsystems_from_csv(line)
90
      architecture_ids = CSV.parse_line(line[ARCHITECTURES]).collect do |name|
91
        foreman_architecture(:name => name)
92
      end unless line[ARCHITECTURES].empty?
93
      architecture_ids ||= []
94
      partitiontable_ids = CSV.parse_line(line[PARTITIONTABLES]).collect do |name|
95
        foreman_partitiontable(:name => name)
96
      end unless line[PARTITIONTABLES].empty?
97
      partitiontables_ids ||= []
98

    
99
      line[COUNT].to_i.times do |number|
100
        name = namify(line[NAME], number)
101
        (osname, major, minor) = split_os_name(name)
102
        if !@existing.include? name
103
          print "Creating operating system '#{name}'..." if verbose?
104
          @f_operatingsystem_api.create({
105
                                          'operatingsystem' => {
106
                                            'name' => osname,
107
                                            'major' => major,
108
                                            'minor' => minor,
109
                                            'family' => line[FAMILY],
110
                                            'architecture_ids' => architecture_ids,
111
                                            'ptable_ids' => partitiontable_ids
112
                                          }
113
                                        }, HEADERS)
114
        else
115
          print "Updating operatingsystem '#{name}'..." if verbose?
116
          @f_operatingsystem_api.update({
117
                                          'id' => @existing[name],
118
                                          'operatingsystem' => {
119
                                            'name' => osname,
120
                                            'major' => major,
121
                                            'minor' => minor,
122
                                            'family' => line[FAMILY],
123
                                            'architecture_ids' => architecture_ids,
124
                                            'ptable_ids' => partitiontable_ids
125
                                          }
126
                                        }, HEADERS)
127
        end
128
        print "done\n" if verbose?
129
      end
130
    rescue RuntimeError => e
131
      raise RuntimeError.new("#{e}\n       #{line}")
132
    end
133
  end
134

    
135
  HammerCLI::MainCommand.subcommand("csv:operatingsystems", "ping the katello server", HammerCLICsv::OperatingSystemsCommand)
136
end