Red Hat Satellite Upgrade fails with 'Validation failed: Puppet environment can't be blank:Upgrade step import_puppet_modules failed'

Solution Verified - Updated -

Environment

  • Red Hat Satellite 6.x
  • Red Hat Enterprise Linux 6,7

Issue

  • Red Hat Satellite upgrade failing with the following error:

    ActiveRecord::RecordInvalid: Validation failed: Puppet environment can't be blank
    Tasks: TOP => katello:upgrades:2.4:import_puppet_modules
    (See full trace by running task with --trace)
    Importing Puppet Modules
    
    Upgrade step import_puppet_modules failed. Check logs for more information.
    

Resolution

  • Create missing puppet environments for affected content views.

    • Solution applicable for Red Hat Satellite 6.2 or 6.3:
    # foreman-rake console
    
    User.current = User.anonymous_api_admin
    Katello::ContentViewPuppetEnvironment.where(:puppet_environment_id => nil).where("environment_id is not null").each do |cvpe|
    env = Environment.where(:name => cvpe.generate_puppet_env_name).first
    if env.nil?
        env = Environment.create(:name => cvpe.generate_puppet_env_name, :locations => [Location.default_location], :organizations => [cvpe.content_view.organization])
     end
     env.organizations << cvpe.content_view.organization unless env.organizations.include?(cvpe.content_view.organization)
     env.locations << Location.default_location unless env.locations.include?(Location.default_location)
     cvpe.puppet_environment = env
     cvpe.save!
    end
    
    • Solution applicable for Red Hat Satellite 6.4:
    # foreman-rake console
    
    User.current = User.anonymous_api_admin
    Katello::ContentViewPuppetEnvironment.where(:puppet_environment_id => nil).where("environment_id is not null").each do |cvpe|
      env = Environment.where(:name => cvpe.generate_puppet_env_name).first
      if env.nil?
        env = Environment.create(:name => cvpe.generate_puppet_env_name, :location_ids => Location.default_location_ids, :organizations =>   [cvpe.content_view.organization])
      end
      env.organizations << cvpe.content_view.organization unless env.organizations.include?(cvpe.content_view.organization)  
      location = ::Location.find_by_title(::Setting[:default_location_puppet_content])
      env.locations << location unless env.locations.include?(location)
      cvpe.puppet_environment = env
      cvpe.save!
    end
    
  • Ensure there is no content view with empty/undefined puppet environment. Use below command to confirm this and it should return empty value.

    ::Katello::ContentViewPuppetEnvironment.where('puppet_environment_id is NULL AND environment_id is NOT NULL')
    
  • Run the upgrade once again.

    # satellite-installer --scenario satellite --upgrade
    
    • Solution applicable for Red Hat Satellite 6.5 and 6.6:
# foreman-rake console

User.current = User.anonymous_api_admin
location = ::Location.find_by_title(::Setting[:default_location_puppet_content])

Katello::ContentViewPuppetEnvironment.where(:puppet_environment_id => nil).where("environment_id is not null").each do |cvpe|
  ename = Environment.construct_name(cvpe.organization,
                                   cvpe.environment,
                                   cvpe.content_view)
  env = Environment.where(:name => ename).first
  if env.nil?
    env = Environment.create(:name => ename, :locations => [location], :organizations => [cvpe.content_view.organization])
  end

  env.organizations << cvpe.content_view.organization unless env.organizations.include?(cvpe.content_view.organization)
  location = ::Location.find_by_title(::Setting[:default_location_puppet_content])
  env.locations << location unless env.locations.include?(location)

  cvpe.puppet_environment = env
  cvpe.save!
end

Root Cause

  • Every content view has to have it's puppet environment in order to apply potential puppet classes associated with this content view.
  • The puppet environments should be created automatically for every content view and Satellite relies on their presence.

Diagnostic Steps

  • Upgrade failing with below traceback errors:

    # satellite-installer --scenario satellite --upgrade
    Finished with:
    Upgrade Step: import_puppet_modules (this may take a while) ....
    /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_theme_satellite-0.1.42/app/models/concerns/satellite_packages.rb:4: warning: already initialized constant Katello::Ping::PACKAGES
    /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.0.0.125/app/models/katello/ping.rb:7: warning: previous definition of PACKAGES was here
    rake aborted!
    ActiveRecord::RecordInvalid: Validation failed: Puppet environment can't be blank
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/validations.rb:57:in `save!'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:273:in `block in save!'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:208:in `transaction'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:273:in `save!'
    /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.0.0.125/app/lib/katello/lazy_accessor.rb:87:in `save!'
    /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.0.0.125/app/models/katello/content_view_puppet_environment.rb:94:in `index_content'
    /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.0.0.125/app/models/katello/content_view_puppet_environment.rb:99:in `block in import_all'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/relation/batches.rb:52:in `block (2 levels) in find_each'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/relation/batches.rb:52:in `each'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/relation/batches.rb:52:in `block in find_each'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/relation/batches.rb:125:in `find_in_batches'
    /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/relation/batches.rb:51:in `find_each'
    /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.0.0.125/app/models/katello/content_view_puppet_environment.rb:98:in `import_all'
    /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.0.0.125/lib/katello/tasks/upgrades/2.4/import_puppet_modules.rake:9:in `block (4 levels) in <top (required)>'
    Tasks: TOP => katello:upgrades:2.4:import_puppet_modules
    (See full trace by running task with --trace)
    Importing Puppet Modules
    
    Upgrade step import_puppet_modules failed. Check logs for more information.
    

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments