Class: SmartProxy

Inherits:
ApplicationRecord show all
Extended by:
FriendlyId
Includes:
Authorizable, Parameterizable::ByIdName, Taxonomix
Defined in:
app/models/smart_proxy.rb

Defined Under Namespace

Classes: Jail

Constant Summary

Constants included from Taxonomix

Taxonomix::TAXONOMY_JOIN_TABLE

Instance Method Summary collapse

Methods included from Taxonomix

#add_current_location?, #add_current_organization?, #add_current_taxonomy?, #children_of_selected_location_ids, #children_of_selected_organization_ids, #ensure_taxonomies_not_escalated, #set_current_taxonomy, #used_location_ids, #used_or_selected_location_ids, #used_or_selected_organization_ids, #used_organization_ids

Methods included from DirtyAssociations

#reset_dirty_cache_state

Methods included from Authorizable

#authorized?, #check_permissions_after_save

Methods included from PermissionName

#permission_name

Methods inherited from ApplicationRecord

graphql_type, #logger, logger

Methods included from AuditAssociations::AssociationsDefinitions

#audit_associations, #audited, #configure_dirty_associations, #normalize_associations

Instance Method Details

#capabilities(feature) ⇒ Object


97
98
99
# File 'app/models/smart_proxy.rb', line 97

def capabilities(feature)
  smart_proxy_feature_by_name(feature).try(:capabilities)
end

#feature_detailsObject


139
140
141
142
143
# File 'app/models/smart_proxy.rb', line 139

def feature_details
  smart_proxy_features.includes(:feature).each_with_object({}) do |smart_proxy_feature, hash|
    hash[smart_proxy_feature.feature.name] = smart_proxy_feature.details
  end
end

#has_capability?(feature, capability) ⇒ Boolean

Returns:

  • (Boolean)

101
102
103
# File 'app/models/smart_proxy.rb', line 101

def has_capability?(feature, capability)
  capabilities(feature)&.include?(capability.to_s)
end

#has_feature?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)

92
93
94
95
# File 'app/models/smart_proxy.rb', line 92

def has_feature?(feature_name)
  feature_ids = Feature.where(:name => feature_name).pluck(:id)
  smart_proxy_features.any? { |proxy_feature| feature_ids.include?(proxy_feature.feature_id) }
end

#hostnameObject


43
44
45
# File 'app/models/smart_proxy.rb', line 43

def hostname
  URI(url).host
end

#hosts_countObject


51
52
53
# File 'app/models/smart_proxy.rb', line 51

def hosts_count
  Host::Managed.search_for("smart_proxy = #{name}").count
end

#httpboot_http_portObject


109
110
111
# File 'app/models/smart_proxy.rb', line 109

def httpboot_http_port
  setting(:HTTPBoot, 'http_port')
end

#httpboot_http_port!Object


113
114
115
# File 'app/models/smart_proxy.rb', line 113

def httpboot_http_port!
  httpboot_http_port || raise(::Foreman::Exception.new(N_("HTTP boot requires proxy with httpboot feature and http_port exposed setting")))
end

#httpboot_https_portObject


117
118
119
# File 'app/models/smart_proxy.rb', line 117

def httpboot_https_port
  setting(:HTTPBoot, 'https_port')
end

#httpboot_https_port!Object


121
122
123
# File 'app/models/smart_proxy.rb', line 121

def httpboot_https_port!
  httpboot_https_port || raise(::Foreman::Exception.new(N_("HTTPS boot requires proxy with httpboot feature and https_port exposed setting")))
end

#pingObject


61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/smart_proxy.rb', line 61

def ping
  begin
    reply = get_features
    unless reply.is_a?(Hash)
      logger.debug("Invalid response from proxy #{name}: Expected Hash of features, got #{reply}.")
      errors.add(:base, _('An invalid response was received while requesting available features from this proxy'))
    end
  rescue => e
    errors.add(:base, _('Unable to communicate with the proxy: %s') % e)
  end
  !errors.any?
end

#refreshObject


55
56
57
58
59
# File 'app/models/smart_proxy.rb', line 55

def refresh
  statuses.values.each { |status| status.revoke_cache! }
  associate_features
  errors
end

#setting(feature, setting) ⇒ Object


105
106
107
# File 'app/models/smart_proxy.rb', line 105

def setting(feature, setting)
  smart_proxy_feature_by_name(feature).try(:settings).try(:[], setting)
end

#smart_proxy_feature_by_name(feature_name) ⇒ Object


86
87
88
89
90
# File 'app/models/smart_proxy.rb', line 86

def smart_proxy_feature_by_name(feature_name)
  feature_id = Feature.find_by(:name => feature_name).try(:id)
  # loop through the in memory object to work on unsaved objects
  smart_proxy_features.find { |spf| spf.feature_id == feature_id }
end

#statusesObject


125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/smart_proxy.rb', line 125

def statuses
  return @statuses if @statuses
  @statuses = {}
  features.each do |feature|
    name = feature.name.delete(' ')
    if (status = ProxyStatus.find_status_by_humanized_name(name))
      @statuses[name.downcase.to_sym] = status.new(self)
    end
  end
  @statuses[:version] = ProxyStatus::Version.new(self)

  @statuses
end

#taxonomy_foreign_conditionsObject


74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/smart_proxy.rb', line 74

def taxonomy_foreign_conditions
  conditions = {}
  if has_feature?('Puppet') && has_feature?('Puppet CA')
    conditions = "puppet_proxy_id = #{id} OR puppet_ca_proxy_id = #{id}"
  elsif has_feature?('Puppet')
    conditions[:puppet_proxy_id] = id
  elsif has_feature?('Puppet CA')
    conditions[:puppet_ca_proxy_id] = id
  end
  conditions
end

#to_sObject


47
48
49
# File 'app/models/smart_proxy.rb', line 47

def to_s
  hostname
end