From 349c4a2a488a7284307f1c7e8ae41b58af5e62b6 Mon Sep 17 00:00:00 2001 From: Hans-Joachim Kliemeck Date: Wed, 21 Oct 2015 17:52:48 +0200 Subject: [PATCH] added dependency and user settings --- .../modules/extras/windows/win_nssm.ps1 | 120 ++++++++++++++++++ .../modules/extras/windows/win_nssm.py | 31 ++++- 2 files changed, 150 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/extras/windows/win_nssm.ps1 b/lib/ansible/modules/extras/windows/win_nssm.ps1 index 841bc3aa3fd..89fcfa5e407 100644 --- a/lib/ansible/modules/extras/windows/win_nssm.ps1 +++ b/lib/ansible/modules/extras/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/lib/ansible/modules/extras/windows/win_nssm.py b/lib/ansible/modules/extras/windows/win_nssm.py index b3925f0fd50..94d4079e786 100644 --- a/lib/ansible/modules/extras/windows/win_nssm.py +++ b/lib/ansible/modules/extras/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