Skip to content

feat(installation): Add task to remove db migrations#89

Open
nadjaheitmann wants to merge 2 commits into
mainfrom
deinstallation_task
Open

feat(installation): Add task to remove db migrations#89
nadjaheitmann wants to merge 2 commits into
mainfrom
deinstallation_task

Conversation

@nadjaheitmann
Copy link
Copy Markdown
Contributor

Preparation for plugin deinstallation

Refs similar task in ATIX-AG/foreman_resource_quota@37b7c99

@nadjaheitmann nadjaheitmann marked this pull request as draft April 24, 2026 11:49
@nadjaheitmann nadjaheitmann marked this pull request as ready for review April 24, 2026 12:38
@nadjaheitmann nadjaheitmann requested a review from Thorben-D April 27, 2026 13:36
@Thorben-D Thorben-D dismissed a stale review April 27, 2026 15:50

Did not intend to approve

plugin = Foreman::Plugin.find ForemanAnsibleDirector.name.underscore
ActiveRecord::MigrationContext.new(plugin.migrations_paths, ActiveRecord::SchemaMigration).down
end
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will cut it.
Quite a bit more needs to happen for proper cleanup, as Pulp objects also have to be removed.
I see two options:

  • Query all Pulp Ansible Repositories, CollectionRemotes, GitRemotes, and Distributions where the label creator is foreman_ansible_director
  • Iterate over ContentUnitVersions and destroy the associated Pulp objects.

I intend to add a consistency check in general, which will do a mix of both, so this problem may already be solved in the future.

@nadjaheitmann nadjaheitmann force-pushed the deinstallation_task branch from f7e2c60 to ad04db3 Compare May 8, 2026 08:57
Comment on lines +8 to +17
def pulp_proxy_url!
proxy =
::SmartProxy.unscoped.with_features('Ansible_Director')&.find { |smart_proxy| smart_proxy.url.present? } ||
::SmartProxy.unscoped.find { |smart_proxy| smart_proxy.url.present? }
return proxy.url if proxy&.url.present?

raise Foreman::Exception,
'No Smart Proxy with a URL is available for Foreman Ansible Director Pulp cleanup.'
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we want this part of the code in the way it is written here. It misses the organization filtering.

return proxy.url if proxy&.url.present?

raise Foreman::Exception,
'No Smart Proxy with a URL is available for Foreman Ansible Director Pulp cleanup.'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the pulp_proxy_url is not only related to the pulp cleanup. adjust error message.

class << self
def pulp_proxy_url!
proxy =
::SmartProxy.unscoped.with_features('Ansible_Director')&.find { |smart_proxy| smart_proxy.url.present? } ||
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second find should not be necessary.

def pulp3_configuration(config_class)
config_class.new do |config|
uri = URI.parse(::SmartProxy.first.url)
uri = URI.parse(pulp_proxy_url!)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be worth to keep this PR in mind: #95

::ForemanAnsibleDirector::ContentUnitVersion.find_each do |content_unit_version|
::ForemanAnsibleDirector::TaskHelpers.destroy_content_unit_version_pulp_objects(
content_unit_version,
deleted_hrefs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the purpose of this deleted_hrefs?

role_remote: Set.new,
}

::ForemanAnsibleDirector::ContentUnitVersion.find_each do |content_unit_version|
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It need to cleanup the Execution Environments, too.

module TaskHelpers
module_function

def destroy_pulp_object(deleted_hrefs, key, href, action, input_key)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it posible to re-use the introduced labels 1be49d4#diff-bc0ac4dcf2708e3d510050a532b88b8bde8d3e97af27d3b9bde58b62ea777005R13 to find and remove the pulp content / images?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean we could have a call to Pulp which simply removes everything with one specific label?

Copy link
Copy Markdown
Member

@sbernhard sbernhard May 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. this should be possible - for each remote / distribution / publication / etc.


require 'pulpcore_client'
module ForemanAnsibleDirector
module Pulp3
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file being touched at all?
It is an integral requirement of this plugin that Pulp (via Katello) exists on Foreman server.
For this plugin to function at all, the first smart proxy (provisioned on Foreman server) must provide a pulp_ansible API at it's URL.

Use of the Pulp instances of other smart proxies is not and will not be supported.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm unsure if this is a requirement in general or just now that there is a Smart Proxy Ansible Director on the Foreman server. Can you explain why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarification, I'll remove it. I don't think it is necessary for the issue at hand.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't work in the rake context without adjusting the smart proxy urls. I don't completely understand why this is happening but it seems the code is somehow running in another scope during the rake task execution and the proxy does not resolve correctly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned before, there is no guarrantee that the Pulp instance used to store Ansible content has the Ansible_Director feature.
This config will break for all cases where the Smart Proxy provisioned on Foreman Server (id: 1, used to store content) does not have the smart_proxy_ansible_director plugin installed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we need to change this code in a way that it works in rake scope. I will give it another thought.

Comment thread lib/foreman_ansible_director/engine.rb Outdated
engine_name 'foreman_ansible_director'

initializer 'foreman_ansible_director.cleanup_context' do |app|
app.config.x.foreman_ansible_director.running_in_rake = false
Copy link
Copy Markdown
Contributor Author

@nadjaheitmann nadjaheitmann May 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the config.x thing: https://guides.rubyonrails.org/configuring.html#custom-configuration

The idea is that we set a flag here and change it in the Rake task. We can evaluate this in the code and choose a smart proxy accordingly. Turns out this works for the smart proxy base client part but not for the pulp3 base client part.

@nadjaheitmann nadjaheitmann force-pushed the deinstallation_task branch 2 times, most recently from 2fbd6e6 to 7118691 Compare May 13, 2026 12:11
In case we want to remove the plugin, we need clearly defined names for
the settings, so we do not accidently remove some setting from another
plugin or core.
@nadjaheitmann nadjaheitmann force-pushed the deinstallation_task branch from 7118691 to 4a8f44b Compare May 13, 2026 12:12
Preparation for plugin deinstallation
Refs similar task in ATIX-AG/foreman_resource_quota@37b7c99

Also adds task to clean up the created Pulp content.
@nadjaheitmann nadjaheitmann force-pushed the deinstallation_task branch from 4a8f44b to efa13db Compare May 13, 2026 12:13
def pulp3_configuration(config_class)
config_class.new do |config|
uri = URI.parse(::SmartProxy.first.url)
uri = URI.parse(::SmartProxy.unscoped.first.url)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this. But it does not seem to work otherwise, even if I add a Rails flag. Maybe because the task is created in rake env but then scheduled from the Rails application. Not sure what exactly happens here. However, something similar seems to exist in Katello:
https://github.com/Katello/katello/blob/master/app/models/katello/concerns/smart_proxy_extensions.rb#L88

Comment on lines +49 to +61
def ensure_ansible_director_proxy!
proxies = ansible_director_proxies
missing_feature_message =
'No Smart Proxy with the Ansible_Director feature is available for Foreman Ansible Director Pulp cleanup.'
raise Foreman::Exception, missing_feature_message unless records_exist?(proxies)

proxy = proxies.first
return if proxy&.url.present?

missing_url_message =
'No Smart Proxy with a configured URL is available for Foreman Ansible Director Pulp cleanup.'
raise Foreman::Exception, missing_url_message
end
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there is something similar in the proxy base client but I thought it might be a good idea to check on the proxy before even starting a task not when the task is running or whenever this is evaluated.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants