foreman/app/models/concerns/host_common.rb @ 9dd00534
31b7d5de | Dominic Cleal | require 'securerandom'
|
|
e5d3f34e | Ohad Levy | #Common methods between host and hostgroup
|
|
# mostly for template rendering consistency
|
|||
module HostCommon
|
|||
dc457681 | Joseph Mitchell Magen | extend ActiveSupport::Concern
|
|
ace6fbad | Ohad Levy | ||
dc457681 | Joseph Mitchell Magen | included do
|
|
ae4998bc | Tomer Brisker | include CounterCacheFix
|
|
6895854c | Stephen Benjamin | counter_cache = "#{model_name.split(":").first.pluralize.downcase}_count".to_sym # e.g. :hosts_count
|
|
belongs_to :architecture, :counter_cache => counter_cache
|
|||
belongs_to :environment, :counter_cache => counter_cache
|
|||
belongs_to :operatingsystem, :counter_cache => counter_cache
|
|||
dc457681 | Joseph Mitchell Magen | belongs_to :medium
|
|
belongs_to :ptable
|
|||
belongs_to :puppet_proxy, :class_name => "SmartProxy"
|
|||
belongs_to :puppet_ca_proxy, :class_name => "SmartProxy"
|
|||
77f70152 | Stephen Benjamin | belongs_to :realm, :counter_cache => counter_cache
|
|
c6e02bd3 | Joseph Magen | belongs_to :compute_profile
|
|
7acc469b | Amos Benari | ||
c4bfd47f | Stephen Benjamin | before_save :check_puppet_ca_proxy_is_required?, :crypt_root_pass
|
|
111cde57 | Joseph Magen | has_many :host_config_groups, :as => :host
|
|
has_many :config_groups, :through => :host_config_groups, :after_add => :update_config_group_counters,
|
|||
:after_remove => :update_config_group_counters
|
|||
has_many :config_group_classes, :through => :config_groups
|
|||
has_many :group_puppetclasses, :through => :config_groups, :source => :puppetclasses
|
|||
alias_method :all_puppetclasses, :classes
|
|||
9dd00534 | Tom Caspy | has_many :lookup_values, :primary_key => :lookup_value_matcher, :foreign_key => :match, :validate => false
|
|
dc457681 | Joseph Mitchell Magen | # See "def lookup_values_attributes=" under, for the implementation of accepts_nested_attributes_for :lookup_values
|
|
accepts_nested_attributes_for :lookup_values
|
|||
9dd00534 | Tom Caspy | ||
before_save :set_lookup_value_matcher
|
|||
a6810fc6 | Joseph Mitchell Magen | # Replacement of accepts_nested_attributes_for :lookup_values,
|
|
# to work around the lack of `host_id` column in lookup_values.
|
|||
5f029ed6 | Daniel Lobato | def lookup_values_attributes=(lookup_values_attributes)
|
|
a6810fc6 | Joseph Mitchell Magen | lookup_values_attributes.each_value do |attribute|
|
|
attr = attribute.dup
|
|||
c6b42368 | Tom Caspy | if id = attr.delete(:id)
|
|
lookup_value = self.lookup_values.to_a.select{|i| i.id == id}.first
|
|||
a6810fc6 | Joseph Mitchell Magen | if lookup_value
|
|
mark_for_destruction = ActiveRecord::ConnectionAdapters::Column.value_to_boolean attr.delete(:_destroy)
|
|||
lookup_value.attributes = attr
|
|||
c6b42368 | Tom Caspy | lookup_value.mark_for_destruction if mark_for_destruction
|
|
a6810fc6 | Joseph Mitchell Magen | end
|
|
elsif !ActiveRecord::ConnectionAdapters::Column.value_to_boolean attr.delete(:_destroy)
|
|||
c6b42368 | Tom Caspy | self.lookup_values.build(attr.merge(:match => lookup_value_match, :host_or_hostgroup => self))
|
|
7acc469b | Amos Benari | end
|
|
end
|
|||
36f93e4d | Ohad Levy | end
|
|
e5d3f34e | Ohad Levy | end
|
|
dc457681 | Joseph Mitchell Magen | # Returns a url pointing to boot file
|
|
5f029ed6 | Daniel Lobato | def url_for_boot(file)
|
|
dc457681 | Joseph Mitchell Magen | "#{os.medium_uri(self)}/#{os.url_for_boot(file)}"
|
|
end
|
|||
e5d3f34e | Ohad Levy | ||
dc457681 | Joseph Mitchell Magen | def puppetca?
|
|
return false if self.respond_to?(:managed?) and !managed?
|
|||
!!(puppet_ca_proxy and puppet_ca_proxy.url.present?)
|
|||
end
|
|||
36f93e4d | Ohad Levy | ||
dc457681 | Joseph Mitchell Magen | # no need to store anything in the db if the entry is plain "puppet"
|
|
# If the system is using smart proxies and the user has run the smartproxy:migrate task
|
|||
# then the puppetmaster functions handle smart proxy objects
|
|||
def puppetmaster
|
|||
puppet_proxy.to_s
|
|||
end
|
|||
e5d3f34e | Ohad Levy | ||
dc457681 | Joseph Mitchell Magen | def puppet_ca_server
|
|
puppet_ca_proxy.to_s
|
|||
end
|
|||
0539fada | Ohad Levy | ||
dc457681 | Joseph Mitchell Magen | # If the host/hostgroup has a medium then use the path from there
|
|
# Else if the host/hostgroup's operatingsystem has only one media then use the image_path from that as this is automatically displayed when there is only one item
|
|||
# Else we cannot provide a default and it is cut and paste time
|
|||
def default_image_file
|
|||
return "" unless operatingsystem and operatingsystem.supports_image
|
|||
if medium
|
|||
nfs_path = medium.try :image_path
|
|||
if operatingsystem.try(:media) and operatingsystem.media.size == 1
|
|||
nfs_path ||= operatingsystem.media.first.image_path
|
|||
a6db0470 | Paul Kelly | end
|
|
dc457681 | Joseph Mitchell Magen | # We encode the hw_model into the image file name as not all Sparc flashes can contain all possible hw_models. The user can always
|
|
# edit it if required or use symlinks if they prefer.
|
|||
hw_model = model.try :hardware_model if defined?(model_id)
|
|||
operatingsystem.interpolate_medium_vars(nfs_path, architecture.name, operatingsystem) +\
|
|||
"#{operatingsystem.file_prefix}.#{architecture}#{hw_model.empty? ? "" : "." + hw_model.downcase}.#{operatingsystem.image_extension}"
|
|||
else
|
|||
""
|
|||
a6db0470 | Paul Kelly | end
|
|
dc457681 | Joseph Mitchell Magen | end
|
|
a6db0470 | Paul Kelly | ||
5f029ed6 | Daniel Lobato | def image_file=(file)
|
|
dc457681 | Joseph Mitchell Magen | # We only save a value into the image_file field if the value is not the default path, (which was placed in the entry when it was displayed,)
|
|
# and it is not a directory, (ends in /)
|
|||
a6b0eeb0 | Joseph Magen | value = ( (default_image_file == file) or (file =~ /\/\Z/) or file == "") ? nil : file
|
|
dc457681 | Joseph Mitchell Magen | write_attribute :image_file, value
|
|
end
|
|||
a6db0470 | Paul Kelly | ||
dc457681 | Joseph Mitchell Magen | def image_file
|
|
super || default_image_file
|
|||
end
|
|||
da6fa387 | Ohad Levy | ||
c4bfd47f | Stephen Benjamin | def crypt_root_pass
|
|
db176297 | Dominic Cleal | # hosts will always copy and crypt the password from parents when saved, but hostgroups should
|
|
# only crypt if the attribute is stored, else will stay blank and inherit
|
|||
cd032085 | Daniel Lobato | unencrypted_pass = if is_a?(Hostgroup)
|
|
db176297 | Dominic Cleal | read_attribute(:root_pass)
|
|
else
|
|||
root_pass
|
|||
end
|
|||
if unencrypted_pass.present?
|
|||
526e02fb | Tom Caspy | is_actually_encrypted = if PasswordCrypt.crypt_gnu_compatible?
|
|
unencrypted_pass.match('^\$\d+\$.+\$.+')
|
|||
else
|
|||
unencrypted_pass.starts_with?("$")
|
|||
end
|
|||
if is_actually_encrypted
|
|||
self.root_pass = self.grub_pass = unencrypted_pass
|
|||
else
|
|||
self.root_pass = operatingsystem.nil? ? PasswordCrypt.passw_crypt(unencrypted_pass) : PasswordCrypt.passw_crypt(unencrypted_pass, operatingsystem.password_hash)
|
|||
self.grub_pass = PasswordCrypt.grub2_passw_crypt(unencrypted_pass)
|
|||
end
|
|||
2d7f0315 | Dmitri Dolguikh | end
|
|
dc457681 | Joseph Mitchell Magen | end
|
|
36f93e4d | Ohad Levy | ||
5f029ed6 | Daniel Lobato | def param_true?(name)
|
|
c74610f9 | Walden Raines | params.has_key?(name) && Foreman::Cast.to_bool(value)
|
|
3701a8d1 | Lukas Zapletal | end
|
|
5f029ed6 | Daniel Lobato | def param_false?(name)
|
|
c74610f9 | Walden Raines | params.has_key?(name) && Foreman::Cast.to_bool(value) == false
|
|
6807d423 | Stephen Benjamin | end
|
|
111cde57 | Joseph Magen | def cg_class_ids
|
|
cd032085 | Daniel Lobato | cg_ids = if is_a?(Hostgroup)
|
|
111cde57 | Joseph Magen | path.each.map(&:config_group_ids).flatten.uniq
|
|
else
|
|||
config_group_ids + (hostgroup ? hostgroup.path.each.map(&:config_group_ids).flatten.uniq : [] )
|
|||
end
|
|||
ConfigGroupClass.where(:config_group_id => cg_ids).pluck(:puppetclass_id)
|
|||
end
|
|||
def hg_class_ids
|
|||
cd032085 | Daniel Lobato | hg_ids = if is_a?(Hostgroup)
|
|
afe02d30 | Daniel Lobato | path_ids
|
|
111cde57 | Joseph Magen | elsif hostgroup
|
|
afe02d30 | Daniel Lobato | hostgroup.path_ids
|
|
111cde57 | Joseph Magen | end
|
|
HostgroupClass.where(:hostgroup_id => hg_ids).pluck(:puppetclass_id)
|
|||
end
|
|||
def host_class_ids
|
|||
cd032085 | Daniel Lobato | is_a?(Host::Base) ? host_classes.pluck(:puppetclass_id) : []
|
|
111cde57 | Joseph Magen | end
|
|
def all_puppetclass_ids
|
|||
cg_class_ids + hg_class_ids + host_class_ids
|
|||
end
|
|||
def classes(env = environment)
|
|||
conditions = {:id => all_puppetclass_ids }
|
|||
if env
|
|||
env.puppetclasses.where(conditions)
|
|||
else
|
|||
Puppetclass.where(conditions)
|
|||
end
|
|||
end
|
|||
def puppetclass_ids
|
|||
classes.reorder('').pluck('puppetclasses.id')
|
|||
end
|
|||
def classes_in_groups
|
|||
conditions = {:id => cg_class_ids }
|
|||
if environment
|
|||
environment.puppetclasses.where(conditions) - parent_classes
|
|||
else
|
|||
Puppetclass.where(conditions) - parent_classes
|
|||
end
|
|||
end
|
|||
def individual_puppetclasses
|
|||
puppetclasses - classes_in_groups
|
|||
end
|
|||
def available_puppetclasses
|
|||
return Puppetclass.scoped if environment_id.blank?
|
|||
environment.puppetclasses - parent_classes
|
|||
end
|
|||
9dd00534 | Tom Caspy | protected
|
|
def set_lookup_value_matcher
|
|||
self.lookup_value_matcher = lookup_value_match
|
|||
end
|
|||
dc457681 | Joseph Mitchell Magen | private
|
|
ace6fbad | Ohad Levy | ||
dc457681 | Joseph Mitchell Magen | # fall back to our puppet proxy in case our puppet ca is not defined/used.
|
|
def check_puppet_ca_proxy_is_required?
|
|||
return true if puppet_ca_proxy_id.present? or puppet_proxy_id.blank?
|
|||
8f657a84 | Stephen Benjamin | if puppet_proxy.has_feature?('Puppet CA')
|
|
dc457681 | Joseph Mitchell Magen | self.puppet_ca_proxy ||= puppet_proxy
|
|
ace6fbad | Ohad Levy | end
|
|
dc457681 | Joseph Mitchell Magen | rescue
|
|
true # we don't want to break anything, so just skipping.
|
|||
36f93e4d | Ohad Levy | end
|
|
111cde57 | Joseph Magen | ||
def cnt_hostgroups(config_group)
|
|||
3efe1dab | Daniel Lobato | Hostgroup.search_for(%{config_group="#{config_group.name}"}).count
|
|
111cde57 | Joseph Magen | end
|
|
def cnt_hosts(config_group)
|
|||
3efe1dab | Daniel Lobato | Host::Managed.search_for(%{config_group="#{config_group.name}"}).count
|
|
111cde57 | Joseph Magen | end
|
|
def update_config_group_counters(record)
|
|||
record.update_attribute(:hostgroups_count, cnt_hostgroups(record))
|
|||
record.update_attribute(:hosts_count, cnt_hosts(record))
|
|||
83683ed0 | Tomer Brisker | ||
record.update_puppetclasses_total_hosts
|
|||
111cde57 | Joseph Magen | end
|
|
e5d3f34e | Ohad Levy | end
|