foreman_userdata 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +16 -0
- data/Rakefile +1 -1
- data/app/controllers/userdata_controller.rb +8 -5
- data/app/views/foreman/unattended/provisioning_templates/cloud_init/cloud_init_default.erb +39 -0
- data/app/views/foreman/unattended/provisioning_templates/snippet/puppet_setup_cloudinit.erb +85 -0
- data/app/views/foreman/unattended/provisioning_templates/user_data/userdata_open_vm_tools.erb +36 -0
- data/db/seeds.d/{50_cloud_init_template_kind.rb → 050-cloud_init_template_kind.rb} +0 -0
- data/db/seeds.d/103-cloud_init_provisioning_templates.rb +19 -0
- data/lib/foreman_userdata/version.rb +1 -1
- data/lib/tasks/foreman_userdata_tasks.rake +2 -4
- data/test/controllers/userdata_controller_test.rb +12 -12
- data/test/lib/tasks/seeds_test.rb +33 -0
- data/test/test_plugin_helper.rb +0 -15
- data/test/unit/foreman_userdata/renderer_test.rb +103 -0
- metadata +28 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5d6869feb991a28777d8dbb81c5245bf558a1b5749dfa256f4fd4a6e0e5c9db5
|
4
|
+
data.tar.gz: 5b76c05add078863aeed559e72964bdd8896c9bc92c06422e59aa7648fc131dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06075e10a78c2bf3f38f570ff9aee40759ba45604a37dcf1b252d70b52db6f718ece840a018738f0158966a6e17b6c5f3bd33afb8b22d3683e738257b98d5555
|
7
|
+
data.tar.gz: b23336f4d8f2ef1ecb9753355e3821e9f7634abfaa405afdd81036014e95e7418496b071984daffcdd4dd744d78dc7bdacbab28c97fa075de3ff5b010f5a18d9
|
data/README.md
CHANGED
@@ -14,6 +14,22 @@ See [Plugins install instructions](https://theforeman.org/plugins/)
|
|
14
14
|
for how to install Foreman plugins.
|
15
15
|
You need to install the package `tfm-rubygem-foreman_userdata`.
|
16
16
|
|
17
|
+
## Motivation
|
18
|
+
|
19
|
+
This plug-in was developed to be used with VMWare vSphere provisioning from VMWare templates.
|
20
|
+
vSphere offers the functionality to customize the templates during cloning. This is very handy for changing the network settings of a newly cloned VM.
|
21
|
+
Foreman fully supports this feature via a userdata template. [The Foreman handbook](https://theforeman.org/manuals/1.17/index.html#image-provisioning-without-ssh) has details on how to configure this.
|
22
|
+
The amount of things vSphere allows a user to customize are very limited, though. It's not possible to run a finish script or setup puppet with valid certificates on the new VM.
|
23
|
+
This can be worked around by setting up a two step process: vSphere customization is just used to set up the network config of the host. Cloud-init is used to do the rest of the customization on first boot.
|
24
|
+
Templates for Linux hosts are seeded when you install this plugin. Make sure you associate both `CloudInit default` and `UserData open-vm-tools` templates with the operatingsystem of your host and choose them as the default `userdata` and `cloud-init` templates.
|
25
|
+
|
26
|
+
This leads to this workflow:
|
27
|
+
1. An administrator creates a vSphere VM template and makes sure the cloud-init application is installed in the template VM as described below in the client setup section.
|
28
|
+
2. Foreman creates a new cloned VM in vSphere and passes the userdata template (UserData open-vm-tools) to vSphere if the template is properly assigned to the host.
|
29
|
+
3. vSphere changes the network settings of the new host as defined in the userdata template and boots the new VM.
|
30
|
+
4. The VM runs cloud-init and asks Foreman for the cloud-init template (CloudInit default) if the template has been properly assinged to the host. Your VM needs network access to Foreman on ports `tcp/80` and `tcp/443`. Cloud-init then runs the template and executes the defined actions.
|
31
|
+
5. Cloud-init signals Foreman that the host has been built successfully.
|
32
|
+
|
17
33
|
## Client setup
|
18
34
|
|
19
35
|
On RHEL7 using cloud-init from EPEL:
|
data/Rakefile
CHANGED
@@ -51,7 +51,12 @@ class UserdataController < ApplicationController
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def safe_render(template)
|
54
|
-
|
54
|
+
# Foreman >= 1.20
|
55
|
+
if template.respond_to?(:render)
|
56
|
+
template.render(host: @host, params: params)
|
57
|
+
else
|
58
|
+
@host.render_template(template)
|
59
|
+
end
|
55
60
|
rescue StandardError => error
|
56
61
|
Foreman::Logging.exception("Error rendering the #{template.name} template", error)
|
57
62
|
render_error(
|
@@ -60,7 +65,7 @@ class UserdataController < ApplicationController
|
|
60
65
|
:error => error.message,
|
61
66
|
:status => :internal_server_error
|
62
67
|
)
|
63
|
-
|
68
|
+
false
|
64
69
|
end
|
65
70
|
|
66
71
|
def skip_secure_headers
|
@@ -101,9 +106,7 @@ class UserdataController < ApplicationController
|
|
101
106
|
ip = request.env['REMOTE_ADDR']
|
102
107
|
|
103
108
|
# check if someone is asking on behalf of another system (load balancer etc)
|
104
|
-
if request.env['HTTP_X_FORWARDED_FOR'].present? && (ip =~ Regexp.new(Setting[:remote_addr]))
|
105
|
-
ip = request.env['HTTP_X_FORWARDED_FOR']
|
106
|
-
end
|
109
|
+
ip = request.env['HTTP_X_FORWARDED_FOR'] if request.env['HTTP_X_FORWARDED_FOR'].present? && (ip =~ Regexp.new(Setting[:remote_addr]))
|
107
110
|
|
108
111
|
ip
|
109
112
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
<%#
|
2
|
+
kind: cloud_init
|
3
|
+
name: CloudInit default
|
4
|
+
model: ProvisioningTemplate
|
5
|
+
oses:
|
6
|
+
- CentOS
|
7
|
+
- Fedora
|
8
|
+
- Debian
|
9
|
+
- Ubuntu
|
10
|
+
-%>
|
11
|
+
<%#
|
12
|
+
This template accepts the following parameters:
|
13
|
+
- force-puppet: boolean (default=false)
|
14
|
+
- enable-puppetlabs-repo: boolean (default=false)
|
15
|
+
- enable-puppetlabs-pc1-repo: boolean (default=false)
|
16
|
+
- enable-puppetlabs-puppet5-repo: boolean (default=false)
|
17
|
+
%>
|
18
|
+
<%
|
19
|
+
pm_set = @host.puppetmaster.empty? ? false : true
|
20
|
+
puppet_enabled = pm_set || host_param_true?('force-puppet')
|
21
|
+
%>
|
22
|
+
#cloud-config
|
23
|
+
hostname: <%= @host.name %>
|
24
|
+
fqdn: <%= @host %>
|
25
|
+
manage_etc_hosts: true
|
26
|
+
users: {}
|
27
|
+
runcmd:
|
28
|
+
- |
|
29
|
+
<% if puppet_enabled %>
|
30
|
+
<% if host_param_true?('enable-puppetlabs-pc1-repo') || host_param_true?('enable-puppetlabs-repo') || host_param_true?('enable-puppetlabs-puppet5-repo') -%>
|
31
|
+
<%= snippet 'puppetlabs_repo' %>
|
32
|
+
<% end -%>
|
33
|
+
<%= indent(2) { snippet('puppet_setup_cloudinit') } %>
|
34
|
+
<% end -%>
|
35
|
+
|
36
|
+
phone_home:
|
37
|
+
url: <%= foreman_url('built') %>
|
38
|
+
post: []
|
39
|
+
tries: 10
|
@@ -0,0 +1,85 @@
|
|
1
|
+
<%#
|
2
|
+
kind: snippet
|
3
|
+
name: puppet_setup_cloudinit
|
4
|
+
model: ProvisioningTemplate
|
5
|
+
description: this snippet will configure the Puppet agent from within cloudinit
|
6
|
+
snippet: true
|
7
|
+
%>
|
8
|
+
<%
|
9
|
+
os_family = @host.operatingsystem.family
|
10
|
+
os_major = @host.operatingsystem.major.to_i
|
11
|
+
os_name = @host.operatingsystem.name
|
12
|
+
aio_enabled = host_param_true?('enable-puppetlabs-puppet5-repo') || host_param_true?('enable-puppetlabs-pc1-repo') || host_param_true?('enable-puppet4') || host_param_true?('enable-puppet5')
|
13
|
+
if os_family == 'Freebsd'
|
14
|
+
freebsd_package = host_param_true?('enable-puppet5') ? 'puppet5' : 'puppet4'
|
15
|
+
etc_path = '/usr/local/etc/puppet'
|
16
|
+
bin_path = '/usr/local/bin'
|
17
|
+
elsif aio_enabled
|
18
|
+
linux_package = 'puppet-agent'
|
19
|
+
etc_path = '/etc/puppetlabs/puppet'
|
20
|
+
bin_path = '/opt/puppetlabs/bin'
|
21
|
+
else
|
22
|
+
linux_package = os_family == 'Suse' ? 'rubygem-puppet' : 'puppet'
|
23
|
+
etc_path = '/etc/puppet'
|
24
|
+
bin_path = '/usr/bin'
|
25
|
+
end
|
26
|
+
%>
|
27
|
+
|
28
|
+
<% if os_family == 'Debian' -%>
|
29
|
+
apt-get update
|
30
|
+
apt-get install -y <%= linux_package %>
|
31
|
+
<% elsif os_family == 'Freebsd' -%>
|
32
|
+
pkg install -y <%= freebsd_package %>
|
33
|
+
<% elsif os_family == 'Redhat' -%>
|
34
|
+
if [ -f /usr/bin/dnf ]; then
|
35
|
+
dnf -y install <%= linux_package %>
|
36
|
+
else
|
37
|
+
yum -t -y install <%= linux_package %>
|
38
|
+
fi
|
39
|
+
<% elsif os_family == 'Suse' -%>
|
40
|
+
<% if host_param_true?('enable-puppetlabs-pc1-repo') || host_param_true?('enable-puppetlabs-puppet5-repo') -%>
|
41
|
+
rpmkeys --import http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
|
42
|
+
rpmkeys --import http://yum.puppetlabs.com/RPM-GPG-KEY-puppet
|
43
|
+
<% end -%>
|
44
|
+
<% if @host.provision_method == 'image' -%>
|
45
|
+
/usr/bin/zypper -n install <%= linux_package %>
|
46
|
+
<% end -%>
|
47
|
+
|
48
|
+
cat > <%= etc_path %>/puppet.conf << EOF
|
49
|
+
<%= snippet 'puppet.conf' %>
|
50
|
+
EOF
|
51
|
+
|
52
|
+
<% if os_family == 'Redhat' -%>
|
53
|
+
<% if os_major > 6 -%>
|
54
|
+
puppet_unit=puppet
|
55
|
+
/usr/bin/systemctl list-unit-files | grep -q puppetagent && puppet_unit=puppetagent
|
56
|
+
/usr/bin/systemctl enable ${puppet_unit}
|
57
|
+
<% else -%>
|
58
|
+
/sbin/chkconfig --level 345 puppet on
|
59
|
+
<% end -%>
|
60
|
+
<% end -%>
|
61
|
+
<% if os_family == 'Freebsd' -%>
|
62
|
+
echo 'puppet_enable="YES"' >>/etc/rc.conf
|
63
|
+
<% end -%>
|
64
|
+
<% unless aio_enabled -%>
|
65
|
+
<% if os_family == 'Debian' -%>
|
66
|
+
if [ -f "/etc/default/puppet" ]
|
67
|
+
then
|
68
|
+
/bin/sed -i 's/^START=no/START=yes/' /etc/default/puppet
|
69
|
+
fi
|
70
|
+
<%= bin_path %>/puppet agent --enable
|
71
|
+
<% elsif os_family == 'Suse' -%>
|
72
|
+
if [ -f "/etc/sysconfig/puppet" ]
|
73
|
+
then
|
74
|
+
/usr/bin/sed -ie s/^PUPPET_SERVER=.*/PUPPET_SERVER=<%= @host.puppetmaster.blank? ? '' : @host.puppetmaster %>/ /etc/sysconfig/puppet
|
75
|
+
fi
|
76
|
+
<% end -%>
|
77
|
+
<% end -%>
|
78
|
+
<% end -%>
|
79
|
+
<%= bin_path %>/puppet agent --config <%= etc_path %>/puppet.conf --onetime <%= @host.puppetmaster.blank? ? '' : "--server #{@host.puppetmaster}" %> --no-daemonize
|
80
|
+
<% if os_family == 'Suse' || (os_name == 'Debian' && os_major > 8) || (os_name == 'Ubuntu' && os_major >= 15) -%>
|
81
|
+
<%= bin_path %>/puppet resource service puppet enable=true
|
82
|
+
<% if @host.provision_method == 'image' -%>
|
83
|
+
<%= bin_path %>/puppet resource service puppet ensure=running
|
84
|
+
<% end -%>
|
85
|
+
<% end -%>
|
@@ -0,0 +1,36 @@
|
|
1
|
+
<%#
|
2
|
+
kind: user_data
|
3
|
+
name: UserData open-vm-tools
|
4
|
+
model: ProvisioningTemplate
|
5
|
+
oses:
|
6
|
+
- CentOS
|
7
|
+
- Fedora
|
8
|
+
- Debian
|
9
|
+
- Ubuntu
|
10
|
+
-%>
|
11
|
+
# Template for VMWare customization via open-vm-tools
|
12
|
+
|
13
|
+
identity:
|
14
|
+
LinuxPrep:
|
15
|
+
domain: <%= @host.domain %>
|
16
|
+
hostName: <%= @host.shortname %>
|
17
|
+
hwClockUTC: true
|
18
|
+
timeZone: <%= host_param('time-zone') || 'UTC' %>
|
19
|
+
|
20
|
+
globalIPSettings:
|
21
|
+
dnsSuffixList: [<%= @host.domain %>]
|
22
|
+
<%- @host.interfaces.each do |interface| -%>
|
23
|
+
<%- next unless interface.subnet -%>
|
24
|
+
dnsServerList: [<%= interface.subnet.dns_primary %>, <%= interface.subnet.dns_secondary %>]
|
25
|
+
<%- end -%>
|
26
|
+
|
27
|
+
nicSettingMap:
|
28
|
+
<%- @host.interfaces.each do |interface| -%>
|
29
|
+
<%- next unless interface.subnet -%>
|
30
|
+
- adapter:
|
31
|
+
dnsDomain: <%= interface.domain %>
|
32
|
+
dnsServerList: [<%= interface.subnet.dns_primary %>, <%= interface.subnet.dns_secondary %>]
|
33
|
+
gateway: [<%= interface.subnet.gateway %>]
|
34
|
+
ip: <%= interface.ip %>
|
35
|
+
subnetMask: <%= interface.subnet.mask %>
|
36
|
+
<%- end -%>
|
File without changes
|
@@ -0,0 +1,19 @@
|
|
1
|
+
User.as_anonymous_admin do
|
2
|
+
templates = [
|
3
|
+
{ name: 'CloudInit default', source: 'cloud_init/cloud_init_default.erb', template_kind: TemplateKind.find_by(name: 'cloud-init') },
|
4
|
+
{ name: 'puppet_setup_cloudinit', source: 'snippet/puppet_setup_cloudinit.erb', snippet: true },
|
5
|
+
{ name: 'UserData open-vm-tools', source: 'user_data/userdata_open_vm_tools.erb', template_kind: TemplateKind.find_by(name: 'user_data') }
|
6
|
+
]
|
7
|
+
templates.each do |template|
|
8
|
+
template[:contents] = File.read(File.join(ForemanUserdata::Engine.root, 'app/views/foreman/unattended/provisioning_templates', template[:source]))
|
9
|
+
ProvisioningTemplate.where(name: template[:name]).first_or_create do |pt|
|
10
|
+
pt.vendor = 'ForemanUserdata'
|
11
|
+
pt.default = true
|
12
|
+
pt.locked = true
|
13
|
+
pt.name = template[:name]
|
14
|
+
pt.template = template[:contents]
|
15
|
+
pt.template_kind = template[:template_kind] if template[:template_kind]
|
16
|
+
pt.snippet = template[:snippet] if template[:snippet]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -29,7 +29,7 @@ namespace :foreman_userdata do
|
|
29
29
|
"#{ForemanUserdata::Engine.root}/lib/**/*.rb",
|
30
30
|
"#{ForemanUserdata::Engine.root}/test/**/*.rb"]
|
31
31
|
end
|
32
|
-
rescue
|
32
|
+
rescue LoadError
|
33
33
|
puts 'Rubocop not loaded.'
|
34
34
|
end
|
35
35
|
|
@@ -40,6 +40,4 @@ end
|
|
40
40
|
Rake::Task[:test].enhance ['test:foreman_userdata']
|
41
41
|
|
42
42
|
load 'tasks/jenkins.rake'
|
43
|
-
if Rake::Task.task_defined?(:'jenkins:unit')
|
44
|
-
Rake::Task['jenkins:unit'].enhance ['test:foreman_userdata', 'foreman_userdata:rubocop']
|
45
|
-
end
|
43
|
+
Rake::Task['jenkins:unit'].enhance ['test:foreman_userdata', 'foreman_userdata:rubocop'] if Rake::Task.task_defined?(:'jenkins:unit')
|
@@ -2,14 +2,14 @@ require 'test_plugin_helper'
|
|
2
2
|
|
3
3
|
class UserdataControllerTest < ActionController::TestCase
|
4
4
|
context '#user-data' do
|
5
|
-
let(:organization) {
|
6
|
-
let(:tax_location) {
|
5
|
+
let(:organization) { FactoryBot.create(:organization) }
|
6
|
+
let(:tax_location) { FactoryBot.create(:location) }
|
7
7
|
let(:user_data_content) { 'template content user_data' }
|
8
8
|
let(:cloud_init_content) { 'template content cloud-init' }
|
9
|
-
let(:user_data_template_kind) {
|
10
|
-
let(:cloud_init_template_kind) {
|
9
|
+
let(:user_data_template_kind) { FactoryBot.create(:template_kind, :name => 'user_data') }
|
10
|
+
let(:cloud_init_template_kind) { FactoryBot.create(:template_kind, :name => 'cloud-init') }
|
11
11
|
let(:user_data_template) do
|
12
|
-
|
12
|
+
FactoryBot.create(
|
13
13
|
:provisioning_template,
|
14
14
|
:template_kind => user_data_template_kind,
|
15
15
|
:template => user_data_content,
|
@@ -18,7 +18,7 @@ class UserdataControllerTest < ActionController::TestCase
|
|
18
18
|
)
|
19
19
|
end
|
20
20
|
let(:cloud_init_template) do
|
21
|
-
|
21
|
+
FactoryBot.create(
|
22
22
|
:provisioning_template,
|
23
23
|
:template_kind => cloud_init_template_kind,
|
24
24
|
:template => cloud_init_content,
|
@@ -27,7 +27,7 @@ class UserdataControllerTest < ActionController::TestCase
|
|
27
27
|
)
|
28
28
|
end
|
29
29
|
let(:os) do
|
30
|
-
|
30
|
+
FactoryBot.create(
|
31
31
|
:operatingsystem,
|
32
32
|
:with_associations,
|
33
33
|
:family => 'Redhat',
|
@@ -38,7 +38,7 @@ class UserdataControllerTest < ActionController::TestCase
|
|
38
38
|
)
|
39
39
|
end
|
40
40
|
let(:host) do
|
41
|
-
|
41
|
+
FactoryBot.create(
|
42
42
|
:host,
|
43
43
|
:managed,
|
44
44
|
:operatingsystem => os,
|
@@ -48,7 +48,7 @@ class UserdataControllerTest < ActionController::TestCase
|
|
48
48
|
end
|
49
49
|
|
50
50
|
setup do
|
51
|
-
|
51
|
+
FactoryBot.create(
|
52
52
|
:os_default_template,
|
53
53
|
:template_kind => user_data_template_kind,
|
54
54
|
:provisioning_template => user_data_template,
|
@@ -67,7 +67,7 @@ class UserdataControllerTest < ActionController::TestCase
|
|
67
67
|
|
68
68
|
context 'with cloud-init template' do
|
69
69
|
setup do
|
70
|
-
|
70
|
+
FactoryBot.create(
|
71
71
|
:os_default_template,
|
72
72
|
:template_kind => cloud_init_template_kind,
|
73
73
|
:provisioning_template => cloud_init_template,
|
@@ -84,7 +84,7 @@ class UserdataControllerTest < ActionController::TestCase
|
|
84
84
|
end
|
85
85
|
|
86
86
|
context '#metadata' do
|
87
|
-
let(:host) {
|
87
|
+
let(:host) { FactoryBot.create(:host, :managed) }
|
88
88
|
setup do
|
89
89
|
@request.env['REMOTE_ADDR'] = host.ip
|
90
90
|
end
|
@@ -93,7 +93,7 @@ class UserdataControllerTest < ActionController::TestCase
|
|
93
93
|
get :metadata
|
94
94
|
assert_response :success
|
95
95
|
response = @response.body
|
96
|
-
parsed = YAML.
|
96
|
+
parsed = YAML.safe_load(response)
|
97
97
|
assert_equal host.mac, parsed['mac']
|
98
98
|
assert_equal host.hostname, parsed['hostname']
|
99
99
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_plugin_helper'
|
2
|
+
|
3
|
+
module ForemanUserdata
|
4
|
+
class SeedsTest < ActiveSupport::TestCase
|
5
|
+
setup do
|
6
|
+
Foreman.stubs(:in_rake?).returns(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
test 'seeds cloud-init provisioning templates' do
|
10
|
+
seed
|
11
|
+
assert ProvisioningTemplate.unscoped.where(default: true).exists?
|
12
|
+
expected_template_names = [
|
13
|
+
'CloudInit default',
|
14
|
+
'puppet_setup_cloudinit',
|
15
|
+
'UserData open-vm-tools'
|
16
|
+
]
|
17
|
+
|
18
|
+
seeded_templates = ProvisioningTemplate.unscoped.where(default: true, vendor: 'ForemanUserdata').pluck(:name)
|
19
|
+
|
20
|
+
expected_template_names.each do |template|
|
21
|
+
assert_includes seeded_templates, template
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def seed
|
28
|
+
User.current = FactoryBot.build(:user, admin: true,
|
29
|
+
organizations: [], locations: [])
|
30
|
+
load Rails.root.join('db', 'seeds.rb')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/test/test_plugin_helper.rb
CHANGED
@@ -1,18 +1,3 @@
|
|
1
1
|
# This calls the main test_helper in Foreman-core
|
2
2
|
require 'test_helper'
|
3
3
|
require 'database_cleaner'
|
4
|
-
|
5
|
-
# Foreman's setup doesn't handle cleaning up for Minitest::Spec
|
6
|
-
DatabaseCleaner.strategy = :transaction
|
7
|
-
|
8
|
-
module Minitest
|
9
|
-
class Spec
|
10
|
-
before :each do
|
11
|
-
DatabaseCleaner.start
|
12
|
-
end
|
13
|
-
|
14
|
-
after :each do
|
15
|
-
DatabaseCleaner.clean
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'test_plugin_helper'
|
2
|
+
require 'minitest/hooks'
|
3
|
+
|
4
|
+
module ForemanUserdata
|
5
|
+
class RendererTest < ActiveSupport::TestCase
|
6
|
+
# Disable AR transactional tests as we use DatabaseCleaner's truncation
|
7
|
+
# seeds are loaded once for every context block to speed up tests
|
8
|
+
self.use_transactional_tests = false
|
9
|
+
|
10
|
+
# active_support_test_case_helper loads all fixtures by default
|
11
|
+
# This overrides the seeded data so we reset the fixture list here
|
12
|
+
self.fixture_table_names = []
|
13
|
+
|
14
|
+
fixtures :settings, :taxonomies, :users
|
15
|
+
|
16
|
+
TEMPLATES = [
|
17
|
+
'CloudInit default',
|
18
|
+
'UserData open-vm-tools'
|
19
|
+
].freeze
|
20
|
+
|
21
|
+
setup do
|
22
|
+
User.current = FactoryBot.create(:user, admin: true,
|
23
|
+
organizations: [], locations: [])
|
24
|
+
end
|
25
|
+
|
26
|
+
teardown do
|
27
|
+
User.current = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'templates seeded by this plugin' do
|
31
|
+
include Minitest::Hooks
|
32
|
+
before(:all) do
|
33
|
+
DatabaseCleaner.clean_with :truncation
|
34
|
+
seed
|
35
|
+
end
|
36
|
+
|
37
|
+
after(:all) do
|
38
|
+
DatabaseCleaner.clean_with :truncation
|
39
|
+
end
|
40
|
+
|
41
|
+
let(:host) do
|
42
|
+
FactoryBot.create(
|
43
|
+
:host, :managed,
|
44
|
+
:with_puppet, :with_puppet_ca, :with_environment,
|
45
|
+
:with_subnet, :with_hostgroup
|
46
|
+
).tap do |h|
|
47
|
+
h.subnet.dns_primary = '8.8.8.8'
|
48
|
+
h.subnet.dns_secondary = '8.8.4.4'
|
49
|
+
h.save!
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'safe mode' do
|
54
|
+
setup do
|
55
|
+
Setting[:safemode_render] = true
|
56
|
+
end
|
57
|
+
|
58
|
+
TEMPLATES.each do |template_name|
|
59
|
+
test "renders #{template_name} template without errors" do
|
60
|
+
assert_template(template_name)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'unsafe mode' do
|
66
|
+
setup do
|
67
|
+
Setting[:safemode_render] = false
|
68
|
+
end
|
69
|
+
|
70
|
+
TEMPLATES.each do |template_name|
|
71
|
+
test "renders #{template_name} template without errors" do
|
72
|
+
assert_template(template_name)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def assert_template(template_name)
|
81
|
+
template = ProvisioningTemplate.unscoped.find_by(name: template_name)
|
82
|
+
assert_not_nil template
|
83
|
+
rendered_template = render_template(template)
|
84
|
+
assert rendered_template
|
85
|
+
assert YAML.safe_load(rendered_template)
|
86
|
+
end
|
87
|
+
|
88
|
+
def render_template(template)
|
89
|
+
# Foreman >= 1.20
|
90
|
+
if template.respond_to?(:render)
|
91
|
+
template.render(host: host)
|
92
|
+
else
|
93
|
+
host.render_template(template)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def seed
|
98
|
+
User.current = FactoryBot.build(:user, admin: true,
|
99
|
+
organizations: [], locations: [])
|
100
|
+
load Rails.root.join('db', 'seeds.rb')
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foreman_userdata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Timo Goebel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: minitest-hooks
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.54.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.54.0
|
41
55
|
description: This plug-in adds support for serving user-data for cloud-init to The
|
42
56
|
Foreman.
|
43
57
|
email:
|
@@ -50,14 +64,20 @@ files:
|
|
50
64
|
- README.md
|
51
65
|
- Rakefile
|
52
66
|
- app/controllers/userdata_controller.rb
|
67
|
+
- app/views/foreman/unattended/provisioning_templates/cloud_init/cloud_init_default.erb
|
68
|
+
- app/views/foreman/unattended/provisioning_templates/snippet/puppet_setup_cloudinit.erb
|
69
|
+
- app/views/foreman/unattended/provisioning_templates/user_data/userdata_open_vm_tools.erb
|
53
70
|
- config/routes.rb
|
54
|
-
- db/seeds.d/
|
71
|
+
- db/seeds.d/050-cloud_init_template_kind.rb
|
72
|
+
- db/seeds.d/103-cloud_init_provisioning_templates.rb
|
55
73
|
- lib/foreman_userdata.rb
|
56
74
|
- lib/foreman_userdata/engine.rb
|
57
75
|
- lib/foreman_userdata/version.rb
|
58
76
|
- lib/tasks/foreman_userdata_tasks.rake
|
59
77
|
- test/controllers/userdata_controller_test.rb
|
78
|
+
- test/lib/tasks/seeds_test.rb
|
60
79
|
- test/test_plugin_helper.rb
|
80
|
+
- test/unit/foreman_userdata/renderer_test.rb
|
61
81
|
homepage: http://github.com/theforeman/foreman_userdata
|
62
82
|
licenses:
|
63
83
|
- GPL-3
|
@@ -78,10 +98,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
98
|
version: '0'
|
79
99
|
requirements: []
|
80
100
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.
|
101
|
+
rubygems_version: 2.7.3
|
82
102
|
signing_key:
|
83
103
|
specification_version: 4
|
84
104
|
summary: This plug-in adds support for serving user-data for cloud-init to The Foreman.
|
85
105
|
test_files:
|
86
|
-
- test/
|
106
|
+
- test/unit/foreman_userdata/renderer_test.rb
|
107
|
+
- test/lib/tasks/seeds_test.rb
|
87
108
|
- test/test_plugin_helper.rb
|
109
|
+
- test/controllers/userdata_controller_test.rb
|