From da67824a53ba0e7c2db1a9b8e19141c1ffd07bfc Mon Sep 17 00:00:00 2001 From: Pat O'Shea Date: Tue, 7 Jul 2015 00:24:23 -0600 Subject: [PATCH] Adding run_service and minion/master ids for windows --- plugins/provisioners/salt/bootstrap-salt.ps1 | 97 ++++++++++++++------ plugins/provisioners/salt/config.rb | 6 ++ plugins/provisioners/salt/provisioner.rb | 16 +++- 3 files changed, 91 insertions(+), 28 deletions(-) diff --git a/plugins/provisioners/salt/bootstrap-salt.ps1 b/plugins/provisioners/salt/bootstrap-salt.ps1 index 382e1ff81..78f268ea3 100644 --- a/plugins/provisioners/salt/bootstrap-salt.ps1 +++ b/plugins/provisioners/salt/bootstrap-salt.ps1 @@ -1,12 +1,34 @@ Param( - [string]$version + [string]$version, + [string]$runservice, + [string]$minion, + [string]$master ) - -# Salt version to install - default to latest if there is an issue -if ($version -notmatch "201[0-9]\.[0-9]\.[0-9](\-\d{1})?"){ + +# Constants +$ServiceName = "salt-minion" +$startupType = "Manual" + +# Version to install - default to latest if there is an issue +If ($version -notmatch "201[0-9]\.[0-9]\.[0-9](\-\d{1})?"){ $version = '2015.5.2' } +If ($runservice.ToLower() -eq "true"){ + Write-Host "Service is set to run." + [bool]$runservice = $True +} +ElseIf ($runservice.ToLower() -eq "false"){ + Write-Host "Service will be stopped and set to manual." + [bool]$runservice = $False +} +Else { + # Param passed in wasn't clear so defaulting to true. + Write-Host "Service defaulting to run." + [bool]$runservice = $True +} + + # Create C:\tmp\ - if Vagrant doesn't upload keys and/or config it might not exist New-Item C:\tmp\ -ItemType directory -force | out-null @@ -14,32 +36,48 @@ New-Item C:\tmp\ -ItemType directory -force | out-null New-Item C:\salt\conf\pki\minion\ -ItemType directory -force | out-null # Check if minion keys have been uploaded -if (Test-Path C:\tmp\minion.pem) { +If (Test-Path C:\tmp\minion.pem) { cp C:\tmp\minion.pem C:\salt\conf\pki\minion\ cp C:\tmp\minion.pub C:\salt\conf\pki\minion\ } # Detect architecture -if ([IntPtr]::Size -eq 4) { +If ([IntPtr]::Size -eq 4) { $arch = "x86" -} else { +} Else { $arch = "AMD64" } # Download minion setup file -Write-Host "Downloading Salt minion installer $version-$arch..." +Write-Host "Downloading Salt minion installer Salt-Minion-$version-$arch-Setup.exe" $webclient = New-Object System.Net.WebClient $url = "https://docs.saltstack.com/downloads/Salt-Minion-$version-$arch-Setup.exe" $file = "C:\tmp\salt.exe" $webclient.DownloadFile($url, $file) + # Install minion silently Write-Host "Installing Salt minion..." #Wait for process to exit before continuing... -C:\tmp\salt.exe /S | Out-Null +If($PSBoundParameters.ContainsKey('minion') -and $PSBoundParameters.ContainsKey('master')) { + C:\tmp\salt.exe /S /minion-name=$minion /master=$master | Out-Null + Write-Host C:\tmp\salt.exe /S /minion-name=$minion /master=$master | Out-Null +} +ElseIf($PSBoundParameters.ContainsKey('minion') -and !$PSBoundParameters.ContainsKey('master')) { + C:\tmp\salt.exe /S /minion-name=$minion | Out-Null + Write-Host C:\tmp\salt.exe /S /minion-name=$minion | Out-Null +} +ElseIf(!$PSBoundParameters.ContainsKey('minion') -and $PSBoundParameters.ContainsKey('master')) { + C:\tmp\salt.exe /S /master=$master | Out-Null + Write-Host C:\tmp\salt.exe /S /master=$master | Out-Null +} +Else { + C:\tmp\salt.exe /S | Out-Null + Write-Host C:\tmp\salt.exe /S | Out-Null +} # Check if minion config has been uploaded -if (Test-Path C:\tmp\minion) { +If (Test-Path C:\tmp\minion) { cp C:\tmp\minion C:\salt\conf\ } @@ -50,24 +88,31 @@ While (!$service) { $service = Get-Service salt-minion -ErrorAction SilentlyContinue } -# Start service -Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue - -# Check if service is started, otherwise retry starting the -# service 4 times. -$try = 0 -While (($service.Status -ne "Running") -and ($try -ne 4)) { +If($runservice) { + # Start service Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue - $service = Get-Service salt-minion -ErrorAction SilentlyContinue - Start-Sleep -s 2 - $try += 1 -} -# If the salt-minion service is still not running, something probably -# went wrong and user intervention is required - report failure. -if ($service.Status -eq "Stopped") { - Write-Host "Failed to start Salt minion" - exit 1 + # Check if service is started, otherwise retry starting the + # service 4 times. + $try = 0 + While (($service.Status -ne "Running") -and ($try -ne 4)) { + Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue + $service = Get-Service salt-minion -ErrorAction SilentlyContinue + Start-Sleep -s 2 + $try += 1 + } + + # If the salt-minion service is still not running, something probably + # went wrong and user intervention is required - report failure. + If ($service.Status -eq "Stopped") { + Write-Host "Failed to start Salt minion" + exit 1 + } +} +Else { + Write-Host "Stopping salt minion" + Set-Service "$ServiceName" -startupType "$startupType" + Stop-Service "$ServiceName" } Write-Host "Salt minion successfully installed" \ No newline at end of file diff --git a/plugins/provisioners/salt/config.rb b/plugins/provisioners/salt/config.rb index 638a07bfb..4e4dc866a 100644 --- a/plugins/provisioners/salt/config.rb +++ b/plugins/provisioners/salt/config.rb @@ -36,6 +36,8 @@ module VagrantPlugins attr_accessor :no_minion attr_accessor :bootstrap_options attr_accessor :version + attr_accessor :run_service + attr_accessor :master_id def initialize @minion_config = UNSET_VALUE @@ -65,6 +67,8 @@ module VagrantPlugins @masterless = UNSET_VALUE @minion_id = UNSET_VALUE @version = UNSET_VALUE + @run_service = UNSET_VALUE + @master_id = UNSET_VALUE end def finalize! @@ -95,6 +99,8 @@ module VagrantPlugins @masterless = false if @masterless == UNSET_VALUE @minion_id = nil if @minion_id == UNSET_VALUE @version = nil if @version == UNSET_VALUE + @run_service = nil if @run_service == UNSET_VALUE + @master_id = nil if @master_id == UNSET_VALUE end def pillar(data) diff --git a/plugins/provisioners/salt/provisioner.rb b/plugins/provisioners/salt/provisioner.rb index e9f23cd82..1a00f6424 100644 --- a/plugins/provisioners/salt/provisioner.rb +++ b/plugins/provisioners/salt/provisioner.rb @@ -104,7 +104,7 @@ module VagrantPlugins options = "%s %s" % [options, @config.bootstrap_options] end - if configure + if configure && !@machine.config.vm.communicator == :winrm options = "%s -F -c %s" % [options, config_dir] end @@ -238,7 +238,19 @@ module VagrantPlugins bootstrap_path = get_bootstrap if @machine.config.vm.communicator == :winrm if @config.version - options = "-version %s" % @config.version + options += " -version %s" % @config.version + end + if @config.run_service + @machine.env.ui.info "Salt minion will be stopped after installing." + options += " -runservice %s" % @config.run_service + end + if @config.minion_id + @machine.env.ui.info "Setting minion to @config.minion_id." + options += " -minion %s" % @config.minion_id + end + if @config.master_id + @machine.env.ui.info "Setting master to @config.master_id." + options += " -master %s" % @config.master_id end bootstrap_destination = File.join(config_dir, "bootstrap_salt.ps1") else