foreman_ansible 2.2.14 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +2 -0
  3. data/app/controllers/ansible_roles_controller.rb +4 -11
  4. data/app/controllers/ansible_variables_controller.rb +80 -0
  5. data/app/controllers/api/v2/ansible_roles_controller.rb +2 -0
  6. data/app/controllers/api/v2/ansible_variables_controller.rb +68 -0
  7. data/app/controllers/concerns/foreman/controller/parameters/ansible_variable.rb +36 -0
  8. data/app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb +2 -0
  9. data/app/controllers/foreman_ansible/api/v2/hostgroups_param_group_extensions.rb +2 -0
  10. data/app/controllers/foreman_ansible/api/v2/hosts_controller_extensions.rb +2 -0
  11. data/app/controllers/foreman_ansible/api/v2/hosts_param_group_extensions.rb +2 -0
  12. data/app/controllers/foreman_ansible/concerns/hostgroups_controller_extensions.rb +2 -0
  13. data/app/controllers/foreman_ansible/concerns/hosts_controller_extensions.rb +2 -0
  14. data/app/controllers/foreman_ansible/concerns/import_controller_helper.rb +24 -0
  15. data/app/controllers/foreman_ansible/concerns/job_invocation_helper.rb +2 -0
  16. data/app/helpers/foreman_ansible/ansible_plugin_helper.rb +2 -0
  17. data/app/helpers/foreman_ansible/ansible_reports_helper.rb +6 -4
  18. data/app/helpers/foreman_ansible/ansible_roles_helper.rb +2 -0
  19. data/app/helpers/foreman_ansible/hosts_helper_extensions.rb +2 -0
  20. data/app/lib/actions/foreman_ansible/helpers/host_common.rb +2 -0
  21. data/app/lib/actions/foreman_ansible/helpers/play_roles_description.rb +2 -0
  22. data/app/lib/proxy_api/ansible.rb +16 -0
  23. data/app/models/ansible_role.rb +5 -0
  24. data/app/models/ansible_variable.rb +23 -0
  25. data/app/models/concerns/foreman_ansible/host_managed_extensions.rb +3 -1
  26. data/app/models/concerns/foreman_ansible/hostgroup_extensions.rb +2 -0
  27. data/app/models/foreman_ansible/ansible_provider.rb +2 -0
  28. data/app/models/foreman_ansible/fact_name.rb +2 -0
  29. data/app/models/host_ansible_role.rb +2 -0
  30. data/app/models/hostgroup_ansible_role.rb +2 -0
  31. data/app/models/setting/ansible.rb +7 -5
  32. data/app/overrides/ansible_roles_tab.rb +2 -0
  33. data/app/overrides/ansible_variables_edit.rb +15 -0
  34. data/app/overrides/hostgroup_ansible_roles_tab.rb +2 -0
  35. data/app/overrides/hostgroup_play_roles.rb +2 -0
  36. data/app/overrides/report_output.rb +2 -0
  37. data/app/services/foreman_ansible/ansible_report_importer.rb +15 -0
  38. data/app/services/foreman_ansible/ansible_report_scanner.rb +2 -0
  39. data/app/services/foreman_ansible/api_roles_importer.rb +2 -0
  40. data/app/services/foreman_ansible/fact_importer.rb +2 -0
  41. data/app/services/foreman_ansible/fact_parser.rb +13 -1
  42. data/app/services/foreman_ansible/fact_sparser.rb +2 -0
  43. data/app/services/foreman_ansible/insights_notification_builder.rb +2 -0
  44. data/app/services/foreman_ansible/insights_plan_runner.rb +3 -2
  45. data/app/services/foreman_ansible/inventory_creator.rb +0 -6
  46. data/app/services/foreman_ansible/operating_system_parser.rb +3 -1
  47. data/app/services/foreman_ansible/playbook_creator.rb +2 -0
  48. data/app/services/foreman_ansible/proxy_api.rb +24 -0
  49. data/app/services/foreman_ansible/renderer_methods.rb +2 -0
  50. data/app/services/foreman_ansible/roles_importer.rb +4 -17
  51. data/app/services/foreman_ansible/structured_fact_importer.rb +2 -0
  52. data/app/services/foreman_ansible/ui_roles_importer.rb +2 -0
  53. data/app/services/foreman_ansible/variables_importer.rb +100 -0
  54. data/app/views/ansible_roles/index.html.erb +13 -3
  55. data/app/views/ansible_variables/_ansible_roles_list.html.erb +15 -0
  56. data/app/views/ansible_variables/_validator_text.html.erb +5 -0
  57. data/app/views/ansible_variables/edit.html.erb +17 -0
  58. data/app/views/ansible_variables/import.html.erb +53 -0
  59. data/app/views/ansible_variables/index.html.erb +46 -0
  60. data/app/views/api/v2/ansible_roles/import.json.rabl +2 -0
  61. data/app/views/api/v2/ansible_roles/index.json.rabl +2 -0
  62. data/app/views/api/v2/ansible_roles/obsolete.json.rabl +2 -0
  63. data/app/views/api/v2/ansible_roles/show.json.rabl +2 -0
  64. data/app/views/api/v2/ansible_variables/import.json.rabl +3 -0
  65. data/app/views/api/v2/ansible_variables/index.json.rabl +3 -0
  66. data/app/views/api/v2/ansible_variables/obsolete.json.rabl +3 -0
  67. data/app/views/api/v2/ansible_variables/show.json.rabl +20 -0
  68. data/app/views/foreman_ansible/api/v2/ansible_roles/import.json.rabl +2 -0
  69. data/app/views/foreman_ansible/api/v2/ansible_roles/index.json.rabl +2 -0
  70. data/app/views/foreman_ansible/api/v2/ansible_roles/obsolete.json.rabl +2 -0
  71. data/app/views/foreman_ansible/api/v2/ansible_roles/show.json.rabl +2 -0
  72. data/config/routes.rb +17 -0
  73. data/db/migrate/20160705082036_create_ansible_role.rb +2 -0
  74. data/db/migrate/20160706074540_create_join_table_hosts_ansible_roles.rb +2 -0
  75. data/db/migrate/20160707195442_create_host_ansible_roles.rb +2 -0
  76. data/db/migrate/20160729094457_add_columns_to_ansible_role.rb +2 -0
  77. data/db/migrate/20160802153302_create_join_table_hostgroup_ansible_roles.rb +2 -0
  78. data/db/migrate/20160805094233_add_primary_key_hostgroup_ansible_roles.rb +2 -0
  79. data/db/migrate/20161122154057_automatically_set_role_timestamps.rb +2 -0
  80. data/db/migrate/20180410125416_rename_ansible_job_categories.rb +6 -3
  81. data/db/migrate/20180628125416_add_ansible_role_id_to_lookup_keys.rb +14 -0
  82. data/db/seeds.d/62_ansible_proxy_feature.rb +2 -0
  83. data/db/seeds.d/75_job_templates.rb +3 -1
  84. data/db/seeds.d/90_notification_blueprints.rb +2 -0
  85. data/lib/foreman_ansible.rb +2 -0
  86. data/lib/foreman_ansible/engine.rb +2 -0
  87. data/lib/foreman_ansible/register.rb +28 -1
  88. data/lib/foreman_ansible/remote_execution.rb +2 -0
  89. data/lib/foreman_ansible/version.rb +3 -1
  90. data/locale/action_names.rb +2 -2
  91. data/locale/de/LC_MESSAGES/foreman_ansible.mo +0 -0
  92. data/locale/de/foreman_ansible.edit.po +444 -0
  93. data/locale/de/foreman_ansible.po +56 -17
  94. data/locale/de/foreman_ansible.po.time_stamp +0 -0
  95. data/locale/en/LC_MESSAGES/foreman_ansible.mo +0 -0
  96. data/locale/en/foreman_ansible.edit.po +442 -0
  97. data/locale/en/foreman_ansible.po +56 -17
  98. data/locale/en/foreman_ansible.po.time_stamp +0 -0
  99. data/locale/es/LC_MESSAGES/foreman_ansible.mo +0 -0
  100. data/locale/es/foreman_ansible.edit.po +444 -0
  101. data/locale/es/foreman_ansible.po +56 -17
  102. data/locale/es/foreman_ansible.po.time_stamp +0 -0
  103. data/locale/foreman_ansible.pot +174 -93
  104. data/locale/fr/LC_MESSAGES/foreman_ansible.mo +0 -0
  105. data/locale/fr/foreman_ansible.edit.po +444 -0
  106. data/locale/fr/foreman_ansible.po +56 -17
  107. data/locale/fr/foreman_ansible.po.time_stamp +0 -0
  108. data/locale/it/LC_MESSAGES/foreman_ansible.mo +0 -0
  109. data/locale/it/foreman_ansible.edit.po +444 -0
  110. data/locale/it/foreman_ansible.po +56 -17
  111. data/locale/it/foreman_ansible.po.time_stamp +0 -0
  112. data/locale/ja/LC_MESSAGES/foreman_ansible.mo +0 -0
  113. data/locale/ja/foreman_ansible.edit.po +444 -0
  114. data/locale/ja/foreman_ansible.po +56 -17
  115. data/locale/ja/foreman_ansible.po.time_stamp +0 -0
  116. data/locale/ko/LC_MESSAGES/foreman_ansible.mo +0 -0
  117. data/locale/ko/foreman_ansible.edit.po +444 -0
  118. data/locale/ko/foreman_ansible.po +56 -17
  119. data/locale/ko/foreman_ansible.po.time_stamp +0 -0
  120. data/locale/pt_BR/LC_MESSAGES/foreman_ansible.mo +0 -0
  121. data/locale/pt_BR/foreman_ansible.edit.po +444 -0
  122. data/locale/pt_BR/foreman_ansible.po +56 -17
  123. data/locale/pt_BR/foreman_ansible.po.time_stamp +0 -0
  124. data/locale/ru/LC_MESSAGES/foreman_ansible.mo +0 -0
  125. data/locale/ru/foreman_ansible.edit.po +445 -0
  126. data/locale/ru/foreman_ansible.po +56 -17
  127. data/locale/ru/foreman_ansible.po.time_stamp +0 -0
  128. data/locale/zh_CN/LC_MESSAGES/foreman_ansible.mo +0 -0
  129. data/locale/zh_CN/foreman_ansible.edit.po +444 -0
  130. data/locale/zh_CN/foreman_ansible.po +56 -17
  131. data/locale/zh_CN/foreman_ansible.po.time_stamp +0 -0
  132. data/locale/zh_TW/LC_MESSAGES/foreman_ansible.mo +0 -0
  133. data/locale/zh_TW/foreman_ansible.edit.po +444 -0
  134. data/locale/zh_TW/foreman_ansible.po +56 -17
  135. data/locale/zh_TW/foreman_ansible.po.time_stamp +0 -0
  136. data/test/factories/ansible_proxy.rb +2 -0
  137. data/test/factories/ansible_roles.rb +2 -0
  138. data/test/factories/ansible_variables.rb +8 -0
  139. data/test/functional/ansible_roles_controller_test.rb +2 -0
  140. data/test/functional/ansible_variables_controller_test.rb +36 -0
  141. data/test/functional/api/v2/ansible_roles_controller_test.rb +21 -9
  142. data/test/functional/api/v2/ansible_variables_controller_test.rb +41 -0
  143. data/test/functional/api/v2/hostgroups_controller_test.rb +2 -0
  144. data/test/functional/api/v2/hosts_controller_test.rb +2 -0
  145. data/test/functional/hosts_controller_test.rb +2 -0
  146. data/test/test_plugin_helper.rb +2 -0
  147. data/test/unit/ansible_provider_test.rb +2 -0
  148. data/test/unit/ansible_role_test.rb +5 -9
  149. data/test/unit/ansible_variable_test.rb +14 -0
  150. data/test/unit/concerns/config_reports_extensions_test.rb +2 -0
  151. data/test/unit/concerns/host_managed_extensions_test.rb +2 -0
  152. data/test/unit/concerns/hostgroup_extensions_test.rb +2 -0
  153. data/test/unit/helpers/ansible_reports_helper_test.rb +2 -0
  154. data/test/unit/host_ansible_role_test.rb +2 -0
  155. data/test/unit/hostgroup_ansible_role_test.rb +2 -0
  156. data/test/unit/lib/foreman_ansible_core/command_creator_test.rb +2 -0
  157. data/test/unit/lib/foreman_ansible_core/playbook_runner_test.rb +2 -0
  158. data/test/unit/lib/proxy_api/ansible_test.rb +2 -0
  159. data/test/unit/services/ansible_report_importer_test.rb +15 -0
  160. data/test/unit/services/ansible_variables_importer_test.rb +39 -0
  161. data/test/unit/services/api_roles_importer_test.rb +2 -0
  162. data/test/unit/services/fact_importer_test.rb +2 -0
  163. data/test/unit/services/fact_parser_test.rb +47 -0
  164. data/test/unit/services/fact_sparser_test.rb +2 -0
  165. data/test/unit/services/insights_plan_runner_test.rb +2 -0
  166. data/test/unit/services/inventory_creator_test.rb +1 -13
  167. data/test/unit/services/roles_importer_test.rb +2 -0
  168. data/test/unit/services/structured_fact_importer_test.rb +2 -0
  169. data/test/unit/services/ui_roles_importer_test.rb +2 -0
  170. metadata +101 -42
  171. data/test/unit/lib/foreman_ansible_core/roles_reader_test.rb +0 -92
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ipaddress'
2
4
  module ForemanAnsible
3
5
  # Relations to make Host::Managed 'have' ansible roles
@@ -11,7 +13,7 @@ module ForemanAnsible
11
13
  has_many :ansible_roles, :through => :host_ansible_roles,
12
14
  :dependent => :destroy
13
15
  scoped_search :relation => :ansible_roles, :on => :name,
14
- :complete_value => true, :rename => :ansible_role,
16
+ :complete_value => true, :rename => :role,
15
17
  :only_explicit => true
16
18
 
17
19
  before_provision :play_ansible_roles
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Relations to make Hostgroup 'have' ansible roles
3
5
  module HostgroupExtensions
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  if defined? ForemanRemoteExecution
2
4
  module ForemanAnsible
3
5
  # Provider for RemoteExecution that allows to run Ansible playbooks.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Define the class that fact names that come from Ansible should have
3
5
  # It allows us to filter facts by origin, and also to display the origin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Join model that hosts the connection between hosts and ansible_roles
2
4
  class HostAnsibleRole < ApplicationRecord
3
5
  belongs_to_host
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Join model that hosts the connection between hostgroups and ansible_roles
2
4
  class HostgroupAnsibleRole < ApplicationRecord
3
5
  belongs_to :hostgroup
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Setting
2
4
  # Provide settings related with Ansible
3
5
  class Ansible < ::Setting
@@ -59,8 +61,8 @@ class Setting
59
61
  'ansible_post_provision_timeout',
60
62
  N_('Timeout (in seconds) to set when Foreman will trigger a '\
61
63
  'play Ansible roles task after a host is fully provisioned. '\
62
- 'Set this to the maximum time you expect a host to take until'\
63
- ' it is ready after a reboot.'),
64
+ 'Set this to the maximum time you expect a host to take '\
65
+ 'until it is ready after a reboot.'),
64
66
  '360',
65
67
  N_('Post-provision timeout')
66
68
  ),
@@ -68,9 +70,9 @@ class Setting
68
70
  'top_level_ansible_vars',
69
71
  N_('Whether to put Ansible parameters in the "hostvars" '\
70
72
  'top-level key of the inventory. By default it is true, so '\
71
- 'that Host Parameters can be used directly in the playbooks.'\
72
- 'When false, Host Parameters can only be accessed through'\
73
- 'foreman_params["host_parameter"] in the playbooks.'),
73
+ 'that Host Parameters can be used directly in the '\
74
+ 'playbooks. When false, Host Parameters can only be accessed '\
75
+ 'through foreman_params["host_parameter"] in the playbooks.'),
74
76
  true,
75
77
  N_('Top level Ansible variables')
76
78
  ),
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Displays Ansible roles tab on Host form
2
4
  Deface::Override.new(
3
5
  :virtual_path => 'hosts/_form',
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ Deface::Override.new(
4
+ :virtual_path => 'lookup_keys/_fields',
5
+ :name => 'ansible_variables_edit',
6
+ :replace => "erb[loud]:contains('show_puppet_class')",
7
+ :partial => 'ansible_variables/ansible_roles_list'
8
+ )
9
+
10
+ Deface::Override.new(
11
+ :virtual_path => 'lookup_keys/_fields',
12
+ :name => 'ansible_variables_validator_text',
13
+ :replace => '.out.collapse > h6',
14
+ :partial => 'ansible_variables/validator_text'
15
+ )
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Displays Ansible roles tab on Hostgroup form
2
4
  Deface::Override.new(
3
5
  :virtual_path => 'hostgroups/_form',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Displays Ansible roles button in host group action buttons
2
4
  Deface::Override.new(
3
5
  :virtual_path => 'hostgroups/index',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Deface::Override.new(
2
4
  :virtual_path => 'config_reports/_output',
3
5
  :name => 'report_output',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ipaddress'
2
4
  module ForemanAnsible
3
5
  # Ensures Ansible reports from hosts where the IP was used, are assigned
@@ -13,6 +15,19 @@ module ForemanAnsible
13
15
  @host = Nic::Interface.find_by(:ip => hostname).host
14
16
  end
15
17
  super
18
+ partial_hostname_match(hostname)
19
+ end
20
+
21
+ def partial_hostname_match(hostname)
22
+ return @host unless @host.new_record?
23
+ hosts = Host.where(Host.arel_table[:name].matches("#{hostname}.%"))
24
+ if hosts.count > 1
25
+ msg = "More than 1 host found for name #{hostname}, "
26
+ msg += 'please use host FQDN when uploading reports'
27
+ Rails.logger.warn msg
28
+ return @host
29
+ end
30
+ @host = hosts.first || @host
16
31
  end
17
32
  end
18
33
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Scans ConfigReports after import for indicators of an Ansible report and
3
5
  # sets the origin of the report to 'Ansible'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # imports Ansible roles through API
3
5
  class ApiRolesImporter < RolesImporter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Override methods from Foreman app/services/fact_importer so that Ansible
3
5
  # facts are recognized in Foreman as ForemanAnsible facts. It supports
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Override methods from Foreman app/services/fact_parser so that facts
3
5
  # representing host properties are understood when they come from Ansible.
@@ -53,7 +55,11 @@ module ForemanAnsible
53
55
  interface.tr!('-', '_') # virbr1-nic -> virbr1_nic
54
56
  interface_facts = facts[:"ansible_#{interface}"]
55
57
  ipaddress = ip_from_interface(interface)
56
- HashWithIndifferentAccess[interface_facts.merge(:ipaddress => ipaddress)]
58
+ ipaddress6 = ipv6_from_interface(interface)
59
+ HashWithIndifferentAccess[
60
+ interface_facts.merge(:ipaddress => ipaddress,
61
+ :ipaddress6 => ipaddress6)
62
+ ]
57
63
  end
58
64
 
59
65
  def ipmi_interface; end
@@ -70,6 +76,12 @@ module ForemanAnsible
70
76
  facts[:"ansible_#{interface}"]['ipv4']['address']
71
77
  end
72
78
 
79
+ def ipv6_from_interface(interface)
80
+ return if facts[:"ansible_#{interface}"]['ipv6'].blank?
81
+
82
+ facts[:"ansible_#{interface}"]['ipv6'].first['address']
83
+ end
84
+
73
85
  # Returns first non-empty fact. Needed to check for empty strings.
74
86
  def detect_fact(fact_names)
75
87
  facts[
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # See sparse and unsparse documentation
3
5
  class FactSparser
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # A class that builds custom notificaton for REX job if it's insights
3
5
  # remediation feature
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  if defined?(RedhatAccess)
2
4
  module ForemanAnsible
3
5
  # Fetch information about a plan from RH Insights and run it
@@ -30,8 +32,7 @@ if defined?(RedhatAccess)
30
32
  resource = RestClient::Resource.new(
31
33
  "#{insights_api_host}/r/insights/"\
32
34
  "v3/maintenance/#{@plan_id}/playbook",
33
- get_ssl_options_for_org(@organization, nil).\
34
- merge(:proxy => get_portal_http_proxy)
35
+ get_ssl_options_for_org(@organization, nil)
35
36
  )
36
37
  @raw_playbook = resource.get.body
37
38
  YAML.safe_load(@raw_playbook)
@@ -94,7 +94,6 @@ module ForemanAnsible
94
94
  'ansible_become_user' => @template_invocation.effective_user,
95
95
  'ansible_user' => host_setting(host, 'remote_execution_ssh_user'),
96
96
  'ansible_ssh_pass' => rex_ssh_password(host),
97
- 'ansible_sudo_pass' => rex_sudo_password(host),
98
97
  'ansible_ssh_private_key_file' => ansible_or_rex_ssh_private_key(host),
99
98
  'ansible_port' => host_setting(host, 'remote_execution_ssh_port')
100
99
  }
@@ -118,11 +117,6 @@ module ForemanAnsible
118
117
  host_setting(host, 'remote_execution_ssh_password')
119
118
  end
120
119
 
121
- def rex_sudo_password(host)
122
- @template_invocation.job_invocation.sudo_password ||
123
- host_setting(host, 'remote_execution_sudo_password')
124
- end
125
-
126
120
  def ansible_or_rex_ssh_private_key(host)
127
121
  ansible_private_file = host_setting(host, 'ansible_ssh_private_key_file')
128
122
  if !ansible_private_file.empty?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Methods to parse facts related to the OS
3
5
  module OperatingSystemParser
@@ -57,7 +59,7 @@ module ForemanAnsible
57
59
 
58
60
  # rubocop:disable AbcSize
59
61
  def os_minor
60
- _, minor = (os_release.split('.', 2) unless os_release.nil?) ||
62
+ _, minor = (os_release&.split('.', 2)) ||
61
63
  (facts[:version].split('R') if os_name == 'junos')
62
64
  # Until Foreman supports os.minor as something that's not a number,
63
65
  # we should remove the extra dots in the version. E.g:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Service to generate a playbook given roles and a list of hosts
3
5
  class PlaybookCreator
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAnsible
4
+ # Helper methods to create a ProxyAPI object for Ansible
5
+ module ProxyAPI
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ attr_reader :ansible_proxy
10
+
11
+ def find_proxy_api
12
+ if ansible_proxy.blank?
13
+ raise ::Foreman::Exception.new(N_('Proxy not found'))
14
+ end
15
+ @proxy_api = ::ProxyAPI::Ansible.new(:url => ansible_proxy.url)
16
+ end
17
+
18
+ def proxy_api
19
+ return @proxy_api if @proxy_api
20
+ find_proxy_api
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Macro to fetch RH Insights plan playbook
3
5
  module RendererMethods
@@ -1,15 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # Imports roles from smart proxy
3
5
  class RolesImporter
4
- attr_reader :ansible_proxy
6
+ include ::ForemanAnsible::ProxyAPI
5
7
 
6
8
  def initialize(proxy = nil)
7
9
  @ansible_proxy = proxy
8
10
  end
9
11
 
10
12
  def import_role_names
11
- return import_roles remote_roles if ansible_proxy
12
- import_roles local_roles
13
+ import_roles remote_roles if @ansible_proxy.present?
13
14
  end
14
15
 
15
16
  def fetch_role_names
@@ -33,20 +34,6 @@ module ForemanAnsible
33
34
 
34
35
  private
35
36
 
36
- def find_proxy_api
37
- raise ::Foreman::Exception.new(N_('Proxy not found')) unless ansible_proxy
38
- @proxy_api = ::ProxyAPI::Ansible.new(:url => ansible_proxy.url)
39
- end
40
-
41
- def proxy_api
42
- return @proxy_api if @proxy_api
43
- find_proxy_api
44
- end
45
-
46
- def local_roles
47
- ::ForemanAnsibleCore::RolesReader.list_roles
48
- end
49
-
50
37
  def remote_roles
51
38
  proxy_api.roles
52
39
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # On 1.13+ , use the parser for structured facts (like Facter 2) that comes
3
5
  # from core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanAnsible
2
4
  # imports ansible roles through UI
3
5
  class UiRolesImporter < RolesImporter
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanAnsible
4
+ # Methods to transform variable names coming from the proxy into
5
+ # Foreman AnsibleVariable objects
6
+ class VariablesImporter
7
+ include ::ForemanAnsible::ProxyAPI
8
+
9
+ def initialize(proxy = nil)
10
+ @ansible_proxy = proxy
11
+ end
12
+
13
+ def import_variable_names(new_roles)
14
+ return import_variables(remote_variables, new_roles) if @ansible_proxy
15
+ import_variables(local_variables, new_roles)
16
+ end
17
+
18
+ def import_variables(role_variables, new_roles)
19
+ detect_changes(
20
+ role_variables.map do |role_name, variables|
21
+ role = import_new_role(role_name, new_roles)
22
+ next if role.blank?
23
+ initialize_variables(variables, role)
24
+ end.select(&:present?).flatten.compact
25
+ )
26
+ end
27
+
28
+ def import_new_role(role_name, new_roles)
29
+ role = AnsibleRole.find_by(:name => role_name)
30
+ if role.blank? && new_roles.map(&:name).include?(role_name)
31
+ role = new_roles.select { |r| r.name == role_name }.first
32
+ end
33
+ role
34
+ end
35
+
36
+ def initialize_variables(variables, role)
37
+ variables.map do |variable|
38
+ variable = AnsibleVariable.find_or_initialize_by(
39
+ :key => variable
40
+ # :key_type, :default_value, :required
41
+ )
42
+ variable.ansible_role = role
43
+ variable.valid? ? variable : nil
44
+ end
45
+ end
46
+
47
+ def detect_changes(imported)
48
+ changes = {}.with_indifferent_access
49
+ old, changes[:new] = imported.partition { |role| role.id.present? }
50
+ changes[:obsolete] = AnsibleVariable.where.not(:id => old.map(&:id))
51
+ changes
52
+ end
53
+
54
+ def finish_import(new, obsolete)
55
+ results = { :added => [], :obsolete => [] }
56
+ results[:added] = create_new_variables(new) if new.present?
57
+ results[:obsolete] = delete_old_variables(obsolete) if obsolete.present?
58
+ results
59
+ end
60
+
61
+ def create_new_variables(new)
62
+ added = []
63
+ new.each do |role, variables|
64
+ variables.each_value do |variable_properties|
65
+ variable = AnsibleVariable.new(
66
+ JSON.parse(variable_properties)['ansible_variable']
67
+ )
68
+ variable.ansible_role = ::AnsibleRole.find_by(:name => role)
69
+ variable.save
70
+ added << variable.key
71
+ end
72
+ end
73
+ added
74
+ end
75
+
76
+ def delete_old_variables(old)
77
+ removed = []
78
+ old.each_value do |variables|
79
+ variables.each_value do |variable|
80
+ variable = AnsibleVariable.find(
81
+ JSON.parse(variable)['ansible_variable']['id']
82
+ )
83
+ removed << variable.key
84
+ variable.destroy
85
+ end
86
+ end
87
+ removed
88
+ end
89
+
90
+ private
91
+
92
+ def local_variables
93
+ ::AnsibleVariable.all
94
+ end
95
+
96
+ def remote_variables
97
+ proxy_api.all_variables
98
+ end
99
+ end
100
+ end