Class: ForemanPuppet::PuppetClassImporter
- Inherits:
-
Object
- Object
- ForemanPuppet::PuppetClassImporter
- Defined in:
- app/services/foreman_puppet/puppet_class_importer.rb
Constant Summary collapse
- CHANGE_KINDS =
%w[new obsolete updated ignored].freeze
Instance Method Summary collapse
-
#actual_classes(environment) ⇒ Object
Returns an Hash of puppet class names without ignored classes.
- #actual_classes_name(environment) ⇒ Object
- #actual_environments ⇒ Object
-
#changes ⇒ Object
return changes hash, currently exists to keep compatibility with importer html.
-
#changes_for_environment(environment, changes) ⇒ Object
Adds class changes of an environment to a changes hash in new, obsolete and updated.
-
#compare_classes(environment, klass, db_params) ⇒ Object
This method check if the puppet class exists in this environment, and compare the class params.
- #db_classes(environment) ⇒ Object
- #db_classes_name(environment) ⇒ Object
- #db_environments ⇒ Object
- #ignored_boolean_environment_names? ⇒ Boolean
-
#ignored_class?(class_name) ⇒ Boolean
Returns true when the class name matches any pattern in ignored_classes.
-
#ignored_classes_for(environment) ⇒ Object
Gives back the classes ignored for a given environment.
-
#initialize(args = {}) ⇒ PuppetClassImporter
constructor
A new instance of PuppetClassImporter.
- #new_classes_for(environment) ⇒ Object
- #new_environments ⇒ Object
-
#obsolete_and_new(changes = {}) ⇒ Object
Update the environments and puppetclasses based upon the user’s selection It does a best attempt and can fail to perform all operations due to the user requesting impossible selections.
- #old_environments ⇒ Object
-
#proxy_classes_for(environment) ⇒ Object
Returns all classes for a given environment.
- #proxy_environments ⇒ Object
- #removed_classes_for(environment) ⇒ Object
- #to_be_created_environments ⇒ Object
- #updated_classes(actual_params, db_params) ⇒ Object
- #updated_classes_for(environment) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ PuppetClassImporter
Returns a new instance of PuppetClassImporter.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 5 def initialize(args = {}) @foreman_classes = {} @proxy_classes = {} @environment = args[:env] if args[:proxy] @proxy = args[:proxy] elsif args[:url] @proxy = ProxyAPI::Puppet.new(url: args[:url]) else url = SmartProxy.with_features('Puppet').first.try(:url) raise ::Foreman::Exception, N_("Can't find a valid Proxy with a Puppet feature") if url.blank? @proxy = ProxyAPI::Puppet.new(url: url) end end |
Instance Method Details
#actual_classes(environment) ⇒ Object
Returns an Hash of puppet class names without ignored classes
Params:
* +environment+: {String} containing the environment name
203 204 205 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 203 def actual_classes(environment) proxy_classes_for(environment).reject { |key, _| ignored_class? key } end |
#actual_classes_name(environment) ⇒ Object
207 208 209 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 207 def actual_classes_name(environment) actual_classes(environment).keys end |
#actual_environments ⇒ Object
168 169 170 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 168 def actual_environments (proxy_environments & (User.current.visible_environments + to_be_created_environments)) - ignored_environments end |
#changes ⇒ Object
return changes hash, currently exists to keep compatibility with importer html
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 22 def changes changes = CHANGE_KINDS.index_with { |_kind| {} } if @environment.nil? actual_environments.each do |env| changes_for_environment(env, changes) end old_environments.each do |env| changes['obsolete'][env] ||= [] changes['obsolete'][env] << '_destroy_' unless actual_environments.include?(env) end ignored_environments.each do |env| changes['ignored'][env] ||= [] changes['ignored'][env] << '_ignored_' end else changes_for_environment(@environment, changes) end changes end |
#changes_for_environment(environment, changes) ⇒ Object
Adds class changes of an environment to a changes hash in new, obsolete and updated
Params:
* +environment+: {String} of environments name
* +changes+: {Hash} to add changes to
52 53 54 55 56 57 58 59 60 61 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 52 def changes_for_environment(environment, changes) new_classes = new_classes_for(environment) old_classes = removed_classes_for(environment) updated_classes = updated_classes_for(environment) ignored_classes = ignored_classes_for(environment) changes['new'][environment] = new_classes if new_classes.any? changes['obsolete'][environment] = old_classes if old_classes.any? changes['updated'][environment] = updated_classes if updated_classes.any? changes['ignored'][environment] = ignored_classes if ignored_classes.any? end |
#compare_classes(environment, klass, db_params) ⇒ Object
This method check if the puppet class exists in this environment, and compare the class params. Changes in the params are categorized to new parameters, removed parameters and parameters with a new default value.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 140 def compare_classes(environment, klass, db_params) return nil unless (actual_class = actual_classes(environment)[klass]) actual_params = actual_class.parameters db_param_names = db_params.map(&:to_s) param_changes = {} old = db_param_names - actual_params.keys param_changes['obsolete'] = old if old.any? new = actual_params.reject { |key, _value| db_param_names.include?(key) } param_changes['new'] = new if new.any? updated = updated_classes(actual_params, db_params) param_changes['updated'] = updated if updated.any? [klass, param_changes] if param_changes.keys.any? end |
#db_classes(environment) ⇒ Object
188 189 190 191 192 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 188 def db_classes(environment) return @foreman_classes[environment] if @foreman_classes[environment] return [] unless (env = Environment.find_by(name: environment)) @foreman_classes[environment] = env.puppetclasses.includes(:class_params) end |
#db_classes_name(environment) ⇒ Object
194 195 196 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 194 def db_classes_name(environment) db_classes(environment).map(&:name) end |
#db_environments ⇒ Object
164 165 166 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 164 def db_environments @db_environments ||= (Environment.pluck(:name) - ignored_environments) end |
#ignored_boolean_environment_names? ⇒ Boolean
211 212 213 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 211 def ignored_boolean_environment_names? ignored_environments.any? { |item| item.is_a?(TrueClass) || item.is_a?(FalseClass) } end |
#ignored_class?(class_name) ⇒ Boolean
Returns true when the class name matches any pattern in ignored_classes
Params:
* +class_name+: {String} containing the class to be checked
131 132 133 134 135 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 131 def ignored_class?(class_name) ignored_classes.any? do |filter| filter.is_a?(Regexp) && filter =~ class_name end end |
#ignored_classes_for(environment) ⇒ Object
Gives back the classes ignored for a given environment
Params:
* +environment+: {String} name of the environment
120 121 122 123 124 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 120 def ignored_classes_for(environment) proxy_classes_for(environment).keys.select do |class_name| ignored_class?(class_name) end end |
#new_classes_for(environment) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 92 def new_classes_for(environment) old_classes = db_classes_name(environment) HashWithIndifferentAccess[ actual_classes(environment).values.map do |actual_class| [actual_class.to_s, { 'new' => actual_class.parameters }] unless old_classes.include?(actual_class.to_s) end.compact ] end |
#new_environments ⇒ Object
180 181 182 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 180 def new_environments actual_environments - db_environments end |
#obsolete_and_new(changes = {}) ⇒ Object
Update the environments and puppetclasses based upon the user’s selection It does a best attempt and can fail to perform all operations due to the user requesting impossible selections. Repeat the operation if errors are shown, after fixing the request. changed
: Hash with two keys: :new and :obsolete.
changed[:/new|obsolete/] is and Array of Strings
Returns : Array of Strings containing all record errors
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 70 def obsolete_and_new(changes = {}) return if changes.empty? changes.values.map(&:keys).flatten.uniq.each do |env_name| add_classes_to_foreman(env_name, JSON.parse(changes['new'][env_name])) if changes['new'] && changes['new'][env_name].try(:>, '') # we got new classes remove_classes_from_foreman(env_name, JSON.parse(changes['obsolete'][env_name])) if changes['obsolete'] && changes['obsolete'][env_name].try(:>, '') # we need to remove classes update_classes_in_foreman(env_name, JSON.parse(changes['updated'][env_name])) if changes['updated'] && changes['updated'][env_name].try(:>, '') # we need to update classes end [] rescue StandardError => e Foreman::Logging.exception('Failed to calculate obsolete and new', e) [e.to_s] end |
#old_environments ⇒ Object
184 185 186 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 184 def old_environments db_environments - actual_environments end |
#proxy_classes_for(environment) ⇒ Object
Returns all classes for a given environment
Params:
* +environment+: {String} containing the name of the environment
88 89 90 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 88 def proxy_classes_for(environment) @proxy_classes[environment] ||= proxy.classes(environment) end |
#proxy_environments ⇒ Object
172 173 174 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 172 def proxy_environments proxy.environments.map(&:to_s) end |
#removed_classes_for(environment) ⇒ Object
101 102 103 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 101 def removed_classes_for(environment) db_classes_name(environment) - actual_classes_name(environment) end |
#to_be_created_environments ⇒ Object
176 177 178 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 176 def to_be_created_environments proxy_environments - Environment.unscoped.where(name: proxy_environments).pluck(:name) end |
#updated_classes(actual_params, db_params) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 155 def updated_classes(actual_params, db_params) updated = {} db_params.map do |p| param_name = p.to_s updated[param_name] = actual_params[param_name] if !p.override && actual_params.key?(param_name) && actual_params[param_name] != p.default_value end updated end |
#updated_classes_for(environment) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'app/services/foreman_puppet/puppet_class_importer.rb', line 105 def updated_classes_for(environment) return [] unless db_environments.include?(environment) && actual_environments.include?(environment) HashWithIndifferentAccess[ db_classes(environment).map do |db_class| params = EnvironmentClass.all_parameters_for_class(db_class.id, find_or_create_env(environment).id).map(&:puppetclass_lookup_key) compare_classes(environment, db_class.name, params) end.compact ] end |