Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #3860 - automated discovery provisioning
- Loading branch information
1 parent
b221e9c
commit d3e7c8b
Showing
34 changed files
with
906 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module Api | ||
module V2 | ||
class DiscoveryRulesController < ::Api::V2::BaseController | ||
|
||
before_filter :find_resource, :except => %w{index create facts} | ||
|
||
resource_description do | ||
resource_id 'discovery_rules' | ||
api_version 'v2' | ||
api_base_url "/api/v2" | ||
end | ||
|
||
api :GET, "/discovery_rules/", N_("List all discovery rules") | ||
param :search, String, :desc => N_("filter results") | ||
param :order, String, :desc => N_("sort results") | ||
param :page, String, :desc => N_("paginate results") | ||
param :per_page, String, :desc => N_("number of entries per request") | ||
|
||
def index | ||
@discovery_rules = resource_scope.search_for(*search_options).paginate(paginate_options) | ||
end | ||
|
||
api :GET, "/discovery_rules/:id/", N_("Show a discovery rule") | ||
param :id, :identifier_dottable, :required => true | ||
|
||
def show | ||
end | ||
|
||
def_param_group :discovery_rule do | ||
param :discovery_rule, Hash, :action_aware => true do | ||
param :name, String, :required => true | ||
param :search, String, :required => true | ||
param :hostgroup_id, Integer, :required => true | ||
param :hostname, String, :required => true | ||
param :max_count, Integer | ||
param :priority, Integer | ||
param :enabled, :bool | ||
end | ||
end | ||
|
||
api :POST, "/discovery_rules/", N_("Create a discovery rule") | ||
param_group :discovery_rule, :as => :create | ||
|
||
def create | ||
@discovery_rule = DiscoveryRule.new(params[:discovery_rule]) | ||
process_response @discovery_rule.save | ||
end | ||
|
||
api :PUT, "/discovery_rules/:id/", N_("Update a rule") | ||
param :id, :identifier, :required => true | ||
param :discovery_rule, Hash, :action_aware => true do | ||
param :name, String, :required => true | ||
param :search, String, :required => true | ||
param :hostgroup_id, Integer, :required => true | ||
param :hostname, String, :required => true | ||
param :max_count, Integer | ||
param :priority, Integer | ||
param :enabled, :bool | ||
end | ||
|
||
def update | ||
process_response @discovery_rule.update_attributes(params[:discovery_rule]) | ||
end | ||
|
||
api :DELETE, "/discovery_rules/:id/", N_("Delete a rule") | ||
param :id, :identifier, :required => true | ||
|
||
def destroy | ||
process_response @discovery_rule.destroy | ||
end | ||
|
||
private | ||
|
||
def resource_class | ||
DiscoveryRule | ||
end | ||
end | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
app/controllers/concerns/foreman/controller/discovered_extensions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Foreman::Controller::DiscoveredExtensions | ||
extend ActiveSupport::Concern | ||
|
||
# return auto provision rule or false when not present | ||
def find_discovery_rule host | ||
Rails.logger.debug "Finding auto discovery rule for host #{host.name} (#{host.id})" | ||
# for each discovery rule ordered by priority | ||
DiscoveryRule.where(:enabled => true).order(:priority).each do |rule| | ||
max = rule.max_count | ||
usage = rule.hosts.size | ||
Rails.logger.debug "Found rule #{rule.name} (#{rule.id}) [#{usage}/#{max}]" | ||
# if the rule has free slots | ||
if max == 0 || usage < max | ||
# try to match the search | ||
begin | ||
if Host::Discovered.where(:id => host.id).search_for(rule.search).size > 0 | ||
Rails.logger.info "Match found for host #{host.name} (#{host.id}) rule #{rule.name} (#{rule.id})" | ||
return rule | ||
end | ||
rescue ScopedSearch::QueryNotSupported => e | ||
Rails.logger.warn "Invalid query for rule #{rule.name} (#{rule.id}): #{e.message}" | ||
end | ||
else | ||
Rails.logger.info "Skipping drained rule #{rule.name} (#{rule.id}) with max set to #{rule.max_count}" | ||
end | ||
end | ||
return false | ||
end | ||
|
||
# trigger the provisioning | ||
def perform_auto_provision host, rule | ||
host.type = 'Host::Managed' | ||
host.managed = true | ||
host.build = true | ||
host.name = host.render_template(rule.hostname) unless rule.hostname.empty? | ||
host.hostgroup_id = rule.hostgroup_id | ||
host.comment = "Auto-discovered and provisioned via rule '#{rule.name}'" | ||
host.discovery_rule = rule | ||
host.save # save! does not work here | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
class DiscoveryRulesController < ApplicationController | ||
# Two-pane searching must be disabled for rules otherwise query completion will not work | ||
#include Foreman::Controller::AutoCompleteSearch | ||
|
||
before_filter :find_resource, :only => [:edit, :update, :destroy, :enable, :disable, :auto_provision] | ||
|
||
def index | ||
base = resource_base.search_for(params[:search], :order => (params[:order] || 'priority ASC')) | ||
@discovery_rules = base.paginate(:page => params[:page]).includes(:hostgroup) | ||
end | ||
|
||
def new | ||
@discovery_rule = DiscoveryRule.new | ||
end | ||
|
||
def create | ||
@discovery_rule = DiscoveryRule.new(params[:discovery_rule]) | ||
if @discovery_rule.save | ||
process_success | ||
else | ||
process_error | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
if @discovery_rule.update_attributes(params[:discovery_rule]) | ||
process_success | ||
else | ||
process_error | ||
end | ||
end | ||
|
||
def destroy | ||
if @discovery_rule.destroy | ||
process_success | ||
else | ||
process_error | ||
end | ||
end | ||
|
||
def enable | ||
set_enabled true | ||
end | ||
|
||
def disable | ||
set_enabled false | ||
end | ||
|
||
private | ||
|
||
def action_permission | ||
case params[:action] | ||
when 'enable' | ||
:enable | ||
when 'disable' | ||
:disable | ||
else | ||
super | ||
end | ||
end | ||
|
||
def set_enabled(enabled) | ||
@discovery_rule.enabled = enabled | ||
if @discovery_rule.save | ||
process_success :success_msg => (enabled ? _('Rule enabled') : _('Rule disabled')), :success_redirect => :back | ||
else | ||
process_error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.