Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / operating_systems.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
# -= 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

    
48
    def export
49
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
50
        csv << [NAME, COUNT, FAMILY]
51
        @f_operatingsystem_api.index({:per_page => 999999})[0]['results'].each do |operatingsystem|
52
          name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])
53
          count = 1
54
          family = operatingsystem['family']
55
          csv << [name, count, family]
56
        end
57
      end
58
    end
59

    
60
    def import
61
      @existing = {}
62
      @f_operatingsystem_api.index({:per_page => 999999})[0]['results'].each do |operatingsystem|
63
        @existing[build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])] = operatingsystem['id'] if operatingsystem
64
      end
65

    
66
      thread_import do |line|
67
        create_operatingsystems_from_csv(line)
68
      end
69
    end
70

    
71
    def create_operatingsystems_from_csv(line)
72
      line[COUNT].to_i.times do |number|
73
        name = namify(line[NAME], number)
74
        (osname, major, minor) = split_os_name(name)
75
        if !@existing.include? name
76
          print "Creating operating system '#{name}'..." if option_verbose?
77
          @f_operatingsystem_api.create({
78
                                          'operatingsystem' => {
79
                                            'name' => osname,
80
                                            'major' => major,
81
                                            'minor' => minor,
82
                                            'family' => line[FAMILY]
83
                                          }
84
                                        })
85
        else
86
          print "Updating operating system '#{name}'..." if option_verbose?
87
          @f_operatingsystem_api.update({
88
                                          'id' => @existing[name],
89
                                          'operatingsystem' => {
90
                                            'name' => osname,
91
                                            'major' => major,
92
                                            'minor' => minor,
93
                                            'family' => line[FAMILY]
94
                                          }
95
                                        })
96
        end
97
        print "done\n" if option_verbose?
98
      end
99
    rescue RuntimeError => e
100
      raise "#{e}\n       #{line}"
101
    end
102
  end
103

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