win_nssm: restore support of string as dict form for app_parameters and remove support of literal YAML dict

(cherry picked from commit 862855252b)
This commit is contained in:
Kévin Subileau 2018-09-15 14:07:22 +02:00 committed by Toshio Kuratomi
parent 2de895a07b
commit 7601f1c153
3 changed files with 39 additions and 47 deletions

View file

@ -36,8 +36,8 @@ if (($appParameters -ne $null) -and ($appParametersFree -ne $null))
{ {
Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both" Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both"
} }
if (($appParameters -ne $null) -and ($appParameters -isnot [System.Collections.Hashtable])) { if (($appParameters -ne $null) -and ($appParameters -isnot [string])) {
Fail-Json -obj $result -message "The app_parameters parameter must be a dict" Fail-Json -obj $result -message "The app_parameters parameter must be a string representing a dictionary."
} }
Function Nssm-Invoke Function Nssm-Invoke
@ -171,6 +171,20 @@ Function Nssm-Install
} }
} }
Function ParseAppParameters()
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]$appParameters
)
$escapedAppParameters = $appParameters.TrimStart("@").TrimStart("{").TrimEnd("}").Replace("; ","`n").Replace("\","\\")
return ConvertFrom-StringData -StringData $escapedAppParameters
}
Function Nssm-Update-AppParameters Function Nssm-Update-AppParameters
{ {
[CmdletBinding()] [CmdletBinding()]
@ -197,22 +211,22 @@ Function Nssm-Update-AppParameters
if ($null -ne $appParameters) if ($null -ne $appParameters)
{ {
$appParametersHash = ParseAppParameters -appParameters $appParameters
$appParamsArray = @() $appParamsArray = @()
$appParameters.GetEnumerator() | $appParametersHash.GetEnumerator() | foreach {
% { $key = $($_.Name)
$key = $($_.Name) $val = $($_.Value)
$val = $($_.Value)
$appParamKeys += $key $appParamKeys += $key
$appParamVals += $val $appParamVals += $val
if ($key -ne "_") { if ($key -ne "_") {
$appParamsArray += $key $appParamsArray += $key
}
$appParamsArray += $val
} }
$appParamsArray += $val
}
$result.nssm_app_parameters_keys = $appParamKeys $result.nssm_app_parameters_keys = $appParamKeys
$result.nssm_app_parameters_vals = $appParamVals $result.nssm_app_parameters_vals = $appParamVals

View file

@ -49,7 +49,7 @@ options:
- Path to receive error output. - Path to receive error output.
app_parameters: app_parameters:
description: description:
- Parameters to be passed to the application when it starts. - A string representing a dictionary of parameters to be passed to the application when it starts.
- Use either this or C(app_parameters_free_form), not both. - Use either this or C(app_parameters_free_form), not both.
app_parameters_free_form: app_parameters_free_form:
version_added: "2.3.0" version_added: "2.3.0"
@ -87,37 +87,25 @@ EXAMPLES = r'''
application: C:\windows\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
- win_nssm: - win_nssm:
name: foo name: foo
application: C:\windows\foo.exe application: C:\windows\foo.exe
app_parameters: app_parameters: -bar=true
bar: 'true'
# Install and start the foo service with a key-value pair argument, where the argument needs to start with a dash
# This will yield the following command: C:\windows\\foo.exe -bar "true"
- win_nssm:
name: foo
application: C:\windows\foo.exe
app_parameters:
"-bar": 'true'
# Install and start the foo service with a single parameter # Install and start the foo service with a single parameter
# This will yield the following command: C:\windows\\foo.exe bar # This will yield the following command: C:\windows\\foo.exe bar
- win_nssm: - win_nssm:
name: foo name: foo
application: C:\windows\foo.exe application: C:\windows\foo.exe
app_parameters: app_parameters: _=bar
_: bar
# Install and start the foo service with a mix of single params, and key value pairs # Install and start the foo service with a mix of single params, and key value pairs
# This will yield the following command: C:\windows\\foo.exe bar -file output.bat # This will yield the following command: C:\windows\\foo.exe bar -file output.bat -foo false
- win_nssm: - win_nssm:
name: foo name: foo
application: C:\windows\foo.exe application: C:\windows\foo.exe
app_parameters: app_parameters: _=bar; -file=output.bat; -foo=false
_: bar
"-file": "output.bat"
# Use the single line parameters option to specify an arbitrary string of parameters # Use the single line parameters option to specify an arbitrary string of parameters
# for the service executable # for the service executable

View file

@ -187,12 +187,7 @@
win_nssm: win_nssm:
name: '{{ test_service_name }}' name: '{{ test_service_name }}'
application: C:\Windows\System32\cmd.exe application: C:\Windows\System32\cmd.exe
app_parameters: app_parameters: foo=true; -file.out=output.bat; -path=C:\with space\; -str=test"quotes; _=bar
foo: true
'-file.out': 'output.bat'
'-path': 'C:\with space\'
'-str': 'test"quotes'
_: bar
register: mixed_params register: mixed_params
- name: get result of install service with dict parameters - name: get result of install service with dict parameters
@ -205,19 +200,14 @@
- mixed_params.changed == true - mixed_params.changed == true
- (mixed_params_actual.stdout|from_json).Exists == true - (mixed_params_actual.stdout|from_json).Exists == true
- (mixed_params_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" - (mixed_params_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
# Expected value: bar -str "test\"quotes" -file.out output.bat foo True -path "C:\with space\\" (backslashes doubled for jinja) # Expected value: bar -file.out output.bat -str "test\"quotes" foo true -path "C:\with space\\" (backslashes doubled for jinja)
- (mixed_params_actual.stdout|from_json).Parameters.AppParameters == 'bar -str "test\\"quotes" -file.out output.bat foo True -path "C:\\with space\\\\"' - (mixed_params_actual.stdout|from_json).Parameters.AppParameters == 'bar -file.out output.bat -str "test\\"quotes" foo true -path "C:\\with space\\\\"'
- name: install service with dict parameters (idempotent) - name: install service with dict parameters (idempotent)
win_nssm: win_nssm:
name: '{{ test_service_name }}' name: '{{ test_service_name }}'
application: C:\Windows\System32\cmd.exe application: C:\Windows\System32\cmd.exe
app_parameters: app_parameters: foo=true; -file.out=output.bat; -path=C:\with space\; -str=test"quotes; _=bar
foo: true
'-file.out': 'output.bat'
'-path': 'C:\with space\'
'-str': 'test"quotes'
_: bar
register: mixed_params_again register: mixed_params_again
- name: get result of install service with dict parameters (idempotent) - name: get result of install service with dict parameters (idempotent)
@ -230,8 +220,8 @@
- mixed_params_again.changed == false - mixed_params_again.changed == false
- (mixed_params_again_actual.stdout|from_json).Exists == true - (mixed_params_again_actual.stdout|from_json).Exists == true
- (mixed_params_again_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" - (mixed_params_again_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
# Expected value: bar -str "test\"quotes" -file.out output.bat foo True -path "C:\with space\\" (backslashes doubled for jinja) # Expected value: bar -file.out output.bat -str "test\"quotes" foo true -path "C:\with space\\" (backslashes doubled for jinja)
- (mixed_params_again_actual.stdout|from_json).Parameters.AppParameters == 'bar -str "test\\"quotes" -file.out output.bat foo True -path "C:\\with space\\\\"' - (mixed_params_again_actual.stdout|from_json).Parameters.AppParameters == 'bar -file.out output.bat -str "test\\"quotes" foo true -path "C:\\with space\\\\"'
- name: remove service - name: remove service
win_nssm: win_nssm: