From 461a479cbd10fd4208976a21fef20fac83590343 Mon Sep 17 00:00:00 2001 From: Hans-Joachim Kliemeck Date: Wed, 21 Oct 2015 17:52:48 +0200 Subject: [PATCH 1/4] added dependency and user settings --- windows/win_nssm.ps1 | 120 +++++++++++++++++++++++++++++++++++++++++++ windows/win_nssm.py | 31 ++++++++++- 2 files changed, 150 insertions(+), 1 deletion(-) diff --git a/windows/win_nssm.ps1 b/windows/win_nssm.ps1 index 841bc3aa3fd..89fcfa5e407 100644 --- a/windows/win_nssm.ps1 +++ b/windows/win_nssm.ps1 @@ -2,6 +2,8 @@ # This file is part of Ansible # # Copyright 2015, George Frank +# Copyright 2015, Adam Keech +# Copyright 2015, Hans-Joachim Kliemeck # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -86,6 +88,33 @@ Else $stderrFile = $null } +If ($params.dependencies) +{ + $dependencies = $params.dependencies +} +Else +{ + $dependencies = $null +} + +If ($params.user) +{ + $user = $params.user +} +Else +{ + $user = $null +} + +If ($params.password) +{ + $password = $params.password +} +Else +{ + $password = $null +} + Function Service-Exists { [CmdletBinding()] @@ -365,6 +394,89 @@ Function Nssm-Set-Ouput-Files $results = invoke-expression $cmd } +Function Nssm-Update-Credentials +{ + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] + [string]$name, + [Parameter(Mandatory=$false)] + [string]$user, + [Parameter(Mandatory=$false)] + [string]$password + ) + + $cmd = "nssm get ""$name"" ObjectName" + $results = invoke-expression $cmd + + if ($LastExitCode -ne 0) + { + Set-Attr $result "nssm_error_cmd" $cmd + Set-Attr $result "nssm_error_log" "$results" + Throw "Error updating credentials for service ""$name""" + } + + if ($user -ne $null) { + If ($password -eq $null) { + Throw "User without password is informed for service ""$name""" + } + + $fullUser = $user + If (-Not($user -contains "@") -And ($user.Split("\").count -eq 1)) { + $fullUser = ".\" + $user + } + + If ($results -ne $fullUser) { + $cmd = "nssm set ""$name"" ObjectName $fullUser $password" + $results = invoke-expression $cmd + + if ($LastExitCode -ne 0) + { + Set-Attr $result "nssm_error_cmd" $cmd + Set-Attr $result "nssm_error_log" "$results" + Throw "Error updating credentials for service ""$name""" + } + + $result.changed = $true + } + } +} + +Function Nssm-Update-Dependencies +{ + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] + [string]$name, + [Parameter(Mandatory=$false)] + [string]$dependencies + ) + + $cmd = "nssm get ""$name"" DependOnService" + $results = invoke-expression $cmd + + if ($LastExitCode -ne 0) + { + Set-Attr $result "nssm_error_cmd" $cmd + Set-Attr $result "nssm_error_log" "$results" + Throw "Error updating dependencies for service ""$name""" + } + + If (($dependencies -ne $null) -And ($results.Tolower() -ne $dependencies.Tolower())) { + $cmd = "nssm set ""$name"" DependOnService $dependencies" + $results = invoke-expression $cmd + + if ($LastExitCode -ne 0) + { + Set-Attr $result "nssm_error_cmd" $cmd + Set-Attr $result "nssm_error_log" "$results" + Throw "Error updating dependencies for service ""$name""" + } + + $result.changed = $true + } +} + Function Nssm-Get-Status { [CmdletBinding()] @@ -508,23 +620,31 @@ Try Nssm-Install -name $name -application $application Nssm-Update-AppParameters -name $name -appParameters $appParameters Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile + Nssm-Update-Dependencies -name $name -dependencies $dependencies + Nssm-Update-Credentials -name $name -user $user -password $password } "started" { Nssm-Install -name $name -application $application Nssm-Update-AppParameters -name $name -appParameters $appParameters Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile + Nssm-Update-Dependencies -name $name -dependencies $dependencies + Nssm-Update-Credentials -name $name -user $user -password $password Nssm-Start -name $name } "stopped" { Nssm-Install -name $name -application $application Nssm-Update-AppParameters -name $name -appParameters $appParameters Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile + Nssm-Update-Dependencies -name $name -dependencies $dependencies + Nssm-Update-Credentials -name $name -user $user -password $password Nssm-Stop -name $name } "restarted" { Nssm-Install -name $name -application $application Nssm-Update-AppParameters -name $name -appParameters $appParameters Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile + Nssm-Update-Dependencies -name $name -dependencies $dependencies + Nssm-Update-Credentials -name $name -user $user -password $password Nssm-Restart -name $name } } diff --git a/windows/win_nssm.py b/windows/win_nssm.py index b3925f0fd50..94d4079e786 100644 --- a/windows/win_nssm.py +++ b/windows/win_nssm.py @@ -71,16 +71,32 @@ options: - Parameters to be passed to the application when it starts required: false default: null + dependencies: + description: + - Service dependencies that has to be started to trigger startup, separated by comma. + required: false + default: null + user: + description: + - User to be used for service startup + required: false + default: null + password: + description: + - Password to be used for service startup + required: false + default: null author: - "Adam Keech (@smadam813)" - "George Frank (@georgefrank)" + - "Hans-Joachim Kliemeck (@h0nIg)" ''' EXAMPLES = ''' # Install and start the foo service - win_nssm: name: foo - application: C:\windowsk\\foo.exe + application: C:\windows\\foo.exe # Install and start the foo service with a key-value pair argument # This will yield the following command: C:\windows\\foo.exe bar "true" @@ -122,6 +138,19 @@ EXAMPLES = ''' stdout_file: C:\windows\\foo.log stderr_file: C:\windows\\foo.log +# Install and start the foo service, but wait for dependencies tcpip and adf +- win_nssm: + name: foo + application: C:\windows\\foo.exe + dependencies: 'adf,tcpip' + +# Install and start the foo service with dedicated user +- win_nssm: + name: foo + application: C:\windows\\foo.exe + user: foouser + password: secret + # Remove the foo service - win_nssm: name: foo From e7fd5dcde06d04fc387abb0b2f7a5801f04543d2 Mon Sep 17 00:00:00 2001 From: Hans-Joachim Kliemeck Date: Wed, 21 Oct 2015 17:52:59 +0200 Subject: [PATCH 2/4] strict variables fix --- windows/win_nssm.ps1 | 94 +++++--------------------------------------- 1 file changed, 10 insertions(+), 84 deletions(-) diff --git a/windows/win_nssm.ps1 b/windows/win_nssm.ps1 index 89fcfa5e407..915a3889e17 100644 --- a/windows/win_nssm.ps1 +++ b/windows/win_nssm.ps1 @@ -24,96 +24,22 @@ $ErrorActionPreference = "Stop" # POWERSHELL_COMMON $params = Parse-Args $args; + $result = New-Object PSObject; Set-Attr $result "changed" $false; +$name = Get-Attr $params "name" -failifempty $true +$state = Get-Attr $params "state" "present" -validateSet "present", "absent", "started", "stopped", "restarted" -resultobj $result -If ($params.name) -{ - $name = $params.name -} -Else -{ - Fail-Json $result "missing required argument: name" -} +$application = Get-Attr $params "application" $null +$appParameters = Get-Attr $params "app_parameters" $null -If ($params.state) -{ - $state = $params.state.ToString().ToLower() - $validStates = "present", "absent", "started", "stopped", "restarted" +$stdoutFile = Get-Attr $params "stdout_file" $null +$stderrFile = Get-Attr $params "stderr_file" $null +$dependencies = Get-Attr $params "dependencies" $null - If ($validStates -notcontains $state) - { - Fail-Json $result "state is $state; must be one of: $validStates" - } -} -else -{ - $state = "present" -} - -If ($params.application) -{ - $application = $params.application -} -Else -{ - $application = $null -} - -If ($params.app_parameters) -{ - $appParameters = $params.app_parameters -} -Else -{ - $appParameters = $null -} - -If ($params.stdout_file) -{ - $stdoutFile = $params.stdout_file -} -Else -{ - $stdoutFile = $null -} - -If ($params.stderr_file) -{ - $stderrFile = $params.stderr_file -} -Else -{ - $stderrFile = $null -} - -If ($params.dependencies) -{ - $dependencies = $params.dependencies -} -Else -{ - $dependencies = $null -} - -If ($params.user) -{ - $user = $params.user -} -Else -{ - $user = $null -} - -If ($params.password) -{ - $password = $params.password -} -Else -{ - $password = $null -} +$user = Get-Attr $params "user" $null +$password = Get-Attr $params "password" $null Function Service-Exists { From d3f83ee9a7d40eea3e01adea9b7d16bc0d70c124 Mon Sep 17 00:00:00 2001 From: Hans-Joachim Kliemeck Date: Wed, 21 Oct 2015 18:16:49 +0200 Subject: [PATCH 3/4] added start mode feature --- windows/win_nssm.ps1 | 42 ++++++++++++++++++++++++++++++++++++++++++ windows/win_nssm.py | 20 ++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/windows/win_nssm.ps1 b/windows/win_nssm.ps1 index 915a3889e17..99a61d12a90 100644 --- a/windows/win_nssm.ps1 +++ b/windows/win_nssm.ps1 @@ -33,6 +33,7 @@ $state = Get-Attr $params "state" "present" -validateSet "present", "absent", "s $application = Get-Attr $params "application" $null $appParameters = Get-Attr $params "app_parameters" $null +$startMode = Get-Attr $params "start_mode" "auto" -validateSet "auto", "manual", "disabled" -resultobj $result $stdoutFile = Get-Attr $params "stdout_file" $null $stderrFile = Get-Attr $params "stderr_file" $null @@ -403,6 +404,43 @@ Function Nssm-Update-Dependencies } } +Function Nssm-Update-StartMode +{ + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] + [string]$name, + [Parameter(Mandatory=$true)] + [string]$mode + ) + + $cmd = "nssm get ""$name"" Start" + $results = invoke-expression $cmd + + if ($LastExitCode -ne 0) + { + Set-Attr $result "nssm_error_cmd" $cmd + Set-Attr $result "nssm_error_log" "$results" + Throw "Error updating start mode for service ""$name""" + } + + $modes=@{"auto" = "SERVICE_AUTO_START"; "manual" = "SERVICE_DEMAND_START"; "disabled" = "SERVICE_DISABLED"} + $mappedMode = $modes.$mode + if ($mappedMode -ne $results) { + $cmd = "nssm set ""$name"" Start $mappedMode" + $results = invoke-expression $cmd + + if ($LastExitCode -ne 0) + { + Set-Attr $result "nssm_error_cmd" $cmd + Set-Attr $result "nssm_error_log" "$results" + Throw "Error updating start mode for service ""$name""" + } + + $result.changed = $true + } +} + Function Nssm-Get-Status { [CmdletBinding()] @@ -548,6 +586,7 @@ Try Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile Nssm-Update-Dependencies -name $name -dependencies $dependencies Nssm-Update-Credentials -name $name -user $user -password $password + Nssm-Update-StartMode -name $name -mode $startMode } "started" { Nssm-Install -name $name -application $application @@ -555,6 +594,7 @@ Try Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile Nssm-Update-Dependencies -name $name -dependencies $dependencies Nssm-Update-Credentials -name $name -user $user -password $password + Nssm-Update-StartMode -name $name -mode $startMode Nssm-Start -name $name } "stopped" { @@ -563,6 +603,7 @@ Try Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile Nssm-Update-Dependencies -name $name -dependencies $dependencies Nssm-Update-Credentials -name $name -user $user -password $password + Nssm-Update-StartMode -name $name -mode $startMode Nssm-Stop -name $name } "restarted" { @@ -571,6 +612,7 @@ Try Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile Nssm-Update-Dependencies -name $name -dependencies $dependencies Nssm-Update-Credentials -name $name -user $user -password $password + Nssm-Update-StartMode -name $name -mode $startMode Nssm-Restart -name $name } } diff --git a/windows/win_nssm.py b/windows/win_nssm.py index 94d4079e786..1f42f21f45a 100644 --- a/windows/win_nssm.py +++ b/windows/win_nssm.py @@ -86,6 +86,19 @@ options: - Password to be used for service startup required: false default: null + password: + description: + - Password to be used for service startup + required: false + default: null + start_mode: + description: + - If C(auto) is selected, the service will start at bootup. C(manual) means that the service will start only when another service needs it. C(disabled) means that the service will stay off, regardless if it is needed or not. + required: false + choices: + - auto + - manual + - disabled author: - "Adam Keech (@smadam813)" - "George Frank (@georgefrank)" @@ -151,6 +164,13 @@ EXAMPLES = ''' user: foouser password: secret +# Install the foo service but do not start it automatically +- win_nssm: + name: foo + application: C:\windows\\foo.exe + state: present + start_mode: manual + # Remove the foo service - win_nssm: name: foo From adc78eaf873d827c45aa8d610de9e757d1fb59e3 Mon Sep 17 00:00:00 2001 From: Hans-Joachim Kliemeck Date: Wed, 21 Oct 2015 20:06:26 +0200 Subject: [PATCH 4/4] fixed documentation --- windows/win_nssm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/windows/win_nssm.py b/windows/win_nssm.py index 1f42f21f45a..98be076a48b 100644 --- a/windows/win_nssm.py +++ b/windows/win_nssm.py @@ -94,7 +94,8 @@ options: start_mode: description: - If C(auto) is selected, the service will start at bootup. C(manual) means that the service will start only when another service needs it. C(disabled) means that the service will stay off, regardless if it is needed or not. - required: false + required: true + default: auto choices: - auto - manual