Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / hosts.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
# -= Hosts CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Host 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
#   MAC Address
35
#     - MAC address
36
#     - May contain '%d' which will be replaced with current iteration number of Count
37
#     - eg. "FF:FF:FF:FF:FF:%02x" -> "FF:FF:FF:FF:FF:0A"
38
#     - Warning: be sure to keep count below 255 or MAC hex will exceed limit
39
#
40

    
41
require 'hammer_cli'
42
require 'katello_api'
43
require 'foreman_api'
44
require 'json'
45
require 'csv'
46
require 'uri'
47

    
48
module HammerCLICsv
49
  class HostsCommand < BaseCommand
50

    
51
    ORGANIZATION = 'Organization'
52
    ENVIRONMENT = 'Environment'
53
    OPERATINGSYSTEM = 'Operating System'
54
    ARCHITECTURE = 'Architecture'
55
    MACADDRESS = 'MAC Address'
56
    DOMAIN = 'Domain'
57
    PARTITIONTABLE = 'Partition Table'
58

    
59
    def execute
60
      super
61
      csv_export? ? export : import
62
      HammerCLI::EX_OK
63
    end
64

    
65
    def export
66
      CSV.open(csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
67
        csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE, MACADDRESS, DOMAIN, PARTITIONTABLE]
68
        @f_host_api.index({:per_page => 999999}, HEADERS)[0].each do |host|
69
          host = @f_host_api.show({'id' => host['id']}, HEADERS)[0]
70
          raise RuntimeError.new("Host 'id=#{host['id']}' not found") if !host || host.empty?
71

    
72
          name = host['name']
73
          count = 1
74
          organization = foreman_organization(:id => host['organization_id'])
75
          environment = foreman_environment(:id => host['environment_id'])
76
          operatingsystem = foreman_operatingsystem(:id => host['operatingsystem_id'])
77
          architecture = foreman_architecture(:id => host['architecture_id'])
78
          mac = host['mac']
79
          domain = foreman_domain(:id => host['domain_id'])
80
          ptable = foreman_partitiontable(:id => host['ptable_id'])
81

    
82
          csv << [name, count, organization, environment, operatingsystem, architecture, mac, domain, ptable]
83
        end
84
      end
85
    end
86

    
87
    def import
88
      @existing = {}
89
      @f_host_api.index({:per_page => 999999}, HEADERS)[0].each do |host|
90
        @existing[host['name']] = host['id']
91
      end
92

    
93
      thread_import do |line|
94
        create_hosts_from_csv(line)
95
      end
96
    end
97

    
98
    def create_hosts_from_csv(line)
99
      line[COUNT].to_i.times do |number|
100
        name = namify(line[NAME], number)
101
        if !@existing.include? name
102
          print "Creating host '#{name}'..." if verbose?
103
          @f_host_api.create({
104
                             'host' => {
105
                               'name' => name,
106
                               'mac' => namify(line[MACADDRESS], number),
107
                               'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
108
                               'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
109
                               'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
110
                               'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
111
                               'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
112
                               'domain_id' => foreman_domain(:name => line[DOMAIN]),
113
                               'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
114
                             }
115
                           }, HEADERS)
116
          print "done\n" if verbose?
117
        else
118
          print "Updating host '#{name}'..." if verbose?
119
          @f_host_api.update({
120
                               'id' => @existing[name],
121
                               'host' => {
122
                                 'name' => name,
123
                                 'mac' => namify(line[MACADDRESS], number),
124
                                 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
125
                                 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
126
                                 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
127
                                 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
128
                                 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
129
                                 'domain_id' => foreman_domain(:name => line[DOMAIN]),
130
                                 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
131
                               }
132
                             }, HEADERS)
133
          print "done\n" if verbose?
134
        end
135
      end
136
    rescue RuntimeError => e
137
      raise RuntimeError.new("#{e}\n       #{line}")
138
    end
139
  end
140

    
141
  HammerCLI::MainCommand.subcommand("csv:hosts", "import/export hosts", HammerCLICsv::HostsCommand)
142
end