Class: HammerCLICsv::CsvCommand::HostGroupsCommand

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

Constant Summary collapse

PARENT =
'Parent Host Group'
ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
SUBNET =
'Subnet'
DOMAIN =
'Domain'
OPERATING_SYSTEM =
'Operating System'
ENVIRONMENT =
'Puppet Environment'
COMPUTE_PROFILE =
'Compute Profile'
PARTITION_TABLE =
'Partition Table'
MEDIUM =
'Medium'
ARCHITECTURE =
'Architecture'
REALM =
'Realm'
PUPPET_PROXY =
'Puppet Proxy'
PUPPET_CA_PROXY =
'Puppet CA Proxy'
CONTENT_SOURCE =
'Content Source'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#apipie_check_param, #associate_locations, #associate_organizations, #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_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_hosts_from_csv(line) ⇒ Object



71
72
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/host_groups.rb', line 71

def create_hosts_from_csv(line)
  return if option_organization && !CSV.parse_line(line[ORGANIZATIONS], {:skip_blanks => true}).include?(option_organization)

  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    if !@existing.include? name
      print "Creating host group '#{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
rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#exportObject



23
24
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
# File 'lib/hammer_cli_csv/host_groups.rb', line 23

def export
  CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, PARENT, ORGANIZATIONS, LOCATIONS, SUBNET, DOMAIN, OPERATING_SYSTEM,
            ENVIRONMENT, COMPUTE_PROFILE, PARTITION_TABLE, MEDIUM, ARCHITECTURE, REALM,
            PUPPET_PROXY, PUPPET_CA_PROXY, CONTENT_SOURCE]
    search_options = {:per_page => 999999}
    search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
    @api.resource(:hostgroups).call(:index, search_options)['results'].each do |hostgroup|
      hostgroup = @api.resource(:hostgroups).call(:show, {'id' => hostgroup['id']})
      raise "Host Group 'id=#{hostgroup['id']}' not found" if !hostgroup || hostgroup.empty?

      name = hostgroup['name']
      organizations = export_column(hostgroup, 'organizations', 'name')
      locations = export_column(hostgroup, 'locations', 'name')
      subnet = hostgroup['subnet_name']
      operating_system = hostgroup['operatingsystem_name']
      domain = hostgroup['domain_name']
      puppet_environment = hostgroup['environment_name']
      compute_profile = hostgroup['compute_profile_name']
      partition_table = hostgroup['ptable_name']
      medium = hostgroup['medium_name']
      architecture = hostgroup['architecture_name']
      realm = hostgroup['realm_name']
      puppet_proxy = hostgroup['puppet_proxy_id'] ? foreman_host(:id => hostgroup['puppet_proxy_id']) : nil
      puppet_ca_proxy = hostgroup['puppet_ca_proxy_id'] ? foreman_host(:id => hostgroup['puppet_ca_proxy_id']) : nil
      # TODO: http://projects.theforeman.org/issues/7597
      # content_source = hostgroup['content_source_id'] ? foreman_host(:id => hostgroup['content_source_id']) : nil
      content_source = nil
      parent = hostgroup['ancestry'] ? foreman_hostgroup(:id => hostgroup['ancestry']) : nil

      csv << [name, parent, organizations, locations, subnet, domain, operating_system,
              puppet_environment, compute_profile, partition_table, medium, architecture,
              realm, puppet_proxy, puppet_ca_proxy, content_source]
    end
  end
end

#importObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/hammer_cli_csv/host_groups.rb', line 60

def import
  @existing = {}
  @api.resource(:hostgroups).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