Module: RemoteExecutionHelper
- Included in:
- Api::V2::JobInvocationsController
- Defined in:
- app/helpers/remote_execution_helper.rb
Instance Method Summary collapse
- #description_checkbox_f(f, job_template, disabled) ⇒ Object
- #description_format_help ⇒ Object
- #description_format_textarea_f(f, job_template, disabled) ⇒ Object
- #documentation_button_rex(section = '') ⇒ Object
- #host_counter(label, count) ⇒ Object
- #host_tasks_authorizer ⇒ Object
- #invocation_count(invocation, options = {}) ⇒ Object
- #invocation_description(invocation, keep_tooltip: true) ⇒ Object
- #invocation_result(invocation, key) ⇒ Object
- #job_hosts_authorizer ⇒ Object
- #job_invocation_task_buttons(task) ⇒ Object
- #job_invocations_buttons ⇒ Object
- #job_report_template ⇒ Object
- #job_report_template_parameters(job_invocation, template) ⇒ Object
- #link_to_invocation_task_if_authorized(invocation) ⇒ Object
- #load_template_from_task(template_invocation, target) ⇒ Object
-
#normalize_line_sets(line_sets) ⇒ Object
we assume that a line_set will always end with a line break.
- #preview_box(template_invocation, target) ⇒ Object
- #providers_options ⇒ Object
- #remote_execution_provider_for(template_invocation) ⇒ Object
- #targeting_hosts(job_invocation, hosts) ⇒ Object
- #template_invocation_actions(task, host, job_invocation, template_invocation) ⇒ Object
- #template_invocation_status(task, parent_task) ⇒ Object
- #template_invocation_task_buttons(task, invocation) ⇒ Object
- #time_in_words_span(time) ⇒ Object
Instance Method Details
#description_checkbox_f(f, job_template, disabled) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'app/helpers/remote_execution_helper.rb', line 217 def description_checkbox_f(f, job_template, disabled) check_box_tag('description_format_override', job_template.generate_description_format, f.object.description_format.nil?, :class => 'description_format_override', :name => f.object_name + '[description_override]', :disabled => disabled, :onchange => 'description_override(this);') + ' ' + _('Use default description template') end |
#description_format_help ⇒ Object
238 239 240 241 242 243 |
# File 'app/helpers/remote_execution_helper.rb', line 238 def description_format_help _('This template is used to generate the description.<br/>' + 'Input values can be used using the syntax %{package}.<br/>' + 'You may also include the job category and template<br/>' + 'name using %{job_category} and %{template_name}.').html_safe # rubocop:disable Rails/OutputSafety end |
#description_format_textarea_f(f, job_template, disabled) ⇒ Object
227 228 229 230 231 232 233 234 235 236 |
# File 'app/helpers/remote_execution_helper.rb', line 227 def description_format_textarea_f(f, job_template, disabled) textarea_f f, 'description_format', :label => _('Description template'), :value => f.object.description_format || job_template.generate_description_format, :rows => 2, :onchange => 'regenerate_description(this);', :class => 'description_format advanced', :disabled => disabled, :label_help => description_format_help end |
#documentation_button_rex(section = '') ⇒ Object
211 212 213 214 215 |
# File 'app/helpers/remote_execution_helper.rb', line 211 def (section = '') url = 'http://theforeman.org/plugins/foreman_remote_execution/' + "#{ForemanRemoteExecution::VERSION.split('.').take(2).join('.')}/index.html#" section, :root_url => url end |
#host_counter(label, count) ⇒ Object
14 15 16 17 18 |
# File 'app/helpers/remote_execution_helper.rb', line 14 def host_counter(label, count) content_tag(:div, :class => 'host_counter') do content_tag(:div, label, :class => 'header') + content_tag(:div, count.to_s, :class => 'count') end end |
#host_tasks_authorizer ⇒ Object
10 11 12 |
# File 'app/helpers/remote_execution_helper.rb', line 10 def @host_tasks_authorizer ||= Authorizer.new(User.current, :collection => @job_invocation.sub_tasks) end |
#invocation_count(invocation, options = {}) ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'app/helpers/remote_execution_helper.rb', line 145 def invocation_count(invocation, = {}) = { :unknown_string => 'N/A' }.merge() if invocation.queued? [:unknown_string] elsif [:output_key] == :total_count invocation.total_hosts_count else (invocation.task.try(:output) || {}).fetch([:output_key], [:unknown_string]) end end |
#invocation_description(invocation, keep_tooltip: true) ⇒ Object
156 157 158 159 160 |
# File 'app/helpers/remote_execution_helper.rb', line 156 def invocation_description(invocation, keep_tooltip: true) description = invocation.description.try(:capitalize) || invocation.job_category description = trunc_with_tooltip(description, 80) if keep_tooltip description end |
#invocation_result(invocation, key) ⇒ Object
162 163 164 165 166 167 |
# File 'app/helpers/remote_execution_helper.rb', line 162 def invocation_result(invocation, key) unknown = '—' result = invocation_count(invocation, :output_key => key, :unknown_string => unknown.html_safe) # rubocop:disable Rails/OutputSafety label = key == :failed_count ? 'danger' : 'info' result == unknown ? result : report_event_column(result, "label-#{label}") end |
#job_hosts_authorizer ⇒ Object
6 7 8 |
# File 'app/helpers/remote_execution_helper.rb', line 6 def @job_hosts_authorizer ||= Authorizer.new(User.current, :collection => @hosts) end |
#job_invocation_task_buttons(task) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/helpers/remote_execution_helper.rb', line 62 def (task) job_invocation = task.task_groups.find { |group| group.class == JobInvocationTaskGroup }.job_invocation = Authorizer.new(User.current, :collection => [task]) = [] if (template = job_report_template) && (controller: :report_templates, action: :generate) << link_to(_('Create Report'), generate_report_template_path(template, job_report_template_parameters(job_invocation, template)), class: 'btn btn-default', title: _('Create report for this job'), disabled: task.pending?) end if (controller: :job_invocations, action: :create) << link_to(_('Rerun'), rerun_job_invocation_path(:id => job_invocation.id), :class => 'btn btn-default', :title => _('Rerun the job')) << link_to(_('Rerun failed'), rerun_job_invocation_path(:id => job_invocation.id, :failed_only => 1), :class => 'btn btn-default', :disabled => job_invocation.failed_hosts.none?, :title => _('Rerun on failed hosts')) << link_to(_('Rerun succeeded'), rerun_job_invocation_path(:id => job_invocation.id, :succeeded_only => 1), :class => 'btn btn-default', :disabled => job_invocation.succeeded_hosts.none?, :title => _('Rerun on succeeded hosts')) end if (:permission => :view_foreman_tasks, :auth_object => task, :authorizer => ) << link_to(_('Job Task'), foreman_tasks_task_path(task), :class => 'btn btn-default', :title => _('See the last task details')) end if (:permission => :cancel_job_invocations, :auth_object => job_invocation) << (_('Cancel Job'), cancel_job_invocation_path(job_invocation), :class => 'btn btn-danger', :title => _('Try to cancel the job'), :disabled => !task.cancellable?, :method => :post) << (_('Abort Job'), cancel_job_invocation_path(job_invocation, :force => true), :class => 'btn btn-danger', :title => _('Try to abort the job without waiting for the results from the remote hosts'), :disabled => !task.cancellable?, :method => :post) end if Setting[:lab_features] << link_to(_('New UI'), new_job_invocation_detail_path(:id => job_invocation.id), class: 'btn btn-default', title: _('Switch to the new job invocation detail UI')) end return end |
#job_invocations_buttons ⇒ Object
55 56 57 58 59 60 |
# File 'app/helpers/remote_execution_helper.rb', line 55 def [ ('3.2ExecutingaJob'), (controller: :job_invocations, action: :create) ? link_to(_('Run Job'), hash_for_new_job_invocation_path, {:class => "btn btn-primary"}) : '', ] end |
#job_report_template ⇒ Object
252 253 254 255 256 |
# File 'app/helpers/remote_execution_helper.rb', line 252 def job_report_template template = ReportTemplate.where(name: Setting['remote_execution_job_invocation_report_template']).first template if template && template.template_inputs.where(name: 'job_id').exists? end |
#job_report_template_parameters(job_invocation, template) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'app/helpers/remote_execution_helper.rb', line 258 def job_report_template_parameters(job_invocation, template) template_input = template.template_inputs.where(name: 'job_id').first raise "#job_report_template_parameters need template that has 'job_id' input" unless template_input { report_template_report: { input_values: { "#{template_input.id}": { value: job_invocation.id, }, }, }, } end |
#link_to_invocation_task_if_authorized(invocation) ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'app/helpers/remote_execution_helper.rb', line 134 def (invocation) status = job_invocation_status(invocation, nil, false) if invocation.queued? status else = Authorizer.new(User.current, :collection => [invocation.task]) job_invocation_status(invocation), hash_for_foreman_tasks_task_path(invocation.task).merge(:auth_object => invocation.task, :permission => :view_foreman_tasks, :authorizer => ) end end |
#load_template_from_task(template_invocation, target) ⇒ Object
245 246 247 248 249 250 |
# File 'app/helpers/remote_execution_helper.rb', line 245 def load_template_from_task(template_invocation, target) task = template_invocation.job_invocation.sub_task_for_host(target) return if [nil, 'scheduled', 'planning'].include?(task&.state) task.execution_plan.actions[1].try(:input).try(:[], 'script') end |
#normalize_line_sets(line_sets) ⇒ Object
we assume that a line_set will always end with a line break. Sometimes however, the lines can be cut in the middle. This methods makes sure the cut line ends up in on line set
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'app/helpers/remote_execution_helper.rb', line 185 def normalize_line_sets(line_sets) previous_line_break = true line_sets.each_with_index do |line_set, index| # if previous line_set was missing break, add the first line from next set unless previous_line_break first_line_pattern = /\A.*\n/ first_line = line_set['output'][first_line_pattern] if first_line line_sets[index - 1]['output'] << first_line line_set['output'].sub!(first_line_pattern, '') end end previous_line_break = line_set['output'] =~ /\n\Z/ end line_sets end |
#preview_box(template_invocation, target) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'app/helpers/remote_execution_helper.rb', line 169 def preview_box(template_invocation, target) renderer = InputTemplateRenderer.new(template_invocation.template, target, template_invocation) if (preview = (load_template_from_task(template_invocation, target) || renderer.preview)) content_tag :pre, preview elsif target.nil? alert :text => _('Could not render the preview because no host matches the search query.'), :class => 'alert alert-block alert-warning base', :close => false else alert :class => 'alert-block alert-danger base in fade has-error', :text => renderer..html_safe # rubocop:disable Rails/OutputSafety end end |
#providers_options ⇒ Object
2 3 4 |
# File 'app/helpers/remote_execution_helper.rb', line 2 def RemoteExecutionProvider.providers.reject { |key, _provider| key == 'SSH' }.map { |key, provider| [ key, _(provider.humanized_name) ] } end |
#remote_execution_provider_for(template_invocation) ⇒ Object
51 52 53 |
# File 'app/helpers/remote_execution_helper.rb', line 51 def remote_execution_provider_for(template_invocation) template_invocation.nil? ? _('N/A') : template_invocation.template.provider.humanized_name end |
#targeting_hosts(job_invocation, hosts) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'app/helpers/remote_execution_helper.rb', line 273 def targeting_hosts(job_invocation, hosts) hosts.map do |host| template_invocation = job_invocation.template_invocations.find { |template_inv| template_inv.host_id == host.id } task = template_invocation.try(:run_host_job_task) = !task.nil? && (hash_for_template_invocation_path(:id => template_invocation).merge(:auth_object => host, :permission => :view_hosts, :authorizer => )) { name: host.to_label, link: ? template_invocation_path(:id => template_invocation) : '', status: template_invocation_status(task, job_invocation.task), actions: template_invocation_actions(task, host, job_invocation, template_invocation) } end end |
#template_invocation_actions(task, host, job_invocation, template_invocation) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/helpers/remote_execution_helper.rb', line 28 def template_invocation_actions(task, host, job_invocation, template_invocation) links = [] host_task = template_invocation.try(:run_host_job_task) if (hash_for_host_path(host).merge(auth_object: host, permission: :view_hosts, authorizer: )) links << { title: _('Host detail'), action: { href: current_host_details_path(host), 'data-method': 'get', id: "#{host.name}-actions-detail" } } end if (controller: :job_invocations, action: :create) && (!host.infrastructure_host? || User.current.can?(:execute_jobs_on_infrastructure_hosts)) links << { title: (_('Rerun on %s') % host), action: { href: rerun_job_invocation_path(job_invocation, host_ids: [ host.id ]), 'data-method': 'get', id: "#{host.name}-actions-rerun" } } end if host_task.present? && (hash_for_foreman_tasks_task_path(host_task).merge(auth_object: host_task, permission: :view_foreman_tasks, authorizer: )) links << { title: _('Host task'), action: { href: foreman_tasks_task_path(host_task), 'data-method': 'get', id: "#{host.name}-actions-task" } } end links end |
#template_invocation_status(task, parent_task) ⇒ Object
20 21 22 23 24 25 26 |
# File 'app/helpers/remote_execution_helper.rb', line 20 def template_invocation_status(task, parent_task) return(parent_task.result == 'cancelled' ? 'cancelled' : 'N/A') if task.nil? return task.state if task.state == 'running' || task.state == 'planned' return 'error' if task.result == 'warning' task.result end |
#template_invocation_task_buttons(task, invocation) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/helpers/remote_execution_helper.rb', line 110 def (task, invocation) = [] if (:permission => :view_foreman_tasks, :auth_object => task) << link_to(_('Task Details'), foreman_tasks_task_path(task), :class => 'btn btn-default', :title => _('See the task details')) end if (:permission => :cancel_job_invocations, :auth_object => invocation) << link_to(_('Cancel Job'), cancel_foreman_tasks_task_path(task), :class => 'btn btn-danger', :title => _('Try to cancel the job on a host'), :disabled => !task.cancellable?, :method => :post, :remote => true) << link_to(_('Abort Job'), abort_foreman_tasks_task_path(task), :class => 'btn btn-danger', :title => _('Try to abort the job on a host without waiting for its result'), :disabled => !task.cancellable?, :method => :post, :remote => true) end end |
#time_in_words_span(time) ⇒ Object
202 203 204 205 206 207 208 209 |
# File 'app/helpers/remote_execution_helper.rb', line 202 def time_in_words_span(time) if time.nil? _('N/A') else content_tag :span, (time > Time.now.utc ? _('in %s') : _('%s ago')) % time_ago_in_words(time), { :'data-original-title' => time.try(:in_time_zone), :rel => 'twipsy' } end end |