Skip to content

Instantly share code, notes, and snippets.

@mccun934
Forked from jlsherrill/deleted_puppet_envs.md
Last active February 1, 2023 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mccun934/8cdeb1a2e23a95eef9b751a54959196c to your computer and use it in GitHub Desktop.
Save mccun934/8cdeb1a2e23a95eef9b751a54959196c to your computer and use it in GitHub Desktop.
recover from deleted puppet environments from 6.5 or later

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|
  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
@metalcated
Copy link

Is this still applicable?

@mccun934
Copy link
Author

mccun934 commented Feb 1, 2023

I'd be skeptical at this point. This is pretty old and Foreman + Katello has come a long way since I wrote this. I'd reach out to the team via https://community.theforeman.org/ to find out if there is something applicable today

@metalcated
Copy link

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