foreman_acd 0.0.6 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +107 -56
  3. data/app/controllers/foreman_acd/ansible_playbooks_controller.rb +124 -0
  4. data/app/controllers/foreman_acd/api/v2/ansible_playbooks_controller.rb +72 -0
  5. data/app/controllers/foreman_acd/api/v2/app_definitions_controller.rb +2 -2
  6. data/app/controllers/foreman_acd/api/v2/app_instances_controller.rb +62 -0
  7. data/app/controllers/foreman_acd/app_definitions_controller.rb +46 -9
  8. data/app/controllers/foreman_acd/app_instances_controller.rb +96 -90
  9. data/app/controllers/foreman_acd/concerns/ansible_playbook_parameters.rb +23 -0
  10. data/app/controllers/foreman_acd/concerns/app_definition_parameters.rb +1 -1
  11. data/app/controllers/foreman_acd/concerns/app_instance_parameters.rb +1 -1
  12. data/app/controllers/foreman_acd/remote_execution_controller.rb +46 -0
  13. data/app/controllers/ui_acd_controller.rb +20 -4
  14. data/app/lib/actions/foreman_acd/deploy_all_hosts.rb +42 -0
  15. data/app/lib/actions/foreman_acd/run_configurator.rb +41 -0
  16. data/app/models/concerns/foreman_acd/host_managed_extensions.rb +51 -0
  17. data/app/models/foreman_acd/acd_provider.rb +30 -0
  18. data/app/models/foreman_acd/ansible_playbook.rb +67 -0
  19. data/app/models/foreman_acd/app_definition.rb +25 -1
  20. data/app/models/foreman_acd/app_instance.rb +42 -0
  21. data/app/models/foreman_acd/foreman_host.rb +23 -0
  22. data/app/models/foreman_acd/taxonomy_extensions.rb +17 -0
  23. data/app/services/foreman_acd/app_configurator.rb +54 -0
  24. data/app/services/foreman_acd/app_deployer.rb +151 -0
  25. data/app/services/foreman_acd/inventory_creator.rb +54 -0
  26. data/app/views/foreman_acd/ansible_playbooks/_form.html.erb +30 -0
  27. data/app/views/foreman_acd/ansible_playbooks/edit.html.erb +11 -0
  28. data/app/views/foreman_acd/ansible_playbooks/index.html.erb +30 -0
  29. data/app/views/foreman_acd/ansible_playbooks/new.html.erb +3 -0
  30. data/app/views/foreman_acd/api/v2/ansible_playbooks/base.json.rabl +5 -0
  31. data/app/views/foreman_acd/api/v2/ansible_playbooks/index.json.rabl +5 -0
  32. data/app/views/foreman_acd/api/v2/ansible_playbooks/show.json.rabl +9 -0
  33. data/app/views/foreman_acd/api/v2/app_definitions/base.json.rabl +5 -0
  34. data/app/views/foreman_acd/api/v2/app_definitions/index.json.rabl +5 -0
  35. data/app/views/foreman_acd/api/v2/app_definitions/show.json.rabl +9 -0
  36. data/app/views/foreman_acd/api/v2/app_instances/base.json.rabl +5 -0
  37. data/app/views/foreman_acd/api/v2/app_instances/index.json.rabl +5 -0
  38. data/app/views/foreman_acd/api/v2/app_instances/show.json.rabl +5 -0
  39. data/app/views/foreman_acd/app_definitions/_form.html.erb +34 -20
  40. data/app/views/foreman_acd/app_definitions/edit.html.erb +10 -0
  41. data/app/views/foreman_acd/app_definitions/import.html.erb +18 -0
  42. data/app/views/foreman_acd/app_definitions/index.html.erb +10 -6
  43. data/app/views/foreman_acd/app_instances/_form.html.erb +12 -10
  44. data/app/views/foreman_acd/app_instances/edit.html.erb +10 -0
  45. data/app/views/foreman_acd/app_instances/index.html.erb +87 -9
  46. data/app/views/foreman_acd/app_instances/report.html.erb +22 -0
  47. data/app/views/templates/job/run_acd_ansible_playbook.erb +62 -0
  48. data/app/views/ui_acd/ansible_data.json.rabl +6 -0
  49. data/app/views/ui_acd/app.json.rabl +6 -2
  50. data/app/views/ui_acd/app_definition.json.rabl +1 -1
  51. data/app/views/ui_acd/{fdata.json.rabl → foreman_data.json.rabl} +1 -1
  52. data/config/routes.rb +35 -1
  53. data/db/migrate/20190610202252_create_app_definitions.rb +1 -3
  54. data/db/migrate/20190625140305_create_app_instances.rb +1 -1
  55. data/db/migrate/20200916091018_create_ansible_playbooks.rb +20 -0
  56. data/db/migrate/20200917120220_add_ansible_playbook_id.rb +14 -0
  57. data/db/migrate/20201016002819_add_ansible_vars_all_to_app_definitions.rb +8 -0
  58. data/db/migrate/20201016104338_add_ansible_vars_all_to_app_instances.rb +8 -0
  59. data/db/migrate/20210112111548_add_organization_to_app_instance.rb +22 -0
  60. data/db/migrate/20210112113853_add_location_to_app_instance.rb +8 -0
  61. data/db/migrate/20210202141658_create_foreman_hosts.rb +24 -0
  62. data/db/migrate/20210204111306_remove_hosts_from_app_instances.rb +8 -0
  63. data/db/migrate/20210209091014_rename_acd_tables.rb +16 -0
  64. data/db/migrate/20210216083522_add_last_progress_report.rb +8 -0
  65. data/db/migrate/20210216091529_add_last_deploy_task.rb +8 -0
  66. data/db/seeds.d/62_acd_proxy_feature.rb +4 -0
  67. data/db/seeds.d/75-job_templates.rb +13 -0
  68. data/lib/foreman_acd/engine.rb +21 -3
  69. data/lib/foreman_acd/plugin.rb +112 -20
  70. data/lib/foreman_acd/version.rb +1 -1
  71. data/locale/en/foreman_acd.edit.po +326 -0
  72. data/locale/en/foreman_acd.po +232 -2
  73. data/locale/en/foreman_acd.po.time_stamp +0 -0
  74. data/locale/foreman_acd.pot +343 -8
  75. data/package.json +1 -1
  76. data/test/controllers/ansible_playbooks_controller_test.rb +27 -0
  77. data/webpack/components/ApplicationDefinition/ApplicationDefinition.js +391 -0
  78. data/webpack/components/ApplicationDefinition/ApplicationDefinition.scss +1 -0
  79. data/webpack/components/ApplicationDefinition/ApplicationDefinitionActions.js +303 -0
  80. data/webpack/components/ApplicationDefinition/ApplicationDefinitionConstants.js +15 -0
  81. data/webpack/components/ApplicationDefinition/ApplicationDefinitionHelper.js +26 -0
  82. data/webpack/components/ApplicationDefinition/ApplicationDefinitionReducer.js +269 -0
  83. data/webpack/components/ApplicationDefinition/ApplicationDefinitionSelectors.js +9 -0
  84. data/webpack/components/ApplicationDefinition/components/AnsiblePlaybookSelector.js +49 -0
  85. data/webpack/components/ApplicationDefinition/index.js +35 -0
  86. data/webpack/components/ApplicationInstance/ApplicationInstance.js +451 -0
  87. data/webpack/components/ApplicationInstance/ApplicationInstance.scss +11 -0
  88. data/webpack/components/ApplicationInstance/ApplicationInstanceActions.js +247 -0
  89. data/webpack/components/ApplicationInstance/ApplicationInstanceConstants.js +15 -0
  90. data/webpack/components/ApplicationInstance/ApplicationInstanceReducer.js +309 -0
  91. data/webpack/components/ApplicationInstance/ApplicationInstanceSelectors.js +10 -0
  92. data/webpack/components/ApplicationInstance/components/AppDefinitionSelector.js +49 -0
  93. data/webpack/components/ApplicationInstance/components/Service.js +30 -0
  94. data/webpack/components/ApplicationInstance/components/ServiceCounter.js +37 -0
  95. data/webpack/components/ApplicationInstance/index.js +37 -0
  96. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReport.js +148 -0
  97. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReport.scss +44 -0
  98. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportActions.js +42 -0
  99. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportConstants.js +2 -0
  100. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportReducer.js +38 -0
  101. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportSelectors.js +4 -0
  102. data/webpack/components/ApplicationInstanceReport/components/ReportViewer.js +26 -0
  103. data/webpack/components/ApplicationInstanceReport/index.js +25 -0
  104. data/webpack/components/ParameterSelection/ParameterSelection.js +131 -192
  105. data/webpack/components/ParameterSelection/ParameterSelection.scss +9 -0
  106. data/webpack/components/ParameterSelection/ParameterSelectionActions.js +87 -110
  107. data/webpack/components/ParameterSelection/ParameterSelectionConstants.js +15 -19
  108. data/webpack/components/ParameterSelection/ParameterSelectionHelper.js +3 -35
  109. data/webpack/components/ParameterSelection/ParameterSelectionReducer.js +116 -84
  110. data/webpack/components/ParameterSelection/ParameterSelectionSelectors.js +3 -7
  111. data/webpack/components/ParameterSelection/__fixtures__/parameterSelection.fixtures.js +12 -21
  112. data/webpack/components/ParameterSelection/__fixtures__/parameterSelectionData_1.fixtures.js +1 -1
  113. data/webpack/components/ParameterSelection/__fixtures__/parameterSelectionReducer.fixtures.js +3 -45
  114. data/webpack/components/ParameterSelection/__tests__/ParameterSelection.test.js +20 -0
  115. data/webpack/components/ParameterSelection/__tests__/ParameterSelectionReducer.test.js +22 -46
  116. data/webpack/components/ParameterSelection/__tests__/ParameterSelectionSelectors.test.js +6 -6
  117. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelection.test.js.snap +40 -265
  118. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelectionReducer.test.js.snap +11 -96
  119. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelectionSelectors.test.js.snap +3 -9
  120. data/webpack/components/ParameterSelection/index.js +6 -8
  121. data/webpack/components/common/AddTableEntry.js +30 -0
  122. data/webpack/components/common/DeleteTableEntry.js +39 -0
  123. data/webpack/components/common/EasyHeaderFormatter.js +18 -0
  124. data/webpack/components/common/EditTableEntry.js +50 -0
  125. data/webpack/components/common/ExtSelect.js +43 -0
  126. data/webpack/components/common/LockTableEntry.js +60 -0
  127. data/webpack/components/common/RailsData.js +27 -0
  128. data/webpack/components/common/__tests__/AddTableEntry.test.js +26 -0
  129. data/webpack/components/common/__tests__/DeleteTableEntry.test.js +29 -0
  130. data/webpack/components/common/__tests__/ExtSelect.test.js +38 -0
  131. data/webpack/components/common/__tests__/RailsData.test.js +16 -0
  132. data/webpack/components/common/__tests__/__snapshots__/AddParameter.test.js.snap +35 -0
  133. data/webpack/components/common/__tests__/__snapshots__/AddTableEntry.test.js.snap +35 -0
  134. data/webpack/components/common/__tests__/__snapshots__/DeleteParameter.test.js.snap +41 -0
  135. data/webpack/components/common/__tests__/__snapshots__/DeleteTableEntry.test.js.snap +41 -0
  136. data/webpack/components/common/__tests__/__snapshots__/ExtSelect.test.js.snap +18 -0
  137. data/webpack/components/common/__tests__/__snapshots__/RailsData.test.js.snap +10 -0
  138. data/webpack/helper.js +20 -0
  139. data/webpack/index.js +6 -0
  140. data/webpack/reducer.js +43 -3
  141. metadata +114 -20
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Actions
4
+ module ForemanAcd
5
+ # DeployAllHosts implements a Foreman Task EntryAction
6
+ class DeployAllHosts < Actions::EntryAction
7
+ def plan(app_instance)
8
+ action_subject(app_instance)
9
+ plan_self(:id => app_instance.id)
10
+ end
11
+
12
+ def run
13
+ output[:status] = 'IN PROGRESS'
14
+ app_instance = ::ForemanAcd::AppInstance.find(input.fetch(:id))
15
+
16
+ # Goal: all or nothing
17
+ begin
18
+ ::Foreman::Logging.logger('foreman_acd').info "Start to deploy all hosts of the app #{app_instance}"
19
+ app_deployer = ::ForemanAcd::AppDeployer.new(app_instance)
20
+ output[:data] = app_deployer.deploy
21
+ output[:status] = 'SUCCESS'
22
+ rescue StandardError => e
23
+ ::Foreman::Logging.logger('foreman_acd').error "Error while deploying hosts for application instance. Clean up all other hosts: #{e}"
24
+ app_instance.clean_all_hosts
25
+
26
+ output[:error] = e.to_s
27
+ output[:status] = 'FAILURE'
28
+ end
29
+ end
30
+
31
+ def finalize; end
32
+
33
+ def rescue_strategy
34
+ Dynflow::Action::Rescue::Fail
35
+ end
36
+
37
+ def humanized_name
38
+ _('Deploy application instance hosts')
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Actions
4
+ module ForemanAcd
5
+ # RunConfigurator implements a Foreman Task EntryAction
6
+ class RunConfigurator < Actions::EntryAction
7
+ def plan(app_instance)
8
+ action_subject(app_instance)
9
+ plan_self(:id => app_instance.id)
10
+ end
11
+
12
+ def run
13
+ output[:status] = 'SUCCESS'
14
+ begin
15
+ app_instance = ::ForemanAcd::AppInstance.find(input.fetch(:id))
16
+ app_configurator = ::ForemanAcd::AppConfigurator.new(app_instance)
17
+
18
+ jobs = app_configurator.configure
19
+ ::Foreman::Logging.logger('foreman_acd').info "Creating #{jobs.count} job(s) to configure the app #{app_instance}"
20
+
21
+ # TODO: would it be better to create a sub task for each job we need to run?
22
+ jobs.each(&:trigger)
23
+ rescue StandardError => e
24
+ ::Foreman::Logging.logger('foreman_acd').error "Error while configuring application instance: #{e}"
25
+ output[:error] = e.to_s
26
+ output[:status] = 'FAILURE'
27
+ end
28
+ end
29
+
30
+ def finalize; end
31
+
32
+ def rescue_strategy
33
+ Dynflow::Action::Rescue::Fail
34
+ end
35
+
36
+ def humanized_name
37
+ _('Configure application instance after hosts deployment')
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # Extends the Host Managed
5
+ module HostManagedExtensions
6
+ extend ActiveSupport::Concern
7
+
8
+ RUN_CONFIGURATOR_DELAY = 240
9
+
10
+ def self.prepended(base)
11
+ base.instance_eval do
12
+ before_provision :initiate_acd_app_configurator, :if => :deployed_via_acd?
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def deployed_via_acd?
19
+ find_app_instance_host
20
+ @app_instance_host.present?
21
+ end
22
+
23
+ def find_app_instance_host
24
+ @app_instance_host = ForemanAcd::ForemanHost.find_by(:host_id => id)
25
+ end
26
+
27
+ def initiate_acd_app_configurator
28
+ return false if @app_instance_host.blank?
29
+
30
+ run_it = true
31
+ @app_instance_host.app_instance.foreman_hosts.each do |foreman_host|
32
+ # if there is ONE host, which is still in build phase we don't let the app_configuator run
33
+ next unless foreman_host.host.build?
34
+ ::Foreman::Logging.logger('foreman_acd').info("Another host (#{foreman_host.host.name} is still in build-phase. Wait for it...")
35
+ run_it = false
36
+ break
37
+ end
38
+
39
+ return unless run_it
40
+
41
+ ::Foreman::Logging.logger('foreman_acd').info("All hosts of app (#{@app_instance_host.app_instance.name}) were built. Schedule app configurator...")
42
+ start_acd_app_configurator
43
+ end
44
+
45
+ def start_acd_app_configurator
46
+ ForemanTasks.delay(::Actions::ForemanAcd::RunConfigurator,
47
+ { :start_at => Time.zone.now + RUN_CONFIGURATOR_DELAY },
48
+ @app_instance_host.app_instance)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # Implement a RemoteExecutionProvider
5
+ class AcdProvider < RemoteExecutionProvider
6
+ class << self
7
+ def supports_effective_user?
8
+ true
9
+ end
10
+
11
+ def proxy_operation_name
12
+ 'acd'
13
+ end
14
+
15
+ def humanized_name
16
+ 'ACD'
17
+ end
18
+
19
+ def proxy_command_options(template_invocation, host)
20
+ super(template_invocation, host).merge(:name => host.name)
21
+ end
22
+
23
+ def ssh_password(_host); end
24
+
25
+ def ssh_key_passphrase(_host); end
26
+
27
+ def sudo_password(_host); end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # Ansible playbook
5
+ class AnsiblePlaybook < ApplicationRecord
6
+ include Authorizable
7
+ include Taxonomix
8
+ extend FriendlyId
9
+ friendly_id :name
10
+ include Parameterizable::ByIdName
11
+
12
+ self.table_name = 'acd_ansible_playbooks'
13
+ has_many :app_definitions, :inverse_of => :ansible_playbook, :foreign_key => 'acd_ansible_playbook_id', :dependent => :restrict_with_error
14
+ validates :name, :presence => true, :uniqueness => true
15
+ scoped_search :on => :name
16
+
17
+ default_scope do
18
+ with_taxonomy_scope do
19
+ order('acd_ansible_playbooks.name')
20
+ end
21
+ end
22
+
23
+ def used_location_ids
24
+ Location.joins(:taxable_taxonomies).where(
25
+ 'taxable_taxonomies.taxable_type' => 'ForemanAcd::AnsiblePlaybook',
26
+ 'taxable_taxonomies.taxable_id' => id
27
+ ).pluck("#{Taxonomy.table_name}.id")
28
+ end
29
+
30
+ def used_organization_ids
31
+ Organization.joins(:taxable_taxonomies).where(
32
+ 'taxable_taxonomies.taxable_type' => 'ForemanAcd::AnsiblePlaybook',
33
+ 'taxable_taxonomies.taxable_id' => id
34
+ ).pluck("#{Taxonomy.table_name}.id")
35
+ end
36
+
37
+ def self.humanize_class_name(_name = nil)
38
+ _('Ansible playbook')
39
+ end
40
+
41
+ def self.permission_name
42
+ 'ansible_playbooks'
43
+ end
44
+
45
+ def content
46
+ case scm_type
47
+ when 'directory'
48
+ File.read(File.join(path, playfile))
49
+ else
50
+ raise NotImplementedError.new "scm_type #{scm_type.inspect} not supported!"
51
+ end
52
+ end
53
+
54
+ def as_unified_structobj
55
+ # FIXME: For now, we convert all values to string - even booleans and dicts
56
+ pretty_groups = JSON.parse(vars).each do |_, params|
57
+ params.transform_values!(&:to_s)
58
+ end
59
+
60
+ OpenStruct.new(
61
+ :id => id,
62
+ :name => name,
63
+ :groups => pretty_groups
64
+ )
65
+ end
66
+ end
67
+ end
@@ -4,14 +4,38 @@ module ForemanAcd
4
4
  # Application Definition
5
5
  class AppDefinition < ApplicationRecord
6
6
  include Authorizable
7
+ include Taxonomix
7
8
  extend FriendlyId
8
9
  friendly_id :name
10
+ include Parameterizable::ByIdName
9
11
 
12
+ self.table_name = 'acd_app_definitions'
10
13
  validates :name, :presence => true, :uniqueness => true
11
- belongs_to :hostgroup, :class_name => '::Hostgroup'
14
+ validates :ansible_playbook, :presence => true
12
15
  has_many :app_instances, :inverse_of => :app_definition, :dependent => :destroy
16
+ belongs_to :ansible_playbook, :inverse_of => :app_definitions, :foreign_key => :acd_ansible_playbook_id
13
17
  scoped_search :on => :name
14
18
 
19
+ default_scope do
20
+ with_taxonomy_scope do
21
+ order('acd_app_definitions.name')
22
+ end
23
+ end
24
+
25
+ def used_location_ids
26
+ Location.joins(:taxable_taxonomies).where(
27
+ 'taxable_taxonomies.taxable_type' => 'ForemanAcd::AppDefinition',
28
+ 'taxable_taxonomies.taxable_id' => id
29
+ ).pluck("#{Taxonomy.table_name}.id")
30
+ end
31
+
32
+ def used_organization_ids
33
+ Organization.joins(:taxable_taxonomies).where(
34
+ 'taxable_taxonomies.taxable_type' => 'ForemanAcd::AppDefinition',
35
+ 'taxable_taxonomies.taxable_id' => id
36
+ ).pluck("#{Taxonomy.table_name}.id")
37
+ end
38
+
15
39
  def self.humanize_class_name(_name = nil)
16
40
  _('App Definition')
17
41
  end
@@ -4,11 +4,24 @@ module ForemanAcd
4
4
  # Application Instance
5
5
  class AppInstance < ApplicationRecord
6
6
  include Authorizable
7
+ include ForemanTasks::Concerns::ActionSubject
7
8
  extend FriendlyId
8
9
  friendly_id :name
10
+ include Parameterizable::ByIdName
11
+
12
+ self.table_name = 'acd_app_instances'
13
+ belongs_to :last_deploy_task, :class_name => 'ForemanTasks::Task'
9
14
  validates :name, :presence => true, :uniqueness => true
15
+ validates :app_definition, :presence => true
10
16
  belongs_to :app_definition, :inverse_of => :app_instances
17
+ belongs_to :organization
18
+ validates :organization, :presence => true
19
+ belongs_to :location
20
+ validates :location, :presence => true
21
+ has_many :foreman_hosts, :inverse_of => :app_instance, :dependent => :destroy
11
22
  scoped_search :on => :name
23
+ default_scope -> { order('acd_app_instances.name') }
24
+ attr_accessor :hosts
12
25
 
13
26
  def self.humanize_class_name(_name = nil)
14
27
  _('App Instance')
@@ -17,5 +30,34 @@ module ForemanAcd
17
30
  def self.permission_name
18
31
  'app_instances'
19
32
  end
33
+
34
+ def clean_all_hosts
35
+ remember_host_ids = foreman_hosts.map(&:host_id)
36
+
37
+ # Clean the app instance association first
38
+ foreman_hosts.update_all(:host_id => nil)
39
+
40
+ # Remove all hosts afterwards
41
+ delete_hosts(remember_host_ids)
42
+ end
43
+
44
+ def clean_hosts_by_id(ids = [])
45
+ # Clean the app instance association first
46
+ foreman_hosts.where(:host_id => ids).update_all(:host_id => nil)
47
+
48
+ # Remove all hosts afterwards
49
+ delete_hosts(ids)
50
+ end
51
+
52
+ def delete_hosts(ids = [])
53
+ return if ids.empty?
54
+ ids.each do |host_id|
55
+ h = ::Host.find(host_id) unless host_id.nil?
56
+ if h
57
+ Katello::RegistrationManager.unregister_host(h, :unregistering => false) if ForemanAcd::Engine.with_katello?
58
+ h.destroy
59
+ end
60
+ end
61
+ end
20
62
  end
21
63
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # Foreman Host
5
+ class ForemanHost < ApplicationRecord
6
+ include Authorizable
7
+
8
+ self.table_name = 'acd_foreman_hosts'
9
+ validates :hostname, :presence => true
10
+ belongs_to :app_instance, :inverse_of => :foreman_hosts
11
+ belongs_to :host, :class_name => '::Host::Managed'
12
+ scoped_search :on => :hostname
13
+ default_scope -> { order('acd_foreman_hosts.hostname') }
14
+
15
+ def self.humanize_class_name(_name = nil)
16
+ _('Foreman Host')
17
+ end
18
+
19
+ def self.permission_name
20
+ 'foreman_hosts'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # This is a TaxonomyExtensions
5
+ module TaxonomyExtensions
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ if SETTINGS[:version].to_s.to_f <= 1.7
10
+ def self.enabled_taxonomies
11
+ %w[locations organizations].select { |taxonomy| SETTINGS["#{taxonomy}_enabled".to_sym] }
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ # To be removed after 1.7 compatibility is no longer required
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # application instances configurator
5
+ class AppConfigurator
6
+ delegate :logger, :to => :Rails
7
+
8
+ def initialize(app_instance)
9
+ @app_instance = app_instance
10
+ end
11
+
12
+ def configure
13
+ job_input = {}
14
+ job_input['application_name'] = @app_instance.name
15
+ job_input['playbook_id'] = @app_instance.app_definition.ansible_playbook.id
16
+ job_input['playbook_file'] = @app_instance.app_definition.ansible_playbook.playfile
17
+
18
+ logger.info("Use inventory to configure #{@app_instance.name} with ansible playbook #{@app_instance.app_definition.ansible_playbook.name}")
19
+
20
+ begin
21
+ proxy_hosts = {}
22
+ jobs = []
23
+
24
+ hosts = @app_instance.foreman_hosts
25
+ proxy_selector = RemoteExecutionProxySelector.new
26
+ hosts.each do |h|
27
+ proxy = proxy_selector.determine_proxy(h.host, 'ACD')
28
+ proxy_hosts[proxy.name] = [] unless proxy_hosts.key?(proxy.name)
29
+ proxy_hosts[proxy.name] << h
30
+ end
31
+
32
+ # TODO: just for testing...
33
+ # proxy_hosts = { Host.first.name => [ Host.first.id] }
34
+
35
+ # we need to compose multiple jobs. for each proxy one job.
36
+ proxy_hosts.each do |proxy_name, foreman_hosts|
37
+ # create the inventory file
38
+ inventory = ForemanAcd::InventoryCreator.new(@app_instance, foreman_hosts).create_inventory
39
+ job_input['inventory'] = YAML.dump(inventory)
40
+
41
+ composer = JobInvocationComposer.for_feature(
42
+ :run_acd_ansible_playbook,
43
+ [Host.find_by(:name => proxy_name).id],
44
+ job_input.to_hash
45
+ )
46
+ jobs << composer
47
+ end
48
+ rescue StandardError => e
49
+ logger.error("Failed to configure hosts: #{e.class}: #{e.message}\n#{e.backtrace.join($INPUT_RECORD_SEPARATOR)}")
50
+ end
51
+ jobs
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # application instances deployer
5
+ class AppDeployer
6
+ delegate :logger, :to => :Rails
7
+
8
+ def initialize(app_instance)
9
+ @app_instance = app_instance
10
+ end
11
+
12
+ def deploy
13
+ output = []
14
+ services = JSON.parse(@app_instance.app_definition.services)
15
+ all_hosts = []
16
+
17
+ @app_instance.foreman_hosts.each do |foreman_host|
18
+ service_data = services.select { |k| k['id'] == foreman_host.service.to_i }.first
19
+ host_params = set_host_params(foreman_host, service_data)
20
+
21
+ host = foreman_host.host.presence
22
+
23
+ is_rebuild = false
24
+
25
+ if host.blank?
26
+ params = host_attributes(host_params)
27
+ log_params = params.dup
28
+ log_params['root_pass'] = '****' if log_params.key?('root_pass')
29
+ msg = "Host creation parameters for #{foreman_host.hostname}:\n#{log_params}\n"
30
+ logger.info(msg)
31
+ output << msg
32
+ host = Host.new(params)
33
+ else
34
+ msg = "Update parameters and re-deploy host #{foreman_host.hostname}"
35
+ logger.info(msg)
36
+ output << msg
37
+ host.attributes = host_attributes(host_params, host)
38
+ is_rebuild = true
39
+ end
40
+
41
+ # REMOVE ME (but very nice for testing)
42
+ # host.mac = "00:11:22:33:44:55"
43
+
44
+ apply_compute_profile(host)
45
+ host.suggest_default_pxe_loader
46
+
47
+ all_hosts << OpenStruct.new(:foreman_host => foreman_host, :host => host, :rebuild => is_rebuild)
48
+ end
49
+
50
+ # do this in a second step, so that we get the progress report for all
51
+ all_hosts.each do |os_host|
52
+ # Save the host -> will initiate the deployment
53
+ os_host.host.save!
54
+ msg = "Saved and initiated/updated host #{os_host.foreman_host.hostname}"
55
+ logger.info(msg)
56
+ output << msg
57
+
58
+ os_host.host.power.reset if os_host.rebuild
59
+
60
+ # save the foreman host id
61
+ os_host.foreman_host.update!(:host_id => os_host.host.id)
62
+
63
+ progress_report = Rails.cache.fetch(os_host.host.progress_report_id)
64
+ os_host.foreman_host.update!(:last_progress_report => progress_report)
65
+ if progress_report.empty?
66
+ msg = "Progress report for #{os_host.foreman_host.hostname} is empty!"
67
+ output << msg
68
+ end
69
+ end
70
+ output
71
+ end
72
+
73
+ private
74
+
75
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
76
+ def apply_compute_profile(host)
77
+ host.apply_compute_profile(InterfaceMerge.new(:merge_compute_attributes => true))
78
+ host.apply_compute_profile(ComputeAttributeMerge.new)
79
+ end
80
+
81
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
82
+ def host_attributes(params, host = nil)
83
+ return {} if params.nil?
84
+
85
+ params = params.deep_clone
86
+ if params[:interfaces_attributes]
87
+ # handle both hash and array styles of nested attributes
88
+ params[:interfaces_attributes] = params[:interfaces_attributes].values if params[:interfaces_attributes].is_a?(Hash) || params[:interfaces_attributes].is_a?(ActionController::Parameters)
89
+ # map interface types
90
+ params[:interfaces_attributes] = params[:interfaces_attributes].map do |nic_attr|
91
+ interface_attributes(nic_attr, :allow_nil_type => host.nil?)
92
+ end
93
+ end
94
+ params = host.apply_inherited_attributes(params) if host
95
+ params
96
+ end
97
+
98
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
99
+ def interface_attributes(params, allow_nil_type: false)
100
+ params[:type] = InterfaceTypeMapper.map(params[:type]) if params.key?(:type) || allow_nil_type
101
+ params
102
+ end
103
+
104
+ def hardcoded_params
105
+ result = {}
106
+ result['build'] = true
107
+ result['compute_attributes'] = { 'start' => '1' }
108
+ result['enabled'] = true
109
+ result['host_parameters_attributes'] = []
110
+ result['managed'] = true
111
+ result
112
+ end
113
+
114
+ def set_host_params(foreman_host, service_data)
115
+ result = hardcoded_params
116
+ result['name'] = foreman_host.hostname
117
+ result['hostgroup_id'] = service_data['hostgroup']
118
+
119
+ JSON.parse(foreman_host.foremanParameters).each do |param|
120
+ case param['type']
121
+
122
+ when 'computeprofile'
123
+ result['compute_profile_id'] = param['value']
124
+
125
+ when 'domain'
126
+ result['domain_id'] = param['value']
127
+
128
+ when 'hostparam'
129
+ result['host_parameters_attributes'].push(:name => param['name'], :value => param['value'])
130
+
131
+ when 'ip'
132
+ result['ip'] = param['value']
133
+
134
+ when 'lifecycleenv'
135
+ result['content_facet_attributes'] = { 'lifecycle_environment_id' => param['value'] }
136
+
137
+ when 'ptable'
138
+ result['ptable_id'] = param['value']
139
+
140
+ when 'puppetenv'
141
+ result['environment_id'] = param['value']
142
+
143
+ when 'password'
144
+ result['root_pass'] = param['value']
145
+
146
+ end
147
+ end
148
+ result
149
+ end
150
+ end
151
+ end