Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / hosts.rb @ bf81b9cf

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 HostsCommand < BaseCommand
36

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

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

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

    
62
          csv << [name, count, organization, environment, operatingsystem, architecture, mac, domain, ptable]
63
        end
64
      end
65
    end
66

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

    
73
      thread_import do |line|
74
        create_hosts_from_csv(line)
75
      end
76
    end
77

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

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