Skip to content

Instantly share code, notes, and snippets.

@jlsherrill
Last active February 24, 2020 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jlsherrill/2dd11592853885643251 to your computer and use it in GitHub Desktop.
Save jlsherrill/2dd11592853885643251 to your computer and use it in GitHub Desktop.
recover from deleted puppet environments

when publishing a content view if you get:

Validation failed: Puppet environment can't be blank

or

ActiveRecord::RecordInvalid: Validation failed: Name has already been taken

Run the following to recover:

# 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|
  env = Environment.where(:name => cvpe.generate_puppet_env_name).first
  if env.nil?
    env = Environment.create(:name => cvpe.generate_puppet_env_name, :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
@jlsherrill
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment