Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9568: Add id autodetection in vagrantfile #84

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 38 additions & 6 deletions vagrant.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

#####################################################################################
# Copyright 2012 Normation SAS
#####################################################################################
Expand Down Expand Up @@ -56,10 +55,37 @@
$windows2008 = "opentable/win-2008-enterprise-amd64-nocm"
$windows2012r2 = "opentable/win-2012r2-standard-amd64-nocm"

# Format pf_name => { 'pf_id' => 0, 'last_host_id' => 0, 'host_list' => [ 'host1', 'host2' ] }
$platforms = {
}
$last_pf_id = 0

def configure_box(config, os, pf_name, host_name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function called elsewhere ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it is meant to be called from the varantfile

setup:'empty', version:nil, server:'', host_list:'',
windows_plugin:false, advanced_reporting:false,
ncf_version:nil, cfengine_version:nil, ram:nil
)
pf = $platforms.fetch(pf_name) { |key|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As i understand the goal is to replace all calls to configure to configure_box in vagrantfile in another pull request ?

But, unless you start all platforms, the platforms variable will always start from scratch and so have id 0 for all platform ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not, the vagrantfile is considered to be a configuration file and not just a script. So it is fully evaluated before launching ant VM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means you need to have only configure_box call in your vagrantfile, if you have a mixed state it won't work ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need something to migrate

$last_pf_id = $last_pf_id+1
{ 'pf_id' => $last_pf_id-1, 'host_list' => [ ]}
}
# autodetect platform id and host id
pf_id = pf['pf_id']
host_id = pf['host_list'].length
pf['host_list'].push(host_name)
host_list = host_list + " "
$platforms[pf_name] = pf
configure(config, os, pf_name, pf_id, host_name, host_id,
setup:setup, version:version, server:server, host_list:host_list,
windows_plugin:windows_plugin, advanced_reporting:advanced_reporting,
ncf_version:ncf_version, cfengine_version:cfengine_version, ram:ram)
end

# keep this function separate for compatibility with older Vagrantfiles
def configure(config, os, pf_name, pf_id, host_name, host_id,
setup:'empty', version:nil, server:nil, host_list:'',
setup:'empty', version:nil, server:'', host_list:'',
windows_plugin:false, advanced_reporting:false,
ncf_version:nil, cfengine_version:nil
ncf_version:nil, cfengine_version:nil, ram:nil
)
# Parameters
dev = false
Expand All @@ -82,6 +108,10 @@ def configure(config, os, pf_name, pf_id, host_name, host_id,
else
memory = 256
end
# override allocated ram
unless ram.nil?
memory = ram
end
memory = memory.to_s
name = pf_name + "_" + host_name
net = "192.168." + (pf_id+40).to_s
Expand All @@ -90,15 +120,15 @@ def configure(config, os, pf_name, pf_id, host_name, host_id,

# provisioning script
if os == $windows7 or os == $windows2008 then
command = "c:/vagrant/scripts/network.cmd #{net} #{host_list}\n"
command = "c:/vagrant/scripts/network.cmd #{net} @host_list@\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the @ mean ? I did'nt find anything relatd in ruby ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing, it is a marker
The goal is to avoid interpreting this variable now because it is not yes full.
It is interpreted at the bottom of the script with a sub function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok ! saw it! command.sub("@host_list@", host_list)

It's confusing with the host_list variable passed as parameter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it could be called host_list_to_replace

if setup != "empty" and setup != "ncf" then
command += "mkdir \"c:/Program Files/Cfengine\"\n"
command += "echo #{server} > \"c:/Program Files/Cfengine/policy_server.dat\"\n"
command += "c:/vagrant/rudder-plugins/Rudder-agent-x64.exe /S\n"
end
else
command = "/vagrant/scripts/cleanbox.sh\n"
command += "/vagrant/scripts/network.sh #{net} \"#{host_list}\"\n"
command += "/vagrant/scripts/network.sh #{net} \"@host_list@\"\n"
if setup != "empty" and setup != "ncf" then
command += "ALLOWEDNETWORK=#{net}.0/24 /usr/local/bin/rudder-setup setup-#{setup} \"#{version}\" \"#{server}\"\n"
end
Expand Down Expand Up @@ -141,7 +171,9 @@ def configure(config, os, pf_name, pf_id, host_name, host_id,
end
server_config.vm.network :private_network, ip: ip
server_config.vm.hostname = host_name
server_config.vm.provision :shell, :inline => command
# this is lazy evaluated and so will contain the last definition of host list
host_list = $platforms[pf_name]['host_list'].join(" ") + " " + host_list
server_config.vm.provision :shell, :inline => command.sub("@host_list@", host_list)
end
end