Module: RemoteExecutionHelper

Defined in:
app/helpers/remote_execution_helper.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#advanced_switch_f(default_text, switch_text) ⇒ Object



266
267
268
269
270
271
272
273
274
# File 'app/helpers/remote_execution_helper.rb', line 266

def advanced_switch_f(default_text, switch_text)
   :div, :class => 'form-group' do
    (:div, '', :class => 'col-md-2 control-label') +
    (:div, :class => 'col-md-4') do
      (:i, '', :class => 'fa fa-angle-right') + ' ' +
      link_to(default_text, '#', :class => 'advanced_fields_switch', :'data-alternative-label' => switch_text)
    end
  end
end

#description_checkbox_f(f, job_template, disabled) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'app/helpers/remote_execution_helper.rb', line 238

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_helpObject



259
260
261
262
263
264
# File 'app/helpers/remote_execution_helper.rb', line 259

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



248
249
250
251
252
253
254
255
256
257
# File 'app/helpers/remote_execution_helper.rb', line 248

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



223
224
225
226
227
# File 'app/helpers/remote_execution_helper.rb', line 223

def documentation_button_rex(section = '')
  url = 'http://theforeman.org/plugins/foreman_remote_execution/' +
    "#{ForemanRemoteExecution::VERSION.split('.').take(2).join('.')}/index.html#"
  documentation_button section, :root_url => url
end

#host_counter(label, count) ⇒ Object



52
53
54
55
56
# File 'app/helpers/remote_execution_helper.rb', line 52

def host_counter(label, count)
  (:div, :class => 'host_counter') do
    (:div, label, :class => 'header') + (:div, count.to_s, :class => 'count')
  end
end

#invocation_count(invocation, options = {}) ⇒ Object



168
169
170
171
172
173
174
175
# File 'app/helpers/remote_execution_helper.rb', line 168

def invocation_count(invocation, options = {})
  options = { :unknown_string => 'N/A' }.merge(options)
  if invocation.queued?
    options[:unknown_string]
  else
    (invocation.task.try(:output) || {}).fetch(options[:output_key], options[:unknown_string])
  end
end

#invocation_description(invocation) ⇒ Object



177
178
179
180
# File 'app/helpers/remote_execution_helper.rb', line 177

def invocation_description(invocation)
  description = invocation.description.try(:capitalize) || invocation.job_category
  trunc_with_tooltip(description, 80)
end

#invocation_result(invocation, key) ⇒ Object



182
183
184
185
186
187
# File 'app/helpers/remote_execution_helper.rb', line 182

def invocation_result(invocation, key)
  unknown = '&mdash;'
  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_authorizerObject



28
29
30
# File 'app/helpers/remote_execution_helper.rb', line 28

def job_hosts_authorizer
  @job_hosts_authorizer ||= Authorizer.new(User.current, :collection => @hosts)
end

#job_invocation_active_tab(tab, params) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'app/helpers/remote_execution_helper.rb', line 203

def job_invocation_active_tab(tab, params)
  active = 'active'
  inactive = ''
  hosts_tab_active = params[:page].present? || params[:search].present? || params[:order].present?
  if hosts_tab_active
    tab == :hosts ? active : inactive
  else
    tab == :overview ? active : inactive
  end
end

#job_invocation_chart(invocation) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/remote_execution_helper.rb', line 11

def job_invocation_chart(invocation)
  options = { :class => 'statistics-pie small', :expandable => true, :border => 0, :show_title => true }

  if invocation.task
    # Request precise subtask counts only if the task is stopped
    report = invocation.progress_report
    flot_pie_chart('status', job_invocation_status(invocation, report[:progress]),
                   [{:label => _('Success'),   :data => report[:success],   :color => '#5CB85C'},
                    {:label => _('Failed'),    :data => report[:failed],    :color => '#D9534F'},
                    {:label => _('Pending'),   :data => report[:pending],   :color => '#DEDEDE'},
                    {:label => _('Cancelled'), :data => report[:cancelled], :color => '#B7312D'}],
                   options)
  else
    (:h4, job_invocation_status(invocation))
  end
end

#job_invocation_status(invocation, percent = nil, verbose = true) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/remote_execution_helper.rb', line 32

def job_invocation_status(invocation, percent = nil, verbose = true)
  case invocation.status
    when HostStatus::ExecutionStatus::QUEUED
      if verbose && invocation.task
        _('queued to start executing in %{time}') % {:time => time_ago_in_words(invocation.task.start_at) }
      else
        _('queued')
      end
    when HostStatus::ExecutionStatus::RUNNING
      percent ||= invocation.progress_report[:progress]
      _('running %{percent}%%') % {:percent => percent}
    when HostStatus::ExecutionStatus::OK
      _('succeeded')
    when HostStatus::ExecutionStatus::ERROR
      _('failed')
    else
      _('unknown status')
  end
end

#job_invocation_task_buttons(task) ⇒ Object

rubocop:disable Metrics/AbcSize



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
# File 'app/helpers/remote_execution_helper.rb', line 98

def job_invocation_task_buttons(task)
  job_invocation = task.task_groups.find { |group| group.class == JobInvocationTaskGroup }.job_invocation
  task_authorizer = Authorizer.new(User.current, :collection => [task])
  buttons = []
  buttons << link_to(_('Refresh'), {}, :class => 'btn btn-default', :title => _('Refresh this page'))
  if authorized_for(hash_for_new_job_invocation_path)
    buttons << link_to(_('Rerun'), rerun_job_invocation_path(:id => job_invocation.id),
                       :class => 'btn btn-default',
                       :title => _('Rerun the job'))
  end
  if authorized_for(hash_for_new_job_invocation_path)
    buttons << link_to(_('Rerun failed'), rerun_job_invocation_path(:id => job_invocation.id, :failed_only => 1),
                       :class => 'btn btn-default',
                       :disabled => task.sub_tasks.where(:result => %w(error warning)).count.zero?,
                       :title => _('Rerun on failed hosts'))
  end
  if authorized_for(:permission => :view_foreman_tasks, :auth_object => task, :authorizer => task_authorizer)
    buttons << link_to(_('Job Task'), foreman_tasks_task_path(task),
                       :class => 'btn btn-default',
                       :title => _('See the last task details'))
  end
  if authorized_for(:permission => :edit_foreman_tasks, :auth_object => task, :authorizer => task_authorizer)
    buttons << link_to(_('Cancel Job'), cancel_foreman_tasks_task_path(task),
                       :class => 'btn btn-danger',
                       :title => _('Try to cancel the job'),
                       :disabled => !task.cancellable?,
                       :method => :post)
    buttons << link_to(_('Abort Job'), abort_foreman_tasks_task_path(task),
                       :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
  return buttons
end

#job_invocations_buttonsObject



90
91
92
93
94
95
# File 'app/helpers/remote_execution_helper.rb', line 90

def job_invocations_buttons
  [
    documentation_button_rex('3.2ExecutingaJob'),
    new_link(_('Run Job'))
  ]
end


157
158
159
160
161
162
163
164
165
166
# File 'app/helpers/remote_execution_helper.rb', line 157

def link_to_invocation_task_if_authorized(invocation)
  status = job_invocation_status(invocation, nil, false)
  if invocation.queued?
    status
  else
    task_authorizer = Authorizer.new(User.current, :collection => [invocation.task])
    link_to_if_authorized job_invocation_status(invocation),
                          hash_for_foreman_tasks_task_path(invocation.task).merge(:auth_object => invocation.task, :permission => :view_foreman_tasks, :authorizer => task_authorizer)
  end
end

#preview_box(template_invocation, target) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/helpers/remote_execution_helper.rb', line 189

def preview_box(template_invocation, target)
  renderer = InputTemplateRenderer.new(template_invocation.template, target, template_invocation)
  if (preview = renderer.preview)
     :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.error_message.html_safe # rubocop:disable Rails/OutputSafety
  end
end

#providers_optionsObject



3
4
5
# File 'app/helpers/remote_execution_helper.rb', line 3

def providers_options
  RemoteExecutionProvider.providers.map { |key, provider| [ key, _(provider.humanized_name) ] }
end

#remote_execution_provider_for(template_invocation) ⇒ Object



86
87
88
# File 'app/helpers/remote_execution_helper.rb', line 86

def remote_execution_provider_for(template_invocation)
  template_invocation.nil? ? _('N/A') : template_invocation.template.provider.humanized_name
end

#template_input_header(f, template) ⇒ Object



229
230
231
232
233
234
235
236
# File 'app/helpers/remote_execution_helper.rb', line 229

def template_input_header(f, template)
  header = _('Template input')
  unless template.locked?
    header += ' ' + remove_child_link('x', f, {:rel => 'twipsy', :'data-title' => _('remove template input'), :'data-placement' => 'left',
                                               :class => 'fr badge badge-danger'})
  end
  header.html_safe # rubocop:disable Rails/OutputSafety
end

#template_input_types_optionsObject



7
8
9
# File 'app/helpers/remote_execution_helper.rb', line 7

def template_input_types_options
  TemplateInput::TYPES.map { |key, name| [ _(name), key ] }
end

#template_invocation_actions(task, host, job_invocation, template_invocation) ⇒ Object



79
80
81
82
83
84
# File 'app/helpers/remote_execution_helper.rb', line 79

def template_invocation_actions(task, host, job_invocation, template_invocation)
  [
    display_link_if_authorized(_('Host detail'), hash_for_host_path(host).merge(:auth_object => host, :permission => :view_hosts, :authorizer => job_hosts_authorizer)),
    display_link_if_authorized(_('Rerun on %s') % host.name, hash_for_rerun_job_invocation_path(:id => job_invocation, :host_ids => [ host.id ], :authorizer => job_hosts_authorizer)),
  ]
end

#template_invocation_status(task) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/remote_execution_helper.rb', line 58

def template_invocation_status(task)
  if task.nil?
    icon_text('question', 'N/A', :kind => 'fa')
  elsif task.state == 'running'
    icon_text('running', _('running'), :kind => 'pficon')
  elsif task.state == 'planned'
    icon_text('build', _('planned'), :kind => 'pficon')
  else
    case task.result
      when 'warning', 'error'
        icon_text('error-circle-o', _('failed'), :kind => 'pficon')
      when 'cancelled'
        icon_text('warning-triangle-o', _('cancelled'), :kind => 'pficon')
      when 'success'
        icon_text('ok', _('success'), :kind => 'pficon')
      else
        task.result
    end
  end
end

#template_invocation_task_buttons(task) ⇒ Object

rubocop:enable Metrics/AbcSize



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/helpers/remote_execution_helper.rb', line 135

def template_invocation_task_buttons(task)
  buttons = []
  if authorized_for(:permission => :view_foreman_tasks, :auth_object => task)
    buttons << link_to(_('Task Details'), foreman_tasks_task_path(task),
                       :class => 'btn btn-default',
                       :title => _('See the task details'))
  end
  if authorized_for(:permission => :edit_foreman_tasks, :auth_object => task)
    buttons << 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)
    buttons << 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)
  end
  return buttons
end

#time_in_words_span(time) ⇒ Object



214
215
216
217
218
219
220
221
# File 'app/helpers/remote_execution_helper.rb', line 214

def time_in_words_span(time)
  if time.nil?
    _('N/A')
  else
     :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