Skip to content

Commit

Permalink
Adding run_service and minion/master ids for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
PatOShea committed Jul 7, 2015
1 parent eab702d commit da67824
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 28 deletions.
97 changes: 71 additions & 26 deletions plugins/provisioners/salt/bootstrap-salt.ps1
Original file line number Diff line number Diff line change
@@ -1,45 +1,83 @@
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

# Copy minion keys & config to correct location
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\
}

Expand All @@ -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"
6 changes: 6 additions & 0 deletions plugins/provisioners/salt/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Config < Vagrant.plugin("2", :config)
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
Expand Down Expand Up @@ -65,6 +67,8 @@ def initialize
@masterless = UNSET_VALUE
@minion_id = UNSET_VALUE
@version = UNSET_VALUE
@run_service = UNSET_VALUE
@master_id = UNSET_VALUE
end

def finalize!
Expand Down Expand Up @@ -95,6 +99,8 @@ def finalize!
@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)
Expand Down
16 changes: 14 additions & 2 deletions plugins/provisioners/salt/provisioner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def bootstrap_options(install, configure, config_dir)
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

Expand Down Expand Up @@ -238,7 +238,19 @@ def run_bootstrap_script
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
Expand Down

0 comments on commit da67824

Please sign in to comment.