From 3067a9d2db0d5d3187e1d7e17c49f166d1781826 Mon Sep 17 00:00:00 2001 From: anthony Date: Fri, 21 Oct 2011 14:26:20 -0700 Subject: [PATCH] Swift : add two new files --- .../api/downloadSnapshotFromSwiftCommand.java | 63 +++++++++++++++ scripts/vm/hypervisor/xenserver/swiftxen | 80 +++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 api/src/com/cloud/agent/api/downloadSnapshotFromSwiftCommand.java create mode 100644 scripts/vm/hypervisor/xenserver/swiftxen diff --git a/api/src/com/cloud/agent/api/downloadSnapshotFromSwiftCommand.java b/api/src/com/cloud/agent/api/downloadSnapshotFromSwiftCommand.java new file mode 100644 index 00000000000..db732c0ed5d --- /dev/null +++ b/api/src/com/cloud/agent/api/downloadSnapshotFromSwiftCommand.java @@ -0,0 +1,63 @@ +/** + * Copyright (C) 2011 Citrix Systems, Inc. All rights reserved + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.agent.api; + +import com.cloud.agent.api.to.SwiftTO; + +/** + * When a snapshot of a VDI is taken, it creates two new files, + * a 'base copy' which contains all the new data since the time of the last snapshot and an 'empty snapshot' file. + * Any new data is again written to the VDI with the same UUID. + * This class issues a command for copying the 'base copy' vhd file to secondary storage. + * This currently assumes that both primary and secondary storage are mounted on the XenServer. + */ +public class downloadSnapshotFromSwiftCommand extends SnapshotCommand { + private SwiftTO _swift; + + private String _parent; + + protected downloadSnapshotFromSwiftCommand() { + + } + + public downloadSnapshotFromSwiftCommand(SwiftTO swift, String secondaryStoragePoolUrl, Long dcId, Long accountId, Long volumeId, String parent, String BackupUuid, int wait) { + + super("", secondaryStoragePoolUrl, BackupUuid, "", dcId, accountId, volumeId); + setParent(parent); + setSwift(swift); + setWait(wait); + } + + + public SwiftTO getSwift() { + return this._swift; + } + + public void setSwift(SwiftTO swift) { + this._swift = swift; + } + + public String getParent() { + return _parent; + } + + public void setParent(String parent) { + this._parent = parent; + } + +} \ No newline at end of file diff --git a/scripts/vm/hypervisor/xenserver/swiftxen b/scripts/vm/hypervisor/xenserver/swiftxen new file mode 100644 index 00000000000..201ad8f1e19 --- /dev/null +++ b/scripts/vm/hypervisor/xenserver/swiftxen @@ -0,0 +1,80 @@ +#!/usr/bin/python +# Version @VERSION@ +# +# A plugin for executing script needed by cloud stack + +import os, sys, time +import XenAPIPlugin +sys.path.extend(["/opt/xensource/sm/"]) +import util + +def echo(fn): + def wrapped(*v, **k): + name = fn.__name__ + util.SMlog("#### VMOPS enter %s ####" % name ) + res = fn(*v, **k) + util.SMlog("#### VMOPS exit %s ####" % name ) + return res + return wrapped + +SWIFT = "/opt/xensource/bin/swift" + +MAX_SEG_SIZE = 5 * 1024 * 1024 * 1024 + +def upload(args): + url = args['url'] + account = args['account'] + username = args['username'] + key = args['key'] + container = args['container'] + ldir = args['ldir'] + lfilename = args['lfilename'] + isISCSI = args['isISCSI'] + segment = 0 + util.SMlog("#### VMOPS upload %s to swift ####", lfilename) + savedpath = os.getcwd() + os.chdir(ldir) + try : + if isISCSI == 'ture': + cmd1 = [ lvchange , "-ay", lfilename ] + util.pread2(cmd1) + cmd1 = [ lvdisplay, "-c", lfilename ] + lines = util.pread2(cmd).split(':'); + size = long(lines[6]) * 512 + if size > MAX_SEG_SIZE : + segment = 1 + else : + size = os.path.getsize(lfilename) + if size > MAX_SEG_SIZE : + segment = 1 + if segment : + cmd = [SWIFT, "-A", url, "-U", account + ":" + username, "-K", key, "upload", "-S", MAX_SEG_SIZE, container, lfilename] + else : + cmd = [SWIFT, "-A", url ,"-U", account + ":" + username, "-K", key, "upload", container, lfilename] + util.pread2(cmd) + return 'true' + finally: + os.chdir(savedpath) + return 'false' + + +@echo +def swift(session, args): + op = args['op'] + if op == 'upload': + return upload(args) + elif op == 'download': + return download(args) + elif op == 'delete' : + cmd = ["st", "-A https://" + hostname + ":8080/auth/v1.0 -U " + account + ":" + username + " -K " + token + " delete " + rfilename] + else : + util.SMlog("doesn't support swift operation %s " % op ) + return 'false' + try: + util.pread2(cmd) + return 'true' + except: + return 'false' + +if __name__ == "__main__": + XenAPIPlugin.dispatch({"swift": swift})