smart_proxy_ansible 2.1.2 → 3.1.1

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: d9d9191afda919cc1c6d8ed95f374b6e6824075e
4
- data.tar.gz: d8ba3c8dcd1322349adbeba3cb4a272120e47fb5
2
+ SHA256:
3
+ metadata.gz: 8c78dd643ea513a8ffc694a01c86f12495aff1e1b14c7e106db9d29ab529d26d
4
+ data.tar.gz: 8b2fde1a83bf65fb55fc122ce8ff931fd314f5289f9cf34e255d85959bebb653
5
5
  SHA512:
6
- metadata.gz: 8a4d4039752a5b99984f3af42feb6b7525139134312451e91603bf61f18a2c268217d963ac1984aefaacbd9d276fd86f016dc9e599cafceb3d026dc787c2bb30
7
- data.tar.gz: efe3fc55690b2379863cd67a8b474684a4a293e96908782e2ef2229662216967387a072d63763618bf52f9503ed945c8c89204351959bb7cce12c90500883969
6
+ metadata.gz: 695b0b451cc16439fd4fb76f9567f49e971c609c6d4696c19afa5feb3b90ac6a069e392ce38b177111041cae577662683cb6f594e86adc1096a6bfb5d7ac7ccc
7
+ data.tar.gz: 94a5bac7bce83785b0cc9067e9ffd20e9084e8befd575c3f6fae7da5ac7e78b202bf137daa36e6e726a8fd54de190180e42ac1af96ff6b2fcb1b9381364f2a59
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,32 +13,38 @@ 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
- variables[role_name] ||= VariablesExtractor
41
- .extract_variables("#{path}/#{role_name}")
35
+ role_name_parts = role_name.split('.')
36
+ if role_name_parts.count == 3
37
+ RolesReader.collections_paths.split(':').each do |path|
38
+ variables[role_name] = VariablesExtractor
39
+ .extract_variables("#{path}/ansible_collections/#{role_name_parts[0]}/#{role_name_parts[1]}/roles/#{role_name_parts[2]}") if variables[role_name].nil? || variables[role_name].empty?
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
47
+ end
42
48
  end
43
49
  variables
44
50
  end
@@ -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
 
@@ -6,19 +6,32 @@ module Proxy
6
6
  class RolesReader
7
7
  class << self
8
8
  DEFAULT_CONFIG_FILE = '/etc/ansible/ansible.cfg'.freeze
9
- DEFAULT_ROLES_PATH = '/etc/ansible/roles'.freeze
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
@@ -8,7 +8,7 @@ module Proxy
8
8
  def extract_variables(role_path)
9
9
  role_files = Dir.glob("#{role_path}/defaults/**/*.yml") +
10
10
  Dir.glob("#{role_path}/defaults/**/*.yaml")
11
- role_files.flat_map do |role_file|
11
+ role_files.reduce({}) do |memo, role_file|
12
12
  loaded_yaml = {}
13
13
  begin
14
14
  loaded_yaml = YAML.load_file(role_file)
@@ -16,7 +16,7 @@ module Proxy
16
16
  raise ReadVariablesException.new "#{role_file} is not YAML file"
17
17
  end
18
18
  raise ReadVariablesException.new "Could not parse YAML file: #{role_file}" unless loaded_yaml.is_a? Hash
19
- loaded_yaml.keys
19
+ memo.merge loaded_yaml
20
20
  end
21
21
  end
22
22
  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 = '2.1.2'
5
+ VERSION = '3.1.1'
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: 2.1.2
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Nečas
@@ -9,50 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-03-07 00:00:00.000000000 Z
12
+ date: 2021-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: bundler
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: '1.7'
21
- type: :development
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - "~>"
26
- - !ruby/object:Gem::Version
27
- version: '1.7'
28
14
  - !ruby/object:Gem::Dependency
29
15
  name: rake
30
16
  requirement: !ruby/object:Gem::Requirement
31
17
  requirements:
32
18
  - - "~>"
33
19
  - !ruby/object:Gem::Version
34
- version: '10.0'
35
- type: :development
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - "~>"
40
- - !ruby/object:Gem::Version
41
- version: '10.0'
42
- - !ruby/object:Gem::Dependency
43
- name: minitest
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - "~>"
47
- - !ruby/object:Gem::Version
48
- version: '0'
20
+ version: '13.0'
49
21
  type: :development
50
22
  prerelease: false
51
23
  version_requirements: !ruby/object:Gem::Requirement
52
24
  requirements:
53
25
  - - "~>"
54
26
  - !ruby/object:Gem::Version
55
- version: '0'
27
+ version: '13.0'
56
28
  - !ruby/object:Gem::Dependency
57
29
  name: mocha
58
30
  requirement: !ruby/object:Gem::Requirement
@@ -73,14 +45,14 @@ dependencies:
73
45
  requirements:
74
46
  - - "~>"
75
47
  - !ruby/object:Gem::Version
76
- version: '1'
48
+ version: '3'
77
49
  type: :development
78
50
  prerelease: false
79
51
  version_requirements: !ruby/object:Gem::Requirement
80
52
  requirements:
81
53
  - - "~>"
82
54
  - !ruby/object:Gem::Version
83
- version: '1'
55
+ version: '3'
84
56
  - !ruby/object:Gem::Dependency
85
57
  name: rack-test
86
58
  requirement: !ruby/object:Gem::Requirement
@@ -95,20 +67,6 @@ dependencies:
95
67
  - - "~>"
96
68
  - !ruby/object:Gem::Version
97
69
  version: '0'
98
- - !ruby/object:Gem::Dependency
99
- name: rubocop
100
- requirement: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - '='
103
- - !ruby/object:Gem::Version
104
- version: 0.32.1
105
- type: :development
106
- prerelease: false
107
- version_requirements: !ruby/object:Gem::Requirement
108
- requirements:
109
- - - '='
110
- - !ruby/object:Gem::Version
111
- version: 0.32.1
112
70
  - !ruby/object:Gem::Dependency
113
71
  name: logger
114
72
  requirement: !ruby/object:Gem::Requirement
@@ -163,8 +121,7 @@ extra_rdoc_files:
163
121
  files:
164
122
  - LICENSE
165
123
  - README.md
166
- - bin/json_inventory.sh
167
- - bundler.plugins.d/smart_proxy_ansible.rb
124
+ - bundler.d/ansible.rb
168
125
  - lib/smart_proxy_ansible.rb
169
126
  - lib/smart_proxy_ansible/api.rb
170
127
  - lib/smart_proxy_ansible/exception.rb
@@ -186,15 +143,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
143
  requirements:
187
144
  - - ">="
188
145
  - !ruby/object:Gem::Version
189
- version: '0'
146
+ version: '2.5'
190
147
  required_rubygems_version: !ruby/object:Gem::Requirement
191
148
  requirements:
192
149
  - - ">="
193
150
  - !ruby/object:Gem::Version
194
151
  version: '0'
195
152
  requirements: []
196
- rubyforge_project:
197
- rubygems_version: 2.6.8
153
+ rubygems_version: 3.1.2
198
154
  signing_key:
199
155
  specification_version: 4
200
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