Class: RemoteExecutionProvider

Inherits:
Object
  • Object
show all
Defined in:
app/models/remote_execution_provider.rb

Direct Known Subclasses

ScriptExecutionProvider

Constant Summary collapse

EFFECTIVE_USER_METHODS =
%w[sudo dzdo su].freeze

Class Method Summary collapse

Class Method Details

.alternative_names(host) ⇒ Object



166
167
168
# File 'app/models/remote_execution_provider.rb', line 166

def alternative_names(host)
  { :fqdn => find_fqdn(effective_interfaces(host)) }
end

.cleanup_working_dirs?(host) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'app/models/remote_execution_provider.rb', line 74

def cleanup_working_dirs?(host)
  setting = host_setting(host, :remote_execution_cleanup_working_dirs)
  [true, 'true', 'True', 'TRUE', '1'].include?(setting)
end

.effective_interfaces(host) ⇒ Object



94
95
96
97
98
99
100
# File 'app/models/remote_execution_provider.rb', line 94

def effective_interfaces(host)
  interfaces = []
  %w(execution primary provision).map do |flag|
    interfaces << host.send(flag + '_interface')
  end
  interfaces.compact.uniq
end

.effective_user(template_invocation) ⇒ Object



61
62
63
# File 'app/models/remote_execution_provider.rb', line 61

def effective_user(template_invocation)
  template_invocation.effective_user
end

.effective_user_method(host) ⇒ Object



65
66
67
68
69
70
71
72
# File 'app/models/remote_execution_provider.rb', line 65

def effective_user_method(host)
  method = host_setting(host, :remote_execution_effective_user_method)
  unless EFFECTIVE_USER_METHODS.include?(method)
    raise _('Effective user method "%{current_value}" is not one of %{valid_methods}') %
      { :current_value => method, :valid_methods => EFFECTIVE_USER_METHODS}
  end
  method
end

.effective_user_password(host) ⇒ Object



90
91
92
# File 'app/models/remote_execution_provider.rb', line 90

def effective_user_password(host)
  host_setting(host, :remote_execution_effective_user_password)
end

.find_fqdn(interfaces) ⇒ Object



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

def find_fqdn(interfaces)
  interfaces.find { |i| i.fqdn.present? }.try(:fqdn)
end

.find_ip(host, interfaces) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/remote_execution_provider.rb', line 108

def find_ip(host, interfaces)
  if host_setting(host, :remote_execution_connect_by_ip)
    ip4_address = interfaces.find { |i| i.ip.present? }.try(:ip)
    ip6_address = interfaces.find { |i| i.ip6.present? }.try(:ip6)

    if host_setting(host, :remote_execution_connect_by_ip_prefer_ipv6)
      ip6_address || ip4_address
    else
      ip4_address || ip6_address
    end
  end
end

.find_ip_or_hostname(host) ⇒ Object



102
103
104
105
106
# File 'app/models/remote_execution_provider.rb', line 102

def find_ip_or_hostname(host)
  interfaces = effective_interfaces host

  find_ip(host, interfaces) || find_fqdn(interfaces) || raise(_('Could not find any suitable interface for execution'))
end

.host_setting(host, setting) ⇒ Object



125
126
127
128
# File 'app/models/remote_execution_provider.rb', line 125

def host_setting(host, setting)
  param_value = host.host_param(setting.to_s)
  param_value.nil? ? Setting[setting] : param_value
end

.humanized_nameObject



50
51
52
# File 'app/models/remote_execution_provider.rb', line 50

def humanized_name
  self.name
end

.provider_for(type) ⇒ Object



6
7
8
# File 'app/models/remote_execution_provider.rb', line 6

def provider_for(type)
  providers[type.to_s] || providers[:script]
end

.provider_input_namespaceObject



54
55
# File 'app/models/remote_execution_provider.rb', line 54

def provider_input_namespace
end

.provider_inputsObject



136
137
138
# File 'app/models/remote_execution_provider.rb', line 136

def provider_inputs
  []
end

.provider_inputs_docObject



140
141
142
# File 'app/models/remote_execution_provider.rb', line 140

def provider_inputs_doc
  {}
end

.provider_namesObject



27
28
29
# File 'app/models/remote_execution_provider.rb', line 27

def provider_names
  providers.keys.map(&:to_s)
end

.provider_proxy_featuresObject



31
32
33
# File 'app/models/remote_execution_provider.rb', line 31

def provider_proxy_features
  providers.values.map(&:proxy_feature).flatten.uniq.compact
end

.providersObject



19
20
21
# File 'app/models/remote_execution_provider.rb', line 19

def providers
  @providers ||= { }.with_indifferent_access
end

.proxy_action_classObject



148
149
150
# File 'app/models/remote_execution_provider.rb', line 148

def proxy_action_class
  'Proxy::RemoteExecution::Ssh::Actions::RunScript'
end

.proxy_batch_sizeObject



152
153
154
# File 'app/models/remote_execution_provider.rb', line 152

def proxy_batch_size
  Setting['foreman_tasks_proxy_batch_size']
end

.proxy_command_options(template_invocation, host) ⇒ Object



35
36
37
38
39
40
# File 'app/models/remote_execution_provider.rb', line 35

def proxy_command_options(template_invocation, host)
  {
    :proxy_operation_name => proxy_operation_name,
    :time_to_pickup => time_to_pickup(template_invocation.job_invocation, host),
  }.merge(proxy_command_provider_inputs(template_invocation))
end

.proxy_command_provider_inputs(template_invocation) ⇒ Object



144
145
146
# File 'app/models/remote_execution_provider.rb', line 144

def proxy_command_provider_inputs(template_invocation)
  {}
end

.proxy_featureObject



15
16
17
# File 'app/models/remote_execution_provider.rb', line 15

def proxy_feature
  registered_name
end

.proxy_operation_nameObject



46
47
48
# File 'app/models/remote_execution_provider.rb', line 46

def proxy_operation_name
  'ssh'
end

.register(key, klass) ⇒ Object



23
24
25
# File 'app/models/remote_execution_provider.rb', line 23

def register(key, klass)
  providers[key.to_sym] = klass
end

.registered_nameObject



10
11
12
13
# File 'app/models/remote_execution_provider.rb', line 10

def registered_name
  klass = self
  ::RemoteExecutionProvider.providers.key(klass)
end

.remote_working_dir(host) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'app/models/remote_execution_provider.rb', line 79

def remote_working_dir(host)
  dir = host_setting(host, :remote_execution_remote_working_dir).to_s.strip
  unless Pathname.new(dir).absolute?
    raise ::Foreman::Exception.new(
      N_('Remote working directory "%{current_value}" is not an absolute path'),
      current_value: dir
    )
  end
  dir
end

.required_proxy_selector_for(template) ⇒ Object

Return a specific proxy selector to use for running a given template Returns either nil to use the default selector or an instance of a (sub)class of ::ForemanTasks::ProxySelector



158
159
160
161
162
163
164
# File 'app/models/remote_execution_provider.rb', line 158

def required_proxy_selector_for(template)
  if template.remote_execution_features
             .where(:proxy_selector_override => ::RemoteExecutionProxySelector::INTERNAL_PROXY)
             .any?
    ::DefaultProxyProxySelector.new
  end
end

.secrets(_host) ⇒ Object



42
43
44
# File 'app/models/remote_execution_provider.rb', line 42

def secrets(_host)
  {}
end

.ssh_key_passphrase(_host) ⇒ Object



133
134
# File 'app/models/remote_execution_provider.rb', line 133

def ssh_key_passphrase(_host)
end

.ssh_password(_host) ⇒ Object



130
131
# File 'app/models/remote_execution_provider.rb', line 130

def ssh_password(_host)
end

.supports_effective_user?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/remote_execution_provider.rb', line 57

def supports_effective_user?
  false
end

.time_to_pickup(job_invocation, host) ⇒ Object



170
171
172
173
# File 'app/models/remote_execution_provider.rb', line 170

def time_to_pickup(job_invocation, host)
  time = job_invocation.time_to_pickup || host_setting(host, 'remote_execution_time_to_pickup')
  Integer(time) if time
end