Project

General

Profile

Revision 9d9e4494

Added by Thomas McKay almost 9 years ago

cv - provider, products, repo creation

View differences:

lib/hammer_cli_csv/systems.rb
69 69
    def import
70 70
      @existing = {}
71 71

  
72
      #puts "ENVIRONMENT #{katello_environment('ACME_Corporation', :name => 'Library')}"
73
      #puts "CONTENTVIEW #{katello_contentview('ACME_Corporation', :name => 'Default_Organization_View')}"
74

  
75 72
      thread_import do |line|
76 73
        create_systems_from_csv(line)
77 74
      end
78 75
    end
79 76

  
80 77
    def create_systems_from_csv(line)
81

  
82
      @k_system_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true}, HEADERS)[0]['results'].each do |system|
83
        @existing[line[ORGANIZATION]] ||= {}
84
        @existing[line[ORGANIZATION]][system['name']] = system['id'] if system
85
      end
86

  
87
      facts = {}
88
      facts['cpu.core(s)_per_socket'] = line[CORES]
89
      facts['cpu.cpu_socket(s)'] = line[SOCKETS]
90
      facts['memory.memtotal'] = line[RAM]
91
      facts['uname.machine'] = line[ARCHITECTURE]
92
      if line[OPERATINGSYSTEM].index(' ')
93
        (facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
94
      else
95
        (facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
78
      if !@existing[line[ORGANIZATION]]
79
        @existing[line[ORGANIZATION]] = {}
80
        @k_system_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true})[0]['results'].each do |system|
81
          @existing[line[ORGANIZATION]][system['name']] = system['uuid'] if system
82
        end
96 83
      end
97 84

  
98 85
      line[COUNT].to_i.times do |number|
99 86
        name = namify(line[NAME], number)
100
        if !@existing.include? name
87

  
88
        subscriptions(line).each do |subscription|
89
          puts subscription
90
          katello_subscription(line[ORGANIZATION], :name => subscription[:number])
91
        end
92

  
93
        if !@existing[line[ORGANIZATION]].include? name
101 94
          print "Creating system '#{name}'..." if verbose?
102 95
          @k_system_api.create({
103 96
                                 'name' => name,
104
                                 'organization_id' => 'ACME_Corporation', #foreman_organization(:name => line[ORGANIZATION]),
105
                                 'environment_id' => 1, #katello_environment(:name => line[ENVIRONMENT]),
106
                                 'content_view_id' => 1, #katello_contentview(:name => line[CONTENTVIEW]),
107
                                 'facts' => facts,
108
                                 'cp_type' => 'system'
109
                           }, HEADERS)
110
          print "done\n" if verbose?
97
                                 'organization_id' => line[ORGANIZATION],
98
                                 'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
99
                                 'content_view_id' => 2, # TODO: katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
100
                                 'facts' => facts(line),
101
                                 'installedProducts' => products(line),
102
                                 'type' => 'system'
103
                               })
111 104
        else
112 105
          print "Updating host '#{name}'..." if verbose?
113
=begin
114 106
          @k_system_api.update({
115
                               'id' => @existing[name],
116
                               'host' => {
107
                                 'id' => @existing[line[ORGANIZATION]][name],
117 108
                                 'name' => name,
118
                                 'mac' => namify(line[MACADDRESS], number),
119
                                 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
120
                                 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
121
                                 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
122
                                 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
123
                                 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
124
                                 'domain_id' => foreman_domain(:name => line[DOMAIN]),
125
                                 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
126
                               }
127
                             }, HEADERS)
128
=end
129
          print "done\n" if verbose?
109
                                 'organization_id' => line[ORGANIZATION],
110
                                 'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
111
                                 'content_view_id' => 2, # TODO: katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
112
                                 'facts' => facts(line),
113
                                 'installedProducts' => products(line)
114
                               })
130 115
        end
116
        print "done\n" if verbose?
131 117
      end
132 118
    rescue RuntimeError => e
133 119
      raise RuntimeError.new("#{e}\n       #{line}")
134 120
    end
121

  
122
    private
123

  
124
    def facts(line)
125
      facts = {}
126
      facts['cpu.core(s)_per_socket'] = line[CORES]
127
      facts['cpu.cpu_socket(s)'] = line[SOCKETS]
128
      facts['memory.memtotal'] = line[RAM]
129
      facts['uname.machine'] = line[ARCHITECTURE]
130
      if line[OPERATINGSYSTEM].index(' ')
131
        (facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
132
      else
133
        (facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
134
      end
135
      facts
136
    end
137

  
138
    def products(line)
139
      products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
140
        product = {}
141
        (product[:product_id], product[:productName]) = product_details.split('|')
142
        product
143
      end
144
      products
145
    end
146

  
147
    def subscriptions(line)
148
      subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
149
        subscription = {}
150
        (subscription[:number], subscription[:name]) = subscription_details.split('|')
151
        subscription
152
      end
153
      subscriptions
154
    end
155

  
135 156
  end
136 157

  
137 158
  HammerCLI::MainCommand.subcommand("csv:systems", "import/export systems", HammerCLICsv::SystemsCommand)

Also available in: Unified diff