Project

General

Profile

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

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

1
module HammerCLICsv
2
  class CsvCommand
3
    class OperatingSystemsCommand < BaseCommand
4
      command_name 'operating-systems'
5
      desc         'import or export operating systems'
6

    
7
      FAMILY = 'Family'
8
      DESCRIPTION = 'Description'
9
      PASSWORD_HASH = 'Password Hash'
10
      PARTITION_TABLES = 'Partition Tables'
11
      ARCHITECTURES = 'Architectures'
12
      MEDIA = 'Media'
13
      PROVISIONING_TEMPLATES = 'Provisioning Templates'
14
      PARAMETERS = 'Parameters'
15

    
16
      def export(csv)
17
        csv << [NAME, DESCRIPTION, FAMILY, PASSWORD_HASH, PARTITION_TABLES, ARCHITECTURES, MEDIA,
18
                PROVISIONING_TEMPLATES, PARAMETERS]
19
        @api.resource(:operatingsystems).call(:index, {
20
            :per_page => 999999,
21
            :search => option_search
22
        })['results'].each do |operatingsystem_id|
23
          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
          end
35
          csv << [name, description, family, password_hash, partition_tables, architectures,
36
                  media, partition_tables, parameters]
37
        end
38
      end
39

    
40
      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

    
46
        thread_import do |line|
47
          create_operatingsystems_from_csv(line)
48
        end
49
      end
50

    
51
      def create_operatingsystems_from_csv(line)
52
        params =  {
53
          'operatingsystem' => {
54
            'family' => line[FAMILY],
55
            'description' => line[DESCRIPTION],
56
            'password_hash' => line[PASSWORD_HASH]
57
          }
58
        }
59
        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
        count(line[COUNT]).times do |number|
71
          name = namify(line[NAME], number)
72
          (osname, major, minor) = split_os_name(name)
73
          params['operatingsystem']['name'] = osname
74
          params['operatingsystem']['major'] = major
75
          params['operatingsystem']['minor'] = minor
76
          if !@existing.include? name
77
            print "Creating operating system '#{name}'..." if option_verbose?
78
            @api.resource(:operatingsystems).call(:create, params)
79
          else
80
            print "Updating operating system '#{name}'..." if option_verbose?
81
            params['id'] = @existing[name]
82
            @api.resource(:operatingsystems).call(:update, params)
83
          end
84
          print "done\n" if option_verbose?
85
        end
86
      end
87
    end
88
  end
89
end