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

(CAT-372) - add var support to vagrant provisioner #264

Merged
merged 4 commits into from
May 16, 2024
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ Successful on 1 node: localhost
Ran on 1 node in 51.98 seconds
```

For multi-node provisioning, you can assign arbitrary tags to the nodes you deploy, by passing an optional YAML-string 'vars' to the bolt task. In the example below we are assigning the role of `k8s-controller` to the provisioned node.

```ruby
$ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::vagrant --targets localhost action=provision platform=ubuntu/xenial64 inventory=/Users/tp/workspace/git/provision vars='role: k8s-controller'
```

sudo secure_path fix

As some Vagrant boxes do not allow ssh root logins, the **vagrant** user is used to login and *sudo* is used to execute privileged commands as root user.
Expand Down Expand Up @@ -284,13 +290,13 @@ In the provision step you can invoke bundle exec rake 'litmus:provision_list[tes
Manual invocation of the provision service task from a workflow can be done using:

```ruby
bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::provision_service --targets localhost action=provision platform=centos-7-v20200813 inventory=/Users/tp/workspace/git/provision
bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::provision_service --targets localhost action=provision platform=centos-7-v20200813 inventory=/Users/tp/workspace/git/provision vars='role: puppetserver'
```

Or using Litmus:

```ruby
bundle exec rake 'litmus:provision[provision_service, centos-7-v20200813]'
bundle exec rake 'litmus:provision[provision_service, centos-7-v20200813, role: puppetserver]'
```

#### Synced Folders
Expand Down
34 changes: 34 additions & 0 deletions spec/tasks/vagrant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'json'
require 'rspec'
require 'spec_helper'
require 'net/ssh'

describe 'vagrant' do
let(:provider) { 'virtualbox' }
let(:platform) { 'generic/debian10' }

before(:each) do
# Stub $stdin.read to return a predefined JSON string
allow($stdin).to receive(:read).and_return({
platform: platform,
action: 'provision',
vars: 'role: worker1',
inventory: Dir.pwd.to_s,
enable_synced_folder: 'true',
provider: provider,
hyperv_vswitch: 'hyperv_vswitch',
hyperv_smb_username: 'hyperv_smb_username'
}.to_json)
allow(Open3).to receive(:capture3).with(%r{vagrant up --provider #{provider}}, any_args).and_return(['', '', 0]).once
allow(File).to receive(:read).with(%r{#{Dir.pwd}/.vagrant}).and_return('some_unique_id')
allow(Open3).to receive(:capture3).with(%r{vagrant ssh-config}, any_args).and_return(['', '', 0]).once
allow(Net::SSH).to receive(:start).and_return(true)
require_relative '../../tasks/vagrant'
end

it 'provisions a new vagrant box when action is provision' do
expect { vagrant }.to output(%r{"status":"ok"}).to_stdout
expect { vagrant }.to output(%r{"platform":"generic/debian10"}).to_stdout
expect { vagrant }.to output(%r{"role":"worker1"}).to_stdout
end
end
4 changes: 4 additions & 0 deletions tasks/vagrant.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"password": {
"description": "Password to use for Vagrant boxes without the default Vagrant insecure key",
"type": "Optional[String[1]]"
},
"vars": {
"description": "YAML string of key/value pairs to add to the inventory vars section",
"type": "Optional[String[1]]"
}
},
"files": [
Expand Down
13 changes: 11 additions & 2 deletions tasks/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def configure_remoting(platform, remoting_config_path, password)
remoting_config
end

def provision(platform, inventory_location, enable_synced_folder, provider, cpus, memory, hyperv_vswitch, hyperv_smb_username, hyperv_smb_password, box_url, password)
def provision(platform, inventory_location, enable_synced_folder, provider, cpus, memory, hyperv_vswitch, hyperv_smb_username, hyperv_smb_password, box_url, password, vars)
if platform_is_windows?(platform) && !supports_windows_platform?
raise "To provision a Windows VM with this task you must have vagrant 2.2.0 or later installed; vagrant seems to be installed at v#{vagrant_version}"
end
Expand Down Expand Up @@ -186,6 +186,11 @@ def provision(platform, inventory_location, enable_synced_folder, provider, cpus
}
group_name = 'winrm_nodes'
end
# Add the vars hash to the node if they are passed exists
unless vars.nil?
var_hash = YAML.safe_load(vars)
node['vars'] = var_hash
end
add_node_to_group(inventory_hash, node, group_name)
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
{ status: 'ok', node_name: node_name, node: node }
Expand All @@ -212,6 +217,7 @@ def tear_down(node_name, inventory_location)
platform = params['platform']
action = params['action']
node_name = params['node_name']
vars = params['vars']
inventory_location = sanitise_inventory_location(params['inventory'])
enable_synced_folder = params['enable_synced_folder'].nil? ? ENV.fetch('VAGRANT_ENABLE_SYNCED_FOLDER', nil) : params['enable_synced_folder']
enable_synced_folder = enable_synced_folder.casecmp('true').zero? if enable_synced_folder.is_a?(String)
Expand All @@ -238,7 +244,10 @@ def tear_down(node_name, inventory_location)
end

begin
result = provision(platform, inventory_location, enable_synced_folder, provider, cpus, memory, hyperv_vswitch, hyperv_smb_username, hyperv_smb_password, box_url, password) if action == 'provision'
if action == 'provision'
result = provision(platform, inventory_location, enable_synced_folder, provider, cpus, memory, hyperv_vswitch, hyperv_smb_username, hyperv_smb_password, box_url, password,
vars)
end
result = tear_down(node_name, inventory_location) if action == 'tear_down'
puts result.to_json
exit 0
Expand Down
Loading