Module: ForemanRemoteExecution::HostExtensions

Defined in:
app/models/concerns/foreman_remote_execution/host_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 3

def self.prepended(base)
  base.instance_eval do
    has_many :targeting_hosts, :dependent => :destroy, :foreign_key => 'host_id'
    has_many :template_invocations, :dependent => :destroy, :foreign_key => 'host_id'
    has_one :execution_status_object, :class_name => 'HostStatus::ExecutionStatus', :foreign_key => 'host_id', :dependent => :destroy
    has_many :run_host_job_tasks, :through => :template_invocations
    has_many :host_proxy_invocations, :foreign_key => 'host_id', :dependent => :destroy
    has_many :executed_through_proxies, :through => :host_proxy_invocations, :source => 'smart_proxy'

    scoped_search :relation => :run_host_job_tasks, :on => :result, :rename => 'job_invocation.result',
      :ext_method => :search_by_job_invocation,
      :only_explicit => true,
      :complete_value => TemplateInvocation::TaskResultMap::REVERSE_MAP

    scoped_search :relation => :template_invocations, :on => :job_invocation_id,
      :rename => 'job_invocation.id', :only_explicit => true, :ext_method => :search_by_job_invocation

    scoped_search :relation => :execution_status_object, :on => :status, :rename => :execution_status,
      :complete_value => { :ok => HostStatus::ExecutionStatus::OK, :error => HostStatus::ExecutionStatus::ERROR }

    scope :execution_scope, lambda {
      if User.current&.can?('execute_jobs_on_infrastructure_hosts')
        self
      else
        search_for('not (infrastructure_facet.foreman = true or set? infrastructure_facet.smart_proxy_id)')
      end
    }

    def search_by_job_invocation(key, operator, value)
      if key == 'job_invocation.result'
        operator = operator == '=' ? 'IN' : 'NOT IN'
        value = TemplateInvocation::TaskResultMap.status_to_task_result(value)
      end

      mapping = {
        'job_invocation.id'     => %(#{TemplateInvocation.table_name}.job_invocation_id #{operator} ?),
        'job_invocation.result' => %(#{ForemanTasks::Task.table_name}.result #{operator} (?)),
      }
      {
        :conditions => sanitize_sql_for_conditions([mapping[key], value_to_sql(operator, value)]),
        :joins => { :template_invocations => [:run_host_job_task] },
      }
    end
  end
end

Instance Method Details

#becomes(*args) ⇒ Object



112
113
114
115
116
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 112

def becomes(*args)
  became = super(*args)
  became.drop_execution_interface_cache if became.respond_to? :drop_execution_interface_cache
  became
end

#clear_host_parameters_cache!Object



123
124
125
126
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 123

def clear_host_parameters_cache!
  super
  @cached_rex_host_params_hash = nil
end

#cockpit_urlObject



49
50
51
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 49

def cockpit_url
  SSHExecutionProvider.cockpit_url_for_host(self.name)
end

#drop_execution_interface_cacheObject



108
109
110
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 108

def drop_execution_interface_cache
  @execution_interface = nil
end

#execution_interfaceObject

rubocop:enable Naming/MemoizedInstanceVariableName



67
68
69
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 67

def execution_interface
  get_interface_by_flag(:execution)
end

#execution_status(options = {}) ⇒ Object



53
54
55
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 53

def execution_status(options = {})
  @execution_status ||= get_status(HostStatus::ExecutionStatus).to_status(options)
end

#execution_status_label(options = {}) ⇒ Object



57
58
59
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 57

def execution_status_label(options = {})
  @execution_status_label ||= get_status(HostStatus::ExecutionStatus).to_label(options)
end

#host_params_hashObject

rubocop:disable Naming/MemoizedInstanceVariableName



62
63
64
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 62

def host_params_hash
  @cached_rex_host_params_hash ||= extend_host_params_hash(super)
end

#infrastructure_host?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 128

def infrastructure_host?
  infrastructure_facet&.foreman_instance || infrastructure_facet&.smart_proxy_id
end

#reload(*args) ⇒ Object



118
119
120
121
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 118

def reload(*args)
  drop_execution_interface_cache
  super(*args)
end

#remote_execution_proxies(provider, authorized = true) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 82

def remote_execution_proxies(provider, authorized = true)
  proxies = {}
  proxies[:subnet] = []
  proxies[:subnet] += execution_interface.subnet6.remote_execution_proxies.with_features(provider) if execution_interface&.subnet6
  proxies[:subnet] += execution_interface.subnet.remote_execution_proxies.with_features(provider) if execution_interface&.subnet
  proxies[:subnet].uniq!
  proxies[:fallback] = smart_proxies.with_features(provider) if Setting[:remote_execution_fallback_proxy]

  if Setting[:remote_execution_global_proxy]
    proxy_scope = if User.current.present?
                    ::SmartProxy.with_taxonomy_scope_override(location, organization)
                  else
                    ::SmartProxy.unscoped
                  end

    proxy_scope = proxy_scope.authorized if authorized
    proxies[:global] = proxy_scope.with_features(provider)
  end

  proxies
end

#remote_execution_ssh_keysObject



104
105
106
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 104

def remote_execution_ssh_keys
  remote_execution_proxies(%w(SSH Script), false).values.flatten.uniq.map { |proxy| proxy.pubkey }.compact.uniq
end

#set_execution_interface(identifier) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'app/models/concerns/foreman_remote_execution/host_extensions.rb', line 71

def set_execution_interface(identifier)
  if interfaces.find_by(identifier: identifier).nil?
    msg = (N_("Interface with the '%s' identifier was specified as a remote execution interface, however the interface was not found on the host. If the interface exists, it needs to be created in Foreman during the registration.") % identifier)
    raise ActiveRecord::RecordNotFound, msg
  end

  # Only one interface at time can be used for REX, all other must be set to false
  interfaces.each { |int| int.execution = (int.identifier == identifier) }
  interfaces.each(&:save!)
end