foreman_ansible_core 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3b57a124c8c6d747fd07e7f6efa4a685af79ccb
4
- data.tar.gz: 72d91c1e01f1d2671973bafd66915d3f720b7c91
3
+ metadata.gz: 248f0459cfdc3a7f98c0fea3ede91120a1c81b2d
4
+ data.tar.gz: 87b41e00dfdfe240d6baefc5ab7b6c4cae7a6054
5
5
  SHA512:
6
- metadata.gz: 05ab152cfca333ca3f870b8b9c1e94077256c323c5f6497c4ea28e47b5261189656dd932a9f9c23b6555a32965c12ed99d2c3f32d01cf4ef2da9f2046fd4b3c3
7
- data.tar.gz: b06e4ca3736986e627f0685549c44b6c25859324e6f810bb7daaa4d50498832017f970d0c2e48ef97f75bdac2bb6f7e57d425228c6aa7621e3fd2e9f0305fe14
6
+ metadata.gz: e2e1a74f84b28a277fa0eb4c1f2f4ba1c841286ab44cafe65248a7d8022b1482747665ed71754fee1a07607a37c1f0cfff625fcceffe3137dfe54050690b84e5
7
+ data.tar.gz: 33a0479840efbee0b96f3ce1d27c2dd01b3c40e8f9b0bf5f4f19f31741965b3f79bd47f006399e1d7ae0ae13c593c292caac128b54088af7e8e2d65caf2714a2
@@ -0,0 +1,35 @@
1
+ module ForemanAnsibleCore
2
+ # Taken from Foreman core, this class creates an error code for any exception
3
+ class Exception < ::StandardError
4
+ def initialize(message, *params)
5
+ @message = message
6
+ @params = params
7
+ end
8
+
9
+ def self.calculate_error_code(classname, message)
10
+ return 'ERF00-0000' if classname.nil? || message.nil?
11
+ basename = classname.split(':').last
12
+ class_hash = Zlib.crc32(basename) % 100
13
+ msg_hash = Zlib.crc32(message) % 10_000
14
+ format 'ERF%02d-%04d', class_hash, msg_hash
15
+ end
16
+
17
+ def code
18
+ @code ||= Exception.calculate_error_code(self.class.name, @message)
19
+ @code
20
+ end
21
+
22
+ def message
23
+ # make sure it works without gettext too
24
+ translated_msg = @message % @params
25
+ "#{code} [#{self.class.name}]: #{translated_msg}"
26
+ end
27
+
28
+ def to_s
29
+ message
30
+ end
31
+ end
32
+
33
+ class ReadConfigFileException < ForemanAnsibleCore::Exception
34
+ end
35
+ end
@@ -16,7 +16,7 @@ module ForemanAnsibleCore
16
16
  def start
17
17
  write_inventory
18
18
  write_playbook
19
- logger.debug('Initializing Ansible Runner')
19
+ logger.debug('[foreman_ansible] - Initializing Ansible Runner')
20
20
  Dir.chdir(@ansible_dir) do
21
21
  initialize_command(*command)
22
22
  end
@@ -26,12 +26,12 @@ module ForemanAnsibleCore
26
26
  command = [{ 'JSON_INVENTORY_FILE' => inventory_file }]
27
27
  command << 'ansible-playbook'
28
28
  command.concat(['-i', json_inventory_script])
29
- if !@options[:verbosity_level].nil? && !@options[:verbosity_level].empty?
29
+ if !@options[:verbosity_level].nil? && !@options[:verbosity_level].empty? && @options[:verbosity_level] > 0
30
30
  command.concat([setup_verbosity])
31
31
  end
32
32
  command.concat(['-T', @options[:timeout]]) unless @options[:timeout].nil?
33
33
  command << playbook_file
34
- logger.debug("Running command #{command}")
34
+ logger.debug("[foreman_ansible] - Running command #{command}")
35
35
  command
36
36
  end
37
37
 
@@ -109,12 +109,11 @@ module ForemanAnsibleCore
109
109
 
110
110
  def setup_verbosity
111
111
  verbosity_level = @options[:verbosity_level].to_i
112
- logger.debug("Setting Ansible verbosity level to #{verbosity_level}")
113
- if verbosity_level > 0
114
- verbosity = '-'
115
- verbosity_level.times do
116
- verbosity += 'v'
117
- end
112
+ logger.debug('[foreman_ansible] - Setting Ansible verbosity level to'\
113
+ "#{verbosity_level}")
114
+ verbosity = '-'
115
+ verbosity_level.times do
116
+ verbosity += 'v'
118
117
  end
119
118
  verbosity
120
119
  end
@@ -0,0 +1,45 @@
1
+ module ForemanAnsibleCore
2
+ # Implements the logic needed to read the roles and associated information
3
+ class RolesReader
4
+ class << self
5
+ DEFAULT_CONFIG_FILE = '/etc/ansible/ansible.cfg'.freeze
6
+
7
+ def list_roles
8
+ logger.info('[foreman_ansible] - Reading roles from '\
9
+ '/etc/ansible/ansible.cfg roles_path')
10
+ Dir.glob("#{roles_path}/*").map do |path|
11
+ path.split('/').last
12
+ end
13
+ rescue Errno::ENOENT, Errno::EACCES => e
14
+ logger.debug("[foreman_ansible] - #{e.backtrace}")
15
+ exception_message = '[foreman_ansible] - Could not read Ansible config'\
16
+ " file #{DEFAULT_CONFIG_FILE} - #{e.message}"
17
+ raise ReadConfigFileException.new(exception_message)
18
+ end
19
+
20
+ def roles_path(ansible_config_file = DEFAULT_CONFIG_FILE)
21
+ default_path = '/etc/ansible/roles'
22
+ roles_line = File.readlines(ansible_config_file).select do |line|
23
+ line =~ /roles_path/
24
+ end
25
+ # Default to /etc/ansible/roles if none found
26
+ return default_path if roles_line.empty?
27
+ roles_path_key = roles_line.first.split('=').first.strip
28
+ # In case of commented roles_path key "#roles_path", return default
29
+ return default_path unless roles_path_key == 'roles_path'
30
+ # In case roles_path is there, and is not commented, return the value
31
+ roles_line.first.split('=').last.strip
32
+ end
33
+
34
+ def logger
35
+ # Return a different logger depending on where ForemanAnsibleCore is
36
+ # running from
37
+ if defined?(::Foreman::Logging)
38
+ ::Foreman::Logging.logger('foreman_ansible')
39
+ else
40
+ ::Proxy::LogBuffer::Decorator.instance
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module ForemanAnsibleCore
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -6,6 +6,7 @@ module ForemanAnsibleCore
6
6
  extend ForemanTasksCore::SettingsLoader
7
7
  register_settings(:ansible, :ansible_dir => '/etc/ansible',
8
8
  :working_dir => nil)
9
+ require 'foreman_ansible_core/exception'
9
10
 
10
11
  if ForemanTasksCore.dynflow_present?
11
12
  require 'foreman_tasks_core/runner'
@@ -13,5 +14,6 @@ module ForemanAnsibleCore
13
14
  require 'foreman_ansible_core/actions'
14
15
  end
15
16
 
17
+ require 'foreman_ansible_core/roles_reader'
16
18
  require 'foreman_ansible_core/version'
17
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_ansible_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Lobato Garcia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-19 00:00:00.000000000 Z
11
+ date: 2017-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -51,7 +51,9 @@ files:
51
51
  - bin/json_inventory.sh
52
52
  - lib/foreman_ansible_core.rb
53
53
  - lib/foreman_ansible_core/actions.rb
54
+ - lib/foreman_ansible_core/exception.rb
54
55
  - lib/foreman_ansible_core/playbook_runner.rb
56
+ - lib/foreman_ansible_core/roles_reader.rb
55
57
  - lib/foreman_ansible_core/version.rb
56
58
  homepage: https://github.com/theforeman/foreman_ansible
57
59
  licenses: