-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
882 additions
and
65 deletions.
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.