mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fixing Some Issues with out Parameters are passed around. Also some refactoring.
This commit is contained in:
parent
3ad86e383e
commit
b8fbbdbff0
1 changed files with 48 additions and 55 deletions
|
@ -2,7 +2,7 @@
|
||||||
# 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
|
# Copyright 2015, Adam Keech <akeech@chathamfinancial.com>
|
||||||
# Copyright 2015, Hans-Joachim Kliemeck <git@kliemeck.de>
|
# 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
|
||||||
|
@ -29,18 +29,18 @@ $result = New-Object PSObject;
|
||||||
Set-Attr $result "changed" $false;
|
Set-Attr $result "changed" $false;
|
||||||
|
|
||||||
$name = Get-Attr $params "name" -failifempty $true
|
$name = Get-Attr $params "name" -failifempty $true
|
||||||
$state = Get-Attr $params "state" "present" -validateSet "present", "absent", "started", "stopped", "restarted" -resultobj $result
|
$state = Get-Attr $params "state" -default "present" -validateSet "present", "absent", "started", "stopped", "restarted" -resultobj $result
|
||||||
|
|
||||||
$application = Get-Attr $params "application" $null
|
$application = Get-Attr $params "application" -default $null
|
||||||
$appParameters = Get-Attr $params "app_parameters" $null
|
$appParameters = Get-Attr $params "app_parameters" -default $null
|
||||||
$startMode = Get-Attr $params "start_mode" "auto" -validateSet "auto", "manual", "disabled" -resultobj $result
|
$startMode = Get-Attr $params "start_mode" -default "auto" -validateSet "auto", "manual", "disabled" -resultobj $result
|
||||||
|
|
||||||
$stdoutFile = Get-Attr $params "stdout_file" $null
|
$stdoutFile = Get-Attr $params "stdout_file" -default $null
|
||||||
$stderrFile = Get-Attr $params "stderr_file" $null
|
$stderrFile = Get-Attr $params "stderr_file" -default $null
|
||||||
$dependencies = Get-Attr $params "dependencies" $null
|
$dependencies = Get-Attr $params "dependencies" -default $null
|
||||||
|
|
||||||
$user = Get-Attr $params "user" $null
|
$user = Get-Attr $params "user" -default $null
|
||||||
$password = Get-Attr $params "password" $null
|
$password = Get-Attr $params "password" -default $null
|
||||||
|
|
||||||
Function Service-Exists
|
Function Service-Exists
|
||||||
{
|
{
|
||||||
|
@ -87,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
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -144,6 +145,7 @@ Function ParseAppParameters()
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
|
[AllowEmptyString()]
|
||||||
[string]$appParameters
|
[string]$appParameters
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -158,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
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -343,28 +346,29 @@ Function Nssm-Update-Credentials
|
||||||
Throw "Error updating credentials for service ""$name"""
|
Throw "Error updating credentials for service ""$name"""
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user -ne $null) {
|
if ($user) {
|
||||||
If ($password -eq $null) {
|
if (!$password) {
|
||||||
Throw "User without password is informed for service ""$name"""
|
Throw "User without password is informed for service ""$name"""
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
$fullUser = $user
|
$fullUser = $user
|
||||||
If (-Not($user -contains "@") -And ($user.Split("\").count -eq 1)) {
|
If (-not($user -contains "@") -and ($user.Split("\").count -eq 1)) {
|
||||||
$fullUser = ".\" + $user
|
$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
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -389,7 +393,7 @@ Function Nssm-Update-Dependencies
|
||||||
Throw "Error updating dependencies for service ""$name"""
|
Throw "Error updating dependencies for service ""$name"""
|
||||||
}
|
}
|
||||||
|
|
||||||
If (($dependencies -ne $null) -And ($results.Tolower() -ne $dependencies.Tolower())) {
|
If (($dependencies) -and ($results.Tolower() -ne $dependencies.Tolower())) {
|
||||||
$cmd = "nssm set ""$name"" DependOnService $dependencies"
|
$cmd = "nssm set ""$name"" DependOnService $dependencies"
|
||||||
$results = invoke-expression $cmd
|
$results = invoke-expression $cmd
|
||||||
|
|
||||||
|
@ -546,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"""
|
||||||
|
|
||||||
|
@ -575,44 +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
|
|
||||||
Nssm-Update-Dependencies -name $name -dependencies $dependencies
|
|
||||||
Nssm-Update-Credentials -name $name -user $user -password $password
|
|
||||||
Nssm-Update-StartMode -name $name -mode $startMode
|
|
||||||
}
|
}
|
||||||
"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-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
|
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-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
|
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-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
|
Nssm-Restart -name $name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -623,4 +617,3 @@ Catch
|
||||||
{
|
{
|
||||||
Fail-Json $result $_.Exception.Message
|
Fail-Json $result $_.Exception.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue