Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37395 - New Hosts page perform build actions #10146

Merged
merged 1 commit into from
Jun 6, 2024

Conversation

parthaa
Copy link
Contributor

@parthaa parthaa commented Apr 29, 2024

Added build actions to work with the new hosts page.

image

Steps to verify

  1. Have a foreman with multiple hosts
  2. Check out this PR
  3. go to http:///new/hosts
  4. Select 1 host and choose Build Hosts from the kebab
  5. Play with different options

app/controllers/hosts_controller.rb Outdated Show resolved Hide resolved
@@ -533,20 +548,39 @@ def submit_multiple_build
message = _('Failed to redeploy %s.') % host
Foreman::Logging.exception(message, error)
success = false
message = _('Failed to reboot %s.') % @host
warning(message)
Foreman::Logging.exception(message, error)
Copy link
Member

Choose a reason for hiding this comment

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

Logging 2 messages for 1 exception feels redundant. Was this intentional?

@ianballou
Copy link
Contributor

Trying to re-run redmine issues seems to not be working.

@parthaa parthaa force-pushed the build-host branch 2 times, most recently from 1f9bea4 to 5cc9598 Compare May 7, 2024 20:28
manager = BulkHostsManager.new(hosts: @hosts)
missed_hosts = manager.build(reboot: reboot)
if missed_hosts.empty?
process_response(true, { :message => _("Built %{host_count} %{hosts}") % { :host_count => @hosts.count, :hosts => 'host'.pluralize(@hosts.count) }})
Copy link
Member

Choose a reason for hiding this comment

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

This breaks translations because host or hosts will not be correctly translated. There are also languages which have a more complex plural system and n_() is the correct function to use. See https://projects.theforeman.org/projects/foreman/wiki/Translating#Extracting-strings as well:

Suggested change
process_response(true, { :message => _("Built %{host_count} %{hosts}") % { :host_count => @hosts.count, :hosts => 'host'.pluralize(@hosts.count) }})
process_response(true, { :message => n_("Built %s host", "Built %s hosts", @hosts.count) % @hosts.count })

I don't know if @hosts.count will be cached or result in 2 queries, but the previous code already had that too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

if missed_hosts.empty?
process_response(true, { :message => _("Built %{host_count} %{hosts}") % { :host_count => @hosts.count, :hosts => 'host'.pluralize(@hosts.count) }})
else
process_response(false, { :message => _("The following hosts failed the build operation: %s") % missed_hosts.map(&:name).to_sentence})
Copy link
Member

Choose a reason for hiding this comment

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

Does to_sentence actually work with translations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we are not doing to_sentence anymore

end

if message.blank?
process_response(true, { :message => _("Rebuilt configuration for %{host_count} %{hosts}") % { :host_count => @hosts.count, :hosts => 'host'.pluralize(@hosts.count) }})
Copy link
Member

Choose a reason for hiding this comment

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

Same problem as above, so please use n_()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Copy link
Contributor

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

Thanks for re-implementing again @parthaa

I like the BulkHostsManager approach muuuuuch better than the previous tries! 🚀

A few code comments here; I'll retest once ewoud's and my comments are addressed

app/controllers/api/v2/hosts_bulk_actions_controller.rb Outdated Show resolved Hide resolved
app/controllers/hosts_controller.rb Show resolved Hide resolved
app/services/bulk_hosts_manager.rb Outdated Show resolved Hide resolved
app/services/bulk_hosts_manager.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

Just a couple more minor comments :)

Comment on lines 17 to 19
handleSuccess: response => {
if (handleSuccess) handleSuccess(response);
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason this won't work?

Suggested change
handleSuccess: response => {
if (handleSuccess) handleSuccess(response);
},
handleSuccess,

APIRequest.js already handles it for us if you don't pass handleSuccess: https://github.com/theforeman/foreman/blob/develop/webpack/assets/javascripts/react_app/redux/API/APIRequest.js#L20

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

onClick={setModalOpen}
isDisabled={selectedCount === 0}
>
{__('Build Hosts')}
Copy link
Contributor

Choose a reason for hiding this comment

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

As per the mockup:

Suggested change
{__('Build Hosts')}
{__('Build management')}

(and don't forget sentence case, not title case)

successToast: () => {
if (params.rebuild_configuration)
return __('Rebuilt the configuration for hosts.');
return __('Built hosts.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be helpful to have different wording here if you checked reboot?
maybe

Hosts set to build and rebooting.

message = ''
all_fails.each_pair do |key, values|
unless values.empty?
message << ((N_("%{config_type} rebuild failed for host: %{host_names}.",
Copy link
Member

Choose a reason for hiding this comment

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

N_() is for "mark to translate" while n_() is plural form

Suggested change
message << ((N_("%{config_type} rebuild failed for host: %{host_names}.",
message << ((n_("%{config_type} rebuild failed for host: %{host_names}.",

Copy link
Contributor

Choose a reason for hiding this comment

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

@evgeni and I were talking about this the other day and looking at https://projects.theforeman.org/projects/foreman/wiki/Translating, and it was unclear which one is correct for "I want to use the percent thingies, but not necessarily pluralize any words." The examples that use %s all use N_().

end

if message.blank?
process_response(true, { :message => N_("Rebuilt configuration for %{host_count} %{hosts}") % { :host_count => @hosts.count, :hosts => 'host'.pluralize(@hosts.count) }})
Copy link
Member

Choose a reason for hiding this comment

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

Please also correct the translations here:

Suggested change
process_response(true, { :message => N_("Rebuilt configuration for %{host_count} %{hosts}") % { :host_count => @hosts.count, :hosts => 'host'.pluralize(@hosts.count) }})
process_response(true, { :message => n_("Rebuilt configuration for %s host", "Rebuilt configuration for %s hosts, @hosts.count) % @hosts.count })

@parthaa
Copy link
Contributor Author

parthaa commented May 28, 2024

Should be addressed. Also added some toast magic for handling failed hosts

image_720

When you click on the failed hosts you get

image_720

@parthaa
Copy link
Contributor Author

parthaa commented May 28, 2024

To fail I changed => bulk_hosts_manager with something like

      begin
        host.built(false) if host.build? && host.token_expired?
        host.setBuild
        host.power.reset if reboot && host.supports_power_and_running?
      raise "Life ...." if host.id.even?
      rescue => error

jeremylenz
jeremylenz previously approved these changes May 29, 2024
Copy link
Contributor

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

LGTM, tested again and works well 👍

APGHA

Comment on lines 89 to 92
failed_host_ids = []
all_fails.each_pair do |key, values|
failed_host_ids.concat(values.map(&:id)) unless values.empty?
end
Copy link
Member

Choose a reason for hiding this comment

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

Ruby 2.7 introduced flat_map which can greatly simplify this code:

Suggested change
failed_host_ids = []
all_fails.each_pair do |key, values|
failed_host_ids.concat(values.map(&:id)) unless values.empty?
end
failed_host_ids = all_fails.flat_map { |_key, value| values.map(&:id) }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated. @ekohl also let me know if you are ok with the changes. Trying to get this merged.

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

Not a complete review, but I think at least the translation part (that I focused on) is correct now. However, I am concerned about the lack of tests for this.

failed_host_ids.compact!
failed_host_ids.uniq!

if failed_host_ids.blank?
Copy link
Member

Choose a reason for hiding this comment

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

In normal Ruby blank? is not defined. Is that a rails extension? I'd still prefer the native empty?:

Suggested change
if failed_host_ids.blank?
if failed_host_ids.empty?

Copy link
Contributor

Choose a reason for hiding this comment

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

blank is from Rails and seems more useful to me, since it handles more than .empty. It's used all over Foreman so I think it's fine here.

Copy link
Contributor Author

@parthaa parthaa Jun 4, 2024

Choose a reason for hiding this comment

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

I prefer blank? over .empty? to handle nil cases but that being said in this case if failed_host_ids are nil we have other problems. So going to commit this change

app/services/bulk_hosts_manager.rb Outdated Show resolved Hide resolved
@parthaa parthaa force-pushed the build-host branch 2 times, most recently from 39eaac7 to 29dc337 Compare June 4, 2024 20:35
@parthaa parthaa requested a review from ekohl June 5, 2024 14:16
ekohl
ekohl previously approved these changes Jun 6, 2024
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

Not a complete review: I focused on the translation part and that now looks correct. I'll leave the JS and other parts to people who know that stuff.

Refs #37395 - Made the hosts controller handle both json and html
Refs #37395 - Fails correctly with url
Refs #37395 - Moved error notifications to a separate file
Copy link
Contributor

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

Per offline discussion: ACK so this can be merged now and it will unblock Samir. Please add test coverage in a followup PR 👍

thanks @parthaa !

@jeremylenz jeremylenz merged commit 0057f0a into theforeman:develop Jun 6, 2024
67 checks passed
@parthaa parthaa deleted the build-host branch June 6, 2024 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants