Class: ConfigTemplate

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

Defined Under Namespace

Classes: Jail

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Taxonomix

included

Methods included from Authorization

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

Class Method Details

.build_pxe_default(renderer) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/config_template.rb', line 96

def self.build_pxe_default(renderer)
  if (proxies = SmartProxy.tftp_proxies).empty?
    error_msg = _("No TFTP proxies defined, can't continue")
  end

  if (default_template = ConfigTemplate.find_by_name("PXE Default File")).nil?
    error_msg = _("Could not find a Configuration Template with the name \"PXE Default File\", please create one.")
  end

  if error_msg.empty?
    begin
      @profiles = pxe_default_combos
      menu = renderer.render_safe(default_template.template, [:default_template_url], {:profiles => @profiles})
    rescue => e
      error_msg = _("failed to process template: %s" % e)
    end
  end

  return [422, error_msg] unless error_msg.empty?

  error_msgs = []
  proxies.each do |proxy|
    begin
      tftp = ProxyAPI::TFTP.new(:url => proxy.url)
      tftp.create_default({:menu => menu})

      @profiles.each do |combo|
        combo[:hostgroup].operatingsystem.pxe_files(combo[:hostgroup].medium, combo[:hostgroup].architecture).each do |bootfile_info|
          for prefix, path in bootfile_info do
            tftp.fetch_boot_file(:prefix => prefix.to_s, :path => path)
          end
        end
      end
    rescue => exc
      error_msgs << "#{proxy}: #{exc.message}"
    end
  end

  unless error_msgs.empty?
    msg = _("There was an error creating the PXE Default file: %s") % error_msgs.join(",")
    return [500, msg]
  end

  return [200, _("PXE Default file has been deployed to all Smart Proxies")]
end

.find_template(opts = {}) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/config_template.rb', line 51

def self.find_template opts = {}
  raise ::Foreman::Exception.new(N_("Must provide template kind")) unless opts[:kind]
  raise ::Foreman::Exception.new(N_("Must provide an operating systems")) unless opts[:operatingsystem_id]

  # first filter valid templates to our OS and requested template kind.
  templates = ConfigTemplate.joins(:operatingsystems, :template_kind).where('operatingsystems.id' => opts[:operatingsystem_id], 'template_kinds.name' => opts[:kind])


  # once a template has been matched, we no longer look for others.

  if opts[:hostgroup_id] and opts[:environment_id]
    # try to find a full match to our host group and environment
    template = templates.joins(:hostgroups, :environments).where("hostgroups.id" => opts[:hostgroup_id], "environments.id" => opts[:environment_id]).first
  end

  if opts[:hostgroup_id]
    # try to find a match with our hostgroup only
    template ||= templates.joins(:hostgroups).where("hostgroups.id" => opts[:hostgroup_id]).first
  end

  if opts[:environment_id]
    # search for a template based only on our environment
    template ||= templates.joins(:environments).where("environments.id" => opts[:environment_id]).first
  end

  # fall back to the os default template
  template ||= templates.joins(:os_default_templates).where("os_default_templates.operatingsystem_id" => opts[:operatingsystem_id]).first
  template.is_a?(ConfigTemplate) ? template : nil
end

.template_ids_for(hosts) ⇒ Object

TODO: review if we can improve SQL


45
46
47
48
49
# File 'app/models/config_template.rb', line 45

def self.template_ids_for(hosts)
  hosts.with_os.map do |host|
    host.configTemplate.try(:id)
  end.uniq.compact
end

Instance Method Details

#as_json(options = {}) ⇒ Object


91
92
93
94
# File 'app/models/config_template.rb', line 91

def as_json(options={})
  options ||= {}
  super({:only => [:name, :template, :id, :snippet],:include => [:template_kind]}.merge(options))
end

#enforce_permissions(operation) ⇒ Object


81
82
83
84
85
86
87
88
89
# File 'app/models/config_template.rb', line 81

def enforce_permissions operation
  # We get called again with the operation being set to create
  return true if operation == "edit" and new_record?

  return true if User.current and User.current.allowed_to?("#{operation}_templates".to_sym)

  errors.add :base, (_("You do not have permission to %s this template") % operation)
  false
end

#to_paramObject


40
41
42
# File 'app/models/config_template.rb', line 40

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