Permalink
Browse files

Add dbus script wrapper and service definition

The wrapper script is run as a daemon by dbus when the name from
the .service file is looked up. The wrapper script implements a
simple resource interface and runs the script with the method name
as the first argument.

Signed-off-by: David Scott <dave.scott@eu.citrix.com>
  • Loading branch information...
1 parent 72f8a37 commit 4796e3c9f5533be9b47d9d8ee7e4fd6aba7be023 David Scott committed Feb 4, 2014
Showing with 73 additions and 0 deletions.
  1. +60 −0 dbus/vm/python/dbus-resource-script.py
  2. +10 −0 dbus/vm/python/foo
  3. +3 −0 dbus/vm/python/org.xenserver.volume.example.service
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+# simple python-dbus service that exposes 1 method called hello()
+
+import getopt
+import sys
+import subprocess
+import gtk
+import dbus
+import dbus.service
+from dbus.mainloop.glib import DBusGMainLoop
+
+my_name = "dbus-resource-script"
+
+name = None
+script = None
+
+try:
+ opts, args = getopt.getopt(sys.argv[1:],"hn:c:s:",["name=","script="])
+except getopt.GetoptError:
+ print '%s -n <object> -s <script>' % my_name
+ sys.exit(2)
+for opt, arg in opts:
+ if opt == '-h':
+ print '%s -n <object> -s <script>' % my_name
+ sys.exit()
+ elif opt in ("-n", "--name"):
+ name = arg
+ elif opt in ("-s", "--script"):
+ script = arg
+
+if not(name):
+ print "Please supply a bus name argument (-n or --name=)"
+ sys.exit(2)
+if not(script):
+ print "Please supply a script name argument (-s or --script="
+ sys.exit(2)
+
+class Resource(dbus.service.Object):
+ def __init__(self):
+ self.bus = dbus.SessionBus()
+ bus_name = dbus.service.BusName(name, bus=self.bus)
+ dbus.service.Object.__init__(self, bus_name, "/" + name.replace(".", "/"))
+
+ @dbus.service.method(dbus_interface=name, in_signature="s", out_signature="ss")
+ def attach(self, global_uri):
+ print "attach global_uri = %s" % global_uri
+ p = subprocess.Popen([script, "attach", global_uri], stdout=subprocess.PIPE)
+ stdout, stderr = p.communicate()
+ return (stdout, "1")
+
+ @dbus.service.method(dbus_interface=name, in_signature="s", out_signature="")
+ def detach(self, id):
+ print "detach id = %s" % id
+ p = subprocess.Popen([script, "detach", id], stdout=subprocess.PIPE)
+ stdout, stderr = p.communicate()
+ pass
+
+DBusGMainLoop(set_as_default=True)
+resource = Resource()
+gtk.main()
View
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+case "$1" in
+ attach)
+ echo "attached $2"
+ ;;
+ detach)
+ echo "detached $2"
+ ;;
+esac
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name=org.xenserver.volume.example
+Exec=/local/scratch/djs/djs55/dbus-test/dbus/vm/python/dbus-resource-script.py -n org.xenserver.volume.example -s /local/scratch/djs/djs55/dbus-test/dbus/vm/python/foo

0 comments on commit 4796e3c

Please sign in to comment.