Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / hosts.rb @ 8c7ba4af

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
# -= Hosts CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Host 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
#   MAC Address
23
#     - MAC address
24
#     - May contain '%d' which will be replaced with current iteration number of Count
25
#     - eg. "FF:FF:FF:FF:FF:%02x" -> "FF:FF:FF:FF:FF:0A"
26
#     - Warning: be sure to keep count below 255 or MAC hex will exceed limit
27
#
28

    
29
require 'hammer_cli'
30
require 'json'
31
require 'csv'
32
require 'uri'
33

    
34
module HammerCLICsv
35
  class CsvCommand
36
    class HostsCommand < BaseCommand
37
      command_name 'hosts'
38
      desc         'import or export hosts'
39

    
40
      ORGANIZATION = 'Organization'
41
      ENVIRONMENT = 'Environment'
42
      OPERATINGSYSTEM = 'Operating System'
43
      ARCHITECTURE = 'Architecture'
44
      MACADDRESS = 'MAC Address'
45
      DOMAIN = 'Domain'
46
      PARTITIONTABLE = 'Partition Table'
47

    
48
      def export
49
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
50
          csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE, MACADDRESS, DOMAIN, PARTITIONTABLE]
51
          @api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
52
            host = @api.resource(:hosts).call(:show, {'id' => host['id']})
53
            raise "Host 'id=#{host['id']}' not found" if !host || host.empty?
54

    
55
            name = host['name']
56
            count = 1
57
            organization = foreman_organization(:id => host['organization_id'])
58
            environment = foreman_environment(:id => host['environment_id'])
59
            operatingsystem = foreman_operatingsystem(:id => host['operatingsystem_id'])
60
            architecture = foreman_architecture(:id => host['architecture_id'])
61
            mac = host['mac']
62
            domain = foreman_domain(:id => host['domain_id'])
63
            ptable = foreman_partitiontable(:id => host['ptable_id'])
64

    
65
            csv << [name, count, organization, environment, operatingsystem, architecture, mac, domain, ptable]
66
          end
67
        end
68
      end
69

    
70
      def import
71
        @existing = {}
72
        @api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
73
          @existing[host['name']] = host['id'] if host
74
        end
75

    
76
        thread_import do |line|
77
          create_hosts_from_csv(line)
78
        end
79
      end
80

    
81
      def create_hosts_from_csv(line)
82
        line[COUNT].to_i.times do |number|
83
          name = namify(line[NAME], number)
84
          if !@existing.include? name
85
            print "Creating host '#{name}'..." if option_verbose?
86
            @api.resource(:hosts).call(:create, {
87
                                 'name' => name,
88
                                 'root_pass' => 'changeme',
89
                                 'mac' => namify(line[MACADDRESS], number),
90
                                 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
91
                                 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
92
                                 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
93
                                 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
94
                                 'domain_id' => foreman_domain(:name => line[DOMAIN]),
95
                                 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
96
                             })
97
          else
98
            print "Updating host '#{name}'..." if option_verbose?
99
            @api.resource(:hosts).call(:update, {
100
                                 'id' => @existing[name],
101
                                   'name' => name,
102
                                   'mac' => namify(line[MACADDRESS], number),
103
                                   'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
104
                                   'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
105
                                   'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
106
                                   'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
107
                                   'domain_id' => foreman_domain(:name => line[DOMAIN]),
108
                                   'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
109
                               })
110
          end
111
          print "done\n" if option_verbose?
112
        end
113
      rescue RuntimeError => e
114
        raise "#{e}\n       #{line}"
115
      end
116
    end
117
  end
118
end