foreman_acd 0.0.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +79 -26
  3. data/app/controllers/foreman_acd/ansible_playbooks_controller.rb +122 -0
  4. data/app/controllers/foreman_acd/api/v2/ansible_playbooks_controller.rb +54 -0
  5. data/app/controllers/foreman_acd/api/v2/app_definitions_controller.rb +1 -2
  6. data/app/controllers/foreman_acd/api/v2/app_instances_controller.rb +54 -0
  7. data/app/controllers/foreman_acd/api/v2/app_playbooks_controller.rb +0 -0
  8. data/app/controllers/foreman_acd/app_definitions_controller.rb +36 -4
  9. data/app/controllers/foreman_acd/app_instances_controller.rb +24 -90
  10. data/app/controllers/foreman_acd/concerns/ansible_playbook_parameters.rb +23 -0
  11. data/app/controllers/foreman_acd/concerns/app_definition_parameters.rb +1 -1
  12. data/app/controllers/foreman_acd/concerns/app_instance_parameters.rb +1 -1
  13. data/app/controllers/foreman_acd/remote_execution_controller.rb +49 -0
  14. data/app/controllers/ui_acd_controller.rb +11 -4
  15. data/app/models/foreman_acd/ansible_playbook.rb +50 -0
  16. data/app/models/foreman_acd/app_definition.rb +2 -1
  17. data/app/models/foreman_acd/app_instance.rb +7 -0
  18. data/app/services/foreman_acd/app_configurator.rb +70 -0
  19. data/app/services/foreman_acd/app_deployer.rb +139 -0
  20. data/app/services/foreman_acd/inventory_creator.rb +67 -0
  21. data/app/views/foreman_acd/ansible_playbooks/_form.html.erb +21 -0
  22. data/app/views/foreman_acd/ansible_playbooks/edit.html.erb +3 -0
  23. data/app/views/foreman_acd/ansible_playbooks/index.html.erb +30 -0
  24. data/app/views/foreman_acd/ansible_playbooks/new.html.erb +3 -0
  25. data/app/views/foreman_acd/api/v2/ansible_playbooks/base.json.rabl +3 -0
  26. data/app/views/foreman_acd/api/v2/ansible_playbooks/index.json.rabl +3 -0
  27. data/app/views/foreman_acd/api/v2/ansible_playbooks/show.json.rabl +3 -0
  28. data/app/views/foreman_acd/api/v2/app_definitions/base.json.rabl +3 -0
  29. data/app/views/foreman_acd/api/v2/app_definitions/index.json.rabl +3 -0
  30. data/app/views/foreman_acd/api/v2/app_definitions/show.json.rabl +3 -0
  31. data/app/views/foreman_acd/api/v2/app_instances/base.json.rabl +3 -0
  32. data/app/views/foreman_acd/api/v2/app_instances/index.json.rabl +3 -0
  33. data/app/views/foreman_acd/api/v2/app_instances/show.json.rabl +3 -0
  34. data/app/views/foreman_acd/app_definitions/_form.html.erb +25 -19
  35. data/app/views/foreman_acd/app_definitions/edit.html.erb +5 -0
  36. data/app/views/foreman_acd/app_definitions/import.html.erb +18 -0
  37. data/app/views/foreman_acd/app_definitions/index.html.erb +9 -5
  38. data/app/views/foreman_acd/app_instances/_form.html.erb +10 -8
  39. data/app/views/foreman_acd/app_instances/deploy.html.erb +19 -0
  40. data/app/views/foreman_acd/app_instances/index.html.erb +10 -5
  41. data/app/views/foreman_acd/app_instances/report.html.erb +19 -0
  42. data/app/views/templates/job/run_acd_ansible_playbook.erb +49 -0
  43. data/app/views/ui_acd/ansible_data.json.rabl +6 -0
  44. data/app/views/ui_acd/app.json.rabl +6 -2
  45. data/app/views/ui_acd/app_definition.json.rabl +1 -1
  46. data/app/views/ui_acd/{fdata.json.rabl → foreman_data.json.rabl} +1 -1
  47. data/config/routes.rb +32 -1
  48. data/db/migrate/20190610202252_create_app_definitions.rb +1 -3
  49. data/db/migrate/20190625140305_create_app_instances.rb +1 -1
  50. data/db/migrate/20200916091018_create_ansible_playbooks.rb +20 -0
  51. data/db/migrate/20200917120220_add_ansible_playbook_id.rb +14 -0
  52. data/db/migrate/20201016002819_add_ansible_vars_all_to_app_definitions.rb +5 -0
  53. data/db/migrate/20201016104338_add_ansible_vars_all_to_app_instances.rb +5 -0
  54. data/db/seeds.d/75-job_templates.rb +8 -0
  55. data/lib/foreman_acd/engine.rb +3 -0
  56. data/lib/foreman_acd/plugin.rb +72 -4
  57. data/lib/foreman_acd/version.rb +1 -1
  58. data/package.json +1 -2
  59. data/webpack/components/ApplicationDefinition/ApplicationDefinition.js +376 -0
  60. data/webpack/components/ApplicationDefinition/ApplicationDefinition.scss +1 -0
  61. data/webpack/components/ApplicationDefinition/ApplicationDefinitionActions.js +295 -0
  62. data/webpack/components/ApplicationDefinition/ApplicationDefinitionConstants.js +14 -0
  63. data/webpack/components/ApplicationDefinition/ApplicationDefinitionHelper.js +26 -0
  64. data/webpack/components/ApplicationDefinition/ApplicationDefinitionReducer.js +243 -0
  65. data/webpack/components/ApplicationDefinition/ApplicationDefinitionSelectors.js +8 -0
  66. data/webpack/components/ApplicationDefinition/components/AnsiblePlaybookSelector.js +49 -0
  67. data/webpack/components/ApplicationDefinition/index.js +33 -0
  68. data/webpack/components/ApplicationInstance/ApplicationInstance.js +412 -0
  69. data/webpack/components/ApplicationInstance/ApplicationInstance.scss +11 -0
  70. data/webpack/components/ApplicationInstance/ApplicationInstanceActions.js +239 -0
  71. data/webpack/components/ApplicationInstance/ApplicationInstanceConstants.js +14 -0
  72. data/webpack/components/ApplicationInstance/ApplicationInstanceReducer.js +295 -0
  73. data/webpack/components/ApplicationInstance/ApplicationInstanceSelectors.js +9 -0
  74. data/webpack/components/ApplicationInstance/components/AppDefinitionSelector.js +49 -0
  75. data/webpack/components/ApplicationInstance/components/Service.js +30 -0
  76. data/webpack/components/ApplicationInstance/components/ServiceCounter.js +37 -0
  77. data/webpack/components/ApplicationInstance/index.js +35 -0
  78. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReport.js +155 -0
  79. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReport.scss +27 -0
  80. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportActions.js +86 -0
  81. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportConstants.js +4 -0
  82. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportReducer.js +52 -0
  83. data/webpack/components/ApplicationInstanceReport/ApplicationInstanceReportSelectors.js +5 -0
  84. data/webpack/components/ApplicationInstanceReport/components/ReportViewer.js +26 -0
  85. data/webpack/components/ApplicationInstanceReport/index.js +27 -0
  86. data/webpack/components/ParameterSelection/ParameterSelection.js +96 -183
  87. data/webpack/components/ParameterSelection/ParameterSelection.scss +9 -2
  88. data/webpack/components/ParameterSelection/ParameterSelectionActions.js +72 -104
  89. data/webpack/components/ParameterSelection/ParameterSelectionConstants.js +14 -19
  90. data/webpack/components/ParameterSelection/ParameterSelectionHelper.js +3 -35
  91. data/webpack/components/ParameterSelection/ParameterSelectionReducer.js +100 -83
  92. data/webpack/components/ParameterSelection/ParameterSelectionSelectors.js +3 -7
  93. data/webpack/components/ParameterSelection/__fixtures__/parameterSelection.fixtures.js +12 -21
  94. data/webpack/components/ParameterSelection/__fixtures__/parameterSelectionData_1.fixtures.js +1 -1
  95. data/webpack/components/ParameterSelection/__fixtures__/parameterSelectionReducer.fixtures.js +3 -45
  96. data/webpack/components/ParameterSelection/__tests__/ParameterSelection.test.js +20 -0
  97. data/webpack/components/ParameterSelection/__tests__/ParameterSelectionReducer.test.js +22 -46
  98. data/webpack/components/ParameterSelection/__tests__/ParameterSelectionSelectors.test.js +6 -6
  99. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelection.test.js.snap +40 -265
  100. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelectionReducer.test.js.snap +11 -96
  101. data/webpack/components/ParameterSelection/__tests__/__snapshots__/ParameterSelectionSelectors.test.js.snap +3 -9
  102. data/webpack/components/ParameterSelection/index.js +6 -8
  103. data/webpack/components/common/AddTableEntry.js +30 -0
  104. data/webpack/components/common/DeleteTableEntry.js +39 -0
  105. data/webpack/components/common/EasyHeaderFormatter.js +18 -0
  106. data/webpack/components/common/ExtSelect.js +43 -0
  107. data/webpack/components/common/RailsData.js +27 -0
  108. data/webpack/components/common/__tests__/AddTableEntry.test.js +26 -0
  109. data/webpack/components/common/__tests__/DeleteTableEntry.test.js +29 -0
  110. data/webpack/components/common/__tests__/ExtSelect.test.js +38 -0
  111. data/webpack/components/common/__tests__/RailsData.test.js +16 -0
  112. data/webpack/components/common/__tests__/__snapshots__/AddParameter.test.js.snap +35 -0
  113. data/webpack/components/common/__tests__/__snapshots__/AddTableEntry.test.js.snap +35 -0
  114. data/webpack/components/common/__tests__/__snapshots__/DeleteParameter.test.js.snap +41 -0
  115. data/webpack/components/common/__tests__/__snapshots__/DeleteTableEntry.test.js.snap +41 -0
  116. data/webpack/components/common/__tests__/__snapshots__/ExtSelect.test.js.snap +18 -0
  117. data/webpack/components/common/__tests__/__snapshots__/RailsData.test.js.snap +10 -0
  118. data/webpack/helper.js +20 -0
  119. data/webpack/index.js +6 -0
  120. data/webpack/reducer.js +43 -3
  121. metadata +87 -39
@@ -6,19 +6,15 @@ 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]
9
+ before_action :find_resource, :only => [:edit, :update, :destroy, :deploy, :report]
10
+ before_action :read_applications, :only => [:new, :edit]
10
11
 
11
12
  def index
12
13
  @app_instances = resource_base.search_for(params[:search], :order => params[:order]).paginate(:page => params[:page])
13
14
  end
14
15
 
15
- def read_applications
16
- @applications = ForemanAcd::AppDefinition.all.map { |elem| { elem.id => elem.name } }.reduce({}) { |h, v| h.merge v }
17
- end
18
-
19
16
  def new
20
17
  @app_instance = AppInstance.new
21
- read_applications
22
18
  end
23
19
 
24
20
  def create
@@ -31,7 +27,6 @@ module ForemanAcd
31
27
  end
32
28
 
33
29
  def edit
34
- read_applications
35
30
  end
36
31
 
37
32
  def update
@@ -54,105 +49,44 @@ module ForemanAcd
54
49
  case params[:action]
55
50
  when 'deploy'
56
51
  :deploy
52
+ when 'report'
53
+ :report
57
54
  else
58
55
  super
59
56
  end
60
57
  end
61
58
 
62
59
  def deploy
63
- params = host_attributes(set_host_params)
64
-
65
- # Print to log for debugging purposes
66
- logger.info("Host creation parameters:\n#{params}\n")
67
-
68
- @host = Host.new(params)
69
- apply_compute_profile(@host)
70
- @host.suggest_default_pxe_loader
71
- @host.save
72
- success _('Successfully initiated host creation')
73
- rescue StandardError => e
74
- logger.error("Failed to initiate host creation: #{e.backtrace.join($INPUT_RECORD_SEPARATOR)}")
75
- ensure
76
- redirect_to app_instances_path
77
- end
78
-
79
- private
60
+ app_deployer = ForemanAcd::AppDeployer.new(@app_instance)
61
+ app_hosts = app_deployer.deploy
80
62
 
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
63
+ # save any change to the app_hosts json
64
+ @app_instance.hosts = app_hosts.to_json
65
+ @app_instance.save
86
66
 
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
67
+ @deploy_hosts = collect_host_report_data(app_hosts)
102
68
  end
103
69
 
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
70
+ def report
71
+ app_hosts = JSON.parse(@app_instance.hosts)
72
+ @report_hosts = collect_host_report_data(app_hosts)
109
73
 
110
- def hardcoded_params
111
- result = {}
112
- result['managed'] = true
113
- result['enabled'] = true
114
- result['build'] = true
115
- result['compute_attributes'] = { 'start' => '1' }
116
- result['host_parameters_attributes'] = []
117
- result
74
+ logger.debug("deploy report hosts are: #{@report_hosts.inspect}")
118
75
  end
119
76
 
120
- def set_host_params
121
- result = hardcoded_params
122
- result['hostgroup_id'] = @app_instance.app_definition.hostgroup_id
123
- JSON.parse(@app_instance.parameters).each do |param|
124
- case param['type']
125
-
126
- when 'computeprofile'
127
- result['compute_profile_id'] = param['value']
128
-
129
- when 'domain'
130
- result['domain_id'] = param['value']
131
-
132
- when 'hostname'
133
- result['name'] = param['value']
134
-
135
- when 'hostparam'
136
- result['host_parameters_attributes'].push(:name => param['name'], :value => param['value'])
137
-
138
- when 'ip'
139
- result['ip'] = param['value']
140
-
141
- when 'lifecycleenv'
142
- result['content_facet_attributes'] = { 'lifecycle_environment_id' => param['value'] }
143
-
144
- when 'ptable'
145
- result['ptable_id'] = param['value']
146
-
147
- when 'puppetenv'
148
- result['environment_id'] = param['value']
77
+ private
149
78
 
150
- when 'password'
151
- result['root_pass'] = param['value']
79
+ def read_applications
80
+ @applications = AppDefinition.all.map { |elem| { elem.id => elem.name } }.reduce({}) { |h, v| h.merge v }
81
+ end
152
82
 
153
- end
83
+ def collect_host_report_data(app_hosts)
84
+ report_data = []
85
+ app_hosts.each do |host_data|
86
+ host = Host.find(host_data['foreman_host_id'])
87
+ report_data << { id: host.id, name: host_data['hostname'], hostname: host.hostname, hostUrl: host_path(host), progress_report_id: host.progress_report_id}
154
88
  end
155
- result
89
+ report_data
156
90
  end
157
91
  end
158
92
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ module Concerns
5
+ # Parameters for AnsiblePlaybooks
6
+ module AnsiblePlaybookParameters
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ def ansible_playbook_params_filter
11
+ Foreman::ParameterFilter.new(::ForemanAcd::AnsiblePlaybook).tap do |filter|
12
+ filter.permit(:name, :description, :scm_type, :path, :playfile)
13
+ end
14
+ end
15
+ end
16
+
17
+ def ansible_playbook_params
18
+ param_name = parameter_filter_context.api? ? 'ansible_playbook' : 'foreman_acd_ansible_playbook'
19
+ self.class.ansible_playbook_params_filter.filter_params(params, parameter_filter_context, param_name)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -9,7 +9,7 @@ module ForemanAcd
9
9
  class_methods do
10
10
  def app_definition_params_filter
11
11
  Foreman::ParameterFilter.new(::ForemanAcd::AppDefinition).tap do |filter|
12
- filter.permit(:name, :description, :hostgroup_id, :parameters)
12
+ filter.permit(:name, :description, :acd_ansible_playbook_id, :services, :ansible_vars_all)
13
13
  end
14
14
  end
15
15
  end
@@ -9,7 +9,7 @@ module ForemanAcd
9
9
  class_methods do
10
10
  def app_instance_params_filter
11
11
  Foreman::ParameterFilter.new(::ForemanAcd::AppInstance).tap do |filter|
12
- filter.permit(:name, :app_definition_id, :description, :parameters)
12
+ filter.permit(:name, :app_definition_id, :description, :hosts, :ansible_vars_all)
13
13
  end
14
14
  end
15
15
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # Class to run remote execution jobs
5
+ class RemoteExecutionController < JobInvocationsController
6
+
7
+ def new
8
+ jobs = init_configuration
9
+ @composer = jobs.first
10
+ end
11
+
12
+ def create
13
+ customize_first = params[:customize] || false
14
+ jobs = init_configuration
15
+
16
+ if jobs.count == 1 && customize_first == false
17
+ @composer = jobs.first
18
+ @composer.trigger!
19
+ redirect_to job_invocation_path(@composer.job_invocation)
20
+ elsif customize_first == false
21
+ jobs.each do |composer|
22
+ composer.trigger
23
+ end
24
+ # redirect to the job itself if we want to customize the job
25
+ redirect_to job_invocations_path
26
+ else
27
+ @composer = jobs.first
28
+ render :action => 'new'
29
+ end
30
+ end
31
+
32
+ # to overcome the isolated namespace engine difficulties with paths
33
+ helper Rails.application.routes.url_helpers
34
+ def _routes
35
+ Rails.application.routes
36
+ end
37
+
38
+ private
39
+
40
+ def init_configuration
41
+ app_instance = ForemanAcd::AppInstance.find_by(:name => params[:id])
42
+ app_configurator = ForemanAcd::AppConfigurator.new(app_instance)
43
+
44
+ jobs = app_configurator.configure
45
+ logger.debug("Creating #{jobs.count} job(s) to configure the app #{app_instance}. Customize first: #{params[:customize]}")
46
+ return jobs
47
+ end
48
+ end
49
+ end
@@ -6,16 +6,19 @@ class UiAcdController < ::Api::V2::BaseController
6
6
  @app_data = {}
7
7
  app_definition = ForemanAcd::AppDefinition.find(params[:id])
8
8
  @app_data['app_definition'] = app_definition
9
- @app_data['fdata'] = collect_fdata(app_definition.hostgroup_id)
10
9
  end
11
10
 
12
- def fdata
13
- @fdata = collect_fdata(params['id'])
11
+ def foreman_data
12
+ @foreman_data = collect_foreman_data(params['id'])
13
+ end
14
+
15
+ def ansible_data
16
+ @ansible_data = collect_ansible_data(params['id'])
14
17
  end
15
18
 
16
19
  private
17
20
 
18
- def collect_fdata(hostgroup_id)
21
+ def collect_foreman_data(hostgroup_id)
19
22
  hg = Hostgroup.find(hostgroup_id)
20
23
  fdata = OpenStruct.new(
21
24
  :environments => Environment.all,
@@ -27,4 +30,8 @@ class UiAcdController < ::Api::V2::BaseController
27
30
  )
28
31
  fdata
29
32
  end
33
+
34
+ def collect_ansible_data(playbook_id)
35
+ ForemanAcd::AnsiblePlaybook.find(playbook_id).as_unified_structobj
36
+ end
30
37
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # Ansible playbook
5
+ class AnsiblePlaybook < ApplicationRecord
6
+ include Authorizable
7
+ extend FriendlyId
8
+ friendly_id :name
9
+
10
+ self.table_name = 'acd_ansible_playbooks'
11
+ has_many :app_definitions, :inverse_of => :ansible_playbook, :foreign_key => 'acd_ansible_playbook_id', dependent: :restrict_with_error
12
+ validates :name, :presence => true, :uniqueness => true
13
+ scoped_search :on => :name
14
+ default_scope -> { order("acd_ansible_playbooks.name") }
15
+
16
+ def self.humanize_class_name(_name = nil)
17
+ _('Ansible playbook')
18
+ end
19
+
20
+ def self.permission_name
21
+ 'ansible_playbooks'
22
+ end
23
+
24
+ def content
25
+ case scm_type
26
+ when 'directory'
27
+ File.read(File.join(path, playfile))
28
+ else
29
+ raise NotImplementedError.new "scm_type #{scm_type.inspect} not supported!"
30
+ end
31
+ end
32
+
33
+ def as_unified_structobj
34
+ groups = []
35
+
36
+ JSON.load(self.vars).each do |group_name, vars|
37
+ groups << OpenStruct.new(
38
+ :name => group_name,
39
+ :vars => vars.map { |k,v| OpenStruct.new(:name => k, :value => v) })
40
+ end
41
+
42
+ adata = OpenStruct.new(
43
+ :id => self.id,
44
+ :name => self.name,
45
+ :groups => JSON.load(self.vars)
46
+ )
47
+ adata
48
+ end
49
+ end
50
+ end
@@ -8,9 +8,10 @@ module ForemanAcd
8
8
  friendly_id :name
9
9
 
10
10
  validates :name, :presence => true, :uniqueness => true
11
- belongs_to :hostgroup, :class_name => '::Hostgroup'
12
11
  has_many :app_instances, :inverse_of => :app_definition, :dependent => :destroy
12
+ belongs_to :ansible_playbook, :inverse_of => :app_definitions, :foreign_key => :acd_ansible_playbook_id
13
13
  scoped_search :on => :name
14
+ default_scope -> { order("app_definitions.name") }
14
15
 
15
16
  def self.humanize_class_name(_name = nil)
16
17
  _('App Definition')
@@ -9,6 +9,7 @@ module ForemanAcd
9
9
  validates :name, :presence => true, :uniqueness => true
10
10
  belongs_to :app_definition, :inverse_of => :app_instances
11
11
  scoped_search :on => :name
12
+ default_scope -> { order("app_instances.name") }
12
13
 
13
14
  def self.humanize_class_name(_name = nil)
14
15
  _('App Instance')
@@ -17,5 +18,11 @@ module ForemanAcd
17
18
  def self.permission_name
18
19
  'app_instances'
19
20
  end
21
+
22
+ def foreman_hosts
23
+ app_hosts = JSON.parse(self.hosts)
24
+ host_ids = app_hosts.select{ |h| h&.key?('foreman_host_id') }.map{ |h| h['foreman_host_id'] }
25
+ ::Host.find(host_ids)
26
+ end
20
27
  end
21
28
  end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # application instances configurator
5
+ class AppConfigurator
6
+
7
+ delegate :logger, :to => :Rails
8
+
9
+ def initialize(app_instance)
10
+ @app_instance = app_instance
11
+ end
12
+
13
+ def configure
14
+ job_input = {}
15
+ job_input['application_name'] = @app_instance.name
16
+ job_input['playbook_name'] = @app_instance.app_definition.ansible_playbook.name
17
+ job_input['playbook_path'] = File.join(@app_instance.app_definition.ansible_playbook.path,
18
+ @app_instance.app_definition.ansible_playbook.playfile)
19
+
20
+ # TODO should or do we really need it to a file?
21
+ #inventory_file = File.new("/tmp/acd_inventory_file", "w") # we can also use Tempfile.new() but a tempfile will be deleted soon (after transaction finished)
22
+ #inventory_file << inventory.to_yaml
23
+ #inventory_file.close
24
+
25
+ logger.info("Use inventory to configure #{@app_instance.name} with ansible playbook #{@app_instance.app_definition.ansible_playbook.name}")
26
+
27
+ begin
28
+ proxy_hosts = {}
29
+ jobs = []
30
+
31
+ hosts = @app_instance.foreman_hosts
32
+ proxy_selector = RemoteExecutionProxySelector.new
33
+ hosts.each do |h|
34
+ proxy = proxy_selector.determine_proxy(h, 'SSH')
35
+ unless proxy_hosts.has_key?(proxy.name)
36
+ proxy_hosts[proxy.name] = Array.new
37
+ end
38
+ proxy_hosts[proxy.name] << h.id
39
+ end
40
+
41
+ # TODO just for testing...
42
+ #proxy_hosts = { Host.first.name => [ Host.first.id] }
43
+
44
+ # we need to compose multiple jobs. for each proxy one job.
45
+ proxy_hosts.each do |proxy_name, host_names|
46
+ # create the inventory file
47
+ inventory = ForemanAcd::InventoryCreator.new(@app_instance, host_names).create_inventory
48
+ job_input['inventory'] = YAML.dump(inventory)
49
+
50
+ # Unfortunately, we can not use "JobInvocationComposer.for_feature" method
51
+ # because then its not possible to the set effective_user
52
+ job = JobTemplate.find(RemoteExecutionFeature.feature('run_acd_ansible_playbook').job_template_id)
53
+ params = {
54
+ :job_category => job.job_category,
55
+ :job_template_id => job.id,
56
+ :targeting_type => 'static_query',
57
+ :search_query => "name = #{proxy_name}",
58
+ :effective_user => 'foreman-proxy',
59
+ :inputs => job_input.to_hash
60
+ }
61
+ composer = JobInvocationComposer.from_api_params(params)
62
+ jobs << composer
63
+ end
64
+ rescue StandardError => e
65
+ logger.error("Failed to configure hosts: #{e.class}: #{e.message}\n#{e.backtrace.join($INPUT_RECORD_SEPARATOR)}")
66
+ end
67
+ jobs
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAcd
4
+ # application instances deployer
5
+ class AppDeployer
6
+
7
+ delegate :logger, :to => :Rails
8
+
9
+ def initialize(app_instance)
10
+ @app_instance = app_instance
11
+ end
12
+
13
+ def deploy
14
+ services = JSON.parse(@app_instance.app_definition.services)
15
+ app_hosts = JSON.parse(@app_instance.hosts)
16
+
17
+ app_hosts.each do |host_data|
18
+ begin
19
+ service_data = services.select { |k| k['id'] == host_data['service'].to_i }.first
20
+ host_params = set_host_params(host_data, service_data)
21
+
22
+ host = nil
23
+ if host_data.has_key?('foreman_host_id')
24
+ logger.debug("Try to find host with id #{host_data['foreman_host_id']}")
25
+ begin
26
+ host = Host.find(host_data['foreman_host_id'])
27
+ rescue ActiveRecord::RecordNotFound
28
+ logger.info("Host with id #{host_data['foreman_host_id']} couldn\'t be found, create a new one!")
29
+ host = nil
30
+ end
31
+ end
32
+
33
+ if host.nil?
34
+ params = host_attributes(host_params)
35
+ logger.info("Host creation parameters for #{host_data['hostname']}:\n#{params}\n")
36
+ host = Host.new(params)
37
+ else
38
+ logger.info("Update parameters and re-deploy host #{host_data['hostname']}")
39
+ host.attributes = host_attributes(host_params, host)
40
+ host.setBuild
41
+ host.power.reset
42
+ end
43
+
44
+ # REMOVE ME (but very nice for testing)
45
+ # host.mac = "00:11:22:33:44:55"
46
+
47
+ apply_compute_profile(host)
48
+ host.suggest_default_pxe_loader
49
+ host.save
50
+
51
+ # save the foreman host id
52
+ host_data['foreman_host_id'] = host.id
53
+ rescue StandardError => e
54
+ logger.error("Failed to initiate host creation: #{e.class}: #{e.message}\n#{e.backtrace.join($INPUT_RECORD_SEPARATOR)}")
55
+ end
56
+ end
57
+
58
+ return app_hosts
59
+ end
60
+
61
+ private
62
+
63
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
64
+ def apply_compute_profile(host)
65
+ host.apply_compute_profile(InterfaceMerge.new(:merge_compute_attributes => true))
66
+ host.apply_compute_profile(ComputeAttributeMerge.new)
67
+ end
68
+
69
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
70
+ def host_attributes(params, host = nil)
71
+ return {} if params.nil?
72
+
73
+ params = params.deep_clone
74
+ if params[:interfaces_attributes]
75
+ # handle both hash and array styles of nested attributes
76
+ params[:interfaces_attributes] = params[:interfaces_attributes].values if params[:interfaces_attributes].is_a?(Hash) || params[:interfaces_attributes].is_a?(ActionController::Parameters)
77
+ # map interface types
78
+ params[:interfaces_attributes] = params[:interfaces_attributes].map do |nic_attr|
79
+ interface_attributes(nic_attr, :allow_nil_type => host.nil?)
80
+ end
81
+ end
82
+ params = host.apply_inherited_attributes(params) if host
83
+ params
84
+ end
85
+
86
+ # Copied from foreman/app/controllers/api/v2/hosts_controller.rb
87
+ def interface_attributes(params, allow_nil_type: false)
88
+ params[:type] = InterfaceTypeMapper.map(params[:type]) if params.key?(:type) || allow_nil_type
89
+ params
90
+ end
91
+
92
+ def hardcoded_params
93
+ result = {}
94
+ result['managed'] = true
95
+ result['enabled'] = true
96
+ result['build'] = true
97
+ result['compute_attributes'] = { 'start' => '1' }
98
+ result['host_parameters_attributes'] = []
99
+ result
100
+ end
101
+
102
+ def set_host_params(host_data, service_data)
103
+ result = hardcoded_params
104
+ result['name'] = host_data['hostname']
105
+ result['hostgroup_id'] = service_data['hostgroup']
106
+
107
+ host_data['foremanParameters'].each do |param|
108
+ case param['type']
109
+
110
+ when 'computeprofile'
111
+ result['compute_profile_id'] = param['value']
112
+
113
+ when 'domain'
114
+ result['domain_id'] = param['value']
115
+
116
+ when 'hostparam'
117
+ result['host_parameters_attributes'].push(:name => param['name'], :value => param['value'])
118
+
119
+ when 'ip'
120
+ result['ip'] = param['value']
121
+
122
+ when 'lifecycleenv'
123
+ result['content_facet_attributes'] = { 'lifecycle_environment_id' => param['value'] }
124
+
125
+ when 'ptable'
126
+ result['ptable_id'] = param['value']
127
+
128
+ when 'puppetenv'
129
+ result['environment_id'] = param['value']
130
+
131
+ when 'password'
132
+ result['root_pass'] = param['value']
133
+
134
+ end
135
+ end
136
+ result
137
+ end
138
+ end
139
+ end