Module: Staypuft::Concerns::HostgroupExtensions

Extended by:
ActiveSupport::Concern
Defined in:
app/models/staypuft/concerns/hostgroup_extensions.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_puppetclasses_from_resource(resource) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'app/models/staypuft/concerns/hostgroup_extensions.rb', line 15

def add_puppetclasses_from_resource(resource)
  if resource.respond_to?(:puppetclasses)
    resource.puppetclasses.each do |puppetclass|
      unless puppetclasses.include?(puppetclass)
        puppetclasses << puppetclass
      end
    end
  end
end

#current_param_value(key) ⇒ Object



25
26
27
28
29
30
31
32
# File 'app/models/staypuft/concerns/hostgroup_extensions.rb', line 25

def current_param_value(key)
  lookup_value = LookupValue.where(:lookup_key_id => key.id, :id => lookup_values).first
  if lookup_value
    [lookup_value.value, to_label]
  else
    inherited_lookup_value(key)
  end
end

#current_param_value_str(key) ⇒ Object



34
35
36
37
# File 'app/models/staypuft/concerns/hostgroup_extensions.rb', line 34

def current_param_value_str(key)
  lookup_value, _ = current_param_value(key)
  return key.value_before_type_cast(lookup_value)
end

#free_hostsObject



51
52
53
# File 'app/models/staypuft/concerns/hostgroup_extensions.rb', line 51

def free_hosts
  Host::Base.where('hostgroup_id IS NULL')
end

#openstack_hosts(errors = nil) ⇒ Object

Returns all hosts associated with this hostgroup with the openstack deployment environment set. These can be filtered based by setting errors=true or errors=false to return only the hosts with no errors or with errors respectively.



59
60
61
62
63
64
65
66
# File 'app/models/staypuft/concerns/hostgroup_extensions.rb', line 59

def openstack_hosts(errors=nil)
  oshosts = hosts.select { |h| h.open_stack_environment_set? }

  unless errors.nil?
    oshosts.select! { |h| (!errors ^ h.error?) }
  end
  oshosts
end

#set_param_value_if_changed(puppetclass, key, value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/staypuft/concerns/hostgroup_extensions.rb', line 39

def set_param_value_if_changed(puppetclass, key, value)
  lookup_key         = puppetclass.class_params.where(:key => key).first
  lookup_value_value = current_param_value(lookup_key)[0]
  current_value      = lookup_key.value_before_type_cast(lookup_value_value).to_s.chomp
  if current_value != value
    lookup       = LookupValue.where(:match         => hostgroup.send(:lookup_value_match),
                                     :lookup_key_id => lookup_key.id).first_or_initialize
    lookup.value = value
    lookup.save!
  end
end