salt icon indicating copy to clipboard operation
salt copied to clipboard

salt winrepo with jinja 'parses' differently - how to overcome?

Open TheBigBear opened this issue 11 years ago • 15 comments

@UtahDave @jfindlay I have the following new winrepo sls file with jinja and just can't get the proper quoting, to get the right output, can one of you help me out with this, sorry if this is a really simple jinja and quoting related issue.

install_flags: '/S /D="{{ PROGRAM_FILES }}\7-zip_beta"'

renders as:

install_flags:
    /S /D="C:\Program Files\7-zip_beta" 

Which looks perfectly fine to me, but when it get's executed it is broken-up in an un-expected way, the space between Program and Files suddenly breaks up the params passed, even though it is in between double-quotes.

, '/S', '/D="C:\\Program', 'Files\\7-zip_beta"'
7zip_beta.sls:
{% if grains['cpuarch'] == 'AMD64' %}
    {% set PROGRAM_FILES = "C:\Program Files" %}
{% else %}
    {% set PROGRAM_FILES = "C:\Program Files (x86)" %}
{% endif %}
7zip_beta:
  15.05:
    {% if grains['cpuarch'] == 'AMD64' %}
    full_name: '7-Zip 15.05 beta x64'
    installer: 'http://heanet.dl.sourceforge.net/project/sevenzip/7-Zip/15.05/7z1505-x64.exe'
    {% elif grains['cpuarch'] == 'x86' %}
    full_name: '7-Zip 15.05 beta'
    installer: 'http://heanet.dl.sourceforge.net/project/sevenzip/7-Zip/15.05/7z1505.exe'
    {% endif %}
    install_flags: '/S /D="{{ PROGRAM_FILES }}\7-zip_beta"'
    uninstaller: '{{ PROGRAM_FILES }}\7-zip_beta\Uninstall.exe' 
    uninstall_flags: '/S'
    reboot: False
    locale: en_US

Here is the -l info level output

C:\salt>salt-call --local pkg.install 7zip_beta -l info
[WARNING ] Specified file http://heanet.dl.sourceforge.net/project/sevenzip/7-Zip/15.05/7z1505-x64.exe is not present to generate hash
[INFO    ] Executing command ['c:\\salt\\var\\cache\\salt\\minion\\extrn_files\\base\\heanet.dl.sourceforge.net\\project\\sevenzip\\7-Zip\\15.05\\7z1505-x64.exe', '/S', '/D="C:\\Program', 'Files\\7-zip_beta"'] in directory 'c:\\salt\\var\\cache\\salt\\minion\\extrn_files\\base\\heanet.dl.sourceforge.net\\project\\sevenzip\\7-Zip\\15.05'
[ERROR   ] Command ['c:\\salt\\var\\cache\\salt\\minion\\extrn_files\\base\\heanet.dl.sourceforge.net\\project\\sevenzip\\7-Zip\\15.05\\7z1505-x64.exe', '/S', '/D="C:\\Program', 'Files\\7-zip_beta"'] failed with return code: 1
[ERROR   ] output:
local:
    ----------

How do I have to write this winrepo sls file with jinja to get the right output?

TheBigBear avatar Aug 07 '15 21:08 TheBigBear

So I think this isn't a jinja rendering problem, but a python_shell problem.

I'll look into this over the weekend.

UtahDave avatar Aug 07 '15 23:08 UtahDave

@twangboy ^^

UtahDave avatar Aug 07 '15 23:08 UtahDave

@UtahDave, ok, so it is a python shell problem then?

Any joy over the wekend in resolving this one?

TheBigBear avatar Aug 10 '15 12:08 TheBigBear

I have also run across another very similar issue.

I had problems with uninstaller entries of the form

{% set Program_Files = "C:\Program Files (x86)" %}
...
uninstaller: '{{ Program_Files }}\myprog\myuninst.exe'

Giving me 'broken' cmd lines that had trailing spaces after the jinja rendered part and then I realised that the whole string wasn't quoted. So I tried improving it to:

{% set Program_Files = "C:\Program Files (x86)" %}
...
uninstaller: '"{{ Program_Files }}\myprog\myuninst.exe"'

now resulting in a error 5 - access denied error message?

 Error running 'pkg.remove': Unable to run command ['"C:\\Program Files \\CCleaner\\uninst.exe"', '/S'] with the context {'with_communicate': True, 'shell': False, 'env': {'TMP': 'C:\\Users\\sysadmin.uk\\AppData\\Local\\Temp', 'COMPUTERNAME': 'UK-IT-DEV1', 'USERDOMAIN': 'GLOBAL', 'PYTHON': 'C:\\salt\\bin\\python.exe', 'PSMODULEPATH': 'C:\\Users\\sysadmin.uk\\Documents\\WindowsPowerShell\\Modules;C:\\Users\\sysadmin.uk\\AppData\\Roaming\\Boxstarter', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 70 Stepping 1, GenuineIntel', 'PROGRAMFILES': 'C:\\Program Files', 'PROCESSOR_REVISION': '4601', 'SYSTEMROOT': 'C:\\Windows', 'PATH': 'C:\\Program Files (x86)\\GnuWin32\\bin;C:\\Program Files (x86)\\Notepad++;C:\\ProgramData\\chocolatey\\bin;c:\\sysutl;c:\\utl;C:\\Windows;C:\\Windows\\system32;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;c:\\python27;c:\\python27\\scripts;C:\\Program Files (x86)\\Skype\\Phone\\;C:\\Users\\sysadmin.uk\\AppData\\Roaming\\Boxstarter;C:\\salt\\bin\\lib\\site-packages\\pywin32_system32', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'TEMP': 'C:\\Users\\sysadmin.uk\\AppData\\Local\\Temp', 'SCRIPT': 'C:\\salt\\bin\\Scripts\\salt-call', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'CHOCOLATEYINSTALL': 'C:\\ProgramData\\chocolatey', 'ALLUSERSPROFILE': 'C:\\ProgramData', 'SALTDIR': 'C:\\salt', 'LOCALAPPDATA': 'C:\\Users\\sysadmin.uk\\AppData\\Local', 'HOMEPATH': '\\Users\\sysadmin.uk', 'USERDOMAIN_ROAMINGPROFILE': 'GLOBAL', 'PROGRAMW6432': 'C:\\Program Files', 'USERNAME': 'sysadmin.uk', 'LOGONSERVER': '\\\\UK-DC1', 'PROMPT': '$P$G', 'COMSPEC': 'C:\\Windows\\system32\\cmd.exe', 'PROGRAMDATA': 'C:\\ProgramData', 'USERDNSDOMAIN': 'GLOBAL.LOCAL', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'FP_NO_HOST_CHECK': 'NO', 'WINDIR': 'C:\\Windows', 'OPENSSL_CONF': 'C:\\OpenSSL-Win64\\bin\\openssl.cfg', 'HOMEDRIVE': 'C:', 'APPDATA': 'C:\\Users\\sysadmin.uk\\AppData\\Roaming', 'SYSTEMDRIVE': 'C:', 'NUMBER_OF_PROCESSORS': '1', 'PROCESSOR_LEVEL': '6', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'OS': 'Windows_NT', 'PUBLIC': 'C:\\Users\\Public', 'USERPROFILE': 'C:\\Users\\sysadmin.uk'}, 'stdout': -1, 'stdin': None, 'stderr': -2, 'cwd': 'C:\\Users\\sysadmin.uk'}, reason: [Error 5] Access is denied

TheBigBear avatar Aug 10 '15 12:08 TheBigBear

Gents,

I'm getting new errors on my server with the latest jinja based win-repo syntax - related?

root@automate:/srv/salt/base/win/repo/salt-winrepo.git# salt-run winrepo.genrepo
Exception occurred in runner winrepo.genrepo: Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 317, in low
    data['return'] = self.functions[fun](*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/salt/runners/winrepo.py", line 49, in genrepo
    config = yaml.safe_load(slsfile.read()) or {}
  File "/usr/lib/python2.7/dist-packages/yaml/__init__.py", line 93, in safe_load
    return load(stream, SafeLoader)
  File "/usr/lib/python2.7/dist-packages/yaml/__init__.py", line 71, in load
    return loader.get_single_data()
  File "/usr/lib/python2.7/dist-packages/yaml/constructor.py", line 37, in get_single_data
    node = self.get_single_node()
  File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 35, in get_single_node
    if not self.check_event(StreamEndEvent):
  File "/usr/lib/python2.7/dist-packages/yaml/parser.py", line 98, in check_event
    self.current_event = self.state()
  File "/usr/lib/python2.7/dist-packages/yaml/parser.py", line 143, in parse_implicit_document_start
    StreamEndToken):
  File "/usr/lib/python2.7/dist-packages/yaml/scanner.py", line 116, in check_token
    self.fetch_more_tokens()
  File "/usr/lib/python2.7/dist-packages/yaml/scanner.py", line 257, in fetch_more_tokens
    % ch.encode('utf-8'), self.get_mark())
ScannerError: while scanning for the next token
found character '%' that cannot start any token
  in "<string>", line 1, column 2:
    {% if grains['cpuarch'] == 'AMD64' %}
     ^
root@automate:/srv/salt/base/win/repo/salt-winrepo.git#

getabc avatar Aug 12 '15 02:08 getabc

@getabc, thanks for the report. Yes, we are aware of the issue and in the process of fixing it.

There was a git usage error that accidentally published part of the WIP_Berryllium+jinja branch into the salt-winrepo master branch. Which as you noticed 'breaks' the winrepo for all minions prior to the 2015.8.x (Berryllium) reeleases.

I will post here when the problem is solved. At this point I expect it to be finnished sometime before the weekend.

Very sorry for any inconvenience caused.

TheBigBear avatar Aug 12 '15 07:08 TheBigBear

@getabc OK, we should be back in business, with the 'side' isue. All those jinja snippets are removed from the master branch again.

TheBigBear avatar Aug 12 '15 19:08 TheBigBear

But I still haven't found a way to quote the sls file in such a way that the jinja templating engine or the python shell execution parsing works for me. Any ideas what else I should try?

TheBigBear avatar Aug 13 '15 11:08 TheBigBear

@TheBigBear thanks :+1:

getabc avatar Aug 13 '15 17:08 getabc

@twangboy ok, I looked into the code and it seems that the following line is responsible for the 'broken up' cmd line call.

https://github.com/saltstack/salt/blob/develop/salt/modules/win_pkg.py#L569

So maybe 'shlex' could help here too? (see https://stackoverflow.com/questions/16603310/python-split-string-on-quotes )

TheBigBear avatar Aug 17 '15 10:08 TheBigBear

@twangboy @UthaDave what are the 'extra_install_flags' and 'extra_uninstall_flags' really used for? Could they also contain things that need quoting like double quotes etc?

https://github.com/saltstack/salt/blob/develop/salt/modules/win_pkg.py#L563 and https://github.com/saltstack/salt/blob/develop/salt/modules/win_pkg.py#L677

Do we need to shlex.split() those as well?

TheBigBear avatar Aug 17 '15 11:08 TheBigBear

@TheBigBear It looks like extra_install_flags becomes a part of install_flags which then goes through your shlex.split.

extra_uninstall_flags doesn't, so I would say to do extra_uninstall_flags

twangboy avatar Aug 17 '15 18:08 twangboy

@twangboy ok, I have done the extra_uninstall_flags as well, but did I get the python right? could you please look it over? Thanks.

TheBigBear avatar Aug 17 '15 18:08 TheBigBear

@TheBigBear It looks good to me. I'm waiting for the tests to pass and then I'll merge it.

twangboy avatar Aug 17 '15 18:08 twangboy

@TheBigBear Awesome fix, man. Thanks!

twangboy avatar Aug 17 '15 19:08 twangboy