Class: ForemanAcd::AppDeployer

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

Overview

application instances deployer

Instance Method Summary collapse

Constructor Details

#initialize(app_instance) ⇒ AppDeployer

Returns a new instance of AppDeployer.



8
9
10
# File 'app/services/foreman_acd/app_deployer.rb', line 8

def initialize(app_instance)
  @app_instance = app_instance
end

Instance Method Details

#deploy(safe_deploy) ⇒ Object



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
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
# File 'app/services/foreman_acd/app_deployer.rb', line 12

def deploy(safe_deploy)
  output = []
  services = JSON.parse(@app_instance.app_definition.services)
  all_hosts = []

  foreman_hosts = if safe_deploy
                    @app_instance.foreman_hosts.find(safe_deploy)
                  else
                    @app_instance.foreman_hosts
                  end

  foreman_hosts.each do |foreman_host|
    service_data = services.find { |k| k['id'] == foreman_host.service.to_i }

    # Handle already deployed hosts
    if foreman_host.existing_host?
      domain = Hostgroup.find(service_data['hostgroup']).domain.name
      fqdn = "#{foreman_host.hostname}.#{domain}"
      h =  Host.find_by(:name => fqdn)
      foreman_host.update!(:host_id => h.id)
      next
    end

    host_params = set_host_params(foreman_host, service_data)
    host = foreman_host.host.presence

    is_rebuild = false

    if host.blank?
      params = host_attributes(host_params)
      log_params = params.dup
      log_params['root_pass'] = '****' if log_params.key?('root_pass')
      msg = "Host creation parameters for #{foreman_host.hostname}:\n#{log_params}\n"
      logger.info(msg)
      output << msg
      host = Host.new(params)
    else
      msg = "Update parameters and re-deploy host #{foreman_host.hostname}"
      logger.info(msg)
      output << msg
      host.attributes = host_attributes(host_params, host)
      is_rebuild = true
    end

    # REMOVE ME (but very nice for testing)
    # prng = Random.new
    # x = prng.rand(100)
    # y = prng.rand(100)
    # host.mac = "00:11:22:33:#{x}:#{y}"

    apply_compute_profile(host)
    host.suggest_default_pxe_loader

    all_hosts << OpenStruct.new(:foreman_host => foreman_host, :host => host, :rebuild => is_rebuild)
  end

  # do this in a second step, so that we get the progress report for all
  all_hosts.each do |os_host|
    # Save the host -> will initiate the deployment
    os_host.host.save!
    msg = "Saved and initiated/updated host #{os_host.foreman_host.hostname}"
    logger.info(msg)
    output << msg

    os_host.host.power.reset if os_host.rebuild

    # save the foreman host id
    os_host.foreman_host.update!(:host_id => os_host.host.id)

    progress_report = Rails.cache.fetch(os_host.host.progress_report_id)
    os_host.foreman_host.update!(:last_progress_report => progress_report)
    if progress_report.empty?
      msg = "Progress report for #{os_host.foreman_host.hostname} is empty!"
      output << msg
    end
  end

  # Try to start the configuration, too. In case of a app instance including only already deployed hosts
  # this would start the configuration job then.
  ForemanAcd.initiate_acd_app_configurator(@app_instance)

  output
end