Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
mriedel-pgdx committed Aug 1, 2017
2 parents 7619263 + 2fa8a2e commit f1b0c3e
Show file tree
Hide file tree
Showing 31 changed files with 247 additions and 98 deletions.
39 changes: 39 additions & 0 deletions .codeclimate.yml
@@ -0,0 +1,39 @@
---
engines:
brakeman:
enabled: true
duplication:
enabled: true
config:
languages:
- ruby
- javascript
- python
- php
fixme:
enabled: true
radon:
enabled: true
rubocop:
enabled: true
checks:
Rubocop/Style/PercentLiteralDelimiters:
enabled: false # it's outdated vs rubocop
ratings:
paths:
- Gemfile.lock
- "**.erb"
- "**.haml"
- "**.rb"
- "**.rhtml"
- "**.slim"
- "**.inc"
- "**.js"
- "**.jsx"
- "**.module"
- "**.php"
- "**.py"
exclude_paths:
- config/
- db/
- test/
10 changes: 9 additions & 1 deletion .rubocop.yml
Expand Up @@ -2,6 +2,8 @@ Rails:
Enabled: true

AllCops:
TargetRubyVersion: 2.1
TargetRailsVersion: 4.2
Exclude:
- 'extras/**/*'
- 'locale/**/*'
Expand All @@ -11,11 +13,17 @@ AllCops:
Style/HashSyntax:
EnforcedStyle: hash_rockets

Style/DotPosition:
Layout/DotPosition:
EnforcedStyle: 'trailing'

Rails/ActionFilter:
EnforcedStyle: action

Style/RaiseArgs:
EnforcedStyle: compact

Style/SymbolArray:
EnforcedStyle: brackets

Style/FormatStringToken:
EnforcedStyle: template
2 changes: 1 addition & 1 deletion Rakefile 100644 → 100755
Expand Up @@ -6,7 +6,7 @@ rescue LoadError
end

require 'rake/testtask'
Rake::TestTask.new("test:core") do |test|
Rake::TestTask.new('test:core') do |test|
test_dir = File.join(File.dirname(__FILE__), 'test/lib')
test.pattern = "#{test_dir}/**/*_test.rb"
test.libs << test_dir
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ansible_roles_controller.rb
Expand Up @@ -49,7 +49,7 @@ def create_importer
end

def no_changed_roles_message
return _('No changes in roles detected.') unless @proxy.present?
return _('No changes in roles detected.') if @proxy.blank?
_('No changes in roles detected on %s.') % @proxy.name
end
end
Expand Up @@ -7,7 +7,8 @@ module HostsControllerExtensions
include ForemanTasks::Triggers

included do
api :POST, '/hosts/:id/play_roles', N_('Plays Ansible roles on a host')
api :POST, '/hosts/:id/play_roles',
N_('Plays Ansible roles on a host')
param :id, String, :required => true

def play_roles
Expand Down
7 changes: 3 additions & 4 deletions app/lib/actions/foreman_ansible/play_host_roles.rb
Expand Up @@ -23,10 +23,9 @@ def plan(host, proxy_selector = ::ForemanAnsible::ProxySelector.new,
end

def humanized_input
_('on host %{name} through %{proxy}') % {
:name => input.fetch(:host, {})[:name],
:proxy => running_proxy_name
}
format(_('on host %{name} through %{proxy}'),
:name => input.fetch(:host, {})[:name],
:proxy => running_proxy_name)
end

private
Expand Down
7 changes: 3 additions & 4 deletions app/lib/actions/foreman_ansible/play_hostgroup_roles.rb
Expand Up @@ -23,10 +23,9 @@ def plan(hostgroup, proxy_selector = ::ForemanAnsible::ProxySelector.new,
end

def humanized_input
_('on host group %{name} through proxy %{proxy}') % {
:name => input.fetch(:hostgroup, {})[:name],
:proxy => running_proxy_name
}
format(_('on host group %{name} through proxy %{proxy}'),
:name => input.fetch(:hostgroup, {})[:name],
:proxy => running_proxy_name)
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/ansible_role.rb
@@ -1,5 +1,5 @@
# Simple model to store basic info about the Ansible role
class AnsibleRole < ActiveRecord::Base
class AnsibleRole < ApplicationRecord
include Authorizable

self.include_root_in_json = false
Expand Down
2 changes: 1 addition & 1 deletion app/models/host_ansible_role.rb
@@ -1,5 +1,5 @@
# Join model that hosts the connection between hosts and ansible_roles
class HostAnsibleRole < ActiveRecord::Base
class HostAnsibleRole < ApplicationRecord
audited :associated_with => :host, :allow_mass_assignment => true
belongs_to_host
belongs_to :ansible_role
Expand Down
2 changes: 1 addition & 1 deletion app/models/hostgroup_ansible_role.rb
@@ -1,5 +1,5 @@
# Join model that hosts the connection between hostgroups and ansible_roles
class HostgroupAnsibleRole < ActiveRecord::Base
class HostgroupAnsibleRole < ApplicationRecord
belongs_to :hostgroup
belongs_to :ansible_role

Expand Down
2 changes: 1 addition & 1 deletion app/services/foreman_ansible/fact_importer.rb
Expand Up @@ -31,7 +31,7 @@ def add_new_facts
end

def add_missing_facts(imported_facts, parent = nil, prefix = '')
imported_facts.select! { |_fact_name, fact_value| !fact_value.nil? }
imported_facts.reject! { |_fact_name, fact_value| fact_value.nil? }

imported_facts.each do |imported_name, imported_value|
fact_fqn = fact_fqn(imported_name, prefix)
Expand Down
34 changes: 26 additions & 8 deletions app/services/foreman_ansible/fact_parser.rb
Expand Up @@ -18,19 +18,19 @@ def environment; end # Don't do anything as there's no env in Ansible

def architecture
name = facts[:ansible_architecture] || facts[:facter_architecture]
Architecture.where(:name => name).first_or_create unless name.blank?
Architecture.where(:name => name).first_or_create if name.present?
end

def model
name = detect_fact([:ansible_product_name, :facter_virtual,
:facter_productname, :facter_model, :model])
Model.where(:name => name.strip).first_or_create unless name.blank?
Model.where(:name => name.strip).first_or_create if name.present?
end

def domain
name = detect_fact([:ansible_domain, :facter_domain,
:ohai_domain, :domain])
Domain.where(:name => name).first_or_create unless name.blank?
Domain.where(:name => name).first_or_create if name.present?
end

def support_interfaces_parsing?
Expand Down Expand Up @@ -65,12 +65,12 @@ def ipmi_interface; end
private

def ansible_interfaces
return [] unless facts[:ansible_interfaces].present?
return [] if facts[:ansible_interfaces].blank?
facts[:ansible_interfaces].sort
end

def ip_from_interface(interface)
return unless facts[:"ansible_#{interface}"]['ipv4'].present?
return if facts[:"ansible_#{interface}"]['ipv4'].blank?
facts[:"ansible_#{interface}"]['ipv4']['address']
end

Expand All @@ -79,10 +79,28 @@ def os_name
facts[:ansible_lsb] && facts[:ansible_lsb]['id']
end

def debian_os_major_sid
case facts[:ansible_distribution_major_version]
when /wheezy/i
'7'
when /jessie/i
'8'
when /stretch/i
'9'
when /buster/i
'10'
end
end

def os_major
facts[:ansible_distribution_major_version] ||
facts[:ansible_lsb] && facts[:ansible_lsb]['major_release'] ||
(facts[:version].split('R')[0] if os_name == 'junos')
if os_name == 'Debian' &&
facts[:ansible_distribution_major_version][%r{\/sid}i]
debian_os_major_sid
else
facts[:ansible_distribution_major_version] ||
facts[:ansible_lsb] && facts[:ansible_lsb]['major_release'] ||
(facts[:version].split('R')[0] if os_name == 'junos')
end
end

def os_release
Expand Down
37 changes: 12 additions & 25 deletions app/services/foreman_ansible/inventory_creator.rb
Expand Up @@ -35,29 +35,13 @@ def host_vars(host)
end

def connection_params(host)
params = {
'ansible_port' => host_port(host),
'ansible_user' => host_user(host),
'ansible_become' => host_become(host),
'ansible_connection' => connection_type(host),
'ansible_ssh_pass' => host_ssh_pass(host),
'ansible_winrm_server_cert_validation' => winrm_cert_validation(host)
}
params = ansible_settings.merge ansible_extra_options(host)
# Backward compatibility for Ansible 1.x
params['ansible_ssh_port'] = params['ansible_port']
params['ansible_ssh_user'] = params['ansible_user']
params
end

def winrm_cert_validation(host)
host.host_params['ansible_winrm_server_cert_validation'] ||
Setting['ansible_winrm_server_cert_validation']
end

def connection_type(host)
host.host_params['ansible_connection'] || Setting['ansible_connection']
end

def host_roles(host)
host.all_ansible_roles.map(&:name)
end
Expand All @@ -70,16 +54,19 @@ def host_params(host)
host.host_params
end

def host_port(host)
host.host_params['ansible_port'] || Setting[:ansible_port]
def ansible_settings
Hash[
%w[port user ssh_pass connection
winrm_server_cert_validation].map do |setting|
["ansible_#{setting}", Setting["ansible_#{setting}"]]
end
]
end

def host_user(host)
host.host_params['ansible_user'] || Setting[:ansible_user]
end

def host_ssh_pass(host)
host.host_params['ansible_ssh_pass'] || Setting[:ansible_ssh_pass]
def ansible_extra_options(host)
host.host_params.select do |key, _|
/ansible_/.match(key) || Setting[key]
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/services/foreman_ansible/ui_roles_importer.rb
Expand Up @@ -6,7 +6,7 @@ def import!
end

def finish_import(changes)
return unless changes.present?
return if changes.blank?
create_new_roles changes['new'] if changes['new']
delete_old_roles changes['obsolete'] if changes['obsolete']
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,3 +1,4 @@
# rubocop:disable BlockLength
Rails.application.routes.draw do
scope '/ansible' do
constraints(:id => %r{[^\/]+}) do
Expand Down
13 changes: 9 additions & 4 deletions db/migrate/20161122154057_automatically_set_role_timestamps.rb
@@ -1,11 +1,16 @@
# Creation and updates timestamps for Ansible Roles
class AutomaticallySetRoleTimestamps < ActiveRecord::Migration
def up
change_column :ansible_roles, :created_at, :datetime, :null => true, :default => nil
change_column :ansible_roles, :updated_at, :datetime, :null => true, :default => nil
change_column :ansible_roles, :created_at, :datetime, :null => true,
:default => nil
change_column :ansible_roles, :updated_at, :datetime, :null => true,
:default => nil
end

def down
change_column :ansible_roles, :created_at, :datetime, :null => false, :default => Time.now.utc
change_column :ansible_roles, :updated_at, :datetime, :null => false, :default => Time.now.utc
change_column :ansible_roles, :created_at, :datetime,
:null => false, :default => Time.now.utc
change_column :ansible_roles, :updated_at, :datetime,
:null => false, :default => Time.now.utc
end
end
4 changes: 2 additions & 2 deletions foreman_ansible_core.gemspec
Expand Up @@ -9,8 +9,8 @@ Gem::Specification.new do |s|
s.homepage = 'https://github.com/theforeman/foreman_ansible'
s.summary = 'Ansible integration with Foreman (theforeman.org): core bits'
s.description = <<DESC
Ansible integration with Foreman - core parts for dealing with Ansible concepts,
usable by foreman_ansible or smart_proxy_ansible to delegate the execution.
Ansible integration with Foreman - core parts for dealing with Ansible concepts,
usable by foreman_ansible or smart_proxy_ansible to delegate the execution.
DESC
s.licenses = ['GPL-3.0']

Expand Down
4 changes: 3 additions & 1 deletion lib/foreman_ansible/engine.rb
Expand Up @@ -5,6 +5,7 @@

module ForemanAnsible
# This engine connects ForemanAnsible with Foreman core
# rubocop:disable ClassLength
class Engine < ::Rails::Engine
engine_name 'foreman_ansible'

Expand All @@ -30,6 +31,7 @@ class Engine < ::Rails::Engine
Foreman::Gettext::Support.add_text_domain locale_domain, locale_dir
end

# rubocop:disable BlockLength
initializer 'foreman_ansible.register_plugin', :before => :finisher_hook do
Foreman::Plugin.register :foreman_ansible do
requires_foreman '>= 1.15'
Expand Down Expand Up @@ -98,7 +100,7 @@ class Engine < ::Rails::Engine
end

initializer 'foreman_ansible.assets.precompile' do |app|
app.config.assets.precompile += %w(foreman_ansible/Ansible.png)
app.config.assets.precompile += %w[foreman_ansible/Ansible.png]
end

initializer 'foreman_ansible.configure_assets', :group => :assets do
Expand Down
43 changes: 43 additions & 0 deletions lib/foreman_ansible_core/command_creator.rb
@@ -0,0 +1,43 @@
module ForemanAnsibleCore
# Creates the actual command to be passed to foreman_tasks_core to run
class CommandCreator
attr_reader :command

def initialize(inventory_file, playbook_file, options = {})
@options = options
@command = [{ 'JSON_INVENTORY_FILE' => inventory_file }]
@command << 'ansible-playbook'
@command = command_options(@command)
@command << playbook_file
end

private

def command_options(command)
command.concat(['-i', json_inventory_script])
command.concat([setup_verbosity]) if verbose?
command.concat(['-T', @options[:timeout]]) unless @options[:timeout].nil?
command
end

def json_inventory_script
File.expand_path('../../bin/json_inventory.sh', File.dirname(__FILE__))
end

def setup_verbosity
verbosity_level = @options[:verbosity_level].to_i
verbosity = '-'
verbosity_level.times do
verbosity += 'v'
end
verbosity
end

def verbose?
verbosity_level = @options[:verbosity_level]
# rubocop:disable Rails/Present
!verbosity_level.nil? && !verbosity_level.empty? &&
verbosity_level.to_i > 0
end
end
end

0 comments on commit f1b0c3e

Please sign in to comment.