Project

General

Profile

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

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

1 104144fb Tom McKay
module HammerCLICsv
2 7bea883f Grant Gainey
  class CsvCommand
3
    class OperatingSystemsCommand < BaseCommand
4 26b7d467 Tom McKay
      command_name 'operating-systems'
5
      desc         'import or export operating systems'
6 104144fb Tom McKay
7 7bea883f Grant Gainey
      FAMILY = 'Family'
8 088e0854 Tom McKay
      DESCRIPTION = 'Description'
9 6b4c161c Tom McKay
      PASSWORD_HASH = 'Password Hash'
10
      PARTITION_TABLES = 'Partition Tables'
11
      ARCHITECTURES = 'Architectures'
12
      MEDIA = 'Media'
13
      PROVISIONING_TEMPLATES = 'Provisioning Templates'
14
      PARAMETERS = 'Parameters'
15 7bea883f Grant Gainey
16 c06f1783 Tom McKay
      def export(csv)
17
        csv << [NAME, DESCRIPTION, FAMILY, PASSWORD_HASH, PARTITION_TABLES, ARCHITECTURES, MEDIA,
18
                PROVISIONING_TEMPLATES, PARAMETERS]
19 6498291e Tom McKay
        @api.resource(:operatingsystems).call(:index, {
20
            :per_page => 999999,
21
            :search => option_search
22
        })['results'].each do |operatingsystem_id|
23 c06f1783 Tom McKay
          operatingsystem = @api.resource(:operatingsystems).call(:show, {:id => operatingsystem_id['id']})
24
          name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])
25
          description = operatingsystem['description']
26
          family = operatingsystem['family']
27
          password_hash = operatingsystem['password_hash']
28
          partition_tables = export_column(operatingsystem, 'ptables', 'name')
29
          architectures = export_column(operatingsystem, 'architectures', 'name')
30
          media = export_column(operatingsystem, 'media', 'name')
31
          partition_tables = export_column(operatingsystem, 'ptables', 'name')
32
          parameters = export_column(operatingsystem, 'parameters') do |parameter|
33
            "#{parameter['name']}|#{parameter['value']}"
34 7bea883f Grant Gainey
          end
35 c06f1783 Tom McKay
          csv << [name, description, family, password_hash, partition_tables, architectures,
36
                  media, partition_tables, parameters]
37 104144fb Tom McKay
        end
38
      end
39
40 7bea883f Grant Gainey
      def import
41
        @existing = {}
42
        @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
43
          @existing[build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])] = operatingsystem['id'] if operatingsystem
44
        end
45 104144fb Tom McKay
46 7bea883f Grant Gainey
        thread_import do |line|
47
          create_operatingsystems_from_csv(line)
48
        end
49 104144fb Tom McKay
      end
50
51 7bea883f Grant Gainey
      def create_operatingsystems_from_csv(line)
52 088e0854 Tom McKay
        params =  {
53
          'operatingsystem' => {
54
            'family' => line[FAMILY],
55 6b4c161c Tom McKay
            'description' => line[DESCRIPTION],
56
            'password_hash' => line[PASSWORD_HASH]
57 088e0854 Tom McKay
          }
58
        }
59 6b4c161c Tom McKay
        params['operatingsystem']['architecture_ids'] = collect_column(line[ARCHITECTURES]) do |name|
60
          foreman_architecture(:name => name)
61
        end
62
        # TODO: http://projects.theforeman.org/issues/12919
63
        #params['operatingsystem']['provisioning_template_ids'] = collect_column(line[PROVISIONING_TEMPLATES]) do |name|
64
        #  foreman_provisioning_template(:name => name)
65
        #end
66
        # TODO: http://projects.theforeman.org/issues/12920
67
        #params['operatingsystem']['os_parameters?'] = collect_column(line[PARAMETERS]) do |name_value|
68
        #  ????
69
        #end
70 02387fb5 Tom McKay
        count(line[COUNT]).times do |number|
71 7bea883f Grant Gainey
          name = namify(line[NAME], number)
72
          (osname, major, minor) = split_os_name(name)
73 088e0854 Tom McKay
          params['operatingsystem']['name'] = osname
74
          params['operatingsystem']['major'] = major
75
          params['operatingsystem']['minor'] = minor
76 7bea883f Grant Gainey
          if !@existing.include? name
77
            print "Creating operating system '#{name}'..." if option_verbose?
78 088e0854 Tom McKay
            @api.resource(:operatingsystems).call(:create, params)
79 7bea883f Grant Gainey
          else
80
            print "Updating operating system '#{name}'..." if option_verbose?
81 088e0854 Tom McKay
            params['id'] = @existing[name]
82
            @api.resource(:operatingsystems).call(:update, params)
83 7bea883f Grant Gainey
          end
84
          print "done\n" if option_verbose?
85 104144fb Tom McKay
        end
86
      end
87
    end
88
  end
89
end