Class: HammerCLICsv::CsvCommand::HostsCommand

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

Constant Summary collapse

ORGANIZATION =
'Organization'
LOCATION =
'Location'
ENVIRONMENT =
'Puppet Environment'
OPERATINGSYSTEM =
'Operating System'
ARCHITECTURE =
'Architecture'
MACADDRESS =
'MAC Address'
DOMAIN =
'Domain'
PARTITIONTABLE =
'Partition Table'
SUBNET =
'Subnet'
REALM =
'Realm'
MEDIUM =
'Medium'
HOSTGROUP =
'Hostgroup'
COMPUTERESOURCE =
'Compute Resource'
COMPUTEPROFILE =
'Compute Profile'
IMAGE =
'Image'
ENABLED =
'Enabled'
MANAGED =
'Managed'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#apipie_check_param, #associate_locations, #associate_organizations, #authenticate_request, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_medium, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #help, help_columns, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #supported?, supported?, #thread_import

Methods included from Utils::Config

#api_connection

Instance Method Details

#create_hosts_from_csv(line) ⇒ Object



73
74
75
76
77
78
79
80
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
# File 'lib/hammer_cli_csv/hosts.rb', line 73

def create_hosts_from_csv(line)
  return if option_organization && line[ORGANIZATION] != option_organization

  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    if !@existing.include? name
      print "Creating host '#{name}'..." if option_verbose?
      @api.resource(:hosts).call(:create, {
          'host' => {
            'name' => name,
            'root_pass' => 'changeme',
            'mac' => namify(line[MACADDRESS], number),
            'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
            'location_id' => foreman_location(:name => line[LOCATION]),
            '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],
          'host' => {
            '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
end

#export(csv) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hammer_cli_csv/hosts.rb', line 25

def export(csv)
  csv << [NAME, ORGANIZATION, LOCATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE,
          MACADDRESS, DOMAIN, PARTITIONTABLE, SUBNET, REALM, MEDIUM, HOSTGROUP,
          COMPUTERESOURCE, COMPUTEPROFILE, IMAGE, ENABLED, MANAGED]
  search_options = {:per_page => 999999}
  search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
  search_options['search'] = "#{search_options['search']} AND #{option_search}" if option_search
  @api.resource(:hosts).call(:index, search_options)['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']
    organization = host['organization_name']
    location = host['location_name']
    environment = host['environment_name']
    operatingsystem = host['operatingsystem_name']
    architecture = host['architecture_name']
    mac = host['mac']
    domain = host['domain_name']
    ptable = host['ptable_name']
    subnet = host['subnet_name']
    realm = host['realm_name']
    medium = host['medium_name']
    hostgroup = host['hostgroup_name']
    compute_resource = host['compute_resource_name']
    compute_profile = host['compute_profile_name']
    image = host['image_name']

    enabled = host['enabled'] ? 'Yes' : 'No'
    managed = host['managed'] ? 'Yes' : 'No'

    csv << [name, organization, location, environment, operatingsystem, architecture,
            mac, domain, ptable, subnet, realm, medium, hostgroup, compute_resource,
            compute_profile, image, enabled, managed]
  end
end

#importObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/hammer_cli_csv/hosts.rb', line 62

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