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
@@ -23,6 +23,7 @@ module ForemanAcd
23
23
  def_param_group :app_definition do
24
24
  param :app_definition, Hash, :required => true, :action_aware => true do
25
25
  param :name, String, :required => true
26
+ param_group :taxonomies, ::Api::V2::BaseController
26
27
  param :description, String, :required => true
27
28
  param :services, String, :required => true
28
29
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ module Api
5
+ module V2
6
+ # API controller for App Instances
7
+ class AppInstancesController < ::ForemanAcd::Api::V2::BaseController
8
+ include ::ForemanAcd::Concerns::AppInstanceParameters
9
+
10
+ before_action :find_resource, :except => [:index, :create]
11
+
12
+ api :GET, '/app_instances/:id', N_('Show application instance')
13
+ param :id, :identifier, :required => true
14
+ param :organization_id, :identifier, :required => true
15
+ param :location_id, :identifier, :required => true
16
+ def show; end
17
+
18
+ api :GET, '/app_instances', N_('List application instances')
19
+ param :organization_id, :identifier, :required => true
20
+ param :location_id, :identifier, :required => true
21
+ param_group :search_and_pagination, ::Api::V2::BaseController
22
+ add_scoped_search_description_for(AppInstance)
23
+ def index
24
+ scope = resource_scope_for_index.where(:organization => params[:organization_id]) if params[:organization_id].present?
25
+ scope = scope.where(:location => params[:location_id]) if params[:location_id].present?
26
+ @app_instances = scope
27
+ end
28
+
29
+ def_param_group :app_instance do
30
+ param :app_instance, Hash, :required => true, :action_aware => true do
31
+ param :name, String, :required => true
32
+ param :organization_id, :identifier, :required => true
33
+ param :location_id, :identifier, :required => true
34
+ param :description, String, :required => true
35
+ param :services, String, :required => true
36
+ end
37
+ end
38
+
39
+ api :POST, '/app_instances', N_('Create a application instance')
40
+ param_group :app_instance, :as => :create
41
+ def create
42
+ @app_instance = AppInstance.new(app_instance_params)
43
+ process_response @app_instance.save
44
+ end
45
+
46
+ api :DELETE, '/app_instances/:id', N_('Deletes application instance')
47
+ param :id, :identifier, :required => true
48
+ def destroy
49
+ process_response @app_instance.destroy
50
+ end
51
+
52
+ def controller_permission
53
+ 'app_instances'
54
+ end
55
+
56
+ def resource_class
57
+ ForemanAcd::AppInstance
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -7,33 +7,41 @@ module ForemanAcd
7
7
  include ::ForemanAcd::Concerns::AppDefinitionParameters
8
8
 
9
9
  before_action :find_resource, :only => [:edit, :update, :destroy, :export]
10
+ before_action :read_hostgroups, :only => [:edit, :new, :import]
11
+ before_action :read_ansible_playbooks, :only => [:edit, :new]
10
12
  before_action :handle_file_upload, :only => [:create]
11
13
 
12
14
  def index
13
15
  @app_definitions = resource_base.search_for(params[:search], :order => params[:order]).paginate(:page => params[:page])
14
16
  end
15
17
 
18
+ def read_ansible_playbooks
19
+ # Only use ansible playbooks for which the user pressed import group vars once.
20
+ @ansible_playbooks = AnsiblePlaybook.where.not(:vars => nil).map { |elem| { elem.id => elem.name } }.reduce({}) { |h, v| h.merge v }
21
+ end
22
+
16
23
  def read_hostgroups
17
24
  @hostgroups = Hostgroup.all.map { |elem| { elem.id => elem.name } }.reduce({}) { |h, v| h.merge v }
18
25
  end
19
26
 
20
27
  def new
21
- read_hostgroups
22
28
  @app_definition = AppDefinition.new
23
29
  end
24
30
 
25
31
  def create
26
32
  @app_definition = AppDefinition.new(app_definition_params)
27
- if @app_definition.save
28
- process_success
29
- else
30
- process_error
33
+ begin
34
+ if @app_definition.save!
35
+ process_success
36
+ else
37
+ process_error
38
+ end
39
+ rescue StandardError, ValidationError => e
40
+ redirect_to new_app_definition_path, :flash => { :error => _(e.message) }
31
41
  end
32
42
  end
33
43
 
34
- def edit
35
- read_hostgroups
36
- end
44
+ def edit; end
37
45
 
38
46
  def update
39
47
  if @app_definition.update(app_definition_params)
@@ -55,13 +63,14 @@ module ForemanAcd
55
63
  case params[:action]
56
64
  when 'export'
57
65
  :export
66
+ when 'import'
67
+ :import
58
68
  else
59
69
  super
60
70
  end
61
71
  end
62
72
 
63
73
  def import
64
- read_hostgroups
65
74
  @app_definition = AppDefinition.new
66
75
  end
67
76
 
@@ -74,7 +83,7 @@ module ForemanAcd
74
83
  private
75
84
 
76
85
  def handle_file_upload
77
- return unless params[:foreman_acd_app_definition] && raw_file = params[:foreman_acd_app_definition][:app_definition_file]
86
+ return unless params[:foreman_acd_app_definition] && (raw_file = params[:foreman_acd_app_definition][:app_definition_file])
78
87
  params[:foreman_acd_app_definition][:services] = YAML.load_file(raw_file.tempfile).to_json
79
88
  end
80
89
  end
@@ -6,45 +6,53 @@ module ForemanAcd
6
6
  include Foreman::Controller::AutoCompleteSearch
7
7
  include ::ForemanAcd::Concerns::AppInstanceParameters
8
8
 
9
- before_action :find_resource, :only => [:edit, :update, :destroy, :deploy, :report]
9
+ before_action :find_resource, :only => [:edit, :update, :destroy_with_hosts, :deploy, :report]
10
+ before_action :read_applications, :only => [:new, :edit]
11
+ before_action :find_taxonomy
12
+ helper_method :collect_hosts_data
10
13
 
11
14
  def index
12
- @app_instances = resource_base.search_for(params[:search], :order => params[:order]).paginate(:page => params[:page])
13
- end
14
-
15
- def read_applications
16
- @applications = ForemanAcd::AppDefinition.all.map { |elem| { elem.id => elem.name } }.reduce({}) { |h, v| h.merge v }
15
+ @app_instances = resource_base.where(:organization => @organization).
16
+ where(:location => @location).
17
+ search_for(params[:search], :order => params[:order]).paginate(:page => params[:page])
17
18
  end
18
19
 
19
20
  def new
20
21
  @app_instance = AppInstance.new
21
- read_applications
22
+ @app_instance.organization = @organization
23
+ @app_instance.location = @location
22
24
  end
23
25
 
24
26
  def create
25
27
  @app_instance = AppInstance.new(app_instance_params)
26
- if @app_instance.save
27
- process_success
28
- else
29
- process_error
28
+ begin
29
+ if @app_instance.save!
30
+ app_instance_has_foreman_hosts
31
+ process_success
32
+ else
33
+ process_error
34
+ end
35
+ rescue StandardError, ValidationError => e
36
+ redirect_to new_app_instance_path, :flash => { :error => _(e.message) }
30
37
  end
31
38
  end
32
39
 
33
- def edit
34
- read_applications
35
- end
40
+ def edit; end
36
41
 
37
42
  def update
38
43
  if @app_instance.update(app_instance_params)
44
+ app_instance_has_foreman_hosts
39
45
  process_success
40
46
  else
41
47
  process_error
42
48
  end
43
49
  end
44
50
 
45
- def destroy
51
+ def destroy_with_hosts
52
+ @app_instance = AppInstance.find(params[:id])
53
+ @app_instance.clean_hosts_by_id(params[:foreman_host_ids]) if params[:foreman_host_ids]
46
54
  if @app_instance.destroy
47
- process_success
55
+ redirect_to app_instances_path, :flash => { :success => _('Successfully deleted %s') % @app_instance }
48
56
  else
49
57
  process_error
50
58
  end
@@ -56,152 +64,118 @@ module ForemanAcd
56
64
  :deploy
57
65
  when 'report'
58
66
  :report
67
+ when 'destroy_with_hosts'
68
+ :destroy
59
69
  else
60
70
  super
61
71
  end
62
72
  end
63
73
 
64
74
  def deploy
65
- services = JSON.parse(@app_instance.app_definition.services)
66
- @deploy_hosts = []
67
-
68
- app_hosts = JSON.parse(@app_instance.hosts)
69
-
70
- app_hosts.each do |host_data|
71
- begin
72
- service_data = services.select { |k| k['id'] == host_data['service'].to_i }.first
73
- host_params = set_host_params(host_data, service_data)
74
-
75
- host = nil
76
- if host_data.has_key?('foreman_host_id')
77
- logger.debug("Try to find host with id #{host_data['foreman_host_id']}")
78
- begin
79
- host = Host.find(host_data['foreman_host_id'])
80
- rescue ActiveRecord::RecordNotFound
81
- logger.info("Host with id #{host_data['foreman_host_id']} couldn\'t be found, create a new one!")
82
- host = nil
83
- end
84
- end
85
-
86
- if host.nil?
87
- logger.info("Host creation parameters for #{host_data['hostname']}:\n#{params}\n")
88
- params = host_attributes(host_params)
89
- host = Host.new(params)
90
- else
91
- logger.info("Update parameters and re-deploy host #{host_data['hostname']}")
92
- host.attributes = host_attributes(host_params, host)
93
- host.setBuild
94
- host.power.reset
95
- end
96
-
97
- # REMOVE ME (but very nice for testing)
98
- #host.mac = "00:11:22:33:44:55"
99
-
100
- apply_compute_profile(host)
101
- host.suggest_default_pxe_loader
102
- host.save
103
-
104
- # save the foreman host id
105
- host_data['foreman_host_id'] = host.id
106
-
107
- @deploy_hosts.push({ id: host.id, name: host_data['hostname'], hostname: host.hostname, hostUrl: host_path(h), progress_report_id: host.progress_report_id})
108
- rescue StandardError => e
109
- logger.error("Failed to initiate host creation: #{e.backtrace.join($INPUT_RECORD_SEPARATOR)}")
110
- end
111
- end
112
-
113
- # save any change to the app_hosts json
114
- @app_instance.hosts = app_hosts.to_json
115
- @app_instance.save
75
+ value = false
76
+ @app_instance.clean_all_hosts if params[:delete_hosts]
77
+ value = safe_deploy? if params[:safe_deploy]
78
+ session.delete(:remember_hosts)
79
+ logger.info('Run async foreman task to deploy hosts')
80
+ async_task = ForemanTasks.async_task(::Actions::ForemanAcd::DeployAllHosts, @app_instance, value)
81
+ @app_instance.update!(:last_deploy_task => async_task)
82
+ process_success(:success_msg => _('Started task to deploy hosts for %s') % @app_instance)
83
+ rescue StandardError => e
84
+ error_msg = "Error happend while deploying hosts of #{@app_instance}: #{e.message}"
85
+ logger.error("#{error_msg} - #{e.class}\n#{e.backtrace.join($INPUT_RECORD_SEPARATOR)}")
86
+ process_error :error_msg => error_msg
116
87
  end
117
88
 
118
- def report
119
- @report_hosts = []
120
- app_hosts = JSON.parse(@app_instance.hosts)
121
- app_hosts.each do |host_data|
122
- h = Host.find(host_data['foreman_host_id'])
123
- @report_hosts.push({id: h.id, name: host_data['hostname'], hostname: h.hostname, hostUrl: host_path(h), powerStatusUrl: power_api_host_path(h) })
124
- end
125
-
126
- logger.debug("deploy report hosts are: #{@report_hosts.inspect}")
89
+ def safe_deploy?
90
+ return false if session[:remember_hosts].empty?
91
+ session[:remember_hosts]
127
92
  end
128
93
 
129
- private
130
-
131
- # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
132
- def apply_compute_profile(host)
133
- host.apply_compute_profile(InterfaceMerge.new(:merge_compute_attributes => true))
134
- host.apply_compute_profile(ComputeAttributeMerge.new)
94
+ def report
95
+ @report_hosts = collect_host_report_data
96
+ logger.debug("app instance host details: #{@report_hosts.inspect}")
135
97
  end
136
98
 
137
- # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
138
- def host_attributes(params, host = nil)
139
- return {} if params.nil?
140
-
141
- params = params.deep_clone
142
- if params[:interfaces_attributes]
143
- # handle both hash and array styles of nested attributes
144
- params[:interfaces_attributes] = params[:interfaces_attributes].values if params[:interfaces_attributes].is_a?(Hash) || params[:interfaces_attributes].is_a?(ActionController::Parameters)
145
- # map interface types
146
- params[:interfaces_attributes] = params[:interfaces_attributes].map do |nic_attr|
147
- interface_attributes(nic_attr, :allow_nil_type => host.nil?)
99
+ def app_instance_has_foreman_hosts
100
+ hosts = JSON.parse(@app_instance.hosts)
101
+ session[:remember_hosts] = []
102
+ hosts.each do |h|
103
+ if @app_instance.foreman_hosts.where(:hostname => h['hostname']).exists?
104
+ old_host = @app_instance.foreman_hosts.find_by(:hostname => h['hostname'])
105
+
106
+ @app_instance.foreman_hosts.where(:hostname => h['hostname']).
107
+ update(:service => h['service'], :description => h['description'],
108
+ :foremanParameters => JSON.dump(h['foremanParameters']), :ansibleParameters => JSON.dump(h['ansibleParameters']))
109
+
110
+ updated_host = @app_instance.foreman_hosts.find_by(:hostname => h['hostname'])
111
+
112
+ # Store hosts if updated for safe deploy
113
+ session[:remember_hosts] << updated_host.id if updated_host.updated_at != old_host.updated_at
114
+ else
115
+ @app_instance.foreman_hosts.create(:hostname => h['hostname'], :service => h['service'], :description => h['description'],
116
+ :foremanParameters => JSON.dump(h['foremanParameters']), :ansibleParameters => JSON.dump(h['ansibleParameters']))
117
+ # Store new hosts for safe deploy
118
+ session[:remember_hosts] << @app_instance.foreman_hosts.find_by(:hostname => h['hostname']).id
148
119
  end
149
120
  end
150
- params = host.apply_inherited_attributes(params) if host
151
- params
152
- end
153
121
 
154
- # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
155
- def interface_attributes(params, allow_nil_type: false)
156
- params[:type] = InterfaceTypeMapper.map(params[:type]) if params.key?(:type) || allow_nil_type
157
- params
122
+ # Delete record if json hosts are deleted
123
+ deleted_json_hosts = @app_instance.foreman_hosts.pluck('hostname') - hosts.pluck('hostname')
124
+ @app_instance.foreman_hosts.where(:hostname => deleted_json_hosts).destroy_all if deleted_json_hosts
158
125
  end
159
126
 
160
- def hardcoded_params
161
- result = {}
162
- result['managed'] = true
163
- result['enabled'] = true
164
- result['build'] = true
165
- result['compute_attributes'] = { 'start' => '1' }
166
- result['host_parameters_attributes'] = []
167
- result
127
+ def collect_hosts_data
128
+ hosts_data = []
129
+ @app_instance.foreman_hosts.each do |h|
130
+ hosts_data << {
131
+ :id => h.id,
132
+ :hostname => h.hostname,
133
+ :service => h.service,
134
+ :description => h.description,
135
+ :foremanParameters => JSON.parse(h.foremanParameters),
136
+ :ansibleParameters => JSON.parse(h.ansibleParameters)
137
+ }
138
+ end
139
+ hosts_data
168
140
  end
169
141
 
170
- def set_host_params(host_data, service_data)
171
- result = hardcoded_params
172
- result['name'] = host_data['hostname']
173
- result['hostgroup_id'] = service_data['hostgroup']
174
-
175
- host_data['parameters'].each do |param|
176
- case param['type']
177
-
178
- when 'computeprofile'
179
- result['compute_profile_id'] = param['value']
180
-
181
- when 'domain'
182
- result['domain_id'] = param['value']
183
-
184
- when 'hostparam'
185
- result['host_parameters_attributes'].push(:name => param['name'], :value => param['value'])
186
-
187
- when 'ip'
188
- result['ip'] = param['value']
189
-
190
- when 'lifecycleenv'
191
- result['content_facet_attributes'] = { 'lifecycle_environment_id' => param['value'] }
142
+ private
192
143
 
193
- when 'ptable'
194
- result['ptable_id'] = param['value']
144
+ def find_taxonomy
145
+ @organization = Organization.current
146
+ redirect_to '/select_organization?toState=' + request.path and return unless @organization
195
147
 
196
- when 'puppetenv'
197
- result['environment_id'] = param['value']
148
+ @location = Location.current
149
+ redirect_to root_path, :error => 'Select a location to show App Instances' and return unless @location
150
+ end
198
151
 
199
- when 'password'
200
- result['root_pass'] = param['value']
152
+ def read_applications
153
+ @applications = AppDefinition.all.map { |elem| { elem.id => elem.name } }.reduce({}) { |h, v| h.merge v }
154
+ end
201
155
 
156
+ def collect_host_report_data
157
+ report_data = []
158
+
159
+ @app_instance.foreman_hosts.each do |foreman_host|
160
+ a_host = {
161
+ :id => nil,
162
+ :name => foreman_host.hostname,
163
+ :build => nil,
164
+ :hostUrl => nil,
165
+ :progress_report => foreman_host.last_progress_report.empty? ? [] : JSON.parse(foreman_host.last_progress_report)
166
+ }
167
+
168
+ if foreman_host.host.present?
169
+ a_host.update({
170
+ :id => foreman_host.host.id,
171
+ :build => foreman_host.host.build,
172
+ :hostUrl => host_path(foreman_host.host),
173
+ :powerStatusUrl => power_api_host_path(foreman_host.host)
174
+ })
202
175
  end
176
+ report_data << a_host
203
177
  end
204
- result
178
+ report_data
205
179
  end
206
180
  end
207
181
  end