Class: Hostgroup

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Authorization, HostCommon, Taxonomix
Defined in:
app/models/hostgroup.rb

Defined Under Namespace

Classes: Jail

Instance Method Summary collapse

Methods included from HostCommon

included

Methods included from Taxonomix

included

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, #enforce_permissions, included, #permission_failed?

Instance Method Details

#all_puppetclassesObject

TODO: add a method that returns the valid os for a hostgroup


64
65
66
# File 'app/models/hostgroup.rb', line 64

def all_puppetclasses
  classes
end

#as_json(options = {}) ⇒ Object


77
78
79
# File 'app/models/hostgroup.rb', line 77

def as_json(options={})
  super({:only => [:name, :subnet_id, :operatingsystem_id, :domain_id, :environment_id, :id, :ancestry], :methods => [:label, :parameters, :puppetclass_ids]})
end

#classesObject


89
90
91
# File 'app/models/hostgroup.rb', line 89

def classes
  Puppetclass.joins(:hostgroups).where(:hostgroups => {:id => path_ids})
end

#diskLayoutObject


85
86
87
# File 'app/models/hostgroup.rb', line 85

def diskLayout
  ptable.layout.gsub("\r","")
end

#get_labelObject


136
137
138
139
# File 'app/models/hostgroup.rb', line 136

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

#hostgroupObject


81
82
83
# File 'app/models/hostgroup.rb', line 81

def hostgroup
  self
end

#inherited_lookup_value(key) ⇒ Object


97
98
99
100
101
102
103
104
# File 'app/models/hostgroup.rb', line 97

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

#parameters(include_source = false) ⇒ Object

returns self and parent parameters as a hash


107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/hostgroup.rb', line 107

def parameters include_source = false
  hash = {}
  ids = ancestor_ids
  ids << id unless new_record? or self.frozen?
  # need to pull out the hostgroups to ensure they are sorted first,
  # otherwise we might be overwriting the hash in the wrong order.
  groups = ids.size == 1 ? [self] : Hostgroup.sort_by_ancestry(Hostgroup.find(ids, :include => :group_parameters))
  groups.each do |hg|
    hg.group_parameters.each {|p| hash[p.name] = include_source ? {:value => p.value, :source => :hostgroup} : p.value }
  end
  hash
end

#paramsObject


120
121
122
123
124
125
126
127
128
129
# File 'app/models/hostgroup.rb', line 120

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
  parameters.update self.parameters unless hostgroup.nil?
  parameters
end

#puppetclass_idsObject


93
94
95
# File 'app/models/hostgroup.rb', line 93

def puppetclass_ids
  classes.reorder('').pluck('puppetclasses.id')
end

#root_passObject

no need to store anything in the db if the password is our default


132
133
134
# File 'app/models/hostgroup.rb', line 132

def root_pass
  read_attribute(:root_pass) || nested_root_pw || Setting[:root_pass]
end

#to_labelObject


68
69
70
71
# File 'app/models/hostgroup.rb', line 68

def to_label
  return label if label
  get_label
end

#to_paramObject


73
74
75
# File 'app/models/hostgroup.rb', line 73

def to_param
  "#{id}-#{to_label.parameterize}"
end