Project

General

Profile

Download (10 KB) Statistics
| Branch: | Tag: | Revision:
086ec942 Ohad Levy
class Hostgroup < ActiveRecord::Base
acfbc458 Marek Hulan
include Authorizable
8b737c9c Joseph Magen
extend FriendlyId
friendly_id :title
611f5bff Amos Benari
include Taxonomix
e5d3f34e Ohad Levy
include HostCommon
0a17d4da Tom Caspy
7ff50dd8 Joseph Magen
include NestedAncestryCommon
0a17d4da Tom Caspy
validates :name, :presence => true, :uniqueness => {:scope => :ancestry, :case_sensitive => false}
validates :title, :presence => true, :uniqueness => true

111cde57 Joseph Magen
include ScopedSearchExtensions
83683ed0 Tomer Brisker
include PuppetclassTotalHosts::Indirect
e2d3654e Joseph Mitchell Magen
5fbe760e Ori Rabin
validates_lengths_from_database :except => [:name]
ff8cc704 Joseph Mitchell Magen
before_destroy EnsureNotUsedBy.new(:hosts)
83683ed0 Tomer Brisker
has_many :hostgroup_classes
has_many :puppetclasses, :through => :hostgroup_classes, :dependent => :destroy
dff926cb Tomer Brisker
validates :name, :presence => true
9c30b63e Joseph Magen
validates :root_pass, :allow_blank => true, :length => {:minimum => 8, :message => _('should be 8 characters or more')}
998e1438 Ori Rabin
has_many :group_parameters, :dependent => :destroy, :foreign_key => :reference_id, :inverse_of => :hostgroup
32ced385 Ori Rabin
accepts_nested_attributes_for :group_parameters, :allow_destroy => true
998e1438 Ori Rabin
include ParameterValidators
83683ed0 Tomer Brisker
has_many_hosts :after_add => :update_puppetclasses_total_hosts,
:after_remove => :update_puppetclasses_total_hosts
7d993b41 Joseph Mitchell Magen
has_many :template_combinations, :dependent => :destroy
510d53cd Marek Hulan
has_many :provisioning_templates, :through => :template_combinations
43c4bd72 Marek Hulan
include CounterCacheFix
counter_cache = "#{model_name.split(":").first.pluralize.downcase}_count".to_sym # e.g. :hosts_count
belongs_to :domain, :counter_cache => counter_cache
belongs_to :subnet

50541c05 Ohad Levy
before_save :remove_duplicated_nested_class
83683ed0 Tomer Brisker
after_save :update_ancestry_puppetclasses, :if => :ancestry_changed?
36f93e4d Ohad Levy
1cb5ba7e Stephen Benjamin
alias_attribute :arch, :architecture
e5d3f34e Ohad Levy
alias_attribute :os, :operatingsystem
fe4629de rbirnie
has_many :trends, :as => :trendable, :class_name => "ForemanTrend"
086ec942 Ohad Levy
34897490 Joseph Magen
nested_attribute_for :compute_profile_id, :environment_id, :domain_id, :puppet_proxy_id, :puppet_ca_proxy_id,
77f70152 Stephen Benjamin
:operatingsystem_id, :architecture_id, :medium_id, :ptable_id, :subnet_id, :realm_id
34897490 Joseph Magen
611f5bff Amos Benari
# with proc support, default_scope can no longer be chained
# include all default scoping here
4b9d1fa7 Stephen Benjamin
default_scope lambda {
with_taxonomy_scope do
832c0925 Joseph Magen
order("hostgroups.title")
4b9d1fa7 Stephen Benjamin
end
}
611f5bff Amos Benari
b80fa6f1 Ohad Levy
scoped_search :on => :name, :complete_value => :true
be933d8a Ohad Levy
scoped_search :in => :group_parameters, :on => :value, :on_key=> :name, :complete_value => true, :only_explicit => true, :rename => :params
b80fa6f1 Ohad Levy
scoped_search :in => :hosts, :on => :name, :complete_value => :true, :rename => "host"
scoped_search :in => :puppetclasses, :on => :name, :complete_value => true, :rename => :class, :operators => ['= ', '~ ']
scoped_search :in => :environment, :on => :name, :complete_value => :true, :rename => :environment
1b784c5b Tomer Brisker
scoped_search :on => :id, :complete_enabled => false, :only_explicit => true
d8894ebb Ohad Levy
# for legacy purposes, keep search on :label
scoped_search :on => :title, :complete_value => true, :rename => :label
111cde57 Joseph Magen
scoped_search :in => :config_groups, :on => :name, :complete_value => true, :rename => :config_group, :only_explicit => true, :operators => ['= ', '~ '], :ext_method => :search_by_config_group

def self.search_by_config_group(key, operator, value)
conditions = sanitize_sql_for_conditions(["config_groups.name #{operator} ?", value_to_sql(operator, value)])
hostgroup_ids = Hostgroup.unscoped.with_taxonomy_scope.joins(:config_groups).where(conditions).map(&:subtree_ids).flatten.uniq

opts = 'hostgroups.id < 0'
opts = "hostgroups.id IN(#{hostgroup_ids.join(',')})" unless hostgroup_ids.blank?
96144a47 Daniel Lobato
{:conditions => opts}
111cde57 Joseph Magen
end

b80fa6f1 Ohad Levy
if SETTINGS[:unattended]
0f1349ab Daniel Lobato
scoped_search :in => :architecture, :on => :name, :complete_value => true, :rename => :architecture
scoped_search :in => :operatingsystem, :on => :name, :complete_value => true, :rename => :os
scoped_search :in => :operatingsystem, :on => :description, :complete_value => true, :rename => :os_description
scoped_search :in => :operatingsystem, :on => :title, :complete_value => true, :rename => :os_title
scoped_search :in => :operatingsystem, :on => :major, :complete_value => true, :rename => :os_major
scoped_search :in => :operatingsystem, :on => :minor, :complete_value => true, :rename => :os_minor
1b784c5b Tomer Brisker
scoped_search :in => :operatingsystem, :on => :id, :complete_enabled => false, :rename => :os_id, :only_explicit => true
0f1349ab Daniel Lobato
scoped_search :in => :medium, :on => :name, :complete_value => true, :rename => "medium"
510d53cd Marek Hulan
scoped_search :in => :provisioning_templates, :on => :name, :complete_value => true, :rename => "template"
b80fa6f1 Ohad Levy
end

54358a76 Greg Sutcliffe
# returns reports for hosts in the User's filter set
scope :my_groups, lambda {
user = User.current
f2c78d4a Joseph Magen
unless user.admin?
89776bd5 Ohad Levy
conditions = sanitize_sql_for_conditions([" (hostgroups.id in (?))", user.hostgroup_ids])
54358a76 Greg Sutcliffe
conditions.sub!(/\s*\(\)\s*/, "")
conditions.sub!(/^(?:\(\))?\s?(?:and|or)\s*/, "")
conditions.sub!(/\(\s*(?:or|and)\s*\(/, "((")
end
f2c78d4a Joseph Magen
where(conditions)
54358a76 Greg Sutcliffe
}

e5d3f34e Ohad Levy
class Jail < Safemode::Jail
218bd6e0 Justin Sherrill
allow :name, :diskLayout, :puppetmaster, :operatingsystem, :architecture,
6807d423 Stephen Benjamin
:environment, :ptable, :url_for_boot, :params, :puppetproxy, :param_true?,
1cb5ba7e Stephen Benjamin
:param_false?, :puppet_ca_server, :indent, :os, :arch, :domain, :subnet,
c47be8a8 Tommy McNeely
:realm, :root_pass
e5d3f34e Ohad Levy
end

9fd7478e Paul Kelly
#TODO: add a method that returns the valid os for a hostgroup
086ec942 Ohad Levy
9fd7478e Paul Kelly
def hostgroup
self
end
e5d3f34e Ohad Levy
def diskLayout
de9e7ada Marek Hulan
ptable.layout.tr("\r", '')
e5d3f34e Ohad Levy
end

111cde57 Joseph Magen
def all_config_groups
(config_groups + parent_config_groups).uniq
end

def parent_config_groups
return [] unless parent
groups = []
ancestors.each do |hostgroup|
groups += hostgroup.config_groups
end
96144a47 Daniel Lobato
groups.uniq
2aabb456 Ohad Levy
end

111cde57 Joseph Magen
# the environment used by #clases nees to be self.environment and not self.parent.environment
def parent_classes
return [] unless parent
parent.classes(self.environment)
4d4b84f6 Ohad Levy
end

5f029ed6 Daniel Lobato
def inherited_lookup_value(key)
0f9df583 Amos Benari
ancestors.reverse.each do |hg|
4f7a4d0b David Davis
if (v = LookupValue.where(:lookup_key_id => key.id, :id => hg.lookup_values).first)
0f9df583 Amos Benari
return v.value, hg.to_label
end
end if key.path_elements.flatten.include?("hostgroup") && Setting["host_group_matchers_inheritance"]
96144a47 Daniel Lobato
[key.default_value, _("Default value")]
0f9df583 Amos Benari
end

4d4b84f6 Ohad Levy
# returns self and parent parameters as a hash
5f029ed6 Daniel Lobato
def parameters(include_source = false)
4d4b84f6 Ohad Levy
hash = {}
ids = ancestor_ids
ids << id unless new_record? or self.frozen?
2aabb456 Ohad Levy
# need to pull out the hostgroups to ensure they are sorted first,
# otherwise we might be overwriting the hash in the wrong order.
4cdc0455 Daniel Lobato
groups = ids.size == 1 ? [self] : Hostgroup.sort_by_ancestry(Hostgroup.includes(:group_parameters).find(ids))
2aabb456 Ohad Levy
groups.each do |hg|
04c6b019 Tiffany
hg.group_parameters.each {|p| hash[p.name] = include_source ? {:value => p.value, :source => N_('hostgroup').to_sym, :safe_value => p.safe_value, :source_name => hg.title} : p.value }
96144a47 Daniel Lobato
end
04c6b019 Tiffany
4d4b84f6 Ohad Levy
hash
end

e14b6968 Daniel Lobato
def global_parameters
4cdc0455 Daniel Lobato
Hostgroup.sort_by_ancestry(Hostgroup.includes(:group_parameters).find(ancestor_ids + id)).map(&:group_parameters).uniq
e14b6968 Daniel Lobato
end

def inherited_params(include_source = false)
# same method in Host, similar method below
hp = {}
# read common parameters
CommonParameter.all.each {|p| hp.update Hash[p.name => include_source ? {:value => p.value, :source => N_('common').to_sym} : p.value] }
# read organization and location parameters
hp.update organization.parameters(include_source) if SETTINGS[:organizations_enabled] && organization
hp.update location.parameters(include_source) if SETTINGS[:locations_enabled] && location
# read domain parameters
domain.domain_parameters.each {|p| hp.update Hash[p.name => include_source ? {:value => p.value, :source => N_('domain').to_sym} : p.value] } unless domain.nil?
# read OS parameters
operatingsystem.os_parameters.each {|p| hp.update Hash[p.name => include_source ? {:value => p.value, :source => N_('os').to_sym} : p.value] } unless operatingsystem.nil?
# read group parameters only if a host belongs to a group
hp.update hostgroup.parameters(include_source) if hostgroup
hp
end

e5d3f34e Ohad Levy
def params
parameters = {}
# read common parameters
1fa008a4 Joseph Magen
CommonParameter.scoped.each {|p| parameters.update Hash[p.name => p.value] }
e5d3f34e Ohad Levy
# read OS parameters
1fa008a4 Joseph Magen
operatingsystem.os_parameters.each {|p| parameters.update Hash[p.name => p.value] } if operatingsystem
e5d3f34e Ohad Levy
# read group parameters only if a host belongs to a group
1fa008a4 Joseph Magen
parameters.update self.parameters if hostgroup
e5d3f34e Ohad Levy
parameters
end

f00eaf07 Ohad Levy
# no need to store anything in the db if the password is our default
def root_pass
db176297 Dominic Cleal
return read_attribute(:root_pass) if read_attribute(:root_pass).present?
npw = nested_root_pw
return npw if npw.present?
Setting[:root_pass]
f00eaf07 Ohad Levy
end

5eb88cf5 Ori Rabin
# Clone the hostgroup
def clone(name = "")
new = self.dup
new.name = name
9dd00534 Tom Caspy
new.title = name
5eb88cf5 Ori Rabin
new.puppetclasses = puppetclasses
new.locations = locations
new.organizations = organizations
7691ab4a Tom Caspy
new.config_groups = config_groups
9dd00534 Tom Caspy
new.set_lookup_value_matcher
7691ab4a Tom Caspy
5eb88cf5 Ori Rabin
# Clone any parameters as well
self.group_parameters.each{|param| new.group_parameters << param.clone}
7691ab4a Tom Caspy
self.lookup_values.each do |lookup_value|
new_lookup_value = lookup_value.dup
9dd00534 Tom Caspy
new_lookup_value.match = new.lookup_value_matcher
7691ab4a Tom Caspy
new.lookup_values << new_lookup_value
end
5eb88cf5 Ori Rabin
new
end

83683ed0 Tomer Brisker
def update_ancestry_puppetclasses
unscoped_find(ancestry_was.to_s.split('/').last.to_i).update_puppetclasses_total_hosts if ancestry_was.present?
unscoped_find(ancestry.to_s.split('/').last.to_i).update_puppetclasses_total_hosts if ancestry.present?
end

d5707b63 Ohad Levy
private

7acc469b Amos Benari
def lookup_value_match
"hostgroup=#{to_label}"
end

f00eaf07 Ohad Levy
def nested_root_pw
66a9a696 Ohad Levy
Hostgroup.sort_by_ancestry(ancestors).reverse.each do |a|
f00eaf07 Ohad Levy
return a.root_pass unless a.root_pass.blank?
2aabb456 Ohad Levy
end if ancestry.present?
f00eaf07 Ohad Levy
nil
end

50541c05 Ohad Levy
def remove_duplicated_nested_class
self.puppetclasses -= ancestors.map(&:puppetclasses).flatten
end

1fa008a4 Joseph Magen
# overwrite method in taxonomix, since hostgroup has ancestry
def used_taxonomy_ids(type)
return [] if new_record? && parent_id.blank?
0e1c5371 Tomer Brisker
Host::Base.where(:hostgroup_id => self.path_ids).uniq.pluck(type).compact
1fa008a4 Joseph Magen
end
086ec942 Ohad Levy
end