Class: ForemanWreckingball::HostsController

Inherits:
HostsController
  • Object
show all
Includes:
ApplicationHelper, AuthorizeHelper, ForemanTasks::Concerns::Parameters::Triggering, HostsHelper
Defined in:
app/controllers/foreman_wreckingball/hosts_controller.rb

Constant Summary collapse

AJAX_REQUESTS =
[:status_hosts].freeze

Instance Method Summary collapse

Instance Method Details

#refresh_status_dashboardObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/foreman_wreckingball/hosts_controller.rb', line 100

def refresh_status_dashboard
  if ForemanTasks::Task.active.where(label: ::Actions::ForemanWreckingball::Vmware::ScheduleVmwareSync.to_s).empty?
    flash[:success] = _('Refresh Compute Resource data task was successfully scheduled.')
    task = User.as_anonymous_admin do
      ::ForemanTasks.async_task(::Actions::ForemanWreckingball::Vmware::ScheduleVmwareSync)
    end
    redirect_to(foreman_tasks_task_path(task.id))
  else
    flash[:warning] = _('Refresh Compute Resource data task is already running. Please wait for the running task to finish.')
    redirect_to status_dashboard_hosts_path
  end
end

#schedule_remediateObject



113
114
115
# File 'app/controllers/foreman_wreckingball/hosts_controller.rb', line 113

def schedule_remediate
  @triggering = ForemanTasks::Triggering.new(mode: :immediate)
end

#status_dashboardObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/foreman_wreckingball/hosts_controller.rb', line 17

def status_dashboard
  @newest_data = Host.authorized(:view_hosts).joins(:vmware_facet).maximum('vmware_facets.updated_at')
  host_ids = Host.authorized(:view_hosts)
                 .try { |query| params[:owned_only] ? query.owned_by_current_user_or_group_with_current_user : query }
                 .pluck(:id)

  @data = HostStatus.wreckingball_statuses.map do |status|
    counter = status.where(host_id: host_ids)
                    .select(:status)
                    .group(:status)
                    .count
                    .transform_keys { |key| status.to_global(key) }

    {
      name: status.status_name,
      description: status.description,
      host_association: status.host_association,
      supports_remediate: status.supports_remediate?,
      counter: {
        ok: counter[HostStatus::Global::OK] || 0,
        warning: counter[HostStatus::Global::WARN] || 0,
        critical: counter[HostStatus::Global::ERROR] || 0
      }
    }
  end
end

#status_hostsObject

ajax method



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/foreman_wreckingball/hosts_controller.rb', line 77

def status_hosts
  @status = HostStatus.find_wreckingball_status_by_host_association(params.fetch(:status))

  all_hosts = Host.authorized(:view_hosts, Host)
                  .joins(@status.host_association)
                  .try { |query| params[:owned_only] ? query.owned_by_current_user_or_group_with_current_user : query }
                  .includes(@status.host_association, :vmware_facet, :environment)
                  .where.not('host_status.status': @status.global_ok_list)
                  .preload(:owner)
                  .order(:name)

  @count = all_hosts.size
  @hosts = all_hosts.paginate(page: params.fetch(:page, 1), per_page: params.fetch(:per_page, 100))

  respond_to do |format|
    format.json do
      Rabl::Renderer.json(@hosts, 'foreman_wreckingball/hosts/status_hosts',
                          view_path: "#{ForemanWreckingball::Engine.root}/app/views",
                          scope: self)
    end
  end
end

#status_managed_hosts_dashboardObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/foreman_wreckingball/hosts_controller.rb', line 44

def status_managed_hosts_dashboard
  # NOTE The call to ComputeResource#vms may slow things down
  vms_by_compute_resource_id = Foreman::Model::Vmware.all.each_with_object({}) do |cr, memo|
    memo[cr.id] = cr.vms(eager_loading: true)
  end

  # Find all hosts with duplicate VMs
  @duplicate_vms = vms_by_compute_resource_id.values
                                             .flatten
                                             .group_by(&:uuid)
                                             .select { |_uuid, vms| vms.size > 1 }

  @missing_hosts = []
  @different_hosts = []

  Host::Managed.authorized(:view_hosts, Host)
               .where.not(compute_resource_id: nil)
               .try { |query| params[:owned_only] ? query.owned_by_current_user_or_group_with_current_user : query }
               .each do |host|
    # find the compute resource id of the host in the vm map
    cr_id, _vms = vms_by_compute_resource_id.find { |_cr_id, vms| vms.find { |vm| vm.uuid == host.uuid } }

    if cr_id.nil?
      # No compute resource id is found, vSphere does not have the vm uuid
      @missing_hosts << host
    elsif cr_id != host.compute_resource_id
      # The host uuid is found in a different compute resource
      @different_hosts << host
    end
  end
end

#submit_remediateObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/foreman_wreckingball/hosts_controller.rb', line 117

def submit_remediate
  return not_found unless @statuses.any?

  triggering = ::ForemanTasks::Triggering.new_from_params(triggering_params)
  if triggering.future?
    triggering.parse_start_at!
    triggering.parse_start_before!
  else
    triggering.start_at ||= Time.zone.now
  end

  task = User.as_anonymous_admin do
    triggering.trigger(::Actions::ForemanWreckingball::BulkRemediate, @statuses)
  end

  redirect_to foreman_tasks_task_path(task.id)
end