Skip to content

Commit

Permalink
Fixes #15406 - Moved puppet outside hosts helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ShimShtein authored and xprazak2 committed Aug 28, 2019
1 parent 7fa47cf commit 377ff66
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 112 deletions.
141 changes: 141 additions & 0 deletions app/helpers/host_description_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
module HostDescriptionHelper
UI.register_host_description do
multiple_actions_provider :base_multiple_actions
overview_fields_provider :base_status_overview_fields
overview_fields_provider :base_host_overview_fields
overview_buttons_provider :base_host_overview_buttons
title_actions_provider :base_host_title_actions
end

def base_multiple_actions
actions = []
if authorized_for(:controller => :hosts, :action => :edit)
actions.concat [
{ :action => [_('Change Group'), select_multiple_hostgroup_hosts_path], :priority => 100 },
{ :action => [_('Change Environment'), select_multiple_environment_hosts_path], :priority => 200 },
{ :action => [_('Edit Parameters'), multiple_parameters_hosts_path], :priority => 300 },
{ :action => [_('Disable Notifications'), multiple_disable_hosts_path], :priority => 400 },
{ :action => [_('Enable Notifications'), multiple_enable_hosts_path], :priority => 500 },
{ :action => [_('Disassociate Hosts'), multiple_disassociate_hosts_path], :priority => 600 },
{ :action => [_('Rebuild Config'), rebuild_config_hosts_path], :priority => 700 },
]
actions << { :action => [_('Build Hosts'), multiple_build_hosts_path], :priority => 110 } if SETTINGS[:unattended]
actions << { :action => [_('Assign Organization'), select_multiple_organization_hosts_path], :priority => 800 }
actions << { :action => [_('Assign Location'), select_multiple_location_hosts_path], :priority => 900 }
actions << { :action => [_('Change Owner'), select_multiple_owner_hosts_path], :priority => 1000 } if SETTINGS[:login]
end
actions << { :action => [_('Change Power State'), select_multiple_power_state_hosts_path], :priority => 1100 } if authorized_for(:controller => :hosts, :action => :power)
actions << { :action => [_('Delete Hosts'), multiple_destroy_hosts_path], :priority => 1200 } if authorized_for(:controller => :hosts, :action => :destroy)
actions
end

def base_status_overview_fields(host)
global_status = host.build_global_status
fields = [
{
:field => [
_("Status"),
content_tag(:span, ''.html_safe, :class => host_global_status_icon_class(global_status.status)) +
content_tag(:span, _(global_status.to_label), :class => host_global_status_class(global_status.status)),
],
:priority => 10,
},
]
fields += host_detailed_status_list(host)

fields
end

def host_detailed_status_list(host)
priority = 10
host.host_statuses.sort_by(&:type).map do |status|
next unless status.relevant? && !status.substatus?
{ :field => [
_(status.name),
content_tag(:span, ' '.html_safe, :class => host_global_status_icon_class(status.to_global)) +
content_tag(:span, _(status.to_label), :class => host_global_status_class(status.to_global)),
], :priority => priority += 1 }
end.compact
end

def base_host_overview_fields(host)
fields = []
fields << { :field => [_("Build duration"), build_duration(host)], :priority => 90 }
fields << { :field => [_("Build errors"), link_to("Logs from OS installer", build_errors_host_path(:id => host.id))], :priority => 91 } if host.build_errors.present?
fields << { :field => [_("Token"), host.token || _("N/A")], :priority => 92 } if User.current.admin
fields << { :field => [_("Domain"), link_to(host.domain, hosts_path(:search => "domain = #{host.domain}"))], :priority => 100 } if host.domain.present?
fields << { :field => [_("Realm"), link_to(host.realm, hosts_path(:search => "realm = #{host.realm}"))], :priority => 200 } if host.realm.present?
fields << { :field => [_("IP Address"), host.ip], :priority => 300 } if host.ip.present?
fields << { :field => [_("IPv6 Address"), host.ip6], :priority => 400 } if host.ip6.present?
fields << { :field => [_("Comment"), host.comment], :priority => 500 } if host.comment.present?
fields << { :field => [_("MAC Address"), host.mac], :priority => 600 } if host.mac.present?
fields << { :field => [_("Architecture"), link_to(host.arch, hosts_path(:search => "architecture = #{host.arch}"))], :priority => 700 } if host.arch.present?
fields << { :field => [_("Operating System"), link_to(host.operatingsystem.to_label, hosts_path(:search => "os_description = #{host.operatingsystem.description}"))], :priority => 800 } if host.operatingsystem.present?
fields << { :field => [_("PXE Loader"), host.pxe_loader], :priority => 900 } if host.operatingsystem.present? && !host.image_build?
fields << { :field => [_("Host group"), link_to(host.hostgroup, hosts_path(:search => %{hostgroup_title = "#{host.hostgroup}"}))], :priority => 1000 } if host.hostgroup.present?
fields << { :field => [_("Boot time"), (boot_time = host&.reported_data&.boot_time) ? date_time_relative(boot_time) : _('Not reported')], :priority => 1100 }
fields << { :field => [_("Location"), link_to(host.location.title, hosts_path(:search => "location = #{host.location}"))], :priority => 1200 }
fields << { :field => [_("Organization"), link_to(host.organization.title, hosts_path(:search => "organization = #{host.organization}"))], :priority => 1300 }
if host.owner_type == _("User")
fields << { :field => [_("Owner"), (link_to(host.owner, hosts_path(:search => %{user.login = "#{host.owner.login}"})) if host.owner)], :priority => 1400 }
else
fields << { :field => [_("Owner"), host.owner], :priority => 1400 }
end
fields << { :field => [_("Certificate Name"), host.certname], :priority => 1500 } if Setting[:use_uuid_for_certificates]
fields
end

def base_host_title_actions(host)
[
{
:action => button_group(
link_to_if_authorized(_("Edit"), hash_for_edit_host_path(:id => host).merge(:auth_object => host),
:title => _("Edit this host"), :id => "edit-button", :class => 'btn btn-default'),
display_link_if_authorized(_("Clone"), hash_for_clone_host_path(:id => host).merge(:auth_object => host, :permission => 'create_hosts'),
:title => _("Clone this host"), :id => "clone-button", :class => 'btn btn-default'),
if host.build
link_to_if_authorized(_("Cancel build"), hash_for_cancelBuild_host_path(:id => host).merge(:auth_object => host, :permission => 'build_hosts'),
:disabled => host.can_be_built?,
:title => _("Cancel build request for this host"), :id => "cancel-build-button", :class => 'btn btn-default')
else
link_to_if_authorized(_("Build"), hash_for_host_path(:id => host).merge(:auth_object => host, :permission => 'build_hosts', :anchor => "review_before_build"),
:disabled => !host.can_be_built?,
:title => _("Enable rebuild on next host boot"),
:class => "btn btn-default",
:id => "build-review",
:data => { :toggle => 'modal',
:target => '#review_before_build',
:url => review_before_build_host_path(:id => host),
})
end
),
:priority => 100 },
if host.supports_power?
{
:action => button_group(
link_to(_("Loading power state ..."), '#', :disabled => true, :class => 'btn btn-default', :id => :loading_power_state)
),
:priority => 200,
}
end,
{
:action => button_group(
link_to_if_authorized(_("Delete"), hash_for_host_path(:id => host).merge(:auth_object => host, :permission => 'destroy_hosts'),
:class => "btn btn-danger",
:id => "delete-button",
:data => { :message => delete_host_dialog(host) },
:method => :delete)
),
:priority => 300,
},
].compact
end

def base_host_overview_buttons(host)
[
{ :button => link_to_if_authorized(_("Audits"), hash_for_host_audits_path(:host_id => @host), :title => _("Host audit entries"), :class => 'btn btn-default'), :priority => 100 },
({ :button => link_to_if_authorized(_("Facts"), hash_for_host_facts_path(:host_id => host), :title => _("Browse host facts"), :class => 'btn btn-default'), :priority => 200 } if host.fact_values.any?),
({ :button => link_to_if_authorized(_("Reports"), hash_for_host_config_reports_path(:host_id => host), :title => _("Browse host config management reports"), :class => 'btn btn-default'), :priority => 300 } if host.reports.any?),
].compact
end
end
135 changes: 24 additions & 111 deletions app/helpers/hosts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,10 @@ def searching?

def multiple_actions
actions = []
if authorized_for(:controller => :hosts, :action => :edit)
actions.concat [
[_('Change Group'), select_multiple_hostgroup_hosts_path],
[_('Change Environment'), select_multiple_environment_hosts_path],
[_('Edit Parameters'), multiple_parameters_hosts_path],
[_('Disable Notifications'), multiple_disable_hosts_path],
[_('Enable Notifications'), multiple_enable_hosts_path],
[_('Disassociate Hosts'), multiple_disassociate_hosts_path],
[_('Rebuild Config'), rebuild_config_hosts_path],
]
actions.insert(1, [_('Build Hosts'), multiple_build_hosts_path]) if SETTINGS[:unattended]
actions << [_('Assign Organization'), select_multiple_organization_hosts_path]
actions << [_('Assign Location'), select_multiple_location_hosts_path]
actions << [_('Change Owner'), select_multiple_owner_hosts_path]
actions << [_('Change Puppet Master'), select_multiple_puppet_proxy_hosts_path] if SmartProxy.unscoped.authorized.with_features("Puppet").exists?
actions << [_('Change Puppet CA'), select_multiple_puppet_ca_proxy_hosts_path] if SmartProxy.unscoped.authorized.with_features("Puppet CA").exists?
UI::HostDescription.reduce_providers(:multiple_actions).each do |provider|
actions += send(provider)
end
actions << [_('Run Puppet'), multiple_puppetrun_hosts_path] if Setting[:puppetrun] && authorized_for(:controller => :hosts, :action => :puppetrun)
actions << [_('Change Power State'), select_multiple_power_state_hosts_path] if authorized_for(:controller => :hosts, :action => :power)
actions << [_('Delete Hosts'), multiple_destroy_hosts_path] if authorized_for(:controller => :hosts, :action => :destroy)
actions
prioritized_members(actions, :action)
end

def multiple_actions_select
Expand Down Expand Up @@ -253,50 +236,11 @@ def build_duration(host)
end

def overview_fields(host)
global_status = host.build_global_status
fields = [
[
_("Status"),
content_tag(:span, ''.html_safe, :class => host_global_status_icon_class(global_status.status)) +
content_tag(:span, _(global_status.to_label), :class => host_global_status_class(global_status.status)),
],
]
fields += host_detailed_status_list(host)
fields += [[_("Build duration"), build_duration(host)]]
fields += [[_("Build errors"), link_to("Logs from OS installer", build_errors_host_path(:id => host.id))]] if host.build_errors.present?
fields += [[_("Token"), host.token || _("N/A")]] if User.current.admin?
fields += [[_("Domain"), link_to(host.domain, hosts_path(:search => %{domain = "#{host.domain}"}))]] if host.domain.present?
fields += [[_("Realm"), link_to(host.realm, hosts_path(:search => %{realm = "#{host.realm}"}))]] if host.realm.present?
fields += [[_("IP Address"), host.ip]] if host.ip.present?
fields += [[_("IPv6 Address"), host.ip6]] if host.ip6.present?
fields += [[_("Comment"), host.comment]] if host.comment.present?
fields += [[_("MAC Address"), host.mac]] if host.mac.present?
fields += [[_("Puppet Environment"), link_to(host.environment, hosts_path(:search => %{environment = "#{host.environment}"}))]] if host.environment.present?
fields += [[_("Architecture"), link_to(host.arch, hosts_path(:search => %{architecture = "#{host.arch}"}))]] if host.arch.present?
fields += [[_("Operating System"), link_to(host.operatingsystem.to_label, hosts_path(:search => %{os_title = "#{host.operatingsystem.title}"}))]] if host.operatingsystem.present?
fields += [[_("PXE Loader"), host.pxe_loader]] if host.operatingsystem.present? && !host.image_build?
fields += [[_("Host group"), link_to(host.hostgroup, hosts_path(:search => %{hostgroup_title = "#{host.hostgroup}"}))]] if host.hostgroup.present?
fields += [[_("Boot time"), (boot_time = host&.reported_data&.boot_time) ? date_time_relative(boot_time) : _('Not reported')]]
fields += [[_("Location"), (link_to(host.location.title, hosts_path(:search => %{location = "#{host.location}"})) if host.location)]]
fields += [[_("Organization"), (link_to(host.organization.title, hosts_path(:search => %{organization = "#{host.organization}"})) if host.organization)]]
if host.owner_type == _("User")
fields += [[_("Owner"), (link_to(host.owner, hosts_path(:search => %{user.login = "#{host.owner.login}"})) if host.owner)]]
else
fields += [[_("Owner"), host.owner]]
fields = []
UI::HostDescription.reduce_providers(:overview_fields).each do |provider|
fields += send(provider, host)
end
fields += [[_("Certificate Name"), host.certname]] if Setting[:use_uuid_for_certificates]
fields
end

def host_detailed_status_list(host)
host.host_statuses.sort_by(&:type).map do |status|
next unless status.relevant? && !status.substatus?
[
_(status.name),
content_tag(:span, ' '.html_safe, :class => host_global_status_icon_class(status.to_global)) +
content_tag(:span, _(status.to_label), :class => host_global_status_class(status.to_global)),
]
end.compact
prioritized_members(fields, :field)
end

def possible_images(cr, arch = nil, os = nil)
Expand All @@ -310,49 +254,12 @@ def state(s)
end

def host_title_actions(host)
actions = []
UI::HostDescription.reduce_providers(:title_actions).each do |provider|
actions += send(provider, host)
end
title_actions(
button_group(
link_to_if_authorized(_("Edit"), hash_for_edit_host_path(:id => host).merge(:auth_object => host),
:title => _("Edit this host"), :id => "edit-button", :class => 'btn btn-default'),
display_link_if_authorized(_("Clone"), hash_for_clone_host_path(:id => host).merge(:auth_object => host, :permission => 'create_hosts'),
:title => _("Clone this host"), :id => "clone-button", :class => 'btn btn-default'),
if host.build
link_to_if_authorized(_("Cancel build"), hash_for_cancelBuild_host_path(:id => host).merge(:auth_object => host, :permission => 'build_hosts'),
:disabled => host.can_be_built?,
:title => _("Cancel build request for this host"), :id => "cancel-build-button", :class => 'btn btn-default')
else
link_to_if_authorized(_("Build"), hash_for_host_path(:id => host).merge(:auth_object => host, :permission => 'build_hosts', :anchor => "review_before_build"),
:disabled => !host.can_be_built?,
:title => _("Enable rebuild on next host boot"),
:class => "btn btn-default",
:id => "build-review",
:data => { :toggle => 'modal',
:target => '#review_before_build',
:url => review_before_build_host_path(:id => host),
}
)
end
),
if host.supports_power?
button_group(
link_to(_("Loading power state ..."), '#', :disabled => true, :class => 'btn btn-default', :id => :loading_power_state)
)
end,
button_group(
if host.try(:puppet_proxy)
link_to_if_authorized(_("Run puppet"), hash_for_puppetrun_host_path(:id => host).merge(:auth_object => host, :permission => 'puppetrun_hosts'),
:disabled => !Setting[:puppetrun],
:class => 'btn btn-default',
:title => _("Trigger a puppetrun on a node; requires that puppet run is enabled"))
end
),
button_group(
link_to_if_authorized(_("Delete"), hash_for_host_path(:id => host).merge(:auth_object => host, :permission => 'destroy_hosts'),
:class => "btn btn-danger",
:id => "delete-button",
:data => { :message => delete_host_dialog(host) },
:method => :delete)
)
prioritized_members(actions, :action)
)
end

Expand Down Expand Up @@ -393,12 +300,12 @@ def args_for_compute_resource_partial(host)
end

def show_appropriate_host_buttons(host)
[
link_to_if_authorized(_("Audits"), hash_for_host_audits_path(:host_id => @host), :title => _("Host audit entries"), :class => 'btn btn-default'),
(link_to_if_authorized(_("Facts"), hash_for_host_facts_path(:host_id => host), :title => _("Browse host facts"), :class => 'btn btn-default') if host.fact_values.any?),
(link_to_if_authorized(_("Reports"), hash_for_host_config_reports_path(:host_id => host), :title => _("Browse host config management reports"), :class => 'btn btn-default') if host.reports.any?),
(link_to(_("YAML"), externalNodes_host_path(:name => host), :title => _("Puppet external nodes YAML dump"), :class => 'btn btn-default') if SmartProxy.with_features("Puppet").any?),
].compact
priority_buttons = []
UI::HostDescription.reduce_providers(:overview_buttons).each do |provider|
priority_buttons += send(provider, host)
end

prioritized_members(priority_buttons, :button)
end

def allocation_text_f(f)
Expand Down Expand Up @@ -505,4 +412,10 @@ def power_status_visible?
def host_breadcrumb
breadcrumbs(resource_url: "/api/v2/hosts?thin=true'")
end

def prioritized_members(list, value_key)
list.
sort_by { |member| member[:priority] }.
map { |member_hash| member_hash[value_key] }
end
end
Loading

0 comments on commit 377ff66

Please sign in to comment.