Project

General

Profile

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

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

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, {:per_page => 999999})['results'].each do |operatingsystem_id|
20
          operatingsystem = @api.resource(:operatingsystems).call(:show, {:id => operatingsystem_id['id']})
21
          name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])
22
          description = operatingsystem['description']
23
          family = operatingsystem['family']
24
          password_hash = operatingsystem['password_hash']
25
          partition_tables = export_column(operatingsystem, 'ptables', 'name')
26
          architectures = export_column(operatingsystem, 'architectures', 'name')
27
          media = export_column(operatingsystem, 'media', 'name')
28
          partition_tables = export_column(operatingsystem, 'ptables', 'name')
29
          parameters = export_column(operatingsystem, 'parameters') do |parameter|
30
            "#{parameter['name']}|#{parameter['value']}"
31
          end
32
          csv << [name, description, family, password_hash, partition_tables, architectures,
33
                  media, partition_tables, parameters]
34
        end
35
      end
36

    
37
      def import
38
        @existing = {}
39
        @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
40
          @existing[build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])] = operatingsystem['id'] if operatingsystem
41
        end
42

    
43
        thread_import do |line|
44
          create_operatingsystems_from_csv(line)
45
        end
46
      end
47

    
48
      def create_operatingsystems_from_csv(line)
49
        params =  {
50
          'operatingsystem' => {
51
            'family' => line[FAMILY],
52
            'description' => line[DESCRIPTION],
53
            'password_hash' => line[PASSWORD_HASH]
54
          }
55
        }
56
        params['operatingsystem']['architecture_ids'] = collect_column(line[ARCHITECTURES]) do |name|
57
          foreman_architecture(:name => name)
58
        end
59
        # TODO: http://projects.theforeman.org/issues/12919
60
        #params['operatingsystem']['provisioning_template_ids'] = collect_column(line[PROVISIONING_TEMPLATES]) do |name|
61
        #  foreman_provisioning_template(:name => name)
62
        #end
63
        # TODO: http://projects.theforeman.org/issues/12920
64
        #params['operatingsystem']['os_parameters?'] = collect_column(line[PARAMETERS]) do |name_value|
65
        #  ????
66
        #end
67
        count(line[COUNT]).times do |number|
68
          name = namify(line[NAME], number)
69
          (osname, major, minor) = split_os_name(name)
70
          params['operatingsystem']['name'] = osname
71
          params['operatingsystem']['major'] = major
72
          params['operatingsystem']['minor'] = minor
73
          if !@existing.include? name
74
            print "Creating operating system '#{name}'..." if option_verbose?
75
            @api.resource(:operatingsystems).call(:create, params)
76
          else
77
            print "Updating operating system '#{name}'..." if option_verbose?
78
            params['id'] = @existing[name]
79
            @api.resource(:operatingsystems).call(:update, params)
80
          end
81
          print "done\n" if option_verbose?
82
        end
83
      rescue RuntimeError => e
84
        raise "#{e}\n       #{line}"
85
      end
86
    end
87
  end
88
end