Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions app/services/foreman/renderer/scope/macros/loaders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module Loaders
<% end %>", desc: "Prints users in Europe, their login, when a user was logged on for the last time and the authentication source"
end
define_method name do |search: '', includes: nil, preload: nil, joins: nil, select: nil, batch: 1_000, limit: nil|
load_resource(klass: model, search: search, permission: permission, includes: includes, preload: preload, joins: joins, select: select, batch: batch, limit: limit)
load_resource(klass: model, search: search, permission: permission, includes: includes, preload: preload, joins: joins, select: select, batch: batch, limit: limit, defined_as: name)
end
end

Expand All @@ -76,9 +76,12 @@ module Loaders
# .each { |batch| batch.each { |record| record.name }}
# or
# .each_record { |record| record.name }
def load_resource(klass:, search:, permission:, batch: 1_000, includes: nil, limit: nil, select: nil, joins: nil, where: nil, preload: nil)
def load_resource(klass:, search:, permission:, batch: 1_000, includes: nil, limit: nil, select: nil, joins: nil, where: nil, preload: nil, defined_as: __method__)
limit ||= 10 if preview?

type_check!(defined_as, 'select', select, [Symbol, [Array, Symbol]])
type_check!(defined_as, 'joins', joins, [Symbol, Hash, [Array, Symbol]])

base = klass
base = base.search_for(search)
base = base.preload(preload) unless preload.nil?
Expand All @@ -90,6 +93,16 @@ def load_resource(klass:, search:, permission:, batch: 1_000, includes: nil, lim
base = base.select(select) unless select.nil?
base.in_batches(of: batch)
end

def type_check!(method, label, what, spec)
return if what.nil?
return if spec.any? { |type, subtype| what.is_a?(type) && (subtype.nil? || what.all? { |value| value.is_a?(subtype) }) }

options = spec.map { |type, subtype| subtype.nil? ? type : "#{type} of #{subtype.to_s.pluralize}" }
last = options.pop
options = options.join(', ') + " or #{last}"
raise ArgumentError, "Value of '#{label}' passed to #{method} must be #{options}"
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions app/services/foreman/template_snapshot_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def define_host_params(host)
"syspurpose_usage" => "Development/Test",
"syspurpose_sla" => "Self-Support",
"syspurpose_addons" => "first addon, second addon, third addon",
"ansible_user" => "win_ansible_user",
"create_ansible_user" => "true",
"ansible_ssh_pass" => "win_ansible_user_ssh_pass",
}
host_params.each_pair do |name, value|
FactoryBot.build(:host_parameter, host: host, name: name, value: value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ description: |
<LocalAccounts>
<LocalAccount wcm:action="add">
<Password>
<Value>![CDATA[<%= host_param('ansible_ssh_pass') %>]]</Value>
<Value><![CDATA[<%= host_param('ansible_ssh_pass') %>]]></Value>
<PlainText>true</PlainText>
</Password>
<Description>Ansible login service user</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description: |
os_family = @host.operatingsystem.family
os_name = @host.operatingsystem.name

aio_enabled = host_param_true?('enable-puppetlabs-repo') || host_param_true?('enable-official-puppet8-repo') || host_param_true?('enable-official-puppet7-repo') || host_param_true?('enable-puppet7') || host_param_true?('enable-puppetlabs-puppet6-repo') || host_param_true?('enable-puppet6') || host_param_true?('enable-puppetlabs-puppet5-repo') || host_param_true?('enable-puppet5')
aio_enabled = host_param_true?('enable-puppetlabs-repo') || host_param_true?('enable-official-puppet8-repo') || host_param_true?('enable-puppet8') || host_param_true?('enable-official-puppet7-repo') || host_param_true?('enable-puppet7') || host_param_true?('enable-puppetlabs-puppet6-repo') || host_param_true?('enable-puppet6') || host_param_true?('enable-puppetlabs-puppet5-repo') || host_param_true?('enable-puppet5')
aio_available = os_family == 'Debian' || os_family == 'Redhat' || os_name == 'SLES'

if os_family == 'Windows'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ os_family = @host.operatingsystem.family
os_major = @host.operatingsystem.major.to_i
os_name = @host.operatingsystem.name

aio_enabled = host_param_true?('enable-puppetlabs-repo') || host_param_true?('enable-official-puppet8-repo') || host_param_true?('enable-official-puppet7-repo') || host_param_true?('enable-puppet7') || host_param_true?('enable-puppetlabs-puppet6-repo') || host_param_true?('enable-puppet6') || host_param_true?('enable-puppetlabs-puppet5-repo') || host_param_true?('enable-puppet5')
aio_enabled = host_param_true?('enable-puppetlabs-repo') || host_param_true?('enable-official-puppet8-repo') || host_param_true?('enable-puppet8') || host_param_true?('enable-official-puppet7-repo') || host_param_true?('enable-puppet7') || host_param_true?('enable-puppetlabs-puppet6-repo') || host_param_true?('enable-puppet6') || host_param_true?('enable-puppetlabs-puppet5-repo') || host_param_true?('enable-puppet5')

if host_param('run-puppet-in-installer-tags')
puppet_tags = host_param('run-puppet-in-installer-tags')
Expand Down
2 changes: 1 addition & 1 deletion bundler.d/libvirt.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group :libvirt do
gem 'fog-libvirt', '>= 0.12.0'
gem 'fog-libvirt', '~> 0.12.0'
gem 'ruby-libvirt', '~> 0.5', :require => 'libvirt'
end
48 changes: 48 additions & 0 deletions test/unit/foreman/renderer/scope/macros/loader_macros_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'test_helper'

class LoaderMacrosTest < ActiveSupport::TestCase
setup do
template = OpenStruct.new(
name: 'Test',
template: 'Test'
)
source = Foreman::Renderer::Source::Database.new(
template
)
@scope = Class.new(Foreman::Renderer::Scope::Base) do
include Foreman::Renderer::Scope::Macros::Base
end.send(:new, source: source)
end

describe '#load_resources' do
it 'should accept custom selects' do
@scope.load_hosts(select: :id)
@scope.load_hosts(select: [:id, :name])
end

it 'should reject unacceptable selects' do
assert_raises(ArgumentError) { @scope.load_hosts(select: 'a string value') }
assert_raises(ArgumentError) { @scope.load_hosts(select: {:key => :value}) }
assert_raises(ArgumentError) { @scope.load_hosts(select: [:mixed, 'array']) }
error = assert_raises(ArgumentError) { @scope.load_hosts(select: 7) }
assert_match /Value of 'select'/, error.message
assert_match /load_hosts/, error.message
assert_match /Symbol or Array of Symbols/, error.message
end

it 'should accept custom joins' do
@scope.load_hosts(joins: :interfaces)
@scope.load_hosts(joins: {:interfaces => :subnet})
@scope.load_hosts(joins: [:interfaces, :domain])
end

it 'should reject unacceptable joins' do
assert_raises(ArgumentError) { @scope.load_hosts(joins: 'a string value') }
assert_raises(ArgumentError) { @scope.load_hosts(joins: [:mixed, 'array']) }
error = assert_raises(ArgumentError) { @scope.load_hosts(joins: 7) }
assert_match /Value of 'joins'/, error.message
assert_match /load_hosts/, error.message
assert_match /Symbol, Hash or Array of Symbols/, error.message
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ w32tm /resync

powershell /c "Get-NetConnectionProfile -InterfaceAlias \"Ethernet0\" | Set-NetConnectionProfile -NetworkCategory Private"

powershell /c "set-localuser -name win_ansible_user -passwordneverexpires 1"
powershell /c "Enable-PSRemoting"
cmd /c "netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in localport=5985 protocol=TCP action=allow"
cmd /c winrm set winrm/config/service @{AllowUnencrypted="true"}
cmd /c winrm set winrm/config/client/auth @{Basic="true"}
cmd /c winrm set winrm/config/service/auth @{Basic="true"}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@
<Value>$1$rtd8Ub7R$5Ohzuy8WXlkaK9cA2T1wb0</Value>
<PlainText>false</PlainText>
</AdministratorPassword>
</UserAccounts>
<LocalAccounts>
<LocalAccount wcm:action="add">
<Password>
<Value><![CDATA[win_ansible_user_ssh_pass]]></Value>
<PlainText>true</PlainText>
</Password>
<Description>Ansible login service user</Description>
<DisplayName>win_ansible_user</DisplayName>
<Group>Administrators</Group>
<Name>win_ansible_user</Name>
</LocalAccount>
</LocalAccounts>
</UserAccounts>
<TimeZone>GMT Standard Time</TimeZone>
<OOBE>
<HideEULAPage>true</HideEULAPage>
Expand Down