Class: ForemanAcd::AppConfigurator

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_acd/app_configurator.rb

Overview

application instances configurator

Constant Summary collapse

PROXY_NOT_SUPPORTED_LABELS =
[:not_available, :not_defined].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app_instance) ⇒ AppConfigurator

Returns a new instance of AppConfigurator.



10
11
12
# File 'app/services/foreman_acd/app_configurator.rb', line 10

def initialize(app_instance)
  @app_instance = app_instance
end

Instance Method Details

#configureObject



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/services/foreman_acd/app_configurator.rb', line 14

def configure
  job_input = {}
  job_input['application_name'] = @app_instance.name
  job_input['playbook_id'] = @app_instance.app_definition.ansible_playbook.id
  job_input['playbook_file'] = @app_instance.app_definition.ansible_playbook.playfile

  logger.info("Use inventory to configure #{@app_instance.name} with ansible playbook #{@app_instance.app_definition.ansible_playbook.name}")

  begin
    proxy_hosts = {}
    job = nil
    result = OpenStruct.new

    hosts = @app_instance.foreman_hosts

    # Important: This uses the REX Proxy Selector and not
    #  the AcdProxySelection because the AcdProxySelector
    #  does not find the proxy but only return the given
    #  host. This is required because we want to run the
    #  ansible playbook for a group of hosts on the smart proxy.
    #  So the process need to be:
    #  1. get the proxy which is required to connect to host a,b,c
    #  2. run the job on the proxy
    #  In 2. we need to make sure that REX doesn't try to find the
    #  proxy which is necessary to connect to the proxy
    proxy_selector = RemoteExecutionProxySelector.new
    hosts.each do |h|
      unless h.host
        result.success = false
        result.error = 'App Instance is not deployed'
        return [result, job]
      end
      proxy = proxy_selector.determine_proxy(h.host, 'ACD')
      raise StandardError, 'Proxy without ACD feature' if proxy.nil? || PROXY_NOT_SUPPORTED_LABELS.include?(proxy)
      proxy_hosts[proxy.name] = [] unless proxy_hosts.key?(proxy.name)
      proxy_hosts[proxy.name] << h
      result.success = true
    rescue StandardError => e
      result.success = false
      result.error = "#{e}, Install/Update smart-proxies for ACD"
      return [result, job]
    end

    # TODO: just for testing...
    # proxy_hosts = { Host.first.name => [ Host.first.id] }

    proxy_inventories = {}
    proxy_host_ids =  []
    proxy_hosts.each do |proxy_name, foreman_hosts|
      proxy_inventories[proxy_name] = ForemanAcd::InventoryCreator.new(@app_instance, foreman_hosts).create_inventory

      # Workaround till infrastructure jobs on proxies are possible. See
      #     https://community.theforeman.org/t/infrastructure-roles/22001/33
      #
      # Why do we use 'foreman_hosts.first.host'? REX is 'host-centric'. During
      # job execution, REX will try to find the proxy on which the job should be executed.
      # We throw the first host at REX's feet so that the resolution of the
      # proxy works (... which we have already done above).
      # ACD on the smart proxy side will call the ansible-playbook for ALL hosts
      # which are behind this smart proxy.
      #
      # Additionally, we disable that  AcdProxyProxySelector is used.
      # See required_proxy_selector_for in models/foreman_acd/acd_provider.rb
      proxy_host_ids << foreman_hosts.first.host.id

      # Actually, we want this:
      # proxy_host_ids << Host.find_by(:name => proxy_name).id
    end

    job_input['inventory'] = YAML.dump(proxy_inventories)
    composer = JobInvocationComposer.for_feature(
      :run_acd_ansible_playbook,
      proxy_host_ids,
      job_input.to_hash
    )
    job = composer
  rescue StandardError => e
    result.success = false
    result.error = _('Failed to configure hosts: %{err_msg}' % { :err_msg => e.message })
    logger.error('Failed to configure hosts: %{err_class}: %{err_msg}' % { :err_class => e.class, :err_msg => e.message })
    logger.error(e.backtrace.join("\n"))
    job = nil
  end
  [result, job]
end