foreman/config/initializers/foreman.rb @ 8a65dff7
9fd7478e | Paul Kelly | require 'access_permissions'
|
|
e5224d76 | Ohad Levy | require 'puppet'
|
|
b1a44209 | Ohad Levy | require 'puppet/rails'
|
|
8a65dff7 | Ohad Levy | require 'core_extensions'
|
|
e5224d76 | Ohad Levy | ||
# import settings file
|
|||
9c3ffb6b | Ohad Levy | SETTINGS= YAML.load_file("#{RAILS_ROOT}/config/settings.yaml")
|
|
2dc34ac5 | Ohad Levy | ||
ddee8d43 | Ohad Levy | SETTINGS[:version] = "0.3"
|
|
e5224d76 | Ohad Levy | ||
4dd720f6 | Paul Kelly | SETTINGS[:unattended] = SETTINGS[:unattended].nil? || SETTINGS[:unattended]
|
|
9c3ffb6b | Ohad Levy | Puppet[:config] = SETTINGS[:puppetconfdir] || "/etc/puppet/puppet.conf"
|
|
e5224d76 | Ohad Levy | Puppet.parse_config
|
|
88693384 | Lucas Tolchinsky | $puppet = Puppet.settings.instance_variable_get(:@values) if Rails.env == "test"
|
|
7a4ec5cf | Paul Kelly | SETTINGS[:login] ||= SETTINGS[:ldap]
|
|
ba5b7e74 | Ohad Levy | begin
|
|
require 'virt'
|
|||
SETTINGS[:libvirt] = true
|
|||
rescue LoadError
|
|||
RAILS_DEFAULT_LOGGER.debug "Libvirt binding are missing - hypervisor management is disabled"
|
|||
SETTINGS[:libvirt] = false
|
|||
end
|
|||
29eebabc | Ohad Levy | # We load the default settings if they are not already present
|
|
Foreman::DefaultSettings::Loader.load(false)
|
|||
9fd7478e | Paul Kelly | # We load the default settings for the roles if they are not already present
|
|
Foreman::DefaultData::Loader.load(false)
|
|||
e5224d76 | Ohad Levy | # Add an empty method to nil. Now no need for if x and x.empty?. Just x.empty?
|
|
class NilClass
|
|||
def empty?
|
|||
true
|
|||
end
|
|||
end
|
|||
class ActiveRecord::Base
|
|||
9fd7478e | Paul Kelly | def <=>(other)
|
|
self.name <=> other.name
|
|||
end
|
|||
90b70658 | Ohad Levy | ||
def update_single_attribute(attribute, value)
|
|||
connection.update(
|
|||
"UPDATE #{self.class.table_name} " +
|
|||
"SET #{attribute.to_s} = #{value} " +
|
|||
"WHERE #{self.class.primary_key} = #{id}",
|
|||
"#{self.class.name} Attribute Update"
|
|||
)
|
|||
end
|
|||
88693384 | Lucas Tolchinsky | class Ensure_not_used_by
|
|
def initialize(*attribute)
|
|||
@klasses = attribute
|
|||
@logger = RAILS_DEFAULT_LOGGER
|
|||
e5224d76 | Ohad Levy | end
|
|
34df1be2 | Ohad Levy | ||
88693384 | Lucas Tolchinsky | def before_destroy(record)
|
|
for klass in @klasses
|
|||
for what in eval "record.#{klass.to_s}"
|
|||
record.errors.add_to_base(record.to_label + " is used by " + what.to_s)
|
|||
end
|
|||
end
|
|||
unless record.errors.empty?
|
|||
@logger.error "You may not destroy #{record.to_label} as it is in use!"
|
|||
false
|
|||
else
|
|||
true
|
|||
end
|
|||
end
|
|||
end
|
|||
9c0e127b | Paul Kelly | ||
def id_and_type
|
|||
"#{id}-#{self.class.table_name.humanize}"
|
|||
end
|
|||
81e2d3f3 | Ohad Levy | alias_attribute :to_label, :name
|
|
alias_attribute :to_s, :to_label
|
|||
def self.per_page
|
|||
20
|
|||
end
|
|||
c4b56e43 | Ohad Levy | def self.unconfigured?
|
|
a6273e1f | Ohad Levy | first.nil?
|
|
c4b56e43 | Ohad Levy | end
|
|
9c0e127b | Paul Kelly | ||
e5224d76 | Ohad Levy | end
|
|
90b70658 | Ohad Levy | ||
722e891f | Ohad Levy | module ExemptedFromLogging
|
|
def process(request, *args)
|
|||
logger.silence { super }
|
|||
end
|
|||
end
|
|||
300c8b44 | Ohad Levy | ||
class String
|
|||
def to_gb
|
|||
begin
|
|||
value,unit=self.match(/(\d+|.+) ([KMG]B)$/i)[1..2]
|
|||
case unit.to_sym
|
|||
when nil, :B, :byte then (value.to_f / 1000_000_000)
|
|||
when :GB, :G, :gigabyte then value.to_f
|
|||
when :MB, :M, :megabyte then (value.to_f / 1000)
|
|||
when :KB, :K, :kilobyte, :kB then (value.to_f / 1000_000)
|
|||
else raise "Unknown unit: #{unit.inspect}!"
|
|||
end
|
|||
rescue
|
|||
raise "Unknown string"
|
|||
end
|
|||
end
|
|||
end
|
|||
19bd108a | Paul Kelly | module ActionView::Helpers::ActiveRecordHelper
|
|
def error_messages_for_with_customisation(*params)
|
|||
if flash[:error_customisation]
|
|||
if params[-1].is_a? Hash
|
|||
params[-1].update flash[:error_customisation]
|
|||
else
|
|||
params << flash[:error_customisation]
|
|||
end
|
|||
end
|
|||
error_messages_for_without_customisation(*params)
|
|||
end
|
|||
alias_method_chain :error_messages_for, :customisation
|
|||
end
|