foreman_acd 0.2.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +107 -56
  3. data/app/controllers/foreman_acd/ansible_playbooks_controller.rb +199 -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 +1 -0
  6. data/app/controllers/foreman_acd/api/v2/app_instances_controller.rb +62 -0
  7. data/app/controllers/foreman_acd/app_definitions_controller.rb +19 -10
  8. data/app/controllers/foreman_acd/app_instances_controller.rb +111 -137
  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 +62 -0
  13. data/app/controllers/ui_acd_controller.rb +20 -3
  14. data/app/lib/actions/foreman_acd/deploy_all_hosts.rb +47 -0
  15. data/app/lib/actions/foreman_acd/run_configurator.rb +44 -0
  16. data/app/models/concerns/foreman_acd/host_managed_extensions.rb +51 -0
  17. data/app/models/foreman_acd/acd_provider.rb +36 -0
  18. data/app/models/foreman_acd/ansible_playbook.rb +68 -0
  19. data/app/models/foreman_acd/app_definition.rb +25 -0
  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/acd_proxy_proxy_selector.rb +17 -0
  24. data/app/services/foreman_acd/app_configurator.rb +98 -0
  25. data/app/services/foreman_acd/app_deployer.rb +157 -0
  26. data/app/services/foreman_acd/inventory_creator.rb +68 -0
  27. data/app/views/foreman_acd/ansible_playbooks/_form.html.erb +64 -0
  28. data/app/views/foreman_acd/ansible_playbooks/edit.html.erb +11 -0
  29. data/app/views/foreman_acd/ansible_playbooks/index.html.erb +30 -0
  30. data/app/views/foreman_acd/ansible_playbooks/new.html.erb +3 -0
  31. data/app/views/foreman_acd/api/v2/ansible_playbooks/base.json.rabl +5 -0
  32. data/app/views/foreman_acd/api/v2/ansible_playbooks/index.json.rabl +5 -0
  33. data/app/views/foreman_acd/api/v2/ansible_playbooks/show.json.rabl +9 -0
  34. data/app/views/foreman_acd/api/v2/app_definitions/base.json.rabl +5 -0
  35. data/app/views/foreman_acd/api/v2/app_definitions/index.json.rabl +5 -0
  36. data/app/views/foreman_acd/api/v2/app_definitions/show.json.rabl +9 -0
  37. data/app/views/foreman_acd/api/v2/app_instances/base.json.rabl +5 -0
  38. data/app/views/foreman_acd/api/v2/app_instances/index.json.rabl +5 -0
  39. data/app/views/foreman_acd/api/v2/app_instances/show.json.rabl +5 -0
  40. data/app/views/foreman_acd/app_definitions/_form.html.erb +34 -12
  41. data/app/views/foreman_acd/app_definitions/edit.html.erb +10 -0
  42. data/app/views/foreman_acd/app_definitions/index.html.erb +4 -4
  43. data/app/views/foreman_acd/app_instances/_form.html.erb +11 -9
  44. data/app/views/foreman_acd/app_instances/edit.html.erb +10 -0
  45. data/app/views/foreman_acd/app_instances/index.html.erb +89 -10
  46. data/app/views/foreman_acd/app_instances/report.html.erb +6 -3
  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 +32 -2
  53. data/db/migrate/20200916091018_create_ansible_playbooks.rb +20 -0
  54. data/db/migrate/20200917120220_add_ansible_playbook_id.rb +14 -0
  55. data/db/migrate/20201016002819_add_ansible_vars_all_to_app_definitions.rb +8 -0
  56. data/db/migrate/20201016104338_add_ansible_vars_all_to_app_instances.rb +8 -0
  57. data/db/migrate/20210112111548_add_organization_to_app_instance.rb +22 -0
  58. data/db/migrate/20210112113853_add_location_to_app_instance.rb +8 -0
  59. data/db/migrate/20210202141658_create_foreman_hosts.rb +24 -0
  60. data/db/migrate/20210204111306_remove_hosts_from_app_instances.rb +8 -0
  61. data/db/migrate/20210209091014_rename_acd_tables.rb +16 -0
  62. data/db/migrate/20210216083522_add_last_progress_report.rb +8 -0
  63. data/db/migrate/20210216091529_add_last_deploy_task.rb +8 -0
  64. data/db/migrate/20210316151145_add_git_commit_to_ansible_playbooks.rb +8 -0
  65. data/db/migrate/20210503122809_add_git_url_to_ansible_playbooks.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.rb +12 -0
  69. data/lib/foreman_acd/engine.rb +43 -3
  70. data/lib/foreman_acd/plugin.rb +88 -22
  71. data/lib/foreman_acd/version.rb +1 -1
  72. data/lib/tasks/foreman_acd_tasks.rake +0 -12
  73. data/locale/en/foreman_acd.edit.po +326 -0
  74. data/locale/en/foreman_acd.po +232 -2
  75. data/locale/en/foreman_acd.po.time_stamp +0 -0
  76. data/locale/foreman_acd.pot +343 -8
  77. data/package.json +9 -9
  78. data/test/controllers/ansible_playbooks_controller_test.rb +27 -0
  79. data/test/controllers/app_instances_controller_test.rb +8 -3
  80. data/test/controllers/ui_acd_controller_test.rb +22 -6
  81. data/test/factories/foreman_acd_factories.rb +18 -4
  82. data/test/models/acd_provider_test.rb +37 -0
  83. data/test/models/ansible_playbook_test.rb +11 -0
  84. data/test/models/app_definition_test.rb +1 -1
  85. data/test/models/app_instance_test.rb +2 -0
  86. data/test/models/concerns/host_extensions_test.rb +26 -0
  87. data/test/models/foreman_host_test.rb +12 -0
  88. data/webpack/__mocks__/foremanReact/API.js +2 -0
  89. data/webpack/__mocks__/foremanReact/common/I18n.js +3 -0
  90. data/webpack/__mocks__/foremanReact/common/helpers.js +2 -0
  91. data/webpack/__mocks__/foremanReact/components/ForemanModal.js +7 -0
  92. data/webpack/__mocks__/foremanReact/components/common/forms/CommonForm.js +2 -0
  93. data/webpack/__mocks__/foremanReact/components/common/forms/TextInput.js +2 -0
  94. data/webpack/__mocks__/foremanReact/components/hosts/powerStatus.js +1 -0
  95. data/webpack/__snapshots__/helper.test.js.snap +14 -0
  96. data/webpack/components/ApplicationDefinition/ApplicationDefinition.js +159 -29
  97. data/webpack/components/ApplicationDefinition/ApplicationDefinitionActions.js +106 -14
  98. data/webpack/components/ApplicationDefinition/ApplicationDefinitionConstants.js +8 -2
  99. data/webpack/components/ApplicationDefinition/ApplicationDefinitionHelper.js +26 -0
  100. data/webpack/components/ApplicationDefinition/ApplicationDefinitionReducer.js +143 -21
  101. data/webpack/components/ApplicationDefinition/ApplicationDefinitionSelectors.js +3 -0
  102. data/webpack/components/ApplicationDefinition/__fixtures__/applicationDefinitionConfData_1.fixtures.js +288 -0
  103. data/webpack/components/ApplicationDefinition/__fixtures__/applicationDefinitionReducer.fixtures.js +79 -0
  104. data/webpack/components/ApplicationDefinition/__tests__/ApplicationDefinition.test.js +25 -0
  105. data/webpack/components/ApplicationDefinition/__tests__/ApplicationDefinitionReducer.test.js +119 -0
  106. data/webpack/components/ApplicationDefinition/__tests__/ApplicationDefinitionSelectors.test.js +41 -0
  107. data/webpack/components/ApplicationDefinition/__tests__/__snapshots__/ApplicationDefinition.test.js.snap +200 -0
  108. data/webpack/components/ApplicationDefinition/__tests__/__snapshots__/ApplicationDefinitionReducer.test.js.snap +3033 -0
  109. data/webpack/components/ApplicationDefinition/__tests__/__snapshots__/ApplicationDefinitionSelectors.test.js.snap +299 -0
  110. data/webpack/components/ApplicationDefinition/components/AnsiblePlaybookSelector.js +50 -0
  111. data/webpack/components/ApplicationDefinition/components/__tests__/AnsiblePlaybookSelector.test.js +41 -0
  112. data/webpack/components/ApplicationDefinition/components/__tests__/__snapshots__/AnsiblePlaybookSelector.test.js.snap +121 -0
  113. data/webpack/components/ApplicationDefinition/index.js +6 -0
  114. data/webpack/components/ApplicationInstance/ApplicationInstance.js +151 -44
  115. data/webpack/components/ApplicationInstance/ApplicationInstanceActions.js +47 -10
  116. data/webpack/components/ApplicationInstance/ApplicationInstanceConstants.js +5 -2
  117. data/webpack/components/ApplicationInstance/ApplicationInstanceReducer.js +114 -28
  118. data/webpack/components/ApplicationInstance/ApplicationInstanceSelectors.js +3 -1
  119. data/webpack/components/ApplicationInstance/__fixtures__/applicationInstanceConfData_1.fixtures.js +263 -0
  120. data/webpack/components/ApplicationInstance/__fixtures__/applicationInstanceReducer.fixtures.js +78 -0
  121. data/webpack/components/ApplicationInstance/__tests__/ApplicationInstance.test.js +23 -0
  122. data/webpack/components/ApplicationInstance/__tests__/ApplicationInstanceReducer.test.js +119 -0
  123. data/webpack/components/ApplicationInstance/__tests__/ApplicationInstanceSelectors.test.js +44 -0
  124. data/webpack/components/ApplicationInstance/__tests__/__snapshots__/ApplicationInstance.test.js.snap +209 -0
  125. data/webpack/components/ApplicationInstance/__tests__/__snapshots__/ApplicationInstanceReducer.test.js.snap +2719 -0
  126. data/webpack/components/ApplicationInstance/__tests__/__snapshots__/ApplicationInstanceSelectors.test.js.snap +276 -0
  127. data/webpack/components/ApplicationInstance/components/Service.js +1 -1
  128. data/webpack/components/ApplicationInstance/index.js +4 -0
  129. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReport.js +53 -60
  130. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReport.scss +17 -0
  131. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportActions.js +7 -51
  132. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportConstants.js +2 -4
  133. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportReducer.js +4 -18
  134. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportSelectors.js +0 -1
  135. data/webpack/components/ApplicationInstanceReport/__fixtures__/applicationInstanceReportData_1.fixtures.js +349 -0
  136. data/webpack/components/ApplicationInstanceReport/__fixtures__/applicationInstanceReportReducer.fixtures.js +20 -0
  137. data/webpack/components/ApplicationInstanceReport/__tests__/ApplicationInstanceReport.test.js +47 -0
  138. data/webpack/components/ApplicationInstanceReport/__tests__/ApplicationInstanceReportReducer.test.js +41 -0
  139. data/webpack/components/ApplicationInstanceReport/__tests__/ApplicationInstanceReportSelectors.test.js +26 -0
  140. data/webpack/components/ApplicationInstanceReport/__tests__/__snapshots__/ApplicationInstanceReport.test.js.snap +130 -0
  141. data/webpack/components/ApplicationInstanceReport/__tests__/__snapshots__/ApplicationInstanceReportReducer.test.js.snap +718 -0
  142. data/webpack/components/ApplicationInstanceReport/__tests__/__snapshots__/ApplicationInstanceReportSelectors.test.js.snap +347 -0
  143. data/webpack/components/ApplicationInstanceReport/components/ReportViewer.js +1 -1
  144. data/webpack/components/ApplicationInstanceReport/components/__tests__/ReportViewer.test.js +24 -0
  145. data/webpack/components/ApplicationInstanceReport/components/__tests__/__snapshots__/ReportViewer.test.js.snap +24 -0
  146. data/webpack/components/ApplicationInstanceReport/index.js +0 -2
  147. data/webpack/components/ParameterSelection/ParameterSelection.js +85 -50
  148. data/webpack/components/ParameterSelection/ParameterSelectionActions.js +68 -62
  149. data/webpack/components/ParameterSelection/ParameterSelectionConstants.js +6 -3
  150. data/webpack/components/ParameterSelection/ParameterSelectionHelper.js +0 -32
  151. data/webpack/components/ParameterSelection/ParameterSelectionReducer.js +47 -35
  152. data/webpack/components/ParameterSelection/ParameterSelectionSelectors.js +2 -2
  153. data/webpack/components/ParameterSelection/__fixtures__/parameterSelectionData_1.fixtures.js +116 -84
  154. data/webpack/components/ParameterSelection/__fixtures__/parameterSelectionReducer.fixtures.js +10 -4
  155. data/webpack/components/ParameterSelection/__tests__/ParameterSelection.test.js +36 -46
  156. data/webpack/components/ParameterSelection/__tests__/ParameterSelectionReducer.test.js +31 -25
  157. data/webpack/components/ParameterSelection/__tests__/ParameterSelectionSelectors.test.js +6 -6
  158. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelection.test.js.snap +2 -126
  159. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelectionReducer.test.js.snap +1483 -872
  160. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelectionSelectors.test.js.snap +117 -79
  161. data/webpack/components/ParameterSelection/index.js +4 -4
  162. data/webpack/components/SyncGitRepo/SyncGitRepo.js +210 -0
  163. data/webpack/components/SyncGitRepo/SyncGitRepo.scss +1 -0
  164. data/webpack/components/SyncGitRepo/SyncGitRepoActions.js +124 -0
  165. data/webpack/components/SyncGitRepo/SyncGitRepoConstants.js +9 -0
  166. data/webpack/components/SyncGitRepo/SyncGitRepoReducer.js +80 -0
  167. data/webpack/components/SyncGitRepo/SyncGitRepoSelectors.js +6 -0
  168. data/webpack/components/SyncGitRepo/__fixtures__/syncGitRepoConfData_1.fixtures.js +7 -0
  169. data/webpack/components/SyncGitRepo/__fixtures__/syncGitRepoReducer.fixtures.js +44 -0
  170. data/webpack/components/SyncGitRepo/__tests__/SyncGitRepo.test.js +27 -0
  171. data/webpack/components/SyncGitRepo/__tests__/SyncGitRepoReducer.test.js +95 -0
  172. data/webpack/components/SyncGitRepo/__tests__/SyncGitRepoSelectors.test.js +32 -0
  173. data/webpack/components/SyncGitRepo/__tests__/__snapshots__/SyncGitRepo.test.js.snap +30 -0
  174. data/webpack/components/SyncGitRepo/__tests__/__snapshots__/SyncGitRepoReducer.test.js.snap +137 -0
  175. data/webpack/components/SyncGitRepo/__tests__/__snapshots__/SyncGitRepoSelectors.test.js.snap +13 -0
  176. data/webpack/components/SyncGitRepo/components/FormTextInput.js +42 -0
  177. data/webpack/components/SyncGitRepo/components/ScmTypeSelector.js +46 -0
  178. data/webpack/components/SyncGitRepo/index.js +28 -0
  179. data/webpack/components/common/DeleteTableEntry.js +3 -3
  180. data/webpack/components/common/EditTableEntry.js +50 -0
  181. data/webpack/components/common/ExtTextInput.js +43 -0
  182. data/webpack/components/common/LockTableEntry.js +60 -0
  183. data/webpack/components/common/__tests__/EditTableEntry.test.js +53 -0
  184. data/webpack/components/common/__tests__/LockTableEntry.test.js +35 -0
  185. data/webpack/components/common/__tests__/__snapshots__/DeleteTableEntry.test.js.snap +2 -2
  186. data/webpack/components/common/__tests__/__snapshots__/EditTableEntry.test.js.snap +81 -0
  187. data/webpack/components/common/__tests__/__snapshots__/LockTableEntry.test.js.snap +60 -0
  188. data/webpack/helper.js +15 -1
  189. data/webpack/helper.test.js +37 -0
  190. data/webpack/index.js +2 -0
  191. data/webpack/reducer.js +18 -11
  192. metadata +184 -10
  193. data/app/views/foreman_acd/app_instances/deploy.html.erb +0 -19
  194. data/webpack/components/common/__tests__/__snapshots__/AddParameter.test.js.snap +0 -35
  195. data/webpack/components/common/__tests__/__snapshots__/DeleteParameter.test.js.snap +0 -41
@@ -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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # AcdProxy Selector implementing a RemoteExecutionProxySelector which
5
+ # only returns the Smart Proxy of the given host
6
+ class AcdProxyProxySelector < ::RemoteExecutionProxySelector
7
+ def determine_proxy(*args)
8
+ host, _provider = args
9
+
10
+ # We already did the determine_proxy in app_configurator. We want that REX
11
+ # isn't doing the determination of the proxy twice.
12
+ # Therefore, we will just return the host, which is the proxy!
13
+
14
+ ::SmartProxy.find_by(:name => host.name)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,98 @@
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
+ job = nil
23
+ result = OpenStruct.new
24
+
25
+ hosts = @app_instance.foreman_hosts
26
+
27
+ # Important: This uses the REX Proxy Selector and not
28
+ # the AcdProxySelection because the AcdProxySelector
29
+ # does not find the proxy but only return the given
30
+ # host. This is required because we want to run the
31
+ # ansible playbook for a group of hosts on the smart proxy.
32
+ # So the process need to be:
33
+ # 1. get the proxy which is required to connect to host a,b,c
34
+ # 2. run the job on the proxy
35
+ # In 2. we need to make sure that REX doesn't try to find the
36
+ # proxy which is necessary to connect to the proxy
37
+ proxy_selector = RemoteExecutionProxySelector.new
38
+ hosts.each do |h|
39
+ begin
40
+ unless h.host
41
+ result.success = false
42
+ result.error = 'App Instance is not deployed'
43
+ return [result, job]
44
+ end
45
+ proxy = proxy_selector.determine_proxy(h.host, 'ACD')
46
+ result.success = true
47
+ rescue NoMethodError => e
48
+ result.success = false
49
+ result.error = "#{e}, Install/Update smart-proxies for ACD"
50
+ return [result, job]
51
+ end
52
+ proxy_hosts[proxy.name] = [] unless proxy_hosts.key?(proxy.name)
53
+ proxy_hosts[proxy.name] << h
54
+ end
55
+
56
+ # TODO: just for testing...
57
+ # proxy_hosts = { Host.first.name => [ Host.first.id] }
58
+
59
+ proxy_inventories = {}
60
+ proxy_host_ids = []
61
+ proxy_hosts.each do |proxy_name, foreman_hosts|
62
+ proxy_inventories[proxy_name] = ForemanAcd::InventoryCreator.new(@app_instance, foreman_hosts).create_inventory
63
+
64
+ # Workaround till infrastructure jobs on proxies are possible. See
65
+ # https://community.theforeman.org/t/infrastructure-roles/22001/33
66
+ #
67
+ # Why do we use 'foreman_hosts.first.host'? REX is 'host-centric'. During
68
+ # job execution, REX will try to find the proxy on which the job should be executed.
69
+ # We throw the first host at REX's feet so that the resolution of the
70
+ # proxy works (... which we have already done above).
71
+ # ACD on the smart proxy side will call the ansible-playbook for ALL hosts
72
+ # which are behind this smart proxy.
73
+ #
74
+ # Additionally, we disable that AcdProxyProxySelector is used.
75
+ # See required_proxy_selector_for in models/foreman_acd/acd_provider.rb
76
+ proxy_host_ids << foreman_hosts.first.host.id
77
+
78
+ # Actually, we want this:
79
+ # proxy_host_ids << Host.find_by(:name => proxy_name).id
80
+ end
81
+
82
+ job_input['inventory'] = YAML.dump(proxy_inventories)
83
+ composer = JobInvocationComposer.for_feature(
84
+ :run_acd_ansible_playbook,
85
+ proxy_host_ids,
86
+ job_input.to_hash
87
+ )
88
+ job = composer
89
+ rescue StandardError => e
90
+ result.success = false
91
+ result.error = _('Failed to configure hosts: %{err_msg}' % { :err_msg => e.message })
92
+ logger.error('Failed to configure hosts: %{err_class}: %{err_msg}' % { :err_class => e.class, :err_msg => e.message })
93
+ job = nil
94
+ end
95
+ [result, job]
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,157 @@
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(safe_deploy)
13
+ output = []
14
+ services = JSON.parse(@app_instance.app_definition.services)
15
+ all_hosts = []
16
+
17
+ foreman_hosts = if safe_deploy
18
+ @app_instance.foreman_hosts.find(safe_deploy)
19
+ else
20
+ @app_instance.foreman_hosts
21
+ end
22
+
23
+ foreman_hosts.each do |foreman_host|
24
+ service_data = services.select { |k| k['id'] == foreman_host.service.to_i }.first
25
+ host_params = set_host_params(foreman_host, service_data)
26
+
27
+ host = foreman_host.host.presence
28
+
29
+ is_rebuild = false
30
+
31
+ if host.blank?
32
+ params = host_attributes(host_params)
33
+ log_params = params.dup
34
+ log_params['root_pass'] = '****' if log_params.key?('root_pass')
35
+ msg = "Host creation parameters for #{foreman_host.hostname}:\n#{log_params}\n"
36
+ logger.info(msg)
37
+ output << msg
38
+ host = Host.new(params)
39
+ else
40
+ msg = "Update parameters and re-deploy host #{foreman_host.hostname}"
41
+ logger.info(msg)
42
+ output << msg
43
+ host.attributes = host_attributes(host_params, host)
44
+ is_rebuild = true
45
+ end
46
+
47
+ # REMOVE ME (but very nice for testing)
48
+ # host.mac = "00:11:22:33:44:55"
49
+
50
+ apply_compute_profile(host)
51
+ host.suggest_default_pxe_loader
52
+
53
+ all_hosts << OpenStruct.new(:foreman_host => foreman_host, :host => host, :rebuild => is_rebuild)
54
+ end
55
+
56
+ # do this in a second step, so that we get the progress report for all
57
+ all_hosts.each do |os_host|
58
+ # Save the host -> will initiate the deployment
59
+ os_host.host.save!
60
+ msg = "Saved and initiated/updated host #{os_host.foreman_host.hostname}"
61
+ logger.info(msg)
62
+ output << msg
63
+
64
+ os_host.host.power.reset if os_host.rebuild
65
+
66
+ # save the foreman host id
67
+ os_host.foreman_host.update!(:host_id => os_host.host.id)
68
+
69
+ progress_report = Rails.cache.fetch(os_host.host.progress_report_id)
70
+ os_host.foreman_host.update!(:last_progress_report => progress_report)
71
+ if progress_report.empty?
72
+ msg = "Progress report for #{os_host.foreman_host.hostname} is empty!"
73
+ output << msg
74
+ end
75
+ end
76
+ output
77
+ end
78
+
79
+ private
80
+
81
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
82
+ def apply_compute_profile(host)
83
+ host.apply_compute_profile(InterfaceMerge.new(:merge_compute_attributes => true))
84
+ host.apply_compute_profile(ComputeAttributeMerge.new)
85
+ end
86
+
87
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
88
+ def host_attributes(params, host = nil)
89
+ return {} if params.nil?
90
+
91
+ params = params.deep_clone
92
+ if params[:interfaces_attributes]
93
+ # handle both hash and array styles of nested attributes
94
+ params[:interfaces_attributes] = params[:interfaces_attributes].values if params[:interfaces_attributes].is_a?(Hash) || params[:interfaces_attributes].is_a?(ActionController::Parameters)
95
+ # map interface types
96
+ params[:interfaces_attributes] = params[:interfaces_attributes].map do |nic_attr|
97
+ interface_attributes(nic_attr, :allow_nil_type => host.nil?)
98
+ end
99
+ end
100
+ params = host.apply_inherited_attributes(params) if host
101
+ params
102
+ end
103
+
104
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
105
+ def interface_attributes(params, allow_nil_type: false)
106
+ params[:type] = InterfaceTypeMapper.map(params[:type]) if params.key?(:type) || allow_nil_type
107
+ params
108
+ end
109
+
110
+ def hardcoded_params
111
+ result = {}
112
+ result['build'] = true
113
+ result['compute_attributes'] = { 'start' => '1' }
114
+ result['enabled'] = true
115
+ result['host_parameters_attributes'] = []
116
+ result['managed'] = true
117
+ result
118
+ end
119
+
120
+ def set_host_params(foreman_host, service_data)
121
+ result = hardcoded_params
122
+ result['name'] = foreman_host.hostname
123
+ result['hostgroup_id'] = service_data['hostgroup']
124
+
125
+ JSON.parse(foreman_host.foremanParameters).each do |param|
126
+ case param['type']
127
+
128
+ when 'computeprofile'
129
+ result['compute_profile_id'] = param['value']
130
+
131
+ when 'domain'
132
+ result['domain_id'] = param['value']
133
+
134
+ when 'hostparam'
135
+ result['host_parameters_attributes'].push(:name => param['name'], :value => param['value'])
136
+
137
+ when 'ip'
138
+ result['ip'] = param['value']
139
+
140
+ when 'lifecycleenv'
141
+ result['content_facet_attributes'] = { 'lifecycle_environment_id' => param['value'] }
142
+
143
+ when 'ptable'
144
+ result['ptable_id'] = param['value']
145
+
146
+ when 'puppetenv'
147
+ result['environment_id'] = param['value']
148
+
149
+ when 'password'
150
+ result['root_pass'] = param['value']
151
+
152
+ end
153
+ end
154
+ result
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # inventory creator for application instances
5
+ class InventoryCreator
6
+ delegate :logger, :to => :Rails
7
+
8
+ def initialize(app_instance, foreman_hosts)
9
+ @app_instance = app_instance
10
+ @foreman_hosts = foreman_hosts
11
+ end
12
+
13
+ # TODO: this might be part of the smart proxy plugin.
14
+ def create_inventory
15
+ inventory = {}
16
+ inventory['all'] = {}
17
+
18
+ inventory['all'] = { 'vars' => inventory_all_vars } if @app_instance.ansible_vars_all.present?
19
+
20
+ services = JSON.parse(@app_instance.app_definition.services)
21
+
22
+ children = {}
23
+ @foreman_hosts.each do |foreman_host|
24
+ if foreman_host.host_id.nil?
25
+ logger.warn "Ignore host #{foreman_h.hostname} because no foreman host id could be found. Is the host not provisioned yet?"
26
+ next
27
+ end
28
+
29
+ service_id = foreman_host.service.to_i
30
+ host_service = services.select { |s| s['id'] == service_id }.first
31
+ ansible_group = host_service['ansibleGroup']
32
+
33
+ children[ansible_group] = { 'hosts' => {} } unless children.key?(host_service['ansibleGroup'])
34
+
35
+ ansible_vars = JSON.parse(foreman_host.ansibleParameters).map { |v| { v['name'] => v['value'] } }.reduce({}, :merge!)
36
+
37
+ # in case there is no ansible_user defined, set "root" as default.
38
+ ansible_vars['ansible_user'] = 'root' unless ansible_vars.key?('ansible_user')
39
+ ansible_vars['ansible_ssh_private_key_file'] = ansible_or_rex_ssh_private_key(foreman_host.host)
40
+
41
+ children[ansible_group]['hosts'][foreman_host.host.name] = ansible_vars
42
+ end
43
+ inventory['all']['children'] = children
44
+ inventory
45
+ end
46
+
47
+ private
48
+
49
+ def ansible_or_rex_ssh_private_key(host)
50
+ ansible_private_file = host_setting(host, 'ansible_ssh_private_key_file')
51
+ if !ansible_private_file.empty?
52
+ ansible_private_file
53
+ else
54
+ ForemanRemoteExecutionCore.settings[:ssh_identity_key_file]
55
+ end
56
+ end
57
+
58
+ def host_setting(host, setting)
59
+ host.params[setting] || Setting[setting]
60
+ end
61
+
62
+ def inventory_all_vars
63
+ JSON.parse(@app_instance.ansible_vars_all).map do |a|
64
+ { a['name'] => a['value'] }
65
+ end.reduce({}, :merge!)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,64 @@
1
+ <% content_for(:javascripts) do %>
2
+ <%= webpacked_plugins_js_for :foreman_acd %>
3
+ <% end %>
4
+ <% content_for(:stylesheets) do %>
5
+ <%= webpacked_plugins_css_for :foreman_acd %>
6
+ <% end %>
7
+
8
+ <%
9
+ if @ansible_playbook.new_record?
10
+ json = {
11
+ "scmTypes": {"directory" => "directory", "git" => "git"},
12
+ "mode": "newPlaybook",
13
+ "organization": current_organization,
14
+ "location": current_location,
15
+ "path": "",
16
+ "gitUrl": "",
17
+ "gitCommit": "",
18
+ "scmType": "",
19
+ "appDefinitions": [],
20
+ }
21
+
22
+ else
23
+ json = {
24
+ "scmTypes": {"directory" => "directory", "git" => "git"},
25
+ "mode": "editPlaybook",
26
+ "organization": current_organization,
27
+ "location": current_location,
28
+ "path": @ansible_playbook.path,
29
+ "gitUrl": @ansible_playbook.git_url,
30
+ "gitCommit": @ansible_playbook.git_commit,
31
+ "scmType": @ansible_playbook.scm_type,
32
+ "appDefinitions": @ansible_playbook.app_definitions
33
+ }
34
+ end
35
+ %>
36
+
37
+ <%= form_for @ansible_playbook, :url => (@ansible_playbook.new_record? ? ansible_playbooks_path : ansible_playbook_path(:id => @ansible_playbook.id)) do |f| %>
38
+ <%= base_errors_for @ansible_playbook %>
39
+
40
+ <ul class="nav nav-tabs" data-tabs="tabs">
41
+ <li class="active"><a href="#primary" data-toggle="tab"><%= _('Ansible playbooks') %></a></li>
42
+ <% if show_location_tab? %>
43
+ <li><a href="#locations" data-toggle="tab"><%= _("Locations") %></a></li>
44
+ <% end %>
45
+ <% if show_organization_tab? %>
46
+ <li><a href="#organizations" data-toggle="tab"><%= _("Organizations") %></a></li>
47
+ <% end %>
48
+ </ul>
49
+
50
+ <div class="tab-content">
51
+ <div class="tab-pane active" id="primary">
52
+
53
+ <%= text_f f, :name %>
54
+ <%= text_f f, :description %>
55
+ <div id='ansible_playbook'>
56
+ <%= react_component('SyncGitRepo', :data => json,) %>
57
+ </div>
58
+ <%= text_f f, :playfile, :required => true %>
59
+ </div>
60
+
61
+ <%= render 'taxonomies/loc_org_tabs', :f => f, :obj => @ansible_playbook %>
62
+ </div>
63
+ <%= submit_or_cancel f %>
64
+ <% end %>