Project

General

Profile

Revision 1192ff19

Added by Thomas McKay almost 6 years ago

fixes #14389 - import/export host groups

View differences:

lib/hammer_cli_csv/base.rb
484 484
      result
485 485
    end
486 486

  
487
    def foreman_medium(options = {})
488
      @media ||= {}
489

  
490
      if options[:name]
491
        return nil if options[:name].nil? || options[:name].empty?
492
        options[:id] = @media[options[:name]]
493
        if !options[:id]
494
          ptable = @api.resource(:media).call(:index, {
495
                                                  :per_page => 999999,
496
                                                  'search' => "name=\"#{options[:name]}\""
497
                                                })['results']
498
          raise "Partition table '#{options[:name]}' not found" if !ptable || ptable.empty?
499
          options[:id] = ptable[0]['id']
500
          @media[options[:name]] = options[:id]
501
        end
502
        result = options[:id]
503
      elsif options[:id]
504
        return nil if options[:id].nil?
505
        options[:name] = @media.key(options[:id])
506
        if !options[:name]
507
          ptable = @api.resource(:media).call(:show, {'id' => options[:id]})
508
          options[:name] = ptable['name']
509
          @media[options[:name]] = options[:id]
510
        end
511
        result = options[:name]
512
      elsif !options[:name] && !options[:id]
513
        result = ''
514
      end
515

  
516
      result
517
    end
518

  
487 519
    def foreman_host(options = {})
488 520
      @query_hosts ||= {}
489 521

  
lib/hammer_cli_csv/host_groups.rb
19 19
      PUPPET_PROXY = 'Puppet Proxy'
20 20
      PUPPET_CA_PROXY = 'Puppet CA Proxy'
21 21
      CONTENT_SOURCE = 'Content Source'
22
      PASSWORD = 'Password'
23
      PUPPET_CLASSES = 'Puppet Classes'
22 24

  
23 25
      def export
24 26
        CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
25 27
          csv << [NAME, PARENT, ORGANIZATIONS, LOCATIONS, SUBNET, DOMAIN, OPERATING_SYSTEM,
26 28
                  ENVIRONMENT, COMPUTE_PROFILE, PARTITION_TABLE, MEDIUM, ARCHITECTURE, REALM,
27
                  PUPPET_PROXY, PUPPET_CA_PROXY, CONTENT_SOURCE]
29
                  PUPPET_PROXY, PUPPET_CA_PROXY, CONTENT_SOURCE, PASSWORD, PUPPET_CLASSES]
28 30
          search_options = {:per_page => 999999}
29 31
          search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
30 32
          @api.resource(:hostgroups).call(:index, search_options)['results'].each do |hostgroup|
......
45 47
            realm = hostgroup['realm_name']
46 48
            puppet_proxy = hostgroup['puppet_proxy_id'] ? foreman_host(:id => hostgroup['puppet_proxy_id']) : nil
47 49
            puppet_ca_proxy = hostgroup['puppet_ca_proxy_id'] ? foreman_host(:id => hostgroup['puppet_ca_proxy_id']) : nil
48
            # TODO: http://projects.theforeman.org/issues/7597
49
            # content_source = hostgroup['content_source_id'] ? foreman_host(:id => hostgroup['content_source_id']) : nil
50
            content_source = nil
50
            content_source = hostgroup['content_source_id'] ? foreman_host(:id => hostgroup['content_source_id']) : nil
51 51
            parent = hostgroup['ancestry'] ? foreman_hostgroup(:id => hostgroup['ancestry']) : nil
52
            password = nil
53
            puppet_classes = export_column(hostgroup, 'puppetclasses') do |puppet_class|
54
              "#{puppet_class['module_name']}/#{puppet_class['name']}"
55
            end
56

  
57
            # TODO: http://projects.theforeman.org/issues/6273
58
            # API call to get the smart class variable override values
52 59

  
53 60
            csv << [name, parent, organizations, locations, subnet, domain, operating_system,
54 61
                    puppet_environment, compute_profile, partition_table, medium, architecture,
55
                    realm, puppet_proxy, puppet_ca_proxy, content_source]
62
                    realm, puppet_proxy, puppet_ca_proxy, content_source, password, puppet_classes]
56 63
          end
57 64
        end
58 65
      end
59 66

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

  
66 73
        thread_import do |line|
67
          create_hosts_from_csv(line)
74
          create_from_csv(line)
68 75
        end
69 76
      end
70 77

  
71
      def create_hosts_from_csv(line)
78
      def create_from_csv(line)
72 79
        return if option_organization && !CSV.parse_line(line[ORGANIZATIONS], {:skip_blanks => true}).include?(option_organization)
73 80

  
81
        params = {
82
          'hostgroup' => {
83
            'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
84
            'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATING_SYSTEM]),
85
            'medium_id' => foreman_medium(:name => line[MEDIUM]),
86
            'ptable_id' => foreman_partitiontable(:name => line[PARTITION_TABLE]),
87
            'root_pass' => line[PASSWORD],
88
            'organization_ids' => collect_column(line[ORGANIZATIONS]) do |organization|
89
              foreman_organization(:name => organization)
90
            end,
91
            'location_ids' => collect_column(line[LOCATIONS]) do |location|
92
              foreman_location(:name => location)
93
            end
94
          }
95
        }
96

  
74 97
        count(line[COUNT]).times do |number|
75 98
          name = namify(line[NAME], number)
99
          params['hostgroup']['name'] = name
100

  
76 101
          if !@existing.include? name
77 102
            print "Creating host group '#{name}'..." if option_verbose?
78
            # @api.resource(:hosts).call(:create, {
79
            #     'host' => {
80
            #       'name' => name,
81
            #       'root_pass' => 'changeme',
82
            #       'mac' => namify(line[MACADDRESS], number),
83
            #       'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
84
            #       'location_id' => foreman_location(:name => line[LOCATION]),
85
            #       'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
86
            #       'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
87
            #       'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
88
            #       'domain_id' => foreman_domain(:name => line[DOMAIN]),
89
            #       'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
90
            #     }
91
            # })
103
            hostgroup = @api.resource(:hostgroups).call(:create, params)
104
            @existing[name] = hostgroup['id']
92 105
          else
93 106
            print "Updating host '#{name}'..." if option_verbose?
94
            # @api.resource(:hosts).call(:update, {
95
            #     'id' => @existing[name],
96
            #     'host' => {
97
            #       'name' => name,
98
            #       'mac' => namify(line[MACADDRESS], number),
99
            #       'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
100
            #       'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
101
            #       'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
102
            #       'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
103
            #       'domain_id' => foreman_domain(:name => line[DOMAIN]),
104
            #       'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
105
            #     }
106
            # })
107
            params['id'] = @existing[name]
108
            hostgroup = @api.resource(:hostgroups).call(:update, params)
107 109
          end
110

  
111
          # TODO: puppet classes
112
          puppetclass_ids = collect_column(line[PUPPET_CLASSES]) do |puppet_class|
113
            module_name, name = puppet_class.split('/')
114
            foreman_puppet_class(:name => name)
115
          end
116
          existing_ids = hostgroup['puppet_classes'].collect { |puppet_class| puppet_class['id'] }
117
          # DELETE existing_ids - puppetclass_ids
118
          # POST puppetclass_ids - existing_ids
119

  
108 120
          print "done\n" if option_verbose?
109 121
        end
110 122
      rescue RuntimeError => e

Also available in: Unified diff