Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a script to call a service

This implements taskOwner, but is racy because the task isn't
registered until after the attach/detach call returns.

Signed-off-by: David Scott <dave.scott@eu.citrix.com>
  • Loading branch information...
commit 5dfdda3face2fe9dacb0a47155928614104a03ca 1 parent e3834c3
@djs55 authored
Showing with 132 additions and 0 deletions.
  1. +132 −0 dbus/vm/python/dbus-call.py
View
132 dbus/vm/python/dbus-call.py
@@ -0,0 +1,132 @@
+#!/usr/bin/env python
+
+# Wrapper around single-shot resource attach/detach scripts.
+#
+# The 'resource' interface allows storage and networks to be attached
+# and detached by single-shot scripts. All operations are asynchronous,
+# 'attach' and 'detach' return a 'task' object which has a 'cancel'
+# operation. When 'cancel' is called, the running subprocesses are
+# first sent SIGTERM and then SIGKILL.
+
+import syslog
+import threading
+import urlparse
+import time
+import getopt
+import os
+import sys
+import subprocess
+import gtk
+import gobject
+import dbus
+import dbus.service
+from dbus.mainloop.glib import DBusGMainLoop
+
+my_name = "dbus-call"
+
+uri = None
+debug_on = False
+def debug(message):
+ if debug_on:
+ print "DEBUG: ", message
+ sys.stdout.flush()
+
+syslog.openlog(my_name)
+def info(message):
+ if debug_on:
+ print "INFO: ", message
+ sys.stdout.flush()
+ syslog.syslog(message)
+def error(message):
+ if debug_on:
+ print "ERROR: ", message
+ sys.stdout.flush()
+ syslog.syslog(syslog.LOG_ERR, message)
+
+try:
+ opts, args = getopt.getopt(sys.argv[1:],"du",["debug","uri="])
+except getopt.GetoptError:
+ print '%s -u <uri>' % my_name
+ sys.exit(2)
+for opt, arg in opts:
+ if opt in ("-d", "--debug"):
+ debug_on = True
+ elif opt == '-h':
+ print '%s -u <uri>' % my_name
+ sys.exit()
+ elif opt in ("-u", "--uri"):
+ uri = arg
+
+if not(uri):
+ print "Please supply URI to resource object (-u or --uri=)"
+ sys.exit(2)
+
+if len(args) <> 2:
+ print "Please supply a command and argument (e.g. attach iscsi://foo)"
+ sys.exit(2)
+command = args[0]
+argument = args[1]
+
+TASK_INTERFACE="org.xenserver.api.task"
+TASKOWNER_INTERFACE="org.xenserver.api.taskOwner"
+RESOURCE_INTERFACE="org.xenserver.api.resource"
+
+class TaskOwner(dbus.service.Object):
+ def __init__(self):
+ self.bus = dbus.SessionBus()
+ self.path = "/org/xenserver/task/owner"
+ scheme = self.bus.get_unique_name()[1:] #remove : prefix
+ self.uri = scheme + "://" + self.path
+ info("TaskOwner registered at %s" % self.uri)
+ dbus.service.Object.__init__(self, self.bus, self.path)
+
+ @dbus.service.method(dbus_interface=TASKOWNER_INTERFACE, in_signature="as", out_signature="ab")
+ def ping(self, uris):
+ results = []
+ for uri in uris:
+ debug("%s: I own dat" % uri)
+ results.append(True)
+ return results
+
+ @dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE, in_signature='ss', out_signature='v')
+ def Get(self, interface_name, property_name):
+ return self.GetAll(interface_name)[property_name]
+
+ @dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE, in_signature='s', out_signature='a{sv}')
+ def GetAll(self, interface_name):
+ if interface_name == TASKOWNER_INTERFACE:
+ return { }
+ else:
+ raise dbus.exceptions.DBusException(
+ 'com.example.UnknownInterface',
+ 'The Task object does not implement the %s interface'
+ % interface_name)
+
+#gobject.threads_init()
+DBusGMainLoop(set_as_default=True)
+taskOwner = TaskOwner()
+uri = urlparse.urlparse(uri)
+bus = dbus.SessionBus()
+proxy = bus.get_object(uri.scheme, uri.path)
+
+if command == "attach":
+ task = proxy.attach(argument, taskOwner.uri, dbus_interface=RESOURCE_INTERFACE)
+elif command == "detach":
+ task = proxy.detach(argument, taskOwner.uri, dbus_interface=RESOURCE_INTERFACE)
+else:
+ print "Unknown command %s, expected either 'attach' or 'detach'" % command
+ sys.exit(2)
+info("%s: created" % task)
+uri = urlparse.urlparse(task)
+task_proxy = bus.get_object(uri.scheme, uri.path)
+
+def handler():
+ info("%s: got Completed signal" % task)
+ result = task_proxy.getResult(dbus_interface=TASK_INTERFACE)
+ info("%s: result = %s" % (task, result))
+ task_proxy.destroy(dbus_interface=TASK_INTERFACE)
+ loop.quit()
+
+task_proxy.connect_to_signal("Completed", handler)
+loop = gobject.MainLoop()
+loop.run()
Please sign in to comment.
Something went wrong with that request. Please try again.