require "velum/salt"

# UpdatesController handles all the interaction with the updates of all nodes.
class UpdatesController < ApplicationController
  before_action :admin_needs_update, only: :create

  # Reboot the admin node.
  def create
    ::Velum::Salt.call(
      action:  "cmd.run",
      targets: "admin",
      arg:     "systemctl reboot"
    )

    render json: { status: "rebooting" }
  end

  protected

  def admin_needs_update
    render json: { status: "unknown" } if Minion.where(role: Minion.roles[:admin])
                                                .needs_update.count.zero?
  end
end

require "velum/salt"

# UpdatesController handles all the interaction with the updates of all nodes.
class UpdatesController < ApplicationController
  before_action :admin_needs_update, only: :create

  # Reboot the admin node.
  def create
    # rubocop:disable SkipsModelValidations
    Minion.admin.update_all highstate: Minion.highstates[:applied]
    # rubocop:enable SkipsModelValidations
    ::Velum::Salt.call(
      action:  "cmd.run",
      targets: "admin",
      arg:     "systemctl reboot"
    )

    render json: { status: "rebooting" }
  end

  protected

  def admin_needs_update
    render json: { status: "unknown" } if Minion.where(role: Minion.roles[:admin])
                                                .needs_update.count.zero?
  end
end

Mark admin as updated when we are going to reboot it
Mark admin as updated when we are going to reboot it

This ensures that when the admin is back, the status of the minion on
the database will match the grains from the very beginning, without
having to rely on the background task updating all minions (including
the admin) update status.

Fixes: bsc#1092910
