{"action":"opened","issue":{"url":"https://api.github.com/repos/saltstack/salt/issues/34514","repository_url":"https://api.github.com/repos/saltstack/salt","labels_url":"https://api.github.com/repos/saltstack/salt/issues/34514/labels{/name}","comments_url":"https://api.github.com/repos/saltstack/salt/issues/34514/comments","events_url":"https://api.github.com/repos/saltstack/salt/issues/34514/events","html_url":"https://github.com/saltstack/salt/issues/34514","id":164359274,"number":34514,"title":"pkg.installed can fail if any line in .deb Desciption starts with any of the following: 'Version:', 'Package:' or 'Description:'","user":{"login":"xfernando","id":8411044,"avatar_url":"https://avatars.githubusercontent.com/u/8411044?v=3","gravatar_id":"","url":"https://api.github.com/users/xfernando","html_url":"https://github.com/xfernando","followers_url":"https://api.github.com/users/xfernando/followers","following_url":"https://api.github.com/users/xfernando/following{/other_user}","gists_url":"https://api.github.com/users/xfernando/gists{/gist_id}","starred_url":"https://api.github.com/users/xfernando/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xfernando/subscriptions","organizations_url":"https://api.github.com/users/xfernando/orgs","repos_url":"https://api.github.com/users/xfernando/repos","events_url":"https://api.github.com/users/xfernando/events{/privacy}","received_events_url":"https://api.github.com/users/xfernando/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-07-07T17:03:19Z","updated_at":"2016-07-07T17:03:19Z","closed_at":null,"body":"### Description of Issue\r\nIf a line in the *Descritpion:* field of a .deb file starts with any of the following, salt might not detect that a package was properly installed:\r\n\r\n    Package: \r\n    Version:\r\n    Architecture:\r\n\r\n### Steps to Reproduce Issue\r\nI ran into this weird issue when trying to install some .deb packages needed for the TSM client with **pkg.installed** that have been created in house a long time ago that had the following output (I ommited parts of the output not relevant to the issue) for **dpkg-deb -I gskcrypt64_8.0-15.11_amd64.deb**:\r\n\r\n    Package: gskcrypt64\r\n    Version: 8.0-15.11\r\n    Architecture: amd64\r\n    Maintainer: Alexandre\r\n    Installed-Size: 4029\r\n    Section: alien\r\n    Priority: extra\r\n    Description: IBM GSKit Cryptography Runtime\r\n     Version: 6.3.0\r\n\r\nI used the following state:\r\n\r\n    gskcrypt64\r\n      pkg.installed:\r\n        - sources:\r\n          - gskcrypt64: salt://tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb\r\n\r\nSo what happens is that the package was being successfully installed, but salt was always reporting that the state failed.\r\n\r\n    ID: gskcrypt64\r\n    Function: pkg.installed\r\n    Result: False\r\n    Comment: The following packages failed to install/update: gskcrypt64\r\n\r\nSo I ran salt-call -l debug state.apply tsm.packages and saw this in the output:\r\n\r\n    [DEBUG   ] Current version (['8.0-15.11']) did not match desired version specification (salt://tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb), adding to installation targets\r\n    [DEBUG   ] Could not LazyLoad pkg.normalize_name\r\n    [DEBUG   ] In saltenv 'base', looking at rel_path 'tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb' to resolve 'salt://tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb'\r\n    [DEBUG   ] In saltenv 'base', ** considering ** path '/var/cache/salt/minion/files/base/tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb' to resolve 'salt://tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb'\r\n    [DEBUG   ] Fetching file from saltenv 'base', ** attempting ** 'salt://tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb'\r\n    [DEBUG   ] No dest file found \r\n    [INFO    ] Fetching file from saltenv 'base', ** done ** 'tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb'\r\n    [INFO    ] Executing command ['dpkg', '-I', '/var/cache/salt/minion/files/base/tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb'] in directory '/root'\r\n    [INFO    ] Executing command ['dpkg', '-i', '--force-confold', '/var/cache/salt/minion/files/base/tsm/files/debs/gskcrypt64_8.0-15.11_amd64.deb'] in directory '/root'\r\n    [INFO    ] Executing command ['dpkg-query', '--showformat', '${Status} ${Package} ${Version} ${Architecture}\\n', '-W'] in directory '/root'\r\n    [INFO    ] Executing command ['dpkg', '--get-selections', 'gskcrypt64'] in directory '/root'\r\n    [INFO    ] Executing command ['dpkg', '--get-selections', '*'] in directory '/root'\r\n    [INFO    ] Executing command 'dpkg --set-selections' in directory '/root'\r\n    [ERROR   ] The following packages failed to install/update: gskcrypt64\r\n\r\nI found it weird that it said the current version did not match the specification version, so i checked if it was installed and in the proper version:\r\n\r\n    dpkg -l | grep gskcrypt64\r\n    ii  gskcrypt64                          8.0-15.11                      amd64        IBM GSKit Cryptography Runtime\r\n\r\nIt was properly insalled with the correct version number, so no luck figuring out with the debug log what was going wrong. So I installed pudb and started debugging salt-call.\r\n\r\nA few minutes later I found the problem in the bin_pkg_info fucntion:\r\n\r\nhttps://github.com/saltstack/salt/blob/develop/salt/modules/dpkg.py#L85-L92\r\n\r\n```python\r\n    for line in result['stdout'].splitlines():\r\n        line = line.strip()\r\n        if line.startswith('Package:'):\r\n            ret['name'] = line.split()[-1]\r\n        elif line.startswith('Version:'):\r\n            ret['version'] = line.split()[-1]\r\n        elif line.startswith('Architecture:'):\r\n            ret['arch'] = line.split()[-1]\r\n```\r\n\r\nSo the code was reading the Version correctly from the proper field (8.0-15.11), but when it got to the last line, in the description, it also starts with 'Version:' (this version being the version of the TSM client, not of this particular lib).\r\n\r\nSo it overwrites ret['version'] with this other value, which I assume is used later to compare with the version installed on the system, resulting in failing the state since the versions mismatched.\r\n\r\nI thought a bit about the best way to prevent this from happening and I think that modifying the code above to the following would be the best approach:\r\n\r\n```python\r\n    for line in result['stdout'].splitlines():\r\n        line = line.strip()\r\n        if 'name' not in ret and line.startswith('Package:'):\r\n            ret['name'] = line.split()[-1]\r\n        elif 'version not in ret and line.startswith('Version:'):\r\n            ret['version'] = line.split()[-1]\r\n        elif 'arch' not in ret and line.startswith('Architecture:'):\r\n            ret['arch'] = line.split()[-1]\r\n```\r\n\r\nThis would prevent the dictionary from being updated after the first time setting the value.\r\n\r\n### Versions Report\r\nThe version on the minion is 2016.3.0, but it's not relevant to the issue.\r\n\r\n```\r\nSalt Version:\r\n           Salt: 2016.3.1\r\n \r\nDependency Versions:\r\n           cffi: 0.8.6\r\n       cherrypy: 3.2.2\r\n       dateutil: 1.5\r\n          gitdb: 0.6.4\r\n      gitpython: Not Installed\r\n          ioflo: Not Installed\r\n         Jinja2: 2.7.2\r\n        libgit2: 0.24.0\r\n        libnacl: Not Installed\r\n       M2Crypto: Not Installed\r\n           Mako: Not Installed\r\n   msgpack-pure: Not Installed\r\n msgpack-python: 0.4.7\r\n   mysql-python: Not Installed\r\n      pycparser: 2.14\r\n       pycrypto: 2.6.1\r\n         pygit2: 0.24.0\r\n         Python: 2.7.5 (default, Nov 20 2015, 02:00:19)\r\n   python-gnupg: Not Installed\r\n         PyYAML: 3.11\r\n          PyZMQ: 14.7.0\r\n           RAET: Not Installed\r\n          smmap: 0.9.0\r\n        timelib: Not Installed\r\n        Tornado: 4.2.1\r\n            ZMQ: 4.0.5\r\n \r\nSystem Versions:\r\n           dist: centos 7.2.1511 Core\r\n        machine: x86_64\r\n        release: 3.10.0-327.18.2.el7.x86_64\r\n         system: Linux\r\n        version: CentOS Linux 7.2.1511 Core\r\n```"}}
{"actor":{"display_login":"xfernando"}}