Project

General

Profile

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

runcible / lib / runcible / extensions / consumer.rb @ 1a2d49fd

1
module Runcible
2
  module Extensions
3
    class Consumer < Runcible::Resources::Consumer
4
      # Bind a consumer to all repositories with a given ID
5
      #
6
      # @param  [String]               id       the consumer ID
7
      # @param  [String]               repo_id  the repo ID to bind to
8
      # @param  [String]               type_id the distributor type_id to bind to
9
      # @param  [Hash]                 options  options to pass to the bindings
10
      # @option  options [Boolean]     :notify_agent sends consumer a notification
11
      # @option  options [Hash]        :binding_config sends consumer a notification
12
      # @return [RestClient::Response]          set of tasks representing each bind operation
13
      def bind_all(id, repo_id, type_id, options = {})
14
        details = repository_extension.retrieve_with_details(repo_id)['distributors'].map do |d|
15
          bind(id, repo_id, d['id'], options) if d['distributor_type_id'] == type_id
16
        end
17
        details.compact.flatten
18
      end
19

    
20
      # Unbind a consumer to all repositories with a given ID
21
      #
22
      # @param  [String]               id       the consumer ID
23
      # @param  [String]               repo_id  the repo ID to unbind from
24
      # @param  [String]               type_id  the distributor type_id to unbind from
25
      # @return [RestClient::Response]          set of tasks representing each unbind operation
26
      def unbind_all(id, repo_id, type_id)
27
        details = repository_extension.retrieve_with_details(repo_id)['distributors'].map do |d|
28
          unbind(id, repo_id, d['id']) if d['distributor_type_id'] == type_id
29
        end
30
        details.compact.flatten
31
      end
32

    
33
      # Activate a consumer as a pulp node
34
      #
35
      # @param  [String]               id       the consumer ID
36
      # @param  [String]               update_strategy update_strategy for the node (defaults to additive)
37
      # @return [RestClient::Response]          response from update call
38
      def activate_node(id, update_strategy = 'additive')
39
        delta = {:notes => {'_child-node' => true,
40
                            '_node-update-strategy' => update_strategy}}
41
        self.update(id, delta)
42
      end
43

    
44
      # Deactivate a consumer as a pulp node
45
      #
46
      # @param  [String]               id       the consumer ID
47
      # @return [RestClient::Response]          response from update call
48
      def deactivate_node(id)
49
        delta = {:notes => {'child-node' => nil,
50
                            'update_strategy' => nil}}
51
        self.update(id, :delta => delta)
52
      end
53

    
54
      # Install content to a consumer
55
      #
56
      # @param  [String]               id       the consumer ID
57
      # @param  [String]               type_id  the type of content to install (e.g. rpm, errata)
58
      # @param  [Array]                units    array of units to install
59
      # @param  [Hash]                 options to pass to content install
60
      # @return [RestClient::Response]          task representing the install operation
61
      def install_content(id, type_id, units, options = {})
62
        install_units(id, generate_content(type_id, units), options)
63
      end
64

    
65
      # Update content on a consumer
66
      #
67
      # @param  [String]               id       the consumer ID
68
      # @param  [String]               type_id  the type of content to update (e.g. rpm, errata)
69
      # @param  [Array]                units    array of units to update
70
      # @param  [Hash]                 options to pass to content update
71
      # @return [RestClient::Response]          task representing the update operation
72
      def update_content(id, type_id, units, options = {})
73
        update_units(id, generate_content(type_id, units, options), options)
74
      end
75

    
76
      # Uninstall content from a consumer
77
      #
78
      # @param  [String]               id       the consumer ID
79
      # @param  [String]               type_id  the type of content to uninstall (e.g. rpm, errata)
80
      # @param  [Array]                units    array of units to uninstall
81
      # @return [RestClient::Response]          task representing the uninstall operation
82
      def uninstall_content(id, type_id, units)
83
        uninstall_units(id, generate_content(type_id, units))
84
      end
85

    
86
      # Generate the content units used by other functions
87
      #
88
      # @param  [String]  type_id the type of content (e.g. rpm, errata)
89
      # @param  [Array]   units   array of units
90
      # @param  [Hash]    options contains options which may impact the format of the content (e.g :all => true)
91
      # @return [Array]           array of formatted content units
92
      def generate_content(type_id, units, options = {})
93
        content = []
94

    
95
        case type_id
96
        when 'rpm', 'package_group'
97
          unit_key = :name
98
        when 'erratum'
99
          unit_key = :id
100
        when 'repository'
101
          unit_key = :repo_id
102
        else
103
          unit_key = :id
104
        end
105

    
106
        if options[:all]
107
          content_unit = {}
108
          content_unit[:type_id] = type_id
109
          content_unit[:unit_key] = {}
110
          content.push(content_unit)
111
        elsif units.nil?
112
          content = [{:unit_key => nil, :type_id => type_id}]
113
        else
114
          units.each do |unit|
115
            content_unit = {}
116
            content_unit[:type_id] = type_id
117
            content_unit[:unit_key] = if unit.is_a?(Hash)
118
                                        #allow user to pass in entire unit
119
                                        unit
120
                                      else
121
                                        { unit_key => unit }
122
                                      end
123

    
124
            content.push(content_unit)
125
          end
126
        end
127
        content
128
      end
129

    
130
      # Regenerate the applicability for a set of consumers
131
      #
132
      # @param [String, Array]         ids  array of consumer ids
133
      # @return [RestClient::Response]
134
      def regenerate_applicability_by_ids(ids)
135
        criteria = {
136
          'consumer_criteria' => { 'filters' => { 'id' => { '$in' => ids } } }
137
        }
138
        regenerate_applicability(criteria)
139
      end
140

    
141
      # Retrieve the set of errata that is applicable to a consumer(s)
142
      #
143
      # @param  [String, Array]         ids             string containing a single consumer id or an array of ids
144
      # @return [RestClient::Response]  content applicability hash with details of errata available to consumer(s)
145
      def applicable_errata(ids)
146
        applicable_for_type(ids, Runcible::Extensions::Errata.content_type)
147
      end
148

    
149
      # Retrieve the set of rpms that are applicable to a consumer(s)
150
      #
151
      # @param  [String, Array]         ids             string containing a single consumer id or an array of ids
152
      # @return [RestClient::Response]  content applicability hash with details of rpms available to consumer(s)
153
      def applicable_rpms(ids)
154
        applicable_for_type(ids, Runcible::Extensions::Rpm.content_type)
155
      end
156

    
157
      private
158

    
159
      def applicable_for_type(ids, type)
160
        ids = [ids] if ids.is_a? String
161

    
162
        criteria = {
163
          'criteria' => { 'filters' => { 'id' => { '$in' => ids } } },
164
          'content_types' => [type]
165
        }
166
        applicability(criteria)
167
      end
168

    
169
      def repository_extension
170
        Runcible::Extensions::Repository.new(self.config)
171
      end
172
    end
173
  end
174
end