systemd / pystemd

A thin Cython-based wrapper on top of libsystemd, focused on exposing the dbus API via sd-bus in an automated and easy to consume way.
GNU Lesser General Public License v2.1
411 stars 36 forks source link

is there any way to get better error messages from dbuslib.call_method? #3

Closed kurtbrose closed 6 years ago

kurtbrose commented 6 years ago

Trying to follow the example for starting a transient unit from here: https://github.com/facebookincubator/pystemd/blob/master/examples/start_transient_unit.py

        manager.Manager.StartTransientUnit(
            procname, b'fail',
            {
                b'Description': b'Carbonite Crawl %s on %s' % (name, target),
                b'ExecStart': [(cmd[0], cmd, False)],
                b'RemainAfterExit': True
            })

However, I'm getting the following error:

pystemd/dbuslib.pyx in pystemd.dbuslib.DBus.call_method()

TypeError: expected bytes, int found

> /home/fedora/carbonite/pystemd/dbuslib.pyx(389)pystemd.dbuslib.DBus.call_method()

Because it is cython, I don't know how far the loop got.

I can see the args getting passed in to call_method:

ipdb> args
self = <pystemd.systemd1.manager.Manager object at 0x7f0fd922f160>
interface_name = b'org.freedesktop.systemd1.Manager'
name = b'carbonite.crawl-test-auto-crawl.service'
smode = b'fail'
properties = {b'Description': b'Carbonite Crawl test-auto-crawl on ec2-52-26-199-38.us-west-2.compute.amazonaws.com', b'ExecStart': [(115, b'scrapy crawl sl_spider -a session_name=test-auto-crawl -a usernames=email-1024 -a password=wrong-password -a target=https://ec2-52-26-199-38.us-west-2.compute.amazonaws.com\n', False)], b'RemainAfterExit': True}
extra_units = None

I think putting the value and/or name of the argument causing the problem would improve debugability a lot.

EDIT: the bug was I forgot to split the byte-string into a list, so cmd[0] was returning an integer

aleivag commented 6 years ago

Hi @kurtbrose , thanks for summiting this.

it seems that you already solve this, may i also point to you to pystemd.run

import shlex
import pystemd.run
acmd = shlex.split(cmd) # or just have it as an array
unit = pystemd.run(acmd, remain_after_exit=True)

will return you a pystemd.systemd1.Unit object, that you can query for runtime information (and start/stop/restart/etc)


now about the request:

Yes it would be a nice addition, i was concentrating on making pystemd accept regular strings first (that is done, so i expect to put a new release today).

about this, i would definitely work on this. i still need to think exactly how to do it. The thing is that i dont want to assume anything about the used types, and i would much ratter let libsystemd0 reject types as they come, but with that said i can work on some sort of warning (hey you are putting a type X, but expected something from the family of type Y). let me think about it, and really thanks for the input.

Alvaro

kurtbrose commented 6 years ago

thanks!

kurtbrose commented 6 years ago

Thanks for the suggestion; psytemd.run is launching processes; now I'm trying to figure out how to monitor the running processes:

name = b'test.sleep.service'
pystemd.run([b'/usr/bin/sleep', b'10'], name=name)
unit = Unit(name, _autoload=True)
unit.Service.MainPID  # always 0?

Does this seem like the correct way to use the API?

aleivag commented 6 years ago

yes, MainPID shoudl be 0 when the process exit (or never started). you can do a few extra things:

unit = pystemd.run([b'/usr/bin/sleep', b'10'], remain_after_exit=True)
print('MainPID', unit.Service.MainPID)
print('SubState', unit.Unit.SubState)
print('LoadState', unit.Unit.LoadState)

you'll get

MainPID 3351
SubState b'running'
LoadState b'loaded'

By passing the remain_after_exit you make the unit to stay in memory after it finish, and allow pystemd.run to return a Unit file. if you don't specify, then when the unit finish (your 10 seconds), the unit will just go away.

there some (not the best i agree) documentation here https://github.com/facebookincubator/pystemd/blob/master/_docs/pystemd.run.md

aleivag commented 6 years ago

i just landed something that will help with the debug, is not the best thing, but its there... this will continue to be on our radar,