chef-boneyard / redhat-subscription-manager-cookbook

Cookbook providing custom resources for interacting with Red Hat Subscription Manager
Apache License 2.0
4 stars 18 forks source link

rhsm_register runs even though a system is already registered #23

Open er0 opened 7 years ago

er0 commented 7 years ago

If the the test 'subscription-manager status' fails even though a system is registered, rhsm_register then tries to register again and the registration fails with:

Expected process to exit with [0], but received '64' ---- Begin output of subscription-manager register --activationkey=redacted --org=redacted ---- STDOUT: STDERR: This system is already registered. Use --force to override

I believe this is due to this block of code in libraries/helpers.rb:

def registered_with_rhsm?
  cmd = Mixlib::ShellOut.new('subscription-manager status')
  cmd.run_command
  !cmd.stdout.match(/Overall Status: Unknown/)
end

maybe change registered_with_rhsm? to true on exit status 0, or: cmd.stdout.match(/Overall Status: Current/)

jayhendren commented 7 years ago

We ran into this too. We worked around this with the following monkey-patch, which we placed into the libraries folder of our wrapper cookbook:

module CubRhsmCookbook
  # this is a top-level module documentation comment to please rubocop.
  module RhsmHelpers
    def registered_with_rhsm?
      # err on the side of assuming the system is already registered
      #
      # we only need systems to register themselves on the first converge
      # in the typical case; otherwise avoid errors by assuming a system is
      # registered if it looks even slightly like it is registered
      #
      # product_status.json is a cache of a call to the RHSM API.
      # subscription-manager creates this file upon a successful registration
      # and deletes it when a system is unregistered cleanly.
      File.exist?('/var/lib/rhsm/cache/product_status.json') || super
    end
  end
end

RhsmCookbook::RhsmRegister.include CubRhsmCookbook::RhsmHelpers

Strictly speaking, this is not an issue with the redhat-subscription-manager-cookbook, but with RHSM and subscription-manager, which fail to return consistent values when a system is already registered.