Index: /package_tags/sipb-xen-remote-server/0.2/debian/changelog
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/changelog	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/changelog	(revision 677)
@@ -0,0 +1,13 @@
+sipb-xen-remote-server (0.2) unstable; urgency=low
+
+  * FUSE filesystem for remctl configuration.
+  * Remove *register calls, since the FUSE keeps things up to date.
+  * An init script to keep the FUSE fs up.
+
+ -- Greg Price <price@mit.edu>  Sat, 10 May 2008 22:04:32 -0400
+
+sipb-xen-remote-server (0.1) unstable; urgency=low
+
+  * First draft.
+
+ -- Greg Price <price@mit.edu>  Sun, 30 Mar 2008 01:08:50 -0400
Index: /package_tags/sipb-xen-remote-server/0.2/debian/compat
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/compat	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/compat	(revision 677)
@@ -0,0 +1,1 @@
+5
Index: /package_tags/sipb-xen-remote-server/0.2/debian/control
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/control	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/control	(revision 677)
@@ -0,0 +1,14 @@
+Source: sipb-xen-remote-server
+Section: servers
+Priority: important
+Maintainer: sipb-xen@mit.edu
+Build-Depends: cdbs (>= 0.4.23-1.1), debhelper (>= 5)
+Standards-Version: 3.7.2
+
+Package: sipb-xen-remote-server
+Architecture: all
+Provides: ${diverted-files}
+Conflicts: ${diverted-files}
+Depends: ${shlibs:Depends}, ${misc:Depends}, debathena-kerberos-config, fuse-utils, openssh-server, python-fuse, sipb-xen-chrony-config, sipb-xen-database-common, remctl-server, remctl-client
+Description: SIPB Xen remote-control server
+ This package should be installed to set up the remote-control server.
Index: /package_tags/sipb-xen-remote-server/0.2/debian/control.in
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/control.in	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/control.in	(revision 677)
@@ -0,0 +1,14 @@
+Source: sipb-xen-remote-server
+Section: servers
+Priority: important
+Maintainer: sipb-xen@mit.edu
+Build-Depends: @cdbs@
+Standards-Version: 3.7.2
+
+Package: sipb-xen-remote-server
+Architecture: all
+Provides: ${diverted-files}
+Conflicts: ${diverted-files}
+Depends: ${shlibs:Depends}, ${misc:Depends}, daemon, debathena-kerberos-config, fuse-utils, openssh-server, python-fuse, sipb-xen-chrony-config, sipb-xen-database-common, remctl-server, remctl-client
+Description: SIPB Xen remote-control server
+ This package should be installed to set up the remote-control server.
Index: /package_tags/sipb-xen-remote-server/0.2/debian/copyright
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/copyright	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/copyright	(revision 677)
@@ -0,0 +1,3 @@
+This package was created for internal use of the SIPB Xen Project of
+the MIT Student Information Processing Board.  Ask sipb-xen@mit.edu if
+you have questions about redistribution.
Index: /package_tags/sipb-xen-remote-server/0.2/debian/files
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/files	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/files	(revision 677)
@@ -0,0 +1,1 @@
+sipb-xen-remote-server_0.1_all.deb servers important
Index: /package_tags/sipb-xen-remote-server/0.2/debian/rules
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/rules	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/rules	(revision 677)
@@ -0,0 +1,11 @@
+#!/usr/bin/make -f
+
+DEB_AUTO_UPDATE_DEBIAN_CONTROL = 1
+DEB_DIVERT_EXTENSION = .sipb-xen
+#DEB_DIVERT_FILES_sipb-xen-remote-server += \
+#	/etc/init.d/bootmisc.sh \
+#	/etc/nscd.conf \
+#	/etc/nsswitch.conf \
+#	/etc/ssh/sshd_config
+
+include /usr/share/cdbs/1/rules/debhelper.mk
Index: /package_tags/sipb-xen-remote-server/0.2/debian/sipb-xen-remote-server.init
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/sipb-xen-remote-server.init	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/sipb-xen-remote-server.init	(revision 677)
@@ -0,0 +1,126 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides:          sipb-xen-remote-server
+# Required-Start:    $local_fs $remote_fs
+# Required-Stop:     $local_fs $remote_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: sipb-xen remctl configuration filesystem
+# Description:       
+### END INIT INFO
+
+# Author: SIPB Xen Project <sipb-xen@mit.edu>
+
+# Do NOT "set -e"
+
+# PATH should only include /usr/* if it runs after the mountnfs.sh script
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="The sipb-xen remctl configuration filesystem"
+NAME=sipb-xen-remconffs
+DAEMON=/usr/sbin/sipb-xen-remconffs
+DAEMON_ARGS="/etc/remctl/remconffs"
+PIDFILE=/var/run/$NAME.pid
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Read configuration variable file if it is present
+[ -r /etc/default/$NAME ] && . /etc/default/$NAME
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+	# Return
+	#   0 if daemon has been started
+	#   1 if daemon was already running
+	#   2 if daemon could not be started
+	modprobe fuse
+	daemon --running -n $NAME && return 1
+	daemon -r -O daemon.info -E daemon.err -n $NAME -U $DAEMON $DAEMON_ARGS || return 2
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+	# Return
+	#   0 if daemon has been stopped
+	#   1 if daemon was already stopped
+	#   2 if daemon could not be stopped
+	#   other if a failure occurred
+	daemon --stop -n $NAME
+	RETVAL="$?"
+	[ "$RETVAL" = 2 ] && return 2
+	# Many daemons don't delete their pidfiles when they exit.
+	rm -f $PIDFILE
+	umount "$DAEMON_ARGS"
+	return "$RETVAL"
+}
+
+case "$1" in
+  start)
+	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+	do_start
+	case "$?" in
+		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+	esac
+	;;
+  stop)
+	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+	do_stop
+	case "$?" in
+		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+	esac
+	;;
+  #reload|force-reload)
+	#
+	# If do_reload() is not implemented then leave this commented out
+	# and leave 'force-reload' as an alias for 'restart'.
+	#
+	#log_daemon_msg "Reloading $DESC" "$NAME"
+	#do_reload
+	#log_end_msg $?
+	#;;
+  restart|force-reload)
+	#
+	# If the "reload" option is implemented then remove the
+	# 'force-reload' alias
+	#
+	log_daemon_msg "Restarting $DESC" "$NAME"
+	do_stop
+	case "$?" in
+	  0|1)
+		do_start
+		case "$?" in
+			0) log_end_msg 0 ;;
+			1) log_end_msg 1 ;; # Old process is still running
+			*) log_end_msg 1 ;; # Failed to start
+		esac
+		;;
+	  *)
+	  	# Failed to stop
+		log_end_msg 1
+		;;
+	esac
+	;;
+  *)
+	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
+	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
+	exit 3
+	;;
+esac
+
+:
Index: /package_tags/sipb-xen-remote-server/0.2/debian/sipb-xen-remote-server.install
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/debian/sipb-xen-remote-server.install	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/debian/sipb-xen-remote-server.install	(revision 677)
@@ -0,0 +1,1 @@
+files/* .
Index: /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/acl/web
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/acl/web	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/acl/web	(revision 677)
@@ -0,0 +1,2 @@
+price/root@ATHENA.MIT.EDU
+daemon/sipb-xen.mit.edu@ATHENA.MIT.EDU
Index: /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/conf.d/remconffs
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/conf.d/remconffs	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/conf.d/remconffs	(revision 677)
@@ -0,0 +1,1 @@
+include /etc/remctl/remconffs/conf
Index: /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/conf.d/sipb-xen-web
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/conf.d/sipb-xen-web	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/etc/remctl/conf.d/sipb-xen-web	(revision 677)
@@ -0,0 +1,9 @@
+web lvcreate     /usr/sbin/sipb-xen-remote-proxy-web /etc/remctl/acl/web
+web lvremove     /usr/sbin/sipb-xen-remote-proxy-web /etc/remctl/acl/web
+web lvrename     /usr/sbin/sipb-xen-remote-proxy-web /etc/remctl/acl/web
+web lvresize     /usr/sbin/sipb-xen-remote-proxy-web /etc/remctl/acl/web
+web lvcopy       /usr/sbin/sipb-xen-remote-proxy-web /etc/remctl/acl/web
+web listvms      /usr/sbin/sipb-xen-remote-proxy-web /etc/remctl/acl/web
+test sleep       /usr/bin/env /etc/remctl/acl/web
+control help	 /usr/sbin/sipb-xen-remctl-help	ANYUSER
+help ALL	 /usr/sbin/sipb-xen-remctl-help	ANYUSER
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remconffs
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remconffs	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remconffs	(revision 677)
@@ -0,0 +1,236 @@
+#!/usr/bin/python
+
+import fuse
+from fuse import Fuse
+
+from time import time
+
+import stat	# for file properties
+import os	  # for filesystem modes (O_RDONLY, etc)
+import errno   # for error number codes (ENOENT, etc)
+			   # - note: these must be returned as negatives
+
+from syslog import *
+
+import sipb_xen_database
+
+fuse.fuse_python_api = (0, 2)
+
+def getDepth(path):
+	"""
+	Return the depth of a given path, zero-based from root ('/')
+	"""
+	if path == '/':
+		return 0
+	else:
+		return path.count('/')
+
+def getParts(path):
+	"""
+	Return the slash-separated parts of a given path as a list
+	"""
+	# [1:] to exclude leading empty element
+	split = path.split('/')
+	if split[-1]:
+		return split[1:]
+	else:
+		return split[1:-1]
+
+def parse(path):
+	parts = getParts(path)
+	return parts, len(parts)
+
+class MyStat:
+	def __init__(self):
+		self.st_mode = 0
+		self.st_ino = 0
+		self.st_dev = 0
+		self.st_nlink = 0
+		self.st_uid = 0
+		self.st_gid = 0
+		self.st_size = 0
+		self.st_atime = 0
+		self.st_mtime = 0
+		self.st_ctime = 0
+	
+	def toTuple(self):
+		return (self.st_mode, self.st_ino, self.st_dev, self.st_nlink, self.st_uid, self.st_gid, self.st_size, self.st_atime, self.st_mtime, self.st_ctime)
+
+class RemConfFS(Fuse):
+	"""
+	RemConfFS creates a filesytem for configuring remctl, like this:
+	/
+	|-- acl
+	|   |-- machine1
+	|   ...
+	|   `-- machinen
+	`-- conf.d
+	    |-- machine1
+	    ...
+	    `-- machinen
+
+	The machine list and the acls are drawn from a database.
+	
+	This filesystem only implements the getattr, getdir, read, and readlink
+	calls, because this is a read-only filesystem.
+	"""
+	
+	def __init__(self, *args, **kw):
+		"""Initialize the filesystem and set it to allow_other access besides
+		the user who mounts the filesystem (i.e. root)
+		"""
+		Fuse.__init__(self, *args, **kw)
+		self.lasttime = time()
+		self.allow_other = 1
+		
+		openlog('sipb-xen-remconffs ', LOG_PID, LOG_DAEMON)
+		
+		syslog(LOG_DEBUG, 'Init complete.')
+
+	def getMachines(self):
+		"""Get the list of VMs in the database, clearing the cache if it's 
+		older than 15 seconds"""
+		if time() - self.lasttime > 15:
+			self.lasttime = time()
+			sipb_xen_database.clear_cache()
+		return [machine.name for machine in sipb_xen_database.Machine.select()]
+		
+	def getacl(self, machine_name):
+		"""Build the ACL file for a machine
+		"""
+		machine = sipb_xen_database.Machine.get_by(name=machine_name)
+		users = [acl.user for acl in machine.acl]
+		return "\n".join(map(self.userToPrinc, users)
+				 + ['include /etc/remctl/acl/web',
+				    ''])
+		
+	def getconf(self):
+		"""Build the master conf file, with all machines
+		"""
+		return '\n'.join("control %s /usr/sbin/sipb-xen-remote-proxy-control"
+				 " /etc/remctl/remconffs/acl/%s"
+				 % (machine_name, machine_name)
+				 for machine_name in self.getMachines())+'\n'
+	
+	def userToPrinc(self, user):
+		"""Convert Kerberos v4-style names to v5-style and append a default
+		realm if none is specified
+		"""
+		if '@' in user:
+			(princ, realm) = user.split('@')
+		else:
+			princ = user
+			realm = "ATHENA.MIT.EDU"
+		
+		return princ.replace('.', '/') + '@' + realm
+	
+	def getattr(self, path):
+		"""
+		- st_mode (protection bits)
+		- st_ino (inode number)
+		- st_dev (device)
+		- st_nlink (number of hard links)
+		- st_uid (user ID of owner)
+		- st_gid (group ID of owner)
+		- st_size (size of file, in bytes)
+		- st_atime (time of most recent access)
+		- st_mtime (time of most recent content modification)
+		- st_ctime (platform dependent; time of most recent metadata change on Unix,
+					or the time of creation on Windows).
+		"""
+		
+		syslog(LOG_DEBUG, "*** getattr: " + path)
+		
+		depth = getDepth(path)
+		parts = getParts(path)
+		
+		st = MyStat()
+		if path == '/':
+			st.st_mode = stat.S_IFDIR | 0755
+			st.st_nlink = 2
+		elif depth == 1:
+			if parts[0] == 'acl':
+				st.st_mode = stat.S_IFDIR | 0755
+				st.st_nlink = 2
+			elif parts[0] == 'conf':
+				st.st_mode = stat.S_IFREG | 0444
+				st.st_nlink = 1
+				st.st_size = len(self.getconf())
+			else:
+				return -errno.ENOENT
+		elif depth == 2:
+			if parts[0] != 'acl':
+				return -errno.ENOENT
+			if parts[1] not in self.getMachines():
+				return -errno.ENOENT
+			st.st_mode = stat.S_IFREG | 0444
+			st.st_nlink = 1
+			st.st_size = len(self.getacl(parts[1]))
+
+		return st.toTuple()
+	
+	# This call isn't actually used in the version of Fuse on console, but we
+	# wanted to leave it implemented to ease the transition in the future
+	def readdir(self, path, offset):
+		"""Return a generator with the listing for a directory
+		"""
+		syslog(LOG_DEBUG, '*** readdir %s %s' % (path, offset))
+		for (value, zero) in self.getdir(path):
+			yield fuse.Direntry(value)
+	
+	def getdir(self, path):
+		"""Return a list of tuples of the form (item, 0) with the contents of
+		the directory path
+		
+		Fuse doesn't add '.' or '..' on its own, so we have to
+		"""
+		syslog(LOG_DEBUG, '*** getdir %s' % path)
+		
+		parts, depth = parse(path)
+
+		if depth == 0:
+			contents = ('acl', 'conf')
+		elif depth == 1:
+			if parts[0] == 'acl':
+				contents = self.getMachines()
+			else:
+				return -errno.ENOENT
+		else:
+			return -errno.ENOTDIR
+
+		# Format the list the way that Fuse wants it - and don't forget to add
+		# '.' and '..'
+		return [(i, 0) for i in (list(contents) + ['.', '..'])]
+
+	def read(self, path, length, offset):
+		"""Read length bytes starting at offset of path. In most cases, this
+		just gets passed on to the OS
+		"""
+		syslog(LOG_DEBUG, '*** read %s %s %s' % (path, length, offset))
+		
+		parts, depth = parse(path)
+		
+		if depth == 0:
+			return -errno.EISDIR
+		elif parts[0] == 'conf':
+			return self.getconf()[offset:offset+length]
+		elif parts[0] == 'acl':
+			if depth == 1:
+				return -errno.EISDIR
+			if parts[1] in self.getMachines():
+				return self.getacl(parts[1])[offset:offset+length]
+		return -errno.ENOENT
+	
+	def readlink(self, path):
+		syslog(LOG_DEBUG, '*** readlink %s' % path)
+		return -errno.ENOENT
+
+
+if __name__ == '__main__':
+	sipb_xen_database.connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen')
+	usage="""
+$0 [mount_path]
+"""
+	server = RemConfFS()
+	server.flags = 0
+	server.main()
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remctl-help
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remctl-help	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remctl-help	(revision 677)
@@ -0,0 +1,42 @@
+#!/usr/bin/python
+"""
+Help on using the Invirt remctl functions.
+"""
+import sys
+
+
+help = [
+    ('list',      'show your VM\'s state (with xm list)'),
+    ('listhost',  'show on what host, if any, your VM is running'),
+    ('list-long', 'show your VM\'s state as an sexp (with xm list --long)'),
+    ('vcpu-list', 'show your VM\'s state (with xm vcpu-list)'),
+    ('uptime',    'show your VM\'s state (with xm uptime)'),
+    ('destroy',   'shut down your VM, hard (with xm destroy)'),
+    ('shutdown',  'shut down your VM, softly if paravm (with xm shutdown)'),
+    ('create',    'start up your VM (with xm create)'),
+    ('reboot',    'reboot your VM (with xm destroy and xm create)'),
+    #also install
+    #also CD images on create/reboot
+]
+helpdict = dict(help)
+
+
+def print_help(name, text):
+    print '  %-9s : %s' % (name, text)
+
+def main(args):
+    args = [n for n in args if n in helpdict]
+    print 'remctl remote control <machine> <command>'
+    if args:
+        for name in args:
+            print_help(name, helpdict[name])
+    else:
+        for name, text in help:
+            print_help(name, text)
+        
+    return 0
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv[1:]))
+
+# vim:et:sw=4:ts=4
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-control
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-control	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-control	(revision 677)
@@ -0,0 +1,34 @@
+#!/usr/bin/python
+"""
+Sends remctl commands about a running VM to the host it's running on.
+"""
+
+from subprocess import PIPE, Popen, call
+import sys
+import yaml
+
+def main(argv):
+    if len(argv) < 3:
+        print >>sys.stderr, "usage: sipb-xen-remote-control <machine> <command>"
+        return 2
+    machine_name = argv[1]
+    command = argv[2]
+
+    p = Popen(['/usr/sbin/sipb-xen-remote-proxy-web', 'listvms'], stdout=PIPE)
+    output = p.communicate()[0]
+    if p.returncode != 0:
+        raise RuntimeError("Command '%s' returned non-zero exit status %d"
+                           % ('sipb-xen-remote-proxy-web', p.returncode)) 
+    vms = yaml.load(output, yaml.CSafeLoader)
+
+    if machine_name not in vms:
+        print >>sys.stderr, "machine '%s' is not on" % machine_name
+        return 2
+    host = vms[machine_name]['host']
+
+    return call(['remctl', host, 'remote', 'control'] + argv[1:])
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))
+
+# vim:et:sw=4:ts=4
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-create
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-create	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-create	(revision 677)
@@ -0,0 +1,66 @@
+#!/usr/bin/python
+"""
+Picks a host to "create" (boot) a VM on, and does so.
+
+Current load-balancing algorithm: wherever there's more free RAM.
+
+TODO: use a lock to avoid creating the same VM twice in a race
+"""
+
+
+from subprocess import PIPE, Popen, call
+import sys
+import yaml
+
+
+def choose_host():
+    # Query each of the hosts.
+    # TODO get `servers` from a real list of all the VM hosts (instead of
+    # hardcoding the list here)
+    servers = ['black-mesa.mit.edu', 'sx-blade-2.mit.edu']
+    pipes = [(server,
+              Popen(['remctl', server, 'remote', 'web', 'info'], stdout=PIPE))
+             for server in servers]
+    outputs = [(s, p.communicate()[0]) for (s, p) in pipes]
+    for (s, p) in pipes:
+        if p.returncode != 0:
+            raise RuntimeError("remctl to host %s returned non-zero exit status %d"
+                               % (s, p.returncode)) 
+    results = [(s, yaml.load(o, yaml.CSafeLoader)) for (s, o) in outputs]
+    # XXX will the output of 'xm info' always be parseable YAML?
+
+    return max( (int(o['free_memory']), s) for (s, o) in results )[1]
+
+
+def main(argv):
+    if len(argv) < 2:
+        print >>sys.stderr, "usage: sipb-xen-remote-create <machine> [<other args...>]"
+        return 2
+    machine_name = argv[1]
+    args = argv[2:]
+
+
+    p = Popen(['/usr/sbin/sipb-xen-remote-proxy-web', 'listvms'], stdout=PIPE)
+    output = p.communicate()[0]
+    if p.returncode != 0:
+        raise RuntimeError("Command '%s' returned non-zero exit status %d"
+                           % ('sipb-xen-remote-proxy-web', p.returncode)) 
+    vms = yaml.load(output, yaml.CSafeLoader)
+
+    if machine_name in vms:
+        host = vms[machine_name]['host']
+        print >>sys.stderr, ("machine '%s' is already running on host %s"
+                             % (machine_name, host))
+        return 1
+
+
+    host = choose_host()
+    print 'Creating on host %s...' % host
+    sys.stdout.flush()
+    return call(['remctl', host, 'remote', 'control',
+                 machine_name, 'create'] + args)
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))
+
+# vim:et:sw=4:ts=4
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listhost
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listhost	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listhost	(revision 677)
@@ -0,0 +1,33 @@
+#!/usr/bin/python
+"""
+Say what host a running VM is on.
+"""
+
+from subprocess import PIPE, Popen, call
+import sys
+import yaml
+
+def main(argv):
+    if len(argv) < 2:
+        print >>sys.stderr, "usage: sipb-xen-remote-listhost <machine>"
+        return 2
+    machine_name = argv[1]
+
+    p = Popen(['/usr/sbin/sipb-xen-remote-proxy-web', 'listvms'], stdout=PIPE)
+    output = p.communicate()[0]
+    if p.returncode != 0:
+        raise RuntimeError("Command '%s' returned non-zero exit status %d"
+                           % ('sipb-xen-remote-proxy-web', p.returncode)) 
+    vms = yaml.load(output, yaml.CSafeLoader)
+
+    if machine_name not in vms:
+        print >>sys.stderr, "machine '%s' is not on" % machine_name
+        return 2
+
+    print vms[machine_name]['host']
+    return 0
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))
+
+# vim:et:sw=4:ts=4
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listvms
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listvms	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listvms	(revision 677)
@@ -0,0 +1,41 @@
+#!/usr/bin/python
+
+"""
+Collates the results of listvms from multiple VM servers.  Part of the xvm
+suite.
+"""
+
+from subprocess import PIPE, Popen
+import sys
+import yaml
+
+def main(argv):
+    # Query each of the server for their VMs.
+    # TODO get `servers` from a real list of all the VM hosts (instead of
+    # hardcoding the list here)
+    servers = ['black-mesa.mit.edu', 'sx-blade-2.mit.edu']
+    # XXX
+    pipes = [(server,
+              Popen(['remctl', server, 'remote', 'web', 'listvms'], stdout=PIPE))
+             for server in servers]
+    outputs = [(s, p.communicate()[0]) for (s, p) in pipes]
+    for (s, p) in pipes:
+        if p.returncode != 0:
+            raise RuntimeError("remctl to host %s returned non-zero exit status %d"
+                               % (s, p.returncode)) 
+    results = [(s, yaml.load(o, yaml.CSafeLoader)) for (s, o) in outputs]
+    results = filter(lambda (_, x): x is not None, results)
+
+    # Merge the results and print.
+    merged = {}
+    for server, result in results:
+        for data in result.itervalues():
+            data['host'] = server
+        merged.update(result)
+
+    print yaml.dump(merged, Dumper=yaml.CSafeDumper, default_flow_style=False)
+
+if __name__ == '__main__':
+    main(sys.argv)
+
+# vim:et:sw=4:ts=4
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listvmsd
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listvmsd	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-listvmsd	(revision 677)
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+
+# NOTE: In development; not actually used yet.
+
+#Collates the results of listvms from multiple VM servers.  Part of the xvm
+#suite.
+
+use Net::Remctl ();
+use JSON;
+
+our @servers = qw/black-mesa.mit.edu sx-blade-2.mit.edu/;
+
+our %connections;
+
+sub openConnections() {
+    foreach (@servers) { openConnection($_); }
+}
+
+sub openConnection($) {
+    my ($server) = @_;
+    my $remctl = Net::Remctl->new;
+    $remctl->open($server)
+        or die "Cannot connect to $server: ", $remctl->error, "\n";
+    $connections{$server} = $remctl;
+}
+
+sub doListVMs() {
+    foreach my $remctl (values %connections) {
+	$remctl->command("remote", "web", "listvms", "--json");
+    }
+    my %vmstate;
+    foreach my $server (keys %connections) {
+	my $remctl = $connections{$server};
+	my $jsonData = '';
+	do {
+	    $output = $remctl->output;
+	    if ($output->type eq 'output') {
+		if ($output->stream == 1) {
+		    $jsonData .= $output->data;
+		} elsif ($output->stream == 2) {
+		    print STDERR $output->data;
+		}
+	    } elsif ($output->type eq 'error') {
+		warn $output->error, "\n";
+	    } elsif ($output->type eq 'status') {
+		if ($output->status != 0) {
+		    warn "Exit status was ".$output->status;
+		}
+	    } elsif ($output->type eq 'done') {
+		#next;
+	    } else {
+		die "Unknown output token from library: ", $output->type, "\n";
+	    }
+	} while ($output->type ne 'done');
+	my $vmlist = jsonToObj($jsonData);
+	foreach my $key (keys %$vmlist) {
+	    $vmstate{$key} = $vmlist->{$key};
+	    $vmstate{$key}{"host"} = $server;
+	}
+    }
+    return %vmstate;
+}
+
+openConnections();
+
+use Data::Dumper;
+use Benchmark;
+print Dumper({doListVMs()});
+timethis(100, sub {doListVMs()});
+
+# vim:et:sw=4:ts=4
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy	(revision 677)
@@ -0,0 +1,26 @@
+#!/bin/sh
+# invoke as sipb-xen-remote-proxy-$TYPE, with "TYPE" in the remctl sense.
+
+klist -s || kinit -k host/remote.mit.edu
+
+TYPE="${0##*-}"
+case "$TYPE" in
+    control )
+	MACHINE="$1"; SERVICE="$2"; shift; shift ;;
+    * )
+	SERVICE="$1"; shift ;;
+esac
+
+case "$TYPE/$SERVICE" in
+    web/listvms )
+	sipb-xen-remote-listvms "$@" ;;
+    control/create )
+	sipb-xen-remote-create "$MACHINE" "$@" ;;
+    control/listhost )
+	sipb-xen-remote-listhost "$MACHINE" "$@" ;;
+    control/* )
+	# Everything but create must go where the VM is already running.
+	sipb-xen-remote-control "$MACHINE" "$SERVICE" "$@" ;;
+    * )
+	remctl black-mesa remote "$TYPE" "$SERVICE" "$@" ;;
+esac
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy-control
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy-control	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy-control	(revision 677)
@@ -0,0 +1,1 @@
+link sipb-xen-remote-proxy
Index: /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy-web
===================================================================
--- /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy-web	(revision 677)
+++ /package_tags/sipb-xen-remote-server/0.2/files/usr/sbin/sipb-xen-remote-proxy-web	(revision 677)
@@ -0,0 +1,1 @@
+link sipb-xen-remote-proxy
