Project

General

Profile

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

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

1
module HammerCLICsv
2
  class CsvCommand
3
    class ProvisioningTemplatesCommand < BaseCommand
4
      command_name 'provisioning-templates'
5
      desc         'import or export provisioning templates'
6

    
7
      option %w(--include-locked), :flag, 'Include locked templates (will fail if re-imported)',
8
                                   :attribute_name => :option_include_locked
9

    
10
      ORGANIZATIONS = 'Organizations'
11
      LOCATIONS = 'Locations'
12
      OPERATINGSYSTEMS = 'Operating Systems'
13
      ASSOCIATIONS = 'Host Group / Puppet Environment Combinations'
14
      KIND = 'Kind'
15
      TEMPLATE = 'Template'
16

    
17
      def export(csv)
18
        csv << [NAME, ORGANIZATIONS, LOCATIONS, OPERATINGSYSTEMS, ASSOCIATIONS, KIND, TEMPLATE]
19
        params = {
20
            :per_page => 999999
21
        }
22
        params['search'] =  "organization = \"#{option_organization}\"" if option_organization
23
        params['search'] = "#{params['search']} AND #{option_search}" if option_search
24
        @api.resource(:config_templates).call(:index, params)['results'].each do |template_id|
25
          template = @api.resource(:config_templates).call(:show, {:id => template_id['id']})
26
          next if template['locked'] && !option_include_locked?
27
          name = template['name']
28
          kind = template['snippet'] ? 'snippet' : template['template_kind_name']
29
          organizations = export_column(template, 'organizations', 'name')
30
          locations = export_column(template, 'locations', 'name')
31
          operatingsystems = export_column(template, 'operatingsystems', 'fullname')
32
          # TODO: puppet environments for content views are not present in api
33
          # http://projects.theforeman.org/issues/10293
34
          associations = export_associations(template)
35
          unless name == 'Boot disk iPXE - generic host' || name == 'Boot disk iPXE - host'
36
            csv << [name, organizations, locations, operatingsystems, associations, kind, template['template']]
37
          end
38
        end
39
      end
40

    
41
      def import
42
        @existing = {}
43
        @api.resource(:config_templates).call(:index, {
44
            :per_page => 999999
45
        })['results'].each do |template|
46
          @existing[template['name']] = template['id'] if template
47
        end
48

    
49
        thread_import do |line|
50
          create_templates_from_csv(line)
51
        end
52
      end
53

    
54
      def create_templates_from_csv(line)
55
        organizations = collect_column(line[ORGANIZATIONS]) do |organization|
56
          foreman_organization(:name => organization)
57
        end
58
        if option_organization
59
          org_id = foreman_organization(:name => option_organization)
60
          return if org_id.nil? || !organizations.include?(org_id)
61
          organizations = [org_id]
62
        end
63
        locations = collect_column(line[LOCATIONS]) do |location|
64
          foreman_location(:name => location)
65
        end
66
        operatingsystems = collect_column(line[OPERATINGSYSTEMS]) do |operatingsystem|
67
          foreman_operatingsystem(:name => operatingsystem)
68
        end
69

    
70
        count(line[COUNT]).times do |number|
71
          name = namify(line[NAME], number)
72
          if !@existing.include? name
73
            print _("Creating provisioning template '%{name}'...") % {:name => name } if option_verbose?
74
            template_id = @api.resource(:config_templates).call(:create, {
75
                'config_template' => {
76
                  'name' => name,
77
                  'snippet' => line[KIND] == 'snippet',
78
                  'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
79
                  'operatingsystem_ids' => operatingsystems,
80
                  'location_ids' => locations,
81
                  'template' => line[TEMPLATE]
82
                }
83
            })['id']
84
          else
85
            print _("Updating provisioning template '%{name}'...") % {:name => name} if option_verbose?
86
            template_id = @api.resource(:config_templates).call(:update, {
87
                'id' => @existing[name],
88
                'config_template' => {
89
                  'name' => name,
90
                  'snippet' => line[KIND] == 'snippet',
91
                  'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
92
                  'operatingsystem_ids' => operatingsystems,
93
                  'location_ids' => locations,
94
                  'template' => line[TEMPLATE]
95
                }
96
            })['id']
97
          end
98
          @existing[name] = template_id
99

    
100
          # Update associated resources
101
          @template_organizations ||= {}
102
          organizations.each do |organization_id|
103
            if @template_organizations[organization_id].nil?
104
              @template_organizations[organization_id] = @api.resource(:organizations).call(:show, {
105
                  'id' => organization_id
106
              })['config_templates'].collect do |template|
107
                template['id']
108
              end
109
            end
110
            if !@template_organizations[organization_id].include? template_id
111
              @template_organizations[organization_id] << template_id
112
              @api.resource(:organizations).call(:update, {
113
                  'id' => organization_id,
114
                  'organization' => {
115
                      'config_template_ids' => @template_organizations[organization_id]
116
                  }
117
              })
118
            end
119
          end
120
          @template_locations ||= {}
121
          locations.each do |location_id|
122
            if @template_locations[location_id].nil?
123
              @template_locations[location_id] = @api.resource(:locations).call(:show, {
124
                  'id' => location_id
125
              })['config_templates'].collect do |template|
126
                template['id']
127
              end
128
            end
129
            if !@template_locations[location_id].include? template_id
130
              @template_locations[location_id] += [template_id]
131
              @api.resource(:locations).call(:update, {
132
                  'id' => location_id,
133
                  'location' => {
134
                      'config_template_ids' => @template_locations[location_id]
135
                  }
136
              })
137
            end
138
          end
139

    
140
          puts _('done') if option_verbose?
141
        end
142
      end
143

    
144
      def export_associations(template)
145
        return '' unless template['template_combinations']
146
        values = CSV.generate do |column|
147
          column << template['template_combinations'].collect do |combo|
148
            "#{combo['hostgroup_name']}|#{combo['environment_name']}"
149
          end
150
        end
151
        values.delete!("\n")
152
      end
153
    end
154
  end
155
end