-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: OutputTemplates #813
Open
avitova
wants to merge
1
commit into
theforeman:master
Choose a base branch
from
avitova:OutputTemplates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+882
−65
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
app/assets/javascripts/foreman_remote_execution/output_templates.js
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,10 @@ | ||
function show_import_output_template_modal() { | ||
var modal_window = $('#importOutputTemplateModal'); | ||
modal_window.modal({'show': true}); | ||
modal_window.find('a[rel="popover-modal"]').popover(); | ||
} | ||
|
||
function close_import_output_template_modal() { | ||
var modal_window = $('#importOutputTemplateModal'); | ||
modal_window.modal('hide'); | ||
} |
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,61 @@ | ||
module Api | ||
module V2 | ||
class OutputTemplatesController < ::Api::V2::BaseController | ||
include ::Api::Version2 | ||
include ::Foreman::Renderer | ||
include ::Foreman::Controller::ProvisioningTemplates | ||
include ::Foreman::Controller::Parameters::OutputTemplate | ||
|
||
api :GET, '/output_templates/', N_('List output templates') | ||
# location and Organization | ||
param_group :taxonomy_scope, ::Api::V2::BaseController | ||
# search and pagination allows to display and filter the index page of templates | ||
param_group :search_and_pagination, ::Api::V2::BaseController | ||
def index | ||
# do not show saved runtime templates | ||
@output_templates = resource_scope_for_index.filter { |template| !template.snippet } | ||
end | ||
|
||
def_param_group :output_template do | ||
param :output_template, Hash, :required => true, :action_aware => true do | ||
param :name, String, :required => true, :desc => N_('Template name') | ||
param :description, String | ||
param :template, String, :required => true | ||
param :output, String | ||
param :snippet, :bool, :allow_nil => true | ||
param :locked, :bool, :desc => N_('Whether or not the template is locked for editing') | ||
param :effective_user_attributes, Hash, :desc => N_('Effective user options') do | ||
param :value, String, :desc => N_('What user should be used to run the script (using sudo-like mechanisms)'), :allowed_nil => true | ||
param :overridable, :bool, :desc => N_('Whether it should be allowed to override the effective user from the invocation form.') | ||
param :current_user, :bool, :desc => N_('Whether the current user login should be used as the effective user') | ||
end | ||
param_group :taxonomies, ::Api::V2::BaseController | ||
end | ||
end | ||
|
||
api :POST, '/output_templates/', N_('Create an output template') | ||
param_group :output_template, :as => :create | ||
def create | ||
@output_template = OutputTemplate.new(output_template_params) | ||
process_response @output_template.save | ||
end | ||
|
||
api :DELETE, '/output_templates/:id', N_('Delete an output template') | ||
param :id, :identifier, :required => true | ||
def destroy | ||
process_response @output_template.destroy | ||
end | ||
|
||
api :POST, '/output_templates/import', N_('Import an output template from ERB') | ||
param :template, String, :required => true, :desc => N_('Template ERB') | ||
param :overwrite, :bool, :required => false, :desc => N_('Overwrite template if it already exists') | ||
def import | ||
options = params[:overwrite] ? { :update => true } : { :build_new => true } | ||
|
||
@output_template = OutputTemplate.import_raw(params[:template], options) | ||
@output_template ||= OutputTemplate.new | ||
process_response @output_template.save | ||
end | ||
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
29 changes: 29 additions & 0 deletions
29
app/controllers/concerns/foreman/controller/parameters/output_template.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,29 @@ | ||
module Foreman::Controller::Parameters::OutputTemplate | ||
extend ActiveSupport::Concern | ||
include Foreman::Controller::Parameters::Taxonomix | ||
include ::Foreman::Controller::Parameters::Template | ||
include Foreman::Controller::Parameters::TemplateInput | ||
|
||
class_methods do | ||
def output_template_effective_user_filter | ||
Foreman::ParameterFilter.new(::OutputTemplateEffectiveUser).tap do |filter| | ||
filter.permit_by_context(:value, :current_user, :overridable, | ||
:nested => true) | ||
end | ||
end | ||
|
||
def output_template_params_filter | ||
Foreman::ParameterFilter.new(::TemplateInput).tap do |filter| | ||
filter.permit :description_format, | ||
:effective_user_attributes => [output_template_effective_user_filter], | ||
:template_inputs_attributes => [template_input_params_filter] | ||
add_template_params_filter(filter) | ||
add_taxonomix_params_filter(filter) | ||
end | ||
end | ||
end | ||
|
||
def output_template_params | ||
self.class.output_template_params_filter.filter_params(params, parameter_filter_context, :output_template) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class OutputTemplatesController < ::TemplatesController | ||
include ::Foreman::Controller::Parameters::OutputTemplate | ||
|
||
def import | ||
contents = params.fetch(:imported_template, {}).fetch(:template, nil).try(:read) | ||
|
||
@template = OutputTemplate.import_raw(contents, :update => ActiveRecord::Type::Boolean.new.deserialize(params[:imported_template][:overwrite])) | ||
if @template&.save | ||
flash[:success] = _('Output template imported successfully.') | ||
redirect_to output_templates_path(:search => "name = \"#{@template.name}\"") | ||
else | ||
@template ||= OutputTemplate.import_raw(contents, :build_new => true) | ||
@template.valid? | ||
flash[:warning] = _('Unable to save template. Correct highlighted errors') | ||
render :action => 'new' | ||
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
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
45 changes: 45 additions & 0 deletions
45
app/lib/actions/remote_execution/output_processing_action.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,45 @@ | ||
module Actions | ||
module RemoteExecution | ||
class OutputProcessing < Dynflow::Action | ||
|
||
def process_proxy_template(output, template, invocation) | ||
base = Host.authorized(:view_hosts, Host) | ||
# provide host information for the output template rendering | ||
host = base.find(invocation.host_id) | ||
renderer = InputTemplateRenderer.new(template, host, invocation, nil, false, [], output) | ||
processed_output = renderer.render | ||
unless processed_output | ||
return renderer.error_message.html_safe, false | ||
end | ||
return processed_output, true | ||
end | ||
|
||
def run | ||
processed_outputs = [] | ||
template_invocation = TemplateInvocation.find(input[:template_invocation_id]) | ||
events = template_invocation.template_invocation_events | ||
sq_id = events.max_by { |e| e.sequence_id }.sequence_id + 1 | ||
output_templates = template_invocation.job_invocation.output_templates | ||
output_templates.each_with_index.map do |output_templ, templ_id| | ||
for i in 0..events.length - 1 do | ||
if events[i][:event].instance_of?(String) && events[i][:event_type] == 'stdout' | ||
output, success = process_proxy_template(events[i][:event], output_templ, template_invocation) | ||
processed_outputs << { | ||
sequence_id: sq_id, | ||
template_invocation_id: template_invocation.id, | ||
event: output, | ||
timestamp: events[i][:timestamp] || Time.zone.now, | ||
event_type: success ? 'template_output' : 'template_output_err', | ||
} | ||
# template invocation id and a sequence combination has to be unique | ||
sq_id += 1 | ||
end | ||
end | ||
end | ||
processed_outputs.each_slice(1000) do |batch| | ||
TemplateInvocationEvent.upsert_all(batch, unique_by: [:template_invocation_id, :sequence_id]) # rubocop:disable Rails/SkipsModelValidations | ||
end | ||
end | ||
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
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
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
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,4 @@ | ||
class JobInvocationTemplate < ApplicationRecord | ||
belongs_to :job_invocation | ||
belongs_to :output_template | ||
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,4 @@ | ||
class JobTemplateOutputTemplate < ApplicationRecord | ||
belongs_to :job_template | ||
belongs_to :output_template | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a Go thingy here? 👿