Class: HammerCLICsv::CsvCommand::HostsCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/hosts.rb

Constant Summary collapse

ORGANIZATION =
'Organization'
ENVIRONMENT =
'Environment'
OPERATINGSYSTEM =
'Operating System'
ARCHITECTURE =
'Architecture'
MACADDRESS =
'MAC Address'
DOMAIN =
'Domain'
PARTITIONTABLE =
'Partition Table'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_hosts_from_csv(line) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hammer_cli_csv/hosts.rb', line 81

def create_hosts_from_csv(line)
  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    if !@existing.include? name
      print "Creating host '#{name}'..." if option_verbose?
      @api.resource(:hosts).call(:create, {
                           'name' => name,
                           'root_pass' => 'changeme',
                           'mac' => namify(line[MACADDRESS], number),
                           'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                           'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
                           'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
                           'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
                           'domain_id' => foreman_domain(:name => line[DOMAIN]),
                           'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
                       })
    else
      print "Updating host '#{name}'..." if option_verbose?
      @api.resource(:hosts).call(:update, {
                           'id' => @existing[name],
                           'name' => name,
                           'mac' => namify(line[MACADDRESS], number),
                           'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                           'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
                           'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
                           'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
                           'domain_id' => foreman_domain(:name => line[DOMAIN]),
                           'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
                         })
    end
    print "done\n" if option_verbose?
  end
rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#exportObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hammer_cli_csv/hosts.rb', line 48

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

      name = host['name']
      count = 1
      organization = foreman_organization(:id => host['organization_id'])
      environment = foreman_environment(:id => host['environment_id'])
      operatingsystem = foreman_operatingsystem(:id => host['operatingsystem_id'])
      architecture = foreman_architecture(:id => host['architecture_id'])
      mac = host['mac']
      domain = foreman_domain(:id => host['domain_id'])
      ptable = foreman_partitiontable(:id => host['ptable_id'])

      csv << [name, count, organization, environment, operatingsystem, architecture, mac, domain, ptable]
    end
  end
end

#importObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/hammer_cli_csv/hosts.rb', line 70

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

  thread_import do |line|
    create_hosts_from_csv(line)
  end
end