Project

General

Profile

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

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

1 1a0abf99 Tom McKay
module HammerCLICsv
2 7bea883f Grant Gainey
  class CsvCommand
3
    class HostsCommand < BaseCommand
4 26b7d467 Tom McKay
      command_name 'hosts'
5
      desc         'import or export hosts'
6 1a0abf99 Tom McKay
7 7bea883f Grant Gainey
      ORGANIZATION = 'Organization'
8 088e0854 Tom McKay
      LOCATION = 'Location'
9 04bb710e Tom McKay
      ENVIRONMENT = 'Puppet Environment'
10 7bea883f Grant Gainey
      OPERATINGSYSTEM = 'Operating System'
11
      ARCHITECTURE = 'Architecture'
12
      MACADDRESS = 'MAC Address'
13
      DOMAIN = 'Domain'
14
      PARTITIONTABLE = 'Partition Table'
15 5146ac68 Tom McKay
      SUBNET = 'Subnet'
16
      REALM = 'Realm'
17
      MEDIUM = 'Medium'
18
      HOSTGROUP = 'Hostgroup'
19
      COMPUTERESOURCE = 'Compute Resource'
20
      COMPUTEPROFILE = 'Compute Profile'
21
      IMAGE = 'Image'
22
      ENABLED = 'Enabled'
23
      MANAGED = 'Managed'
24 7bea883f Grant Gainey
25 c06f1783 Tom McKay
      def export(csv)
26
        csv << [NAME, ORGANIZATION, LOCATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE,
27
                MACADDRESS, DOMAIN, PARTITIONTABLE, SUBNET, REALM, MEDIUM, HOSTGROUP,
28
                COMPUTERESOURCE, COMPUTEPROFILE, IMAGE, ENABLED, MANAGED]
29
        search_options = {:per_page => 999999}
30
        search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
31 6498291e Tom McKay
        search_options['search'] = "#{search_options['search']} AND #{option_search}" if option_search
32 c06f1783 Tom McKay
        @api.resource(:hosts).call(:index, search_options)['results'].each do |host|
33
          host = @api.resource(:hosts).call(:show, {'id' => host['id']})
34
          raise "Host 'id=#{host['id']}' not found" if !host || host.empty?
35 42d8c0ad Tom McKay
36 c06f1783 Tom McKay
          name = host['name']
37
          organization = host['organization_name']
38
          location = host['location_name']
39
          environment = host['environment_name']
40
          operatingsystem = host['operatingsystem_name']
41
          architecture = host['architecture_name']
42
          mac = host['mac']
43
          domain = host['domain_name']
44
          ptable = host['ptable_name']
45
          subnet = host['subnet_name']
46
          realm = host['realm_name']
47
          medium = host['medium_name']
48
          hostgroup = host['hostgroup_name']
49
          compute_resource = host['compute_resource_name']
50
          compute_profile = host['compute_profile_name']
51
          image = host['image_name']
52 1a0abf99 Tom McKay
53 c06f1783 Tom McKay
          enabled = host['enabled'] ? 'Yes' : 'No'
54
          managed = host['managed'] ? 'Yes' : 'No'
55 5146ac68 Tom McKay
56 c06f1783 Tom McKay
          csv << [name, organization, location, environment, operatingsystem, architecture,
57
                  mac, domain, ptable, subnet, realm, medium, hostgroup, compute_resource,
58
                  compute_profile, image, enabled, managed]
59 1a0abf99 Tom McKay
        end
60
      end
61
62 7bea883f Grant Gainey
      def import
63
        @existing = {}
64
        @api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
65
          @existing[host['name']] = host['id'] if host
66
        end
67 1a0abf99 Tom McKay
68 7bea883f Grant Gainey
        thread_import do |line|
69
          create_hosts_from_csv(line)
70
        end
71 1a0abf99 Tom McKay
      end
72
73 7bea883f Grant Gainey
      def create_hosts_from_csv(line)
74 6f221b51 Tom McKay
        return if option_organization && line[ORGANIZATION] != option_organization
75
76 02387fb5 Tom McKay
        count(line[COUNT]).times do |number|
77 7bea883f Grant Gainey
          name = namify(line[NAME], number)
78
          if !@existing.include? name
79
            print "Creating host '#{name}'..." if option_verbose?
80
            @api.resource(:hosts).call(:create, {
81 088e0854 Tom McKay
                'host' => {
82
                  'name' => name,
83
                  'root_pass' => 'changeme',
84
                  'mac' => namify(line[MACADDRESS], number),
85
                  'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
86
                  'location_id' => foreman_location(:name => line[LOCATION]),
87
                  'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
88
                  'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
89
                  'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
90
                  'domain_id' => foreman_domain(:name => line[DOMAIN]),
91
                  'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
92
                }
93 1909f2e6 Tom McKay
            })
94 7bea883f Grant Gainey
          else
95
            print "Updating host '#{name}'..." if option_verbose?
96
            @api.resource(:hosts).call(:update, {
97 1909f2e6 Tom McKay
                'id' => @existing[name],
98 088e0854 Tom McKay
                'host' => {
99
                  'name' => name,
100
                  'mac' => namify(line[MACADDRESS], number),
101
                  'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
102
                  'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
103
                  'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
104
                  'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
105
                  'domain_id' => foreman_domain(:name => line[DOMAIN]),
106
                  'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
107
                }
108 1909f2e6 Tom McKay
            })
109 7bea883f Grant Gainey
          end
110
          print "done\n" if option_verbose?
111 1a0abf99 Tom McKay
        end
112
      end
113
    end
114
  end
115
end