summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorRick Harris <rick.harris@rackspace.com>2010-12-16 20:59:27 -0600
committerRick Harris <rick.harris@rackspace.com>2010-12-16 20:59:27 -0600
commite601ab4a1068029b2f0b79789ed506fda1332404 (patch)
treebe601620a15ccb0227476e4233177efb8cdaa951 /plugins
parentcd460a1f661eea7e050891f50a8218fdf24f2c6f (diff)
downloadnova-e601ab4a1068029b2f0b79789ed506fda1332404.tar.gz
nova-e601ab4a1068029b2f0b79789ed506fda1332404.tar.xz
nova-e601ab4a1068029b2f0b79789ed506fda1332404.zip
XenAPI Snapshots first cut
Diffstat (limited to 'plugins')
-rw-r--r--plugins/xenapi/etc/xapi.d/plugins/glance108
1 files changed, 108 insertions, 0 deletions
diff --git a/plugins/xenapi/etc/xapi.d/plugins/glance b/plugins/xenapi/etc/xapi.d/plugins/glance
new file mode 100644
index 000000000..55d71b79e
--- /dev/null
+++ b/plugins/xenapi/etc/xapi.d/plugins/glance
@@ -0,0 +1,108 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2010 Citrix Systems, Inc.
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+#
+# XenAPI plugin for putting images into glance
+#
+
+import base64
+import errno
+import hmac
+import os
+import os.path
+import sha
+import subprocess
+import time
+import urlparse
+
+import XenAPIPlugin
+
+#FIXME(sirp): should this use pluginlib from 5.6?
+from pluginlib_nova import *
+configure_logging('glance')
+
+#FIXME(sirp): Should this just be 64K like elsewhere
+CHUNK_SIZE = 4096
+
+FILE_SR_PATH = '/var/run/sr-mount'
+
+#TODO(sirp): use get_vhd_parent code from Citrix guys
+#TODO(sirp): look for Citrix guys get VDI from VM_ref (does this exist in
+#pluginlib?
+# Need to add glance client to plugins (how do we get this onto XenServer
+# machine?)
+# WHen do we cleanup unused VDI's in SR
+
+def put_vdis(session, args):
+ vdi_uuids = exists(args, 'vdi_uuids').split(',')
+ glance_label = exists(args, 'glance_label')
+
+ sr_path = get_sr_path(session)
+ tar_cmd = ['tar', '-zc', '--directory=%s' % sr_path]
+ paths = [ "%s.vhd" % vdi_uuid for vdi_uuid in vdi_uuids ]
+ tar_cmd.extend(paths)
+ logging.debug("Bundling image with cmd: %s", tar_cmd)
+ tar_proc = subprocess.Popen(tar_cmd, stdout=subprocess.PIPE)
+
+ test_file = "%s.tar.gz" % os.path.join('/tmp', glance_label)
+ logging.debug("Writing to test file %s", test_file)
+ bundle = tar_proc.stdout
+ f = open(test_file, 'w')
+ try:
+ chunk = bundle.read(CHUNK_SIZE)
+ while chunk:
+ f.write(chunk)
+ chunk = bundle.read(CHUNK_SIZE)
+ finally:
+ f.close()
+
+ return "" # FIXME(sirp): return anything useful here?
+
+
+def get_sr_path(session):
+ sr_ref = find_sr(session)
+
+ if sr_ref is None:
+ raise Exception('Cannot find SR to read VDI from')
+
+ sr_rec = session.xenapi.SR.get_record(sr_ref)
+ sr_uuid = sr_rec["uuid"]
+ sr_path = os.path.join(FILE_SR_PATH, sr_uuid)
+ return sr_path
+
+
+#TODO(sirp): both objectstore and glance need this, should this be refactored
+#into common lib
+def find_sr(session):
+ host = get_this_host(session)
+ srs = session.xenapi.SR.get_all()
+ for sr in srs:
+ sr_rec = session.xenapi.SR.get_record(sr)
+ if not ('i18n-key' in sr_rec['other_config'] and
+ sr_rec['other_config']['i18n-key'] == 'local-storage'):
+ continue
+ for pbd in sr_rec['PBDs']:
+ pbd_rec = session.xenapi.PBD.get_record(pbd)
+ if pbd_rec['host'] == host:
+ return sr
+ return None
+
+
+if __name__ == '__main__':
+ XenAPIPlugin.dispatch({'put_vdis': put_vdis})