Class: PuppetClassImporter

Inherits:
Object
  • Object
show all
Defined in:
app/models/puppet_class_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ PuppetClassImporter


3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/models/puppet_class_importer.rb', line 3

def initialize args = { }
  @foreman_classes = { }
  @proxy_classes   = { }
  if args[:proxy]
    @proxy = args[:proxy]
  elsif args[:url]
    @proxy = ProxyAPI::Puppet.new(:url => args[:url])
  else
    url = SmartProxy.puppet_proxies.first.try(:url)
    raise "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


142
143
144
145
146
# File 'app/models/puppet_class_importer.rb', line 142

def actual_classes environment
  @proxy_classes[environment] ||= proxy.classes(environment).reject do |key, value|
    ignored_classes.find { |filter| filter.is_a?(Regexp) && filter =~ key }
  end
end

#actual_classes_name(environment) ⇒ Object


148
149
150
# File 'app/models/puppet_class_importer.rb', line 148

def actual_classes_name environment
  actual_classes(environment).keys
end

#actual_environmentsObject


120
121
122
# File 'app/models/puppet_class_importer.rb', line 120

def actual_environments
  @proxy_envs ||= (proxy.environments.map(&:to_s) - ignored_environments)
end

#changesObject

return changes hash, currently exists to keep compatibility with importer html


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/puppet_class_importer.rb', line 18

def changes
  changes = { 'new' => { }, 'obsolete' => { }, 'updated' => { } }

  actual_environments.each do |env|
    new     = new_classes_for(env)
    old     = removed_classes_for(env)
    updated = updated_classes_for(env)
    changes['new'][env] = new if new.any?
    changes['obsolete'][env] = old if old.any?
    changes['updated'][env] = updated if updated.any?
  end

  old_environments.each do |env|
    changes['obsolete'][env] ||= []
    changes['obsolete'][env] << "_destroy_" unless actual_environments.include?(env)
  end
  changes
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.


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/puppet_class_importer.rb', line 89

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


132
133
134
135
136
# File 'app/models/puppet_class_importer.rb', line 132

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(:lookup_keys, :class_params)
end

#db_classes_name(environment) ⇒ Object


138
139
140
# File 'app/models/puppet_class_importer.rb', line 138

def db_classes_name environment
  db_classes(environment).map(&:name)
end

#db_environmentsObject


116
117
118
# File 'app/models/puppet_class_importer.rb', line 116

def db_environments
  @foreman_envs ||= (Environment.all.map(&:name) - ignored_environments)
end

#new_classes_for(environment) ⇒ Object


63
64
65
66
67
68
69
70
# File 'app/models/puppet_class_importer.rb', line 63

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_environmentsObject


124
125
126
# File 'app/models/puppet_class_importer.rb', line 124

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


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/puppet_class_importer.rb', line 44

def obsolete_and_new changes = { }
  return if changes.empty?
  changes.values.map(&:keys).flatten.uniq.each do |env_name|
    if changes['new'] and changes['new'][env_name].try(:>, '') # we got new classes
      add_classes_to_foreman(env_name, JSON.parse(changes['new'][env_name]))
    end
    if changes['obsolete'] and changes['obsolete'][env_name].try(:>, '') # we need to remove classes
      remove_classes_from_foreman(env_name, JSON.parse(changes['obsolete'][env_name]))
    end
    if changes['updated'] and changes['updated'][env_name].try(:>, '') # we need to update classes
      update_classes_in_foreman(env_name, JSON.parse(changes['updated'][env_name]))
    end
  end
  []
  #rescue => e
  #  logger.error(e)
  #  [e.to_s]
end

#old_environmentsObject


128
129
130
# File 'app/models/puppet_class_importer.rb', line 128

def old_environments
  db_environments - actual_environments
end

#removed_classes_for(environment) ⇒ Object


72
73
74
# File 'app/models/puppet_class_importer.rb', line 72

def removed_classes_for environment
  db_classes_name(environment) - actual_classes_name(environment)
end

#updated_classes(actual_params, db_params) ⇒ Object


104
105
106
107
108
109
110
111
112
113
# File 'app/models/puppet_class_importer.rb', line 104

def updated_classes(actual_params, db_params)
  updated = { }
  db_params.map do |p|
    param_name = p.to_s
    if !p.override && actual_params.has_key?(param_name) && actual_params[param_name] != p.default_value
      updated[param_name] = actual_params[param_name]
    end
  end
  updated
end

#updated_classes_for(environment) ⇒ Object


76
77
78
79
80
81
82
83
84
# File 'app/models/puppet_class_importer.rb', line 76

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(&:lookup_key)
      compare_classes(environment, db_class.name, params)
    end.compact
  ]
end