Tuesday, 10 September 2019

Perfect Wrapper (in Python)

I run a configuration management tool which calls /usr/bin/dpkg, but does not show the stdout/stderr.

Something goes wrong and I want to debug the root of the problem.

I want to see all calls to dpkg and stdout/stderr.

I moved the original /usr/bin/dpkg to /usr/bin/dpkg-orig and wrote a wrapper:

#!/usr/bin/env python
import os
import sys
import datetime
import subx
import psutil
cmd=list(sys.argv)
cmd[0]='dpkg-orig'

def parents(pid=None):
    if pid==1:
        return '\n'
    if pid is None:
        pid = os.getpid()
    process = psutil.Process(pid)
    lines = [parents(process.ppid())]
    lines.append('Parent: %s' % ' '.join(process.cmdline()))
    return '\n'.join(lines)

result = subx.call(cmd, assert_zero_exit_status=False)
with open('/var/tmp/dpkg-calls.log', 'ab') as fd:
    fd.write('----------- %s\n' % (datetime.datetime.now()))
    fd.write('%s\n' % parents())
    fd.write('stdout:\n%s\n\n' % result.stdout)
    sys.stdout.write(result.stdout)
    fd.write('stderr:\n%s\n' % result.stderr)
    fd.write('ret: %s\n' % result.ret)
    sys.stderr.write(result.stderr)
sys.exit(result.ret)

Now I run the configuration management tool again and searched for non zero "ret:" lines.

The output:

Parent: /usr/bin/apt-get -q -y -o DPkg::Options::=--force-confold -o DPkg::Options::=--force-confdef install openssl-foo-bar-aptguettler.cert
Parent: python /usr/bin/dpkg --force-confold --force-confdef --status-fd 67 --no-triggers --unpack --auto-deconfigure /var/cache/apt/archives/openssl-foo-bar-aptguettler.cert_1-2_all.deb

stdout:


stderr:
dpkg: error: unable to read filedescriptor flags for <package status and progress file descriptor>: Bad file descriptor

ret: 2

This happens because my wrapper is not perfect yet.

The tool which calls dpkg wants to read the file descriptor but this does not work with my wrapper.

My goal:

  • Capture all calls to dpkg and write it to a logfile (works)
  • Write out the parent processes (works)
  • The parent process of dpkg should not notice a difference and not fail like above (does not work yet).

Any idea how to achieve this?



from Perfect Wrapper (in Python)

No comments:

Post a Comment