Project

General

Profile

Download (6.31 KB) Statistics
| Branch: | Tag: | Revision:
086ec942 Ohad Levy
class Hostgroup < ActiveRecord::Base
4d4b84f6 Ohad Levy
has_ancestry :orphan_strategy => :rootify
9fd7478e Paul Kelly
include Authorization
611f5bff Amos Benari
include Taxonomix
e5d3f34e Ohad Levy
include HostCommon
e2d3654e Joseph Mitchell Magen
ff8cc704 Joseph Mitchell Magen
before_destroy EnsureNotUsedBy.new(:hosts)
644f7f22 Ohad Levy
has_many :hostgroup_classes, :dependent => :destroy
has_many :puppetclasses, :through => :hostgroup_classes
28d1dd18 Daniel Lobato
has_many :user_hostgroups, :dependent => :destroy
has_many :users, :through => :user_hostgroups
f2c78d4a Joseph Magen
validates :name, :uniqueness => {:scope => :ancestry, :case_sensitive => false },
:format => { :with => /\A(\S+\s?)+\Z/, :message => N_("can't be blank or contain trailing white spaces.")}
aa1796f3 Paul Kelly
has_many :group_parameters, :dependent => :destroy, :foreign_key => :reference_id
bd46fa41 Ohad Levy
accepts_nested_attributes_for :group_parameters, :reject_if => lambda { |a| a[:value].blank? }, :allow_destroy => true
3f77babd Joseph Mitchell Magen
has_many_hosts
7d993b41 Joseph Mitchell Magen
has_many :template_combinations, :dependent => :destroy
c764d9ec Ohad Levy
has_many :config_templates, :through => :template_combinations
50541c05 Ohad Levy
before_save :remove_duplicated_nested_class
793072c5 Joseph Mitchell Magen
before_save :set_label, :on => [:create, :update, :destroy]
after_save :set_other_labels, :on => [:update, :destroy]
36f93e4d Ohad Levy
e5d3f34e Ohad Levy
alias_attribute :os, :operatingsystem
9c864cb6 Ohad Levy
audited :except => [:label], :allow_mass_assignment => true
fe4629de rbirnie
has_many :trends, :as => :trendable, :class_name => "ForemanTrend"
3b656b8d Joseph Mitchell Magen
# attribute used by *_names and *_name methods. default is :name
attr_name :label
086ec942 Ohad Levy
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
order("hostgroups.label")
end
}
611f5bff Amos Benari
b80fa6f1 Ohad Levy
scoped_search :on => :name, :complete_value => :true
793072c5 Joseph Mitchell Magen
scoped_search :on => :label, :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
if SETTINGS[:unattended]
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 => :medium, :on => :name, :complete_value => :true, :rename => "medium"
scoped_search :in => :config_templates, :on => :name, :complete_value => :true, :rename => "template"
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,
b4c2016a Ohad Levy
:environment, :ptable, :url_for_boot, :params, :puppetproxy
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 all_puppetclasses
4d4b84f6 Ohad Levy
classes
end

def to_label
793072c5 Joseph Mitchell Magen
return label if label
get_label
9fd7478e Paul Kelly
end
b09b4515 Ohad Levy
01984fb7 Amos Benari
def to_param
"#{id}-#{to_label.parameterize}"
end

9fd7478e Paul Kelly
def hostgroup
self
end
e5d3f34e Ohad Levy
def diskLayout
d56112fb Joseph Mitchell Magen
ptable.layout.gsub("\r","")
e5d3f34e Ohad Levy
end

4d4b84f6 Ohad Levy
def classes
2aabb456 Ohad Levy
Puppetclass.joins(:hostgroups).where(:hostgroups => {:id => path_ids})
end

def puppetclass_ids
65f764d6 Dominic Cleal
classes.reorder('').pluck('puppetclasses.id')
4d4b84f6 Ohad Levy
end

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

4d4b84f6 Ohad Levy
# returns self and parent parameters as a hash
298756ca Amos Benari
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.
f2c78d4a Joseph Magen
groups = ids.size == 1 ? [self] : Hostgroup.includes(:group_parameters).sort_by_ancestry(Hostgroup.find(ids))
2aabb456 Ohad Levy
groups.each do |hg|
298756ca Amos Benari
hg.group_parameters.each {|p| hash[p.name] = include_source ? {:value => p.value, :source => :hostgroup} : p.value }
4d4b84f6 Ohad Levy
end
hash
end

e5d3f34e Ohad Levy
def params
parameters = {}
# read common parameters
CommonParameter.all.each {|p| parameters.update Hash[p.name => p.value] }
# read OS parameters
operatingsystem.os_parameters.each {|p| parameters.update Hash[p.name => p.value] } unless operatingsystem.nil?
# read group parameters only if a host belongs to a group
4d4b84f6 Ohad Levy
parameters.update self.parameters unless hostgroup.nil?
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
d56112fb Joseph Mitchell Magen
read_attribute(:root_pass) || nested_root_pw || Setting[:root_pass]
f00eaf07 Ohad Levy
end

793072c5 Joseph Mitchell Magen
def get_label
return name if ancestry.empty?
ancestors.map{|a| a.name + "/"}.join + name
end

c6e02bd3 Joseph Magen
def inherited_compute_profile_id
read_attribute(:compute_profile_id) || nested_compute_profile_id
end

def compute_profile
ComputeProfile.find_by_id(inherited_compute_profile_id)
end

d5707b63 Ohad Levy
private

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

793072c5 Joseph Mitchell Magen
def set_label
530cd21e Joseph Mitchell Magen
self.label = get_label if (name_changed? || ancestry_changed? || label.blank?)
793072c5 Joseph Mitchell Magen
end

def set_other_labels
if name_changed? || ancestry_changed?
Hostgroup.where("ancestry IS NOT NULL").each do |hostgroup|
if hostgroup.path_ids.include?(self.id)
hostgroup.update_attributes(:label => hostgroup.get_label)
end
end
end
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

c6e02bd3 Joseph Magen
def nested_compute_profile_id
Hostgroup.sort_by_ancestry(ancestors.where('compute_profile_id > 0')).last.try(:compute_profile_id) if ancestry.present?
end

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

086ec942 Ohad Levy
end