|
|
|
@ -2,6 +2,8 @@
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
|
#
|
|
|
|
|
# Copyright 2015, George Frank <george@georgefrank.net>
|
|
|
|
|
# Copyright 2015, Adam Keech
|
|
|
|
|
# Copyright 2015, Hans-Joachim Kliemeck <git@kliemeck.de>
|
|
|
|
|
#
|
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
@ -22,69 +24,23 @@ $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"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
If ($params.state)
|
|
|
|
|
{
|
|
|
|
|
$state = $params.state.ToString().ToLower()
|
|
|
|
|
$validStates = "present", "absent", "started", "stopped", "restarted"
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
$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
|
|
|
|
|
|
|
|
|
|
If ($params.app_parameters)
|
|
|
|
|
{
|
|
|
|
|
$appParameters = $params.app_parameters
|
|
|
|
|
}
|
|
|
|
|
Else
|
|
|
|
|
{
|
|
|
|
|
$appParameters = $null
|
|
|
|
|
}
|
|
|
|
|
$stdoutFile = Get-Attr $params "stdout_file" $null
|
|
|
|
|
$stderrFile = Get-Attr $params "stderr_file" $null
|
|
|
|
|
$dependencies = Get-Attr $params "dependencies" $null
|
|
|
|
|
|
|
|
|
|
If ($params.stdout_file)
|
|
|
|
|
{
|
|
|
|
|
$stdoutFile = $params.stdout_file
|
|
|
|
|
}
|
|
|
|
|
Else
|
|
|
|
|
{
|
|
|
|
|
$stdoutFile = $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
If ($params.stderr_file)
|
|
|
|
|
{
|
|
|
|
|
$stderrFile = $params.stderr_file
|
|
|
|
|
}
|
|
|
|
|
Else
|
|
|
|
|
{
|
|
|
|
|
$stderrFile = $null
|
|
|
|
|
}
|
|
|
|
|
$user = Get-Attr $params "user" $null
|
|
|
|
|
$password = Get-Attr $params "password" $null
|
|
|
|
|
|
|
|
|
|
Function Service-Exists
|
|
|
|
|
{
|
|
|
|
@ -365,6 +321,126 @@ 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-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()]
|
|
|
|
@ -508,23 +584,35 @@ 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
|
|
|
|
|
Nssm-Update-StartMode -name $name -mode $startMode
|
|
|
|
|
}
|
|
|
|
|
"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-Update-StartMode -name $name -mode $startMode
|
|
|
|
|
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-Update-StartMode -name $name -mode $startMode
|
|
|
|
|
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-Update-StartMode -name $name -mode $startMode
|
|
|
|
|
Nssm-Restart -name $name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|