Merge pull request #1143 from smadam813/devel_win_nssm
win_nssm: strict fix + added dependencies,startmode,user/pw feature
This commit is contained in:
commit
be8aacf899
2 changed files with 203 additions and 72 deletions
|
@ -2,6 +2,8 @@
|
||||||
# This file is part of Ansible
|
# This file is part of Ansible
|
||||||
#
|
#
|
||||||
# Copyright 2015, George Frank <george@georgefrank.net>
|
# Copyright 2015, George Frank <george@georgefrank.net>
|
||||||
|
# Copyright 2015, Adam Keech <akeech@chathamfinancial.com>
|
||||||
|
# Copyright 2015, Hans-Joachim Kliemeck <git@kliemeck.de>
|
||||||
#
|
#
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
@ -22,69 +24,23 @@ $ErrorActionPreference = "Stop"
|
||||||
# POWERSHELL_COMMON
|
# POWERSHELL_COMMON
|
||||||
|
|
||||||
$params = Parse-Args $args;
|
$params = Parse-Args $args;
|
||||||
|
|
||||||
$result = New-Object PSObject;
|
$result = New-Object PSObject;
|
||||||
Set-Attr $result "changed" $false;
|
Set-Attr $result "changed" $false;
|
||||||
|
|
||||||
|
$name = Get-Attr $params "name" -failifempty $true
|
||||||
|
$state = Get-Attr $params "state" -default "present" -validateSet "present", "absent", "started", "stopped", "restarted" -resultobj $result
|
||||||
|
|
||||||
If ($params.name)
|
$application = Get-Attr $params "application" -default $null
|
||||||
{
|
$appParameters = Get-Attr $params "app_parameters" -default $null
|
||||||
$name = $params.name
|
$startMode = Get-Attr $params "start_mode" -default "auto" -validateSet "auto", "manual", "disabled" -resultobj $result
|
||||||
}
|
|
||||||
Else
|
|
||||||
{
|
|
||||||
Fail-Json $result "missing required argument: name"
|
|
||||||
}
|
|
||||||
|
|
||||||
If ($params.state)
|
$stdoutFile = Get-Attr $params "stdout_file" -default $null
|
||||||
{
|
$stderrFile = Get-Attr $params "stderr_file" -default $null
|
||||||
$state = $params.state.ToString().ToLower()
|
$dependencies = Get-Attr $params "dependencies" -default $null
|
||||||
$validStates = "present", "absent", "started", "stopped", "restarted"
|
|
||||||
|
|
||||||
If ($validStates -notcontains $state)
|
$user = Get-Attr $params "user" -default $null
|
||||||
{
|
$password = Get-Attr $params "password" -default $null
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
Function Service-Exists
|
Function Service-Exists
|
||||||
{
|
{
|
||||||
|
@ -131,6 +87,7 @@ Function Nssm-Install
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
[string]$name,
|
[string]$name,
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
|
[AllowEmptyString()]
|
||||||
[string]$application
|
[string]$application
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -188,6 +145,7 @@ Function ParseAppParameters()
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
|
[AllowEmptyString()]
|
||||||
[string]$appParameters
|
[string]$appParameters
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -202,6 +160,7 @@ Function Nssm-Update-AppParameters
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
[string]$name,
|
[string]$name,
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
|
[AllowEmptyString()]
|
||||||
[string]$appParameters
|
[string]$appParameters
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -365,6 +324,127 @@ Function Nssm-Set-Ouput-Files
|
||||||
$results = invoke-expression $cmd
|
$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) {
|
||||||
|
if (!$password) {
|
||||||
|
Throw "User without password is informed for service ""$name"""
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$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) -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-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
|
Function Nssm-Get-Status
|
||||||
{
|
{
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
|
@ -470,7 +550,7 @@ Function Nssm-Stop
|
||||||
Throw "Error stopping service ""$name"""
|
Throw "Error stopping service ""$name"""
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentStatus -ne "SERVICE_STOPPED")
|
if ($currentStatus -ne "SERVICE_STOPPED")
|
||||||
{
|
{
|
||||||
$cmd = "nssm stop ""$name"""
|
$cmd = "nssm stop ""$name"""
|
||||||
|
|
||||||
|
@ -499,32 +579,34 @@ Function Nssm-Restart
|
||||||
Nssm-Start-Service-Command -name $name
|
Nssm-Start-Service-Command -name $name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Function NssmProcedure
|
||||||
|
{
|
||||||
|
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-Update-StartMode -name $name -mode $startMode
|
||||||
|
}
|
||||||
|
|
||||||
Try
|
Try
|
||||||
{
|
{
|
||||||
switch ($state)
|
switch ($state)
|
||||||
{
|
{
|
||||||
"absent" { Nssm-Remove -name $name }
|
"absent" { Nssm-Remove -name $name }
|
||||||
"present" {
|
"present" {
|
||||||
Nssm-Install -name $name -application $application
|
NssmProcedure
|
||||||
Nssm-Update-AppParameters -name $name -appParameters $appParameters
|
|
||||||
Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile
|
|
||||||
}
|
}
|
||||||
"started" {
|
"started" {
|
||||||
Nssm-Install -name $name -application $application
|
NssmProcedure
|
||||||
Nssm-Update-AppParameters -name $name -appParameters $appParameters
|
|
||||||
Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile
|
|
||||||
Nssm-Start -name $name
|
Nssm-Start -name $name
|
||||||
}
|
}
|
||||||
"stopped" {
|
"stopped" {
|
||||||
Nssm-Install -name $name -application $application
|
NssmProcedure
|
||||||
Nssm-Update-AppParameters -name $name -appParameters $appParameters
|
|
||||||
Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile
|
|
||||||
Nssm-Stop -name $name
|
Nssm-Stop -name $name
|
||||||
}
|
}
|
||||||
"restarted" {
|
"restarted" {
|
||||||
Nssm-Install -name $name -application $application
|
NssmProcedure
|
||||||
Nssm-Update-AppParameters -name $name -appParameters $appParameters
|
|
||||||
Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile
|
|
||||||
Nssm-Restart -name $name
|
Nssm-Restart -name $name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -535,4 +617,3 @@ Catch
|
||||||
{
|
{
|
||||||
Fail-Json $result $_.Exception.Message
|
Fail-Json $result $_.Exception.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,16 +71,46 @@ options:
|
||||||
- Parameters to be passed to the application when it starts
|
- Parameters to be passed to the application when it starts
|
||||||
required: false
|
required: false
|
||||||
default: null
|
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
|
||||||
|
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: true
|
||||||
|
default: auto
|
||||||
|
choices:
|
||||||
|
- auto
|
||||||
|
- manual
|
||||||
|
- disabled
|
||||||
author:
|
author:
|
||||||
- "Adam Keech (@smadam813)"
|
- "Adam Keech (@smadam813)"
|
||||||
- "George Frank (@georgefrank)"
|
- "George Frank (@georgefrank)"
|
||||||
|
- "Hans-Joachim Kliemeck (@h0nIg)"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
# Install and start the foo service
|
# Install and start the foo service
|
||||||
- win_nssm:
|
- win_nssm:
|
||||||
name: foo
|
name: foo
|
||||||
application: C:\windowsk\\foo.exe
|
application: C:\windows\\foo.exe
|
||||||
|
|
||||||
# Install and start the foo service with a key-value pair argument
|
# Install and start the foo service with a key-value pair argument
|
||||||
# This will yield the following command: C:\windows\\foo.exe bar "true"
|
# This will yield the following command: C:\windows\\foo.exe bar "true"
|
||||||
|
@ -122,6 +152,26 @@ EXAMPLES = '''
|
||||||
stdout_file: C:\windows\\foo.log
|
stdout_file: C:\windows\\foo.log
|
||||||
stderr_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
|
||||||
|
|
||||||
|
# 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
|
# Remove the foo service
|
||||||
- win_nssm:
|
- win_nssm:
|
||||||
name: foo
|
name: foo
|
||||||
|
|
Loading…
Reference in a new issue