Project

General

Profile

Revision 104144fb

Added by Thomas McKay over 9 years ago

foreman-os - out/in/update works

View differences:

hammer_cli_csv.gemspec
17 17

  
18 18
  spec.add_dependency("hammer_cli", ">= 0.0.6")
19 19
  spec.add_dependency("katello_api")
20
  spec.add_dependency("foreman_api")
20 21
end
lib/hammer_cli_csv.rb
16 16
  require 'hammer_cli_csv/system_groups'
17 17
  require 'hammer_cli_csv/activation_keys'
18 18

  
19
  require 'hammer_cli_csv/operating_systems'
20

  
19 21
end
lib/hammer_cli_csv/base.rb
25 25

  
26 26
require 'hammer_cli'
27 27
require 'katello_api'
28
require 'foreman_api'
28 29
require 'json'
29 30
require 'csv'
30 31

  
......
39 40
    option ['--csv-export'], :flag, 'Export current data instead of importing'
40 41

  
41 42
    def initialize(*args)
42
      @init_options = { :base_url => HammerCLI::Settings.get(:katello, :host),
43
                        :username => HammerCLI::Settings.get(:katello, :username),
44
                        :password => HammerCLI::Settings.get(:katello, :password) }
43
      @init_options = {
44
          :katello => {
45
            :base_url => HammerCLI::Settings.get(:katello, :host),
46
            :username => HammerCLI::Settings.get(:katello, :username),
47
            :password => HammerCLI::Settings.get(:katello, :password)
48
          },
49
          :foreman => {
50
            :base_url => HammerCLI::Settings.get(:foreman, :host),
51
            :username => HammerCLI::Settings.get(:foreman, :username),
52
            :password => HammerCLI::Settings.get(:foreman, :password)
53
          }
54
      }
45 55
    end
46 56

  
47 57
    def get_lines(filename)
lib/hammer_cli_csv/operating_systems.rb
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 'katello_api'
41
require 'foreman_api'
42
require 'json'
43
require 'csv'
44

  
45
module HammerCLICsv
46
  class OperatingSystemsCommand < BaseCommand
47

  
48
    def initialize(*args)
49
      super(args)
50
      @operatingsystem_api = ForemanApi::Resources::OperatingSystem.new(@init_options[:foreman])
51
    end
52

  
53
    def execute
54
      csv_export? ? export : import
55

  
56
      HammerCLI::EX_OK
57
    end
58

  
59
    def export
60
      CSV.open(csv_file, 'wb') do |csv|
61
        csv << ['Name','Count','Major','Minor', 'Family']
62
        @operatingsystem_api.index({}, HEADERS)[0].each do |operatingsystem|
63
          name = operatingsystem['operatingsystem']['name']
64
          count = 1
65
          major = operatingsystem['operatingsystem']['major']
66
          minor = operatingsystem['operatingsystem']['minor']
67
          family = operatingsystem['operatingsystem']['family']
68
          csv << [name, count, major, minor, family]
69
        end
70
      end
71
    end
72

  
73
    def import
74
      @existing = {}
75
      @operatingsystem_api.index[0].each do |operatingsystem|
76
        operatingsystem = operatingsystem['operatingsystem']
77
        @existing["#{operatingsystem['name']}-#{operatingsystem['major']}-#{operatingsystem['minor']}"] = operatingsystem['id']
78
      end
79

  
80
      thread_import do |line|
81
        create_operatingsystems_from_csv(line)
82
      end
83
    end
84

  
85
    def create_operatingsystems_from_csv(line)
86
      details = parse_operatingsystem_csv(line)
87

  
88
      details[:count].times do |number|
89
        name = namify(details[:name_format], number)
90
        if !@existing.include? "#{name}-#{details[:major]}-#{details[:minor]}"
91
          print "Creating operating system '#{name}'..." if verbose?
92
          @operatingsystem_api.create({
93
                             'operatingsystem' => {
94
                               'name' => name,
95
                               'major' => details[:major],
96
                               'minor' => details[:minor],
97
                               'family' => details[:family]
98
                             }
99
                           }, HEADERS)
100
          print "done\n" if verbose?
101
        else
102
          print "Updating operatingsystem '#{name}'..." if verbose?
103
          @operatingsystem_api.create({
104
                             'id' => @existing["#{name}-#{details[:major]}-#{details[:minor]}"],
105
                             'operatingsystem' => {
106
                               'name' => name,
107
                               'major' => details[:major],
108
                               'minor' => details[:minor],
109
                               'family' => details[:family]
110
                             }
111
                           }, HEADERS)
112
          print "done\n" if verbose?
113
        end
114
      end
115
    end
116

  
117
    def parse_operatingsystem_csv(line)
118
      keys = [:name_format, :count, :major, :minor, :family]
119
      details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0]
120

  
121
      details[:count] = details[:count].to_i
122

  
123
      details
124
    end
125
  end
126

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

Also available in: Unified diff