Skip to content

Commit

Permalink
Refs #25264 - Rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dLobatog committed Oct 24, 2018
1 parent 850db0b commit a5a1203
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 30 deletions.
2 changes: 2 additions & 0 deletions lib/smart_proxy_ansible.rb
@@ -1,9 +1,11 @@
require 'smart_proxy_dynflow'

module Proxy
# Basic requires for this plugin
module Ansible
require 'smart_proxy_ansible/version'
require 'smart_proxy_ansible/plugin'
require 'smart_proxy_ansible/roles_reader'
require 'smart_proxy_ansible/variables_extractor'
end
end
7 changes: 5 additions & 2 deletions lib/smart_proxy_ansible/api.rb
@@ -1,5 +1,8 @@
module Proxy
module Ansible
# API endpoints. Most of the code should be calling other classes,
# please keep the actual implementation of the endpoints outside
# of this class.
class Api < Sinatra::Base
get '/roles' do
RolesReader.list_roles.to_json
Expand All @@ -22,8 +25,8 @@ class Api < Sinatra::Base
def extract_variables(role_name)
variables = {}
RolesReader.roles_path.split(':').each do |path|
variables[role_name] = VariablesExtractor.
extract_variables("#{path}/#{role_name}")
variables[role_name] = VariablesExtractor
.extract_variables("#{path}/#{role_name}")
end
variables
end
Expand Down
3 changes: 2 additions & 1 deletion lib/smart_proxy_ansible/exception.rb
Expand Up @@ -2,7 +2,8 @@

module Proxy
module Ansible
# Taken from Foreman core, this class creates an error code for any exception
# Taken from Foreman core, this class creates an error code for any
# exception
class Exception < ::StandardError
def initialize(message, *params)
@message = message
Expand Down
26 changes: 10 additions & 16 deletions lib/smart_proxy_ansible/plugin.rb
@@ -1,20 +1,14 @@
module Proxy::Ansible
class Plugin < Proxy::Plugin
http_rackup_path File.expand_path("http_config.ru", File.expand_path("../", __FILE__))
https_rackup_path File.expand_path("http_config.ru", File.expand_path("../", __FILE__))
module Proxy
module Ansible
# Calls for the smart-proxy API to register the plugin
class Plugin < Proxy::Plugin
http_rackup_path File.expand_path('http_config.ru',
File.expand_path('../', __FILE__))
https_rackup_path File.expand_path('http_config.ru',
File.expand_path('../', __FILE__))

settings_file "ansible.yml"
plugin :ansible, Proxy::Ansible::VERSION

after_activation do
begin
require 'smart_proxy_dynflow_core'
require 'foreman_ansible_core'
ForemanAnsibleCore.initialize_settings(Proxy::Ansible::Plugin.settings.to_h)
rescue LoadError => _
# Dynflow core is not available in the proxy, will be handled
# by standalone Dynflow core
end
settings_file 'ansible.yml'
plugin :ansible, Proxy::Ansible::VERSION
end
end
end
6 changes: 3 additions & 3 deletions lib/smart_proxy_ansible/roles_reader.rb
Expand Up @@ -35,7 +35,7 @@ def logger

def read_roles(roles_path)
rescue_and_raise_file_exception ReadRolesException,
roles_path, 'roles' do
roles_path, 'roles' do
Dir.glob("#{roles_path}/*").map do |path|
path.split('/').last
end
Expand All @@ -44,7 +44,7 @@ def read_roles(roles_path)

def roles_path_from_config
rescue_and_raise_file_exception ReadConfigFileException,
DEFAULT_CONFIG_FILE, 'config file' do
DEFAULT_CONFIG_FILE, 'config file' do
File.readlines(DEFAULT_CONFIG_FILE).select do |line|
line =~ /^\s*roles_path/
end
Expand All @@ -57,7 +57,7 @@ def rescue_and_raise_file_exception(exception, path, type)
logger.debug(e.backtrace)
exception_message = "Could not read Ansible #{type} "\
"#{path} - #{e.message}"
raise exception.new(exception_message)
raise exception.new(exception_message), exception_message
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/smart_proxy_ansible/variables_extractor.rb
Expand Up @@ -5,23 +5,23 @@ class VariablesExtractor
class << self
def extract_variables(role_path)
role_files = Dir.glob("#{role_path}/defaults/**/*.yml") +
Dir.glob("#{role_path}/defaults/**/*.yaml")
Dir.glob("#{role_path}/defaults/**/*.yaml")
# not anything matching item, }}, {{, ansible_hostname or 'if'
variables = role_files.map do |role_file|
candidates = File.read(role_file).scan(/{{(.*?)}}/).select do |param|
candidates = File.read(role_file)
.scan(/{{(.*?)}}/).select do |param|
param.first.scan(/item/) == [] && param.first.scan(/if/) == []

end.flatten
# Sometimes inside the {{ }} there's a OR condition. In such a case,
# let's split and choose possible variables (variables cannot contain
# parenthesis)
# let's split and choose possible variables (variables cannot
# contain parenthesis)

candidates.map do |variable|
variable.split('|').map(&:strip).select do |var|
!var.include?('(') && # variables are not parenthesis
!var.include?('[') && # variables are not arrays
!var.include?('.') && # variables are not objects
!var.include?("'") # variables are not plain strings
!var.include?('[') && # variables are not arrays
!var.include?('.') && # variables are not objects
!var.include?("'") # variables are not plain strings
end
end unless candidates.nil?
end.compact.flatten.uniq.map(&:strip)
Expand Down
2 changes: 2 additions & 0 deletions lib/smart_proxy_ansible/version.rb
@@ -1,4 +1,6 @@
module Proxy
# Version, this allows the proxy and other plugins know
# what version of the Ansible plugin is running
module Ansible
VERSION = '2.0.3'
end
Expand Down

0 comments on commit a5a1203

Please sign in to comment.