smart_proxy_ansible 3.0.1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 131c5f3a3d6c6f70d8ce1ed672b3f5be31b035d7
4
- data.tar.gz: 05f948836fd8463cf9ad1edda37592e64655a94f
2
+ SHA256:
3
+ metadata.gz: 723f4dc74c428f9eb0d34bda1f492e8b5e49eb2103f16ccb36b279db8dfb3045
4
+ data.tar.gz: 553c1a6918b5f345d7017fe9ce691bffed6903c7d1e5fdbcbe5905a863e70542
5
5
  SHA512:
6
- metadata.gz: f087741a49791fbbb5a981945dcac775a751aeed0c430d4fa06cac1f0c27c181f12fbc42621d6c96e4a017d7c4351e45053cace0b0ba03a19d0e4e535fd461a7
7
- data.tar.gz: 4addf2e1c458730cea7f9361adbf26b1c9f79136f808a8e52e67ff81a22f911143b9b04c9e004581d00a67cf534251cb2054fde5c9b31387594046bcd92a7e9a
6
+ metadata.gz: 2bde47200bda54b10495128b81a19f3f6471f1c9142e034537df43e6d8b6badfcf7685a59923d5bc008263231a64d414ab5c6aecb4f0adf3342f3dfb300a35d1
7
+ data.tar.gz: 6793a645db2e1e1ac75d1136cfa5e10285a2fb6b100d97b7a67613e7e4cac504f0154b398ed066f9ea17cb8f65589a8b76df60fece50764a9bb658c9181d145a
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Proxy plugin to make [foreman_ansible](https://github.com/theforeman/foreman_ansible) actions run in the proxy
4
4
 
5
+ ## Compatibility
6
+
7
+ This plugin requires at least Foreman Proxy 2.3.
8
+
5
9
  ## Installation (in development)
6
10
 
7
11
  ### Prerequisites
@@ -13,34 +13,37 @@ module Proxy
13
13
  get '/roles/variables' do
14
14
  variables = {}
15
15
  RolesReader.list_roles.each do |role_name|
16
- begin
17
- variables[role_name] = extract_variables(role_name)[role_name]
18
- rescue ReadVariablesException => e
19
- # skip what cannot be parsed
20
- logger.error e
21
- end
16
+ variables.merge!(extract_variables(role_name))
17
+ rescue ReadVariablesException => e
18
+ # skip what cannot be parsed
19
+ logger.error e
22
20
  end
23
21
  variables.to_json
24
22
  end
25
23
 
26
24
  get '/roles/:role_name/variables' do |role_name|
27
- begin
28
- extract_variables(role_name).to_json
29
- rescue ReadVariablesException => e
30
- logger.error e
31
- {}.to_json
32
- end
25
+ extract_variables(role_name).to_json
26
+ rescue ReadVariablesException => e
27
+ logger.error e
28
+ {}.to_json
33
29
  end
34
30
 
35
31
  private
36
32
 
37
33
  def extract_variables(role_name)
38
34
  variables = {}
39
- RolesReader.roles_path.split(':').each do |path|
40
- role_path = "#{path}/#{role_name}"
41
- if File.directory?(role_path)
35
+ role_name_parts = role_name.split('.')
36
+ if role_name_parts.count == 3
37
+ RolesReader.collections_paths.split(':').each do |path|
42
38
  variables[role_name] ||= VariablesExtractor
43
- .extract_variables(role_path)
39
+ .extract_variables("#{path}/ansible_collections/#{role_name_parts[0]}/#{role_name_parts[1]}/roles/#{role_name_parts[2]}")
40
+ end
41
+ else
42
+ RolesReader.roles_path.split(':').each do |path|
43
+ role_path = "#{path}/#{role_name}"
44
+ if File.directory?(role_path)
45
+ variables[role_name] ||= VariablesExtractor.extract_variables(role_path)
46
+ end
44
47
  end
45
48
  end
46
49
  variables
@@ -2,11 +2,7 @@ module Proxy
2
2
  module Ansible
3
3
  # Calls for the smart-proxy API to register the plugin
4
4
  class Plugin < Proxy::Plugin
5
- http_rackup_path File.expand_path('http_config.ru',
6
- File.expand_path('../', __FILE__))
7
- https_rackup_path File.expand_path('http_config.ru',
8
- File.expand_path('../', __FILE__))
9
-
5
+ rackup_path File.expand_path('http_config.ru', __dir__)
10
6
  settings_file 'ansible.yml'
11
7
  plugin :ansible, Proxy::Ansible::VERSION
12
8
 
@@ -7,18 +7,31 @@ module Proxy
7
7
  class << self
8
8
  DEFAULT_CONFIG_FILE = '/etc/ansible/ansible.cfg'.freeze
9
9
  DEFAULT_ROLES_PATH = '/etc/ansible/roles:/usr/share/ansible/roles'.freeze
10
+ DEFAULT_COLLECTIONS_PATHS = '/etc/ansible/collections:/usr/share/ansible/collections'.freeze
10
11
 
11
12
  def list_roles
12
- roles_path.split(':').map { |path| read_roles(path) }.flatten
13
+ roles = roles_path.split(':').map { |path| read_roles(path) }.flatten
14
+ collection_roles = collections_paths.split(':').map { |path| read_collection_roles(path) }.flatten
15
+ roles + collection_roles
13
16
  end
14
17
 
15
- def roles_path(roles_line = roles_path_from_config)
16
- # Default to /etc/ansible/roles if none found
17
- return DEFAULT_ROLES_PATH if roles_line.empty?
18
- roles_path_key = roles_line.first.split('=').first.strip
19
- # In case of commented roles_path key "#roles_path", return default
20
- return DEFAULT_ROLES_PATH unless roles_path_key == 'roles_path'
21
- roles_line.first.split('=').last.strip
18
+ def roles_path
19
+ config_path(path_from_config('roles_path'), DEFAULT_ROLES_PATH)
20
+ end
21
+
22
+ def collections_paths
23
+ config_path(path_from_config('collections_paths'), DEFAULT_COLLECTIONS_PATHS)
24
+ end
25
+
26
+ def config_path(config_line, default)
27
+ # Default to /etc/ansible/roles if config_line is empty
28
+ return default if config_line.empty?
29
+
30
+ config_line_key = config_line.first.split('=').first.strip
31
+ # In case of commented roles_path key "#roles_path" or #collections_paths, return default
32
+ return default if ['#roles_path', '#collections_paths'].include?(config_line_key)
33
+
34
+ config_line.first.split('=').last.strip
22
35
  end
23
36
 
24
37
  def logger
@@ -34,30 +47,43 @@ module Proxy
34
47
  private
35
48
 
36
49
  def read_roles(roles_path)
37
- rescue_and_raise_file_exception ReadRolesException,
38
- roles_path, 'roles' do
39
- Dir.glob("#{roles_path}/*").map do |path|
40
- path.split('/').last
41
- end
50
+ glob_path("#{roles_path}/*").map do |path|
51
+ path.split('/').last
42
52
  end
53
+ rescue Errno::ENOENT, Errno::EACCES => e
54
+ logger.debug(e.backtrace)
55
+ message = "Could not read Ansible roles #{roles_path} - #{e.message}"
56
+ raise ReadRolesException.new(message), message
57
+ end
58
+
59
+ def glob_path(path)
60
+ Dir.glob path
61
+
43
62
  end
44
63
 
45
- def roles_path_from_config
46
- rescue_and_raise_file_exception ReadConfigFileException,
47
- DEFAULT_CONFIG_FILE, 'config file' do
48
- File.readlines(DEFAULT_CONFIG_FILE).select do |line|
49
- line =~ /^\s*roles_path/
50
- end
64
+ def read_collection_roles(collections_path)
65
+ Dir.glob("#{collections_path}/ansible_collections/*/*/roles/*").map do |path|
66
+ parts = path.split('/')
67
+ role = parts.pop
68
+ parts.pop
69
+ collection = parts.pop
70
+ author = parts.pop
71
+ "#{author}.#{collection}.#{role}"
51
72
  end
73
+ rescue Errno::ENOENT, Errno::EACCES => e
74
+ logger.debug(e.backtrace)
75
+ message = "Could not read Ansible roles #{collections_path} - #{e.message}"
76
+ raise ReadRolesException.new(message), message
52
77
  end
53
78
 
54
- def rescue_and_raise_file_exception(exception, path, type)
55
- yield
79
+ def path_from_config(config_key)
80
+ File.readlines(DEFAULT_CONFIG_FILE).select do |line|
81
+ line =~ /^\s*#{config_key}/
82
+ end
56
83
  rescue Errno::ENOENT, Errno::EACCES => e
57
84
  logger.debug(e.backtrace)
58
- exception_message = "Could not read Ansible #{type} "\
59
- "#{path} - #{e.message}"
60
- raise exception.new(exception_message), exception_message
85
+ message = "Could not read Ansible config file #{DEFAULT_CONFIG_FILE} - #{e.message}"
86
+ raise ReadConfigFileException.new(message), message
61
87
  end
62
88
  end
63
89
  end
@@ -2,6 +2,6 @@ module Proxy
2
2
  # Version, this allows the proxy and other plugins know
3
3
  # what version of the Ansible plugin is running
4
4
  module Ansible
5
- VERSION = '3.0.1'
5
+ VERSION = '3.1.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_proxy_ansible
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Nečas
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-06-28 00:00:00.000000000 Z
12
+ date: 2021-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -17,28 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '10.0'
20
+ version: '13.0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '10.0'
28
- - !ruby/object:Gem::Dependency
29
- name: minitest
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - "~>"
33
- - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :development
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - "~>"
40
- - !ruby/object:Gem::Version
41
- version: '0'
27
+ version: '13.0'
42
28
  - !ruby/object:Gem::Dependency
43
29
  name: mocha
44
30
  requirement: !ruby/object:Gem::Requirement
@@ -59,14 +45,14 @@ dependencies:
59
45
  requirements:
60
46
  - - "~>"
61
47
  - !ruby/object:Gem::Version
62
- version: '1'
48
+ version: '3'
63
49
  type: :development
64
50
  prerelease: false
65
51
  version_requirements: !ruby/object:Gem::Requirement
66
52
  requirements:
67
53
  - - "~>"
68
54
  - !ruby/object:Gem::Version
69
- version: '1'
55
+ version: '3'
70
56
  - !ruby/object:Gem::Dependency
71
57
  name: rack-test
72
58
  requirement: !ruby/object:Gem::Requirement
@@ -81,20 +67,6 @@ dependencies:
81
67
  - - "~>"
82
68
  - !ruby/object:Gem::Version
83
69
  version: '0'
84
- - !ruby/object:Gem::Dependency
85
- name: rubocop
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - '='
89
- - !ruby/object:Gem::Version
90
- version: 0.32.1
91
- type: :development
92
- prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - '='
96
- - !ruby/object:Gem::Version
97
- version: 0.32.1
98
70
  - !ruby/object:Gem::Dependency
99
71
  name: logger
100
72
  requirement: !ruby/object:Gem::Requirement
@@ -149,8 +121,7 @@ extra_rdoc_files:
149
121
  files:
150
122
  - LICENSE
151
123
  - README.md
152
- - bin/json_inventory.sh
153
- - bundler.plugins.d/smart_proxy_ansible.rb
124
+ - bundler.d/ansible.rb
154
125
  - lib/smart_proxy_ansible.rb
155
126
  - lib/smart_proxy_ansible/api.rb
156
127
  - lib/smart_proxy_ansible/exception.rb
@@ -172,15 +143,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
143
  requirements:
173
144
  - - ">="
174
145
  - !ruby/object:Gem::Version
175
- version: '0'
146
+ version: '2.5'
176
147
  required_rubygems_version: !ruby/object:Gem::Requirement
177
148
  requirements:
178
149
  - - ">="
179
150
  - !ruby/object:Gem::Version
180
151
  version: '0'
181
152
  requirements: []
182
- rubyforge_project:
183
- rubygems_version: 2.6.14
153
+ rubygems_version: 3.1.2
184
154
  signing_key:
185
155
  specification_version: 4
186
156
  summary: Smart-Proxy Ansible plugin
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env bash
2
- # An inventory script to load the inventory from JSON file.
3
- #
4
-
5
- if [ -z "$JSON_INVENTORY_FILE" ]; then
6
- echo "JSON_INVENTORY_FILE not specified"
7
- exit 1
8
- fi
9
-
10
- cat $JSON_INVENTORY_FILE