Module: ForemanSalt::Concerns::HostManagedExtensions

Extended by:
ActiveSupport::Concern
Defined in:
app/models/foreman_salt/concerns/host_managed_extensions.rb

Defined Under Namespace

Modules: Overrides

Instance Method Summary collapse

Instance Method Details

#all_salt_modulesObject



72
73
74
75
76
77
# File 'app/models/foreman_salt/concerns/host_managed_extensions.rb', line 72

def all_salt_modules
  return [] unless salt_environment

  modules = salt_modules + (hostgroup ? hostgroup.all_salt_modules : [])
  ForemanSalt::SaltModule.in_environment(salt_environment).where(id: modules)
end

#autosign_grain_nameObject



64
65
66
# File 'app/models/foreman_salt/concerns/host_managed_extensions.rb', line 64

def autosign_grain_name
  'autosign_key'
end

#derive_salt_grains(use_autosign: false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/foreman_salt/concerns/host_managed_extensions.rb', line 93

def derive_salt_grains(use_autosign: false)
  grains = {}
  begin
    Rails.logger.info('Derive Salt Grains from host_params and autosign_key')
    grains[autosign_grain_name] = salt_autosign_key if use_autosign && !salt_autosign_key.nil?
    unless host_params[host_params_grains_name].nil? ||
           host_params[host_params_grains_name].class != Hash
      grains.merge!(host_params[host_params_grains_name])
    end
  rescue Foreman::Exception => e
    Rails.logger.warn("Unable to derive Salt Grains: #{e}")
  end
  grains
end

#host_params_grains_nameObject



60
61
62
# File 'app/models/foreman_salt/concerns/host_managed_extensions.rb', line 60

def host_params_grains_name
  'salt_grains'
end

#salt_masterObject



79
80
81
# File 'app/models/foreman_salt/concerns/host_managed_extensions.rb', line 79

def salt_master
  salt_proxy.to_s
end

#salt_modules_for_encObject



68
69
70
# File 'app/models/foreman_salt/concerns/host_managed_extensions.rb', line 68

def salt_modules_for_enc
  all_salt_modules.collect(&:name).uniq
end

#salt_modules_in_host_environmentObject



83
84
85
86
87
88
89
90
91
# File 'app/models/foreman_salt/concerns/host_managed_extensions.rb', line 83

def salt_modules_in_host_environment
  return unless salt_modules.any?

  if salt_environment
    errors.add(:base, _('Salt states must be in the environment of the host')) unless (salt_modules - salt_environment.salt_modules).empty?
  else
    errors.add(:base, _('Host must have an environment in order to set salt states'))
  end
end

#salt_paramsObject



49
50
51
52
53
54
55
56
57
58
# File 'app/models/foreman_salt/concerns/host_managed_extensions.rb', line 49

def salt_params
  variables = ForemanSalt::SaltVariable.where(salt_module_id: all_salt_modules.pluck(:id), override: true)
  values = variables.values_hash(self)

  variables.each_with_object({}) do |var, memo|
    value = values[var]
    memo[var.key] = value if value
    memo
  end
end