You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ah...@apache.org on 2013/11/15 19:24:37 UTC

[1/8] Merged vmops and vmopspremium. Rename all xapi plugins to start with cloud-plugin-. Rename vmops to cloud-plugin-generic.

Updated Branches:
  refs/heads/4.2-workplace 815b11fce -> 5d13a07db


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot b/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
deleted file mode 100644
index 53f31a9..0000000
--- a/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
+++ /dev/null
@@ -1,601 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Version @VERSION@
-#
-# A plugin for executing script needed by vmops cloud 
-
-import os, sys, time
-import XenAPIPlugin
-sys.path.append("/usr/lib/xcp/sm/")
-import SR, VDI, SRCommand, util, lvutil
-from util import CommandException
-import vhdutil
-import shutil
-import lvhdutil
-import errno
-import subprocess
-import xs_errors
-import cleanup
-import stat
-import random
-
-VHD_UTIL = 'vhd-util'
-VHD_PREFIX = 'VHD-'
-CLOUD_DIR = '/run/cloud_mount'
-
-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
-
-
-@echo
-def create_secondary_storage_folder(session, args):
-    local_mount_path = None
-
-    util.SMlog("create_secondary_storage_folder, args: " + str(args))
-
-    try:
-        try:
-            # Mount the remote resource folder locally
-            remote_mount_path = args["remoteMountPath"]
-            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
-            mount(remote_mount_path, local_mount_path)
-
-            # Create the new folder
-            new_folder = local_mount_path + "/" + args["newFolder"]
-            if not os.path.isdir(new_folder):
-                current_umask = os.umask(0)
-                os.makedirs(new_folder)
-                os.umask(current_umask)
-        except OSError, (errno, strerror):
-            errMsg = "create_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
-            util.SMlog(errMsg)
-            raise xs_errors.XenError(errMsg)
-        except:
-            errMsg = "create_secondary_storage_folder failed."
-            util.SMlog(errMsg)
-            raise xs_errors.XenError(errMsg)
-    finally:
-        if local_mount_path != None:
-            # Unmount the local folder
-            umount(local_mount_path)
-            # Remove the local folder
-            os.system("rmdir " + local_mount_path)
-        
-    return "1"
-
-@echo
-def delete_secondary_storage_folder(session, args):
-    local_mount_path = None
-
-    util.SMlog("delete_secondary_storage_folder, args: " + str(args))
-
-    try:
-        try:
-            # Mount the remote resource folder locally
-            remote_mount_path = args["remoteMountPath"]
-            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
-            mount(remote_mount_path, local_mount_path)
-
-            # Delete the specified folder
-            folder = local_mount_path + "/" + args["folder"]
-            if os.path.isdir(folder):
-                os.system("rm -f " + folder + "/*")
-                os.system("rmdir " + folder)
-        except OSError, (errno, strerror):
-            errMsg = "delete_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
-            util.SMlog(errMsg)
-            raise xs_errors.XenError(errMsg)
-        except:
-            errMsg = "delete_secondary_storage_folder failed."
-            util.SMlog(errMsg)
-            raise xs_errors.XenError(errMsg)
-    finally:
-        if local_mount_path != None:
-            # Unmount the local folder
-            umount(local_mount_path)
-            # Remove the local folder
-            os.system("rmdir " + local_mount_path)
-        
-    return "1"
-     
-@echo
-def post_create_private_template(session, args):
-    local_mount_path = None
-    try:
-        try:
-            # get local template folder 
-            templatePath = args["templatePath"]
-            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
-            mount(templatePath, local_mount_path)
-            # Retrieve args
-            filename = args["templateFilename"]
-            name = args["templateName"]
-            description = args["templateDescription"]
-            checksum = args["checksum"]
-            file_size = args["size"]
-            virtual_size = args["virtualSize"]
-            template_id = args["templateId"]
-           
-            # Create the template.properties file
-            template_properties_install_path = local_mount_path + "/template.properties"
-            f = open(template_properties_install_path, "w")
-            f.write("filename=" + filename + "\n")
-            f.write("vhd=true\n")
-            f.write("id=" + template_id + "\n")
-            f.write("vhd.filename=" + filename + "\n")
-            f.write("public=false\n")
-            f.write("uniquename=" + name + "\n")
-            f.write("vhd.virtualsize=" + virtual_size + "\n")
-            f.write("virtualsize=" + virtual_size + "\n")
-            f.write("checksum=" + checksum + "\n")
-            f.write("hvm=true\n")
-            f.write("description=" + description + "\n")
-            f.write("vhd.size=" + str(file_size) + "\n")
-            f.write("size=" + str(file_size) + "\n")
-            f.close()
-            util.SMlog("Created template.properties file")
-           
-            # Set permissions
-            permissions = stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH
-            os.chmod(template_properties_install_path, permissions)
-            util.SMlog("Set permissions on template and template.properties")
-
-        except:
-            errMsg = "post_create_private_template failed."
-            util.SMlog(errMsg)
-            raise xs_errors.XenError(errMsg)
-
-    finally:
-        if local_mount_path != None:
-            # Unmount the local folder
-            umount(local_mount_path)
-            # Remove the local folder
-            os.system("rmdir " + local_mount_path)
-    return "1" 
-  
-def isfile(path, isISCSI):
-    errMsg = ''
-    exists = True
-    if isISCSI:
-        exists = checkVolumeAvailablility(path)
-    else:
-        exists = os.path.isfile(path)
-        
-    if not exists:
-        errMsg = "File " + path + " does not exist."
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return errMsg
-
-def copyfile(fromFile, toFile, isISCSI):
-    util.SMlog("Starting to copy " + fromFile + " to " + toFile)
-    errMsg = ''
-    try:
-        cmd = ['dd', 'if=' + fromFile, 'of=' + toFile, 'bs=4M']
-        txt = util.pread2(cmd)
-    except:
-        try:
-            os.system("rm -f " + toFile)
-        except:
-            txt = ''
-        txt = ''
-        errMsg = "Error while copying " + fromFile + " to " + toFile + " in secondary storage"
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-
-    util.SMlog("Successfully copied " + fromFile + " to " + toFile)
-    return errMsg
-
-def chdir(path):
-    try:
-        os.chdir(path)
-    except OSError, (errno, strerror):
-        errMsg = "Unable to chdir to " + path + " because of OSError with errno: " + str(errno) + " and strerr: " + strerror
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    util.SMlog("Chdired to " + path)
-    return
-
-def scanParent(path):
-    # Do a scan for the parent for ISCSI volumes
-    # Note that the parent need not be visible on the XenServer
-    parentUUID = ''
-    try:
-        lvName = os.path.basename(path)
-        dirname = os.path.dirname(path)
-        vgName = os.path.basename(dirname) 
-        vhdInfo = vhdutil.getVHDInfoLVM(lvName, lvhdutil.extractUuid, vgName)
-        parentUUID = vhdInfo.parentUuid
-    except:
-        errMsg = "Could not get vhd parent of " + path
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return parentUUID
-
-def getParent(path, isISCSI):
-    parentUUID = ''
-    try :
-        if isISCSI:
-            parentUUID = vhdutil.getParent(path, lvhdutil.extractUuid)
-        else:
-            parentUUID = vhdutil.getParent(path, cleanup.FileVDI.extractUuid)
-    except:
-        errMsg = "Could not get vhd parent of " + path
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return parentUUID
-
-def getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI):
-    snapshotVHD    = getVHD(snapshotUuid, isISCSI)
-    snapshotPath   = os.path.join(primarySRPath, snapshotVHD)
-
-    baseCopyUuid = ''
-    if isISCSI:
-        checkVolumeAvailablility(snapshotPath)
-        baseCopyUuid = scanParent(snapshotPath)
-    else:
-        baseCopyUuid = getParent(snapshotPath, isISCSI)
-    
-    util.SMlog("Base copy of snapshotUuid: " + snapshotUuid + " is " + baseCopyUuid)
-    return baseCopyUuid
-
-def setParent(parent, child):
-    try:
-        cmd = [VHD_UTIL, "modify", "-p", parent, "-n", child]
-        txt = util.pread2(cmd)
-    except:
-        errMsg = "Unexpected error while trying to set parent of " + child + " to " + parent 
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    util.SMlog("Successfully set parent of " + child + " to " + parent)
-    return
-
-def rename(originalVHD, newVHD):
-    try:
-        os.rename(originalVHD, newVHD)
-    except OSError, (errno, strerror):
-        errMsg = "OSError while renaming " + origiinalVHD + " to " + newVHD + "with errno: " + str(errno) + " and strerr: " + strerror
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return
-
-def makedirs(path):
-    if not os.path.isdir(path):
-        try:
-            os.makedirs(path)
-        except OSError, (errno, strerror):
-            umount(path)
-            if os.path.isdir(path):
-                return
-            errMsg = "OSError while creating " + path + " with errno: " + str(errno) + " and strerr: " + strerror
-            util.SMlog(errMsg)
-            raise xs_errors.XenError(errMsg)
-    return
-
-def mount(remoteDir, localDir):
-    makedirs(localDir)
-    options = "soft,tcp,timeo=133,retrans=1"
-    try: 
-        cmd = ['mount', '-o', options, remoteDir, localDir]
-        txt = util.pread2(cmd)
-    except:
-        txt = ''
-        errMsg = "Unexpected error while trying to mount " + remoteDir + " to " + localDir 
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    util.SMlog("Successfully mounted " + remoteDir + " to " + localDir)
-
-    return
-
-def umount(localDir):
-    try: 
-        cmd = ['umount', localDir]
-        util.pread2(cmd)
-    except CommandException:
-        errMsg = "CommandException raised while trying to umount " + localDir 
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-
-    util.SMlog("Successfully unmounted " + localDir)
-    return
-
-def mountSnapshotsDir(secondaryStorageMountPath, localMountPointPath, path):
-    # The aim is to mount secondaryStorageMountPath on 
-    # And create <accountId>/<instanceId> dir on it, if it doesn't exist already.
-    # Assuming that secondaryStorageMountPath  exists remotely
-
-    # Just mount secondaryStorageMountPath/<relativeDir>/SecondaryStorageHost/ everytime
-    # Never unmount.
-    # path is like "snapshots/account/volumeId", we mount secondary_storage:/snapshots
-    relativeDir = path.split("/")[0]
-    restDir = "/".join(path.split("/")[1:])
-    snapshotsDir = os.path.join(secondaryStorageMountPath, relativeDir)
-
-    makedirs(localMountPointPath)
-    # if something is not mounted already on localMountPointPath,
-    # mount secondaryStorageMountPath on localMountPath
-    if os.path.ismount(localMountPointPath):
-        # There is more than one secondary storage per zone.
-        # And we are mounting each sec storage under a zone-specific directory
-        # So two secondary storage snapshot dirs will never get mounted on the same point on the same XenServer.
-        util.SMlog("The remote snapshots directory has already been mounted on " + localMountPointPath)
-    else:
-        mount(snapshotsDir, localMountPointPath)
-
-    # Create accountId/instanceId dir on localMountPointPath, if it doesn't exist
-    backupsDir = os.path.join(localMountPointPath, restDir)
-    makedirs(backupsDir)
-    return backupsDir
-
-def unmountAll(path):
-    try:
-        for dir in os.listdir(path):
-            if dir.isdigit():
-                util.SMlog("Unmounting Sub-Directory: " + dir)
-                localMountPointPath = os.path.join(path, dir)
-                umount(localMountPointPath)
-    except:
-        util.SMlog("Ignoring the error while trying to unmount the snapshots dir")
-
-@echo
-def unmountSnapshotsDir(session, args):
-    dcId = args['dcId']
-    localMountPointPath = os.path.join(CLOUD_DIR, dcId)
-    localMountPointPath = os.path.join(localMountPointPath, "snapshots")
-    unmountAll(localMountPointPath)
-    try:
-        umount(localMountPointPath)
-    except:
-        util.SMlog("Ignoring the error while trying to unmount the snapshots dir.")
-
-    return "1"
-
-def getPrimarySRPath(session, primaryStorageSRUuid, isISCSI):
-    sr = session.xenapi.SR.get_by_uuid(primaryStorageSRUuid)
-    srrec = session.xenapi.SR.get_record(sr)
-    srtype = srrec["type"]
-    if srtype == "file":
-        pbd = session.xenapi.SR.get_PBDs(sr)[0]
-        pbdrec = session.xenapi.PBD.get_record(pbd)
-        primarySRPath = pbdrec["device_config"]["location"]
-        return primarySRPath
-    elif isISCSI:
-        primarySRDir = lvhdutil.VG_PREFIX + primaryStorageSRUuid
-        return os.path.join(lvhdutil.VG_LOCATION, primarySRDir)
-    else:
-        return os.path.join(SR.MOUNT_BASE, primaryStorageSRUuid)
-
-def getBackupVHD(UUID):
-    return UUID + '.' + SR.DEFAULT_TAP
-
-def getVHD(UUID, isISCSI):
-    if isISCSI:
-        return VHD_PREFIX + UUID
-    else:
-        return UUID + '.' + SR.DEFAULT_TAP
-
-def getIsTrueString(stringValue):
-    booleanValue = False
-    if (stringValue and stringValue == 'true'):
-        booleanValue = True
-    return booleanValue 
-
-def makeUnavailable(uuid, primarySRPath, isISCSI):
-    if not isISCSI:
-        return
-    VHD = getVHD(uuid, isISCSI)
-    path = os.path.join(primarySRPath, VHD)
-    manageAvailability(path, '-an')
-    return
-
-def manageAvailability(path, value):
-    if path.__contains__("/var/run/sr-mount"):
-        return
-    util.SMlog("Setting availability of " + path + " to " + value)
-    try:
-        cmd = ['/usr/sbin/lvchange', value, path]
-        util.pread2(cmd)
-    except: #CommandException, (rc, cmdListStr, stderr):
-        #errMsg = "CommandException thrown while executing: " + cmdListStr + " with return code: " + str(rc) + " and stderr: " + stderr
-        errMsg = "Unexpected exception thrown by lvchange"
-        util.SMlog(errMsg)
-        if value == "-ay":
-            # Raise an error only if we are trying to make it available.
-            # Just warn if we are trying to make it unavailable after the 
-            # snapshot operation is done.
-            raise xs_errors.XenError(errMsg)
-    return
-
-
-def checkVolumeAvailablility(path):
-    try:
-        if not isVolumeAvailable(path):
-            # The VHD file is not available on XenSever. The volume is probably
-            # inactive or detached.
-            # Do lvchange -ay to make it available on XenServer
-            manageAvailability(path, '-ay')
-    except:
-        errMsg = "Could not determine status of ISCSI path: " + path
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    
-    success = False
-    i = 0
-    while i < 6:
-        i = i + 1
-        # Check if the vhd is actually visible by checking for the link
-        # set isISCSI to true
-        success = isVolumeAvailable(path)
-        if success:
-            util.SMlog("Made vhd: " + path + " available and confirmed that it is visible")
-            break
-
-        # Sleep for 10 seconds before checking again.
-        time.sleep(10)
-
-    # If not visible within 1 min fail
-    if not success:
-        util.SMlog("Could not make vhd: " +  path + " available despite waiting for 1 minute. Does it exist?")
-
-    return success
-
-def isVolumeAvailable(path):
-    # Check if iscsi volume is available on this XenServer.
-    status = "0"
-    try:
-        p = subprocess.Popen(["/bin/bash", "-c", "if [ -L " + path + " ]; then echo 1; else echo 0;fi"], stdout=subprocess.PIPE)
-        status = p.communicate()[0].strip("\n")
-    except:
-        errMsg = "Could not determine status of ISCSI path: " + path
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-
-    return (status == "1")  
-
-def getVhdParent(session, args):
-    util.SMlog("getParent with " + str(args))
-    primaryStorageSRUuid      = args['primaryStorageSRUuid']
-    snapshotUuid              = args['snapshotUuid']
-    isISCSI                   = getIsTrueString(args['isISCSI']) 
-
-    primarySRPath = getPrimarySRPath(session, primaryStorageSRUuid, isISCSI)
-    util.SMlog("primarySRPath: " + primarySRPath)
-
-    baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
-
-    return  baseCopyUuid
-
-
-def backupSnapshot(session, args):
-    util.SMlog("Called backupSnapshot with " + str(args))
-    primaryStorageSRUuid      = args['primaryStorageSRUuid']
-    secondaryStorageMountPath = args['secondaryStorageMountPath']
-    snapshotUuid              = args['snapshotUuid']
-    prevBackupUuid            = args['prevBackupUuid']
-    backupUuid                = args['backupUuid']
-    isISCSI                   = getIsTrueString(args['isISCSI'])
-    path = args['path']
-    localMountPoint = args['localMountPoint']
-    primarySRPath = getPrimarySRPath(session, primaryStorageSRUuid, isISCSI)
-    util.SMlog("primarySRPath: " + primarySRPath)
-
-    baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
-    baseCopyVHD  = getVHD(baseCopyUuid, isISCSI)
-    baseCopyPath = os.path.join(primarySRPath, baseCopyVHD)
-    util.SMlog("Base copy path: " + baseCopyPath)
-
-
-    # Mount secondary storage mount path on XenServer along the path
-    # /var/run/sr-mount/<dcId>/snapshots/ and create <accountId>/<volumeId> dir
-    # on it.
-    backupsDir = mountSnapshotsDir(secondaryStorageMountPath, localMountPoint, path)
-    util.SMlog("Backups dir " + backupsDir)
-    prevBackupUuid = prevBackupUuid.split("/")[-1]
-    # Check existence of snapshot on primary storage
-    isfile(baseCopyPath, isISCSI)
-    if prevBackupUuid:
-        # Check existence of prevBackupFile
-        prevBackupVHD = getBackupVHD(prevBackupUuid)
-        prevBackupFile = os.path.join(backupsDir, prevBackupVHD)
-        isfile(prevBackupFile, False)
-
-    # copy baseCopyPath to backupsDir with new uuid
-    backupVHD = getBackupVHD(backupUuid)  
-    backupFile = os.path.join(backupsDir, backupVHD)
-    util.SMlog("Back up " + baseCopyUuid + " to Secondary Storage as " + backupUuid)
-    copyfile(baseCopyPath, backupFile, isISCSI)
-    vhdutil.setHidden(backupFile, False)
-
-    # Because the primary storage is always scanned, the parent of this base copy is always the first base copy.
-    # We don't want that, we want a chain of VHDs each of which is a delta from the previous.
-    # So set the parent of the current baseCopyVHD to prevBackupVHD 
-    if prevBackupUuid:
-        # If there was a previous snapshot
-        setParent(prevBackupFile, backupFile)
-
-    txt = "1#" + backupUuid
-    return txt
-
-@echo
-def deleteSnapshotBackup(session, args):
-    util.SMlog("Calling deleteSnapshotBackup with " + str(args))
-    secondaryStorageMountPath = args['secondaryStorageMountPath']
-    backupUUID                = args['backupUUID']
-    path = args['path']
-    localMountPoint = args['localMountPoint']
-
-    backupsDir = mountSnapshotsDir(secondaryStorageMountPath, localMountPoint, path)
-    # chdir to the backupsDir for convenience
-    chdir(backupsDir)
-
-    backupVHD = getBackupVHD(backupUUID)
-    util.SMlog("checking existence of " + backupVHD)
-
-    # The backupVHD is on secondary which is NFS and not ISCSI.
-    if not os.path.isfile(backupVHD):
-        util.SMlog("backupVHD " + backupVHD + "does not exist. Not trying to delete it")
-        return "1"
-    util.SMlog("backupVHD " + backupVHD + " exists.")
-        
-    # Just delete the backupVHD
-    try:
-        os.remove(backupVHD)
-    except OSError, (errno, strerror):
-        errMsg = "OSError while removing " + backupVHD + " with errno: " + str(errno) + " and strerr: " + strerror
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-
-    return "1"
-   
-@echo
-def revert_memory_snapshot(session, args):
-    util.SMlog("Calling revert_memory_snapshot with " + str(args))
-    vmName = args['vmName']
-    snapshotUUID = args['snapshotUUID']
-    oldVmUuid = args['oldVmUuid']
-    snapshotMemory = args['snapshotMemory']
-    hostUUID = args['hostUUID']
-    try:
-        cmd = '''xe vbd-list vm-uuid=%s | grep 'vdi-uuid' | grep -v 'not in database' | sed -e 's/vdi-uuid ( RO)://g' ''' % oldVmUuid
-        vdiUuids = os.popen(cmd).read().split()
-        cmd2 = '''xe vm-param-get param-name=power-state uuid=''' + oldVmUuid
-        if os.popen(cmd2).read().split()[0] != 'halted':
-            os.system("xe vm-shutdown force=true vm=" + vmName)
-        os.system("xe vm-destroy uuid=" + oldVmUuid)
-        os.system("xe snapshot-revert snapshot-uuid=" + snapshotUUID)
-        if snapshotMemory == 'true':
-            os.system("xe vm-resume vm=" + vmName + " on=" + hostUUID)
-        for vdiUuid in vdiUuids:
-            os.system("xe vdi-destroy uuid=" + vdiUuid)
-    except OSError, (errno, strerror):
-        errMsg = "OSError while reverting vm " + vmName + " to snapshot " + snapshotUUID + " with errno: " + str(errno) + " and strerr: " + strerror
-        util.SMlog(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return "0"
-
-if __name__ == "__main__":
-    XenAPIPlugin.dispatch({"getVhdParent":getVhdParent,  "create_secondary_storage_folder":create_secondary_storage_folder, "delete_secondary_storage_folder":delete_secondary_storage_folder, "post_create_private_template":post_create_private_template, "backupSnapshot": backupSnapshot, "deleteSnapshotBackup": deleteSnapshotBackup, "unmountSnapshotsDir": unmountSnapshotsDir, "revert_memory_snapshot":revert_memory_snapshot})
-    
-

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium b/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
deleted file mode 100644
index 9066ee0..0000000
--- a/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
+++ /dev/null
@@ -1,146 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Version @VERSION@
-#
-# A plugin for executing script needed by vmops cloud 
-
-import os, sys, time
-import XenAPIPlugin
-sys.path.append("/usr/lib/xcp/sm/")
-import util
-import socket
-
-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
-
-@echo
-def forceShutdownVM(session, args):
-    domId = args['domId']
-    try:
-        cmd = ["/usr/lib/xcp/debug/xenops", "destroy_domain", "-domid", domId]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-
-@echo
-def create_privatetemplate_from_snapshot(session, args):
-    templatePath = args['templatePath']
-    snapshotPath = args['snapshotPath']
-    tmpltLocalDir = args['tmpltLocalDir']
-    try:
-        cmd = ["bash", "/usr/lib/xcp/bin/create_privatetemplate_from_snapshot.sh",snapshotPath, templatePath, tmpltLocalDir]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-@echo
-def upgrade_snapshot(session, args):
-    templatePath = args['templatePath']
-    snapshotPath = args['snapshotPath']
-    try:
-        cmd = ["bash", "/usr/lib/xcp/bin/upgrate_snapshot.sh",snapshotPath, templatePath]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-@echo
-def copy_vhd_to_secondarystorage(session, args):
-    mountpoint = args['mountpoint']
-    vdiuuid = args['vdiuuid']
-    sruuid = args['sruuid']
-    try:
-        cmd = ["bash", "/usr/lib/xcp/bin/copy_vhd_to_secondarystorage.sh", mountpoint, vdiuuid, sruuid]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-@echo
-def copy_vhd_from_secondarystorage(session, args):
-    mountpoint = args['mountpoint']
-    sruuid = args['sruuid']
-    namelabel = args['namelabel']
-    try:
-        cmd = ["bash", "/usr/lib/xcp/bin/copy_vhd_from_secondarystorage.sh", mountpoint, sruuid, namelabel]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-@echo
-def setup_heartbeat_sr(session, args):
-    host = args['host']
-    sr = args['sr']
-    try:
-        cmd = ["bash", "/usr/lib/xcp/bin/setup_heartbeat_sr.sh", host, sr]
-        txt = util.pread2(cmd)
-    except:
-        txt = ''
-    return txt
-
-@echo
-def setup_heartbeat_file(session, args):
-    host = args['host']
-    sr = args['sr']
-    add = args['add']
-    try:
-        cmd = ["bash", "/usr/lib/xcp/bin/setup_heartbeat_file.sh", host, sr, add]
-        txt = util.pread2(cmd)
-    except:
-        txt = ''
-    return txt
-
-@echo
-def check_heartbeat(session, args):
-    host = args['host']
-    interval = args['interval']
-    try:
-       cmd = ["bash", "/usr/lib/xcp/bin/check_heartbeat.sh", host, interval]
-       txt = util.pread2(cmd)
-    except:
-       txt=''
-    return txt
-    
-   
-@echo
-def heartbeat(session, args):
-    '''
-    host = args['host']
-    interval = args['interval']
-    try: 
-       cmd = ["/bin/bash", "/usr/lib/xcp/bin/launch_hb.sh", host, interval]
-       txt = util.pread2(cmd)
-    except:
-       txt='fail'
-    '''
-    return '> DONE <'
-
-if __name__ == "__main__":
-    XenAPIPlugin.dispatch({"forceShutdownVM":forceShutdownVM, "upgrade_snapshot":upgrade_snapshot, "create_privatetemplate_from_snapshot":create_privatetemplate_from_snapshot, "copy_vhd_to_secondarystorage":copy_vhd_to_secondarystorage, "copy_vhd_from_secondarystorage":copy_vhd_from_secondarystorage, "setup_heartbeat_sr":setup_heartbeat_sr, "setup_heartbeat_file":setup_heartbeat_file, "check_heartbeat":check_heartbeat, "heartbeat": heartbeat})
-

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xcpserver/patch
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xcpserver/patch b/scripts/vm/hypervisor/xenserver/xcpserver/patch
index 8af0314..5a897dd 100644
--- a/scripts/vm/hypervisor/xenserver/xcpserver/patch
+++ b/scripts/vm/hypervisor/xenserver/xcpserver/patch
@@ -27,41 +27,48 @@
 # If [source path] starts with '/', then it is absolute path.
 # If [source path] starts with '~', then it is path relative to management server home directory.
 # If [source path] does not start with '/' or '~', then it is relative path to the location of the patch file. 
+# 
+# The following specifies the paths to deposit files
+# /etc/xapi.d/plugins - All XAPI plugins.  Every file placed here should start with "cloud-".
+# /etc/cloud - Configuration files for scripts.
+# /opt/cloud/bin - All scripts used in the normal operation.
+# /opt/cloud/tools/bin - All scripts used for testing/support that are meant to be run by hand.
+# /etc/logrotate.d - All cloud log rotation configuration files.  Every file placed here must start with "cloud-".
+#
+# All log files should be placed in /var/log/cloud
 NFSSR.py=/opt/xensource/sm
-vmops=..,0755,/etc/xapi.d/plugins
-ovstunnel=..,0755,/etc/xapi.d/plugins
-vmopsSnapshot=..,0755,/etc/xapi.d/plugins
-hostvmstats.py=..,0755,/opt/xensource/sm
+cloud-plugin-generic=..,0755,/etc/xapi.d/plugins
+cloud-plugin-ovstunnel=..,0755,/etc/xapi.d/plugins
+cloud-plugin-snapshot=..,0755,/etc/xapi.d/plugins
 systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
 id_rsa.cloud=../../../systemvm,0600,/root/.ssh
-network_info.sh=..,0755,/opt/cloudstack/bin
-setupxenserver.sh=..,0755,/opt/cloudstack/bin
-make_migratable.sh=..,0755,/opt/cloudstack/bin
-setup_iscsi.sh=..,0755,/opt/cloudstack/bin
-pingtest.sh=../../..,0755,/opt/cloudstack/bin
-dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-createipAlias.sh=..,0755,/opt/cloudstack/bin
-deleteipAlias.sh=..,0755,/opt/cloudstack/bin
-router_proxy.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_firewall.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-cloud-setup-bonding.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_sr.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_file.sh=..,0755,/opt/cloudstack/bin
-check_heartbeat.sh=..,0755,/opt/cloudstack/bin
-xenheartbeat.sh=..,0755,/opt/cloudstack/bin
-launch_hb.sh=..,0755,/opt/cloudstack/bin
-vhd-util=..,0755,/opt/cloudstack/bin
-vmopspremium=..,0755,/etc/xapi.d/plugins
-create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloudstack/bin
-upgrade_snapshot.sh=..,0755,/opt/cloudstack/bin
-cloud-clean-vlan.sh=..,0755,/opt/cloudstack/bin
-cloud-prepare-upgrade.sh=..,0755,/opt/cloudstack/bin
-getRouterStatus.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-getDomRVersion.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-add_to_vcpus_params_live.sh=..,0755,/opt/cloudstack/bin
-cloudstacklog=..,0644,/etc/logrotate.d
+network_info.sh=..,0755,/opt/cloud/bin
+setupxenserver.sh=..,0755,/opt/cloud/bin
+make_migratable.sh=..,0755,/opt/cloud/bin
+setup_iscsi.sh=..,0755,/opt/cloud/bin
+pingtest.sh=../../..,0755,/opt/cloud/bin
+dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloud/bin
+createipAlias.sh=..,0755,/opt/cloud/bin
+deleteipAlias.sh=..,0755,/opt/cloud/bin
+router_proxy.sh=../../../../network/domr/,0755,/opt/cloud/bin
+save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_firewall.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloud/bin
+cloud-setup-bonding.sh=..,0755,/opt/cloud/bin
+copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloud/bin
+copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_sr.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_file.sh=..,0755,/opt/cloud/bin
+check_heartbeat.sh=..,0755,/opt/cloud/bin
+xenheartbeat.sh=..,0755,/opt/cloud/bin
+launch_hb.sh=..,0755,/opt/cloud/bin
+vhd-util=..,0755,/opt/cloud/bin
+create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloud/bin
+upgrade_snapshot.sh=..,0755,/opt/cloud/bin
+cloud-clean-vlan.sh=..,0755,/opt/cloud/bin
+cloud-prepare-upgrade.sh=..,0755,/opt/cloud/bin
+getRouterStatus.sh=../../../../network/domr/,0755,/opt/cloud/bin
+bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloud/bin
+getDomRVersion.sh=../../../../network/domr/,0755,/opt/cloud/bin
+add_to_vcpus_params_live.sh=..,0755,/opt/cloud/bin
+cloudlog=..,0644,/etc/logrotate.d

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xenheartbeat.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenheartbeat.sh b/scripts/vm/hypervisor/xenserver/xenheartbeat.sh
index f875a3a..ae0bd20 100755
--- a/scripts/vm/hypervisor/xenserver/xenheartbeat.sh
+++ b/scripts/vm/hypervisor/xenserver/xenheartbeat.sh
@@ -44,7 +44,7 @@ if [ $interval -gt $2 ]; then
   exit 3
 fi
 
-file=/opt/cloudstack/bin/heartbeat
+file=/etc/cloud/heartbeat
 lastdate=$(($(date +%s) + $interval))
 
 while [ $(date +%s) -lt $(($lastdate + $2)) ]

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xenserver56/patch
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver56/patch b/scripts/vm/hypervisor/xenserver/xenserver56/patch
index 9eac710..a768600 100644
--- a/scripts/vm/hypervisor/xenserver/xenserver56/patch
+++ b/scripts/vm/hypervisor/xenserver/xenserver56/patch
@@ -26,42 +26,50 @@
 # If [source path] starts with '/', then it is absolute path.
 # If [source path] starts with '~', then it is path relative to management server home directory.
 # If [source path] does not start with '/' or '~', then it is relative path to the location of the patch file. 
+# 
+# The following specifies the paths to deposit files
+# /etc/xapi.d/plugins - All XAPI plugins.  Every file placed here should start with "cloud-".
+# /etc/cloud - Configuration files for scripts.
+# /opt/cloud/bin - All scripts used in the normal operation.
+# /opt/cloud/tools/bin - All scripts used for testing/support that are meant to be run by hand.
+# /etc/logrotate.d - All cloud log rotation configuration files.  Every file placed here must start with "cloud-".
+#
+# All log files should be placed in /var/log/cloud
 NFSSR.py=/opt/xensource/sm
-vmops=..,0755,/etc/xapi.d/plugins
-vmopsSnapshot=..,0755,/etc/xapi.d/plugins
+cloud-plugin-generic=..,0755,/etc/xapi.d/plugins
+cloud-plugin-snapshot=..,0755,/etc/xapi.d/plugins
 systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
 id_rsa.cloud=../../../systemvm,0600,/root/.ssh
-network_info.sh=..,0755,/opt/cloudstack/bin
-setupxenserver.sh=..,0755,/opt/cloudstack/bin
-make_migratable.sh=..,0755,/opt/cloudstack/bin
-setup_iscsi.sh=..,0755,/opt/cloudstack/bin
-cloud-setup-bonding.sh=..,0755,/opt/cloudstack/bin
-pingtest.sh=../../..,0755,/opt/cloudstack/bin
-createipAlias.sh=..,0755,/opt/cloudstack/bin
-deleteipAlias.sh=..,0755,/opt/cloudstack/bin
-dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_firewall.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-router_proxy.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-kill_copy_process.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_sr.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_file.sh=..,0755,/opt/cloudstack/bin
-check_heartbeat.sh=..,0755,/opt/cloudstack/bin
-xenheartbeat.sh=..,0755,/opt/cloudstack/bin
-launch_hb.sh=..,0755,/opt/cloudstack/bin
-vhd-util=..,0755,/opt/cloudstack/bin
-vmopspremium=..,0755,/etc/xapi.d/plugins
+network_info.sh=..,0755,/opt/cloud/bin
+setupxenserver.sh=..,0755,/opt/cloud/bin
+make_migratable.sh=..,0755,/opt/cloud/bin
+setup_iscsi.sh=..,0755,/opt/cloud/bin
+cloud-setup-bonding.sh=..,0755,/opt/cloud/bin
+pingtest.sh=../../..,0755,/opt/cloud/bin
+createipAlias.sh=..,0755,/opt/cloud/bin
+deleteipAlias.sh=..,0755,/opt/cloud/bin
+dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloud/bin
+save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_firewall.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloud/bin
+router_proxy.sh=../../../../network/domr/,0755,/opt/cloud/bin
+copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloud/bin
+copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloud/bin
+kill_copy_process.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_sr.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_file.sh=..,0755,/opt/cloud/bin
+check_heartbeat.sh=..,0755,/opt/cloud/bin
+xenheartbeat.sh=..,0755,/opt/cloud/bin
+launch_hb.sh=..,0755,/opt/cloud/bin
+vhd-util=..,0755,/opt/cloud/bin
 InterfaceReconfigure.py=.,0755,/opt/xensource/libexec
-create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloudstack/bin
-upgrade_snapshot.sh=..,0755,/opt/cloudstack/bin
-cloud-clean-vlan.sh=..,0755,/opt/cloudstack/bin
-cloud-prepare-upgrade.sh=..,0755,/opt/cloudstack/bin
-bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-swift=..,0755,/opt/cloudstack/bin
-swiftxen=..,0755,/etc/xapi.d/plugins
-s3xen=..,0755,/etc/xapi.d/plugins
-add_to_vcpus_params_live.sh=..,0755,/opt/cloudstack/bin
-cloudstacklog=..,0644,/etc/logrotate.d
+create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloud/bin
+upgrade_snapshot.sh=..,0755,/opt/cloud/bin
+cloud-clean-vlan.sh=..,0755,/opt/cloud/bin
+cloud-prepare-upgrade.sh=..,0755,/opt/cloud/bin
+bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloud/bin
+swift=..,0755,/opt/cloud/bin
+cloud-plugin-swiftxen=..,0755,/etc/xapi.d/plugins
+cloud-plugin-s3xen=..,0755,/etc/xapi.d/plugins
+add_to_vcpus_params_live.sh=..,0755,/opt/cloud/bin
+cloudlog=..,0644,/etc/logrotate.d

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xenserver56fp1/patch
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver56fp1/patch b/scripts/vm/hypervisor/xenserver/xenserver56fp1/patch
index 573e0c5..10daa36 100644
--- a/scripts/vm/hypervisor/xenserver/xenserver56fp1/patch
+++ b/scripts/vm/hypervisor/xenserver/xenserver56fp1/patch
@@ -26,41 +26,49 @@
 # If [source path] starts with '/', then it is absolute path.
 # If [source path] starts with '~', then it is path relative to management server home directory.
 # If [source path] does not start with '/' or '~', then it is relative path to the location of the patch file. 
+# 
+# The following specifies the paths to deposit files
+# /etc/xapi.d/plugins - All XAPI plugins.  Every file placed here should start with "cloud-".
+# /etc/cloud - Configuration files for scripts.
+# /opt/cloud/bin - All scripts used in the normal operation.
+# /opt/cloud/tools/bin - All scripts used for testing/support that are meant to be run by hand.
+# /etc/logrotate.d - All cloud log rotation configuration files.  Every file placed here must start with "cloud-".
+#
+# All log files should be placed in /var/log/cloud
 NFSSR.py=/opt/xensource/sm
-vmops=..,0755,/etc/xapi.d/plugins
-vmopsSnapshot=..,0755,/etc/xapi.d/plugins
+cloud-plugin-generic=..,0755,/etc/xapi.d/plugins
+cloud-plugin-snapshot=..,0755,/etc/xapi.d/plugins
 systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
 id_rsa.cloud=../../../systemvm,0600,/root/.ssh
-network_info.sh=..,0755,/opt/cloudstack/bin
-setupxenserver.sh=..,0755,/opt/cloudstack/bin
-make_migratable.sh=..,0755,/opt/cloudstack/bin
-setup_iscsi.sh=..,0755,/opt/cloudstack/bin
-pingtest.sh=../../..,0755,/opt/cloudstack/bin
-createipAlias.sh=..,0755,/opt/cloudstack/bin
-deleteipAlias.sh=..,0755,/opt/cloudstack/bin
-dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_firewall.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-router_proxy.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-cloud-setup-bonding.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-kill_copy_process.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_sr.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_file.sh=..,0755,/opt/cloudstack/bin
-check_heartbeat.sh=..,0755,/opt/cloudstack/bin
-xenheartbeat.sh=..,0755,/opt/cloudstack/bin
-launch_hb.sh=..,0755,/opt/cloudstack/bin
-vhd-util=..,0755,/opt/cloudstack/bin
-vmopspremium=..,0755,/etc/xapi.d/plugins
-create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloudstack/bin
-upgrade_snapshot.sh=..,0755,/opt/cloudstack/bin
-cloud-clean-vlan.sh=..,0755,/opt/cloudstack/bin
-cloud-prepare-upgrade.sh=..,0755,/opt/cloudstack/bin
-bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-swift=..,0755,/opt/cloudstack/bin
-swiftxen=..,0755,/etc/xapi.d/plugins
-s3xen=..,0755,/etc/xapi.d/plugins
-add_to_vcpus_params_live.sh=..,0755,/opt/cloudstack/bin
-cloudstacklog=..,0644,/etc/logrotate.d
+network_info.sh=..,0755,/opt/cloud/bin
+setupxenserver.sh=..,0755,/opt/cloud/bin
+make_migratable.sh=..,0755,/opt/cloud/bin
+setup_iscsi.sh=..,0755,/opt/cloud/bin
+pingtest.sh=../../..,0755,/opt/cloud/bin
+createipAlias.sh=..,0755,/opt/cloud/bin
+deleteipAlias.sh=..,0755,/opt/cloud/bin
+dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloud/bin
+save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_firewall.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloud/bin
+router_proxy.sh=../../../../network/domr/,0755,/opt/cloud/bin
+cloud-setup-bonding.sh=..,0755,/opt/cloud/bin
+copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloud/bin
+copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloud/bin
+kill_copy_process.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_sr.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_file.sh=..,0755,/opt/cloud/bin
+check_heartbeat.sh=..,0755,/opt/cloud/bin
+xenheartbeat.sh=..,0755,/opt/cloud/bin
+launch_hb.sh=..,0755,/opt/cloud/bin
+vhd-util=..,0755,/opt/cloud/bin
+create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloud/bin
+upgrade_snapshot.sh=..,0755,/opt/cloud/bin
+cloud-clean-vlan.sh=..,0755,/opt/cloud/bin
+cloud-prepare-upgrade.sh=..,0755,/opt/cloud/bin
+bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloud/bin
+swift=..,0755,/opt/cloud/bin
+cloud-plugin-swiftxen=..,0755,/etc/xapi.d/plugins
+cloud-plugin-s3xen=..,0755,/etc/xapi.d/plugins
+add_to_vcpus_params_live.sh=..,0755,/opt/cloud/bin
+cloudlog=..,0644,/etc/logrotate.d

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xenserver60/patch
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver60/patch b/scripts/vm/hypervisor/xenserver/xenserver60/patch
index 11185ee..04fea8f 100644
--- a/scripts/vm/hypervisor/xenserver/xenserver60/patch
+++ b/scripts/vm/hypervisor/xenserver/xenserver60/patch
@@ -26,52 +26,60 @@
 # If [source path] starts with '/', then it is absolute path.
 # If [source path] starts with '~', then it is path relative to management server home directory.
 # If [source path] does not start with '/' or '~', then it is relative path to the location of the patch file. 
+# 
+# The following specifies the paths to deposit files
+# /etc/xapi.d/plugins - All XAPI plugins.  Every file placed here should start with "cloud-".
+# /etc/cloud - Configuration files for scripts.
+# /opt/cloud/bin - All scripts used in the normal operation.
+# /opt/cloud/tools/bin - All scripts used for testing/support that are meant to be run by hand.
+# /etc/logrotate.d - All cloud log rotation configuration files.  Every file placed here must start with "cloud-".
+#
+# All log files should be placed in /var/log/cloud
 NFSSR.py=/opt/xensource/sm
-vmops=..,0755,/etc/xapi.d/plugins
+cloud-plugin-generic=..,0755,/etc/xapi.d/plugins
 xen-ovs-vif-flows.rules=..,0644,/etc/udev/rules.d
 ovs-vif-flows.py=..,0755,/etc/xapi.d/plugins
-cloudstack_plugins.conf=..,0644,/etc/xensource
-cloudstack_pluginlib.py=..,0755,/etc/xapi.d/plugins
-ovstunnel=..,0755,/etc/xapi.d/plugins
-vmopsSnapshot=..,0755,/etc/xapi.d/plugins
+cloud-plugins.conf=..,0644,/etc/xensource
+cloud-plugin-lib.py=..,0755,/etc/xapi.d/plugins
+cloud-plugin-ovstunnel=..,0755,/etc/xapi.d/plugins
+cloud-plugin-snapshot=..,0755,/etc/xapi.d/plugins
 systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
 id_rsa.cloud=../../../systemvm,0600,/root/.ssh
-network_info.sh=..,0755,/opt/cloudstack/bin
-setupxenserver.sh=..,0755,/opt/cloudstack/bin
-make_migratable.sh=..,0755,/opt/cloudstack/bin
-createipAlias.sh=..,0755,/opt/cloudstack/bin
-deleteipAlias.sh=..,0755,/opt/cloudstack/bin
-setup_iscsi.sh=..,0755,/opt/cloudstack/bin
-pingtest.sh=../../..,0755,/opt/cloudstack/bin
-dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_firewall.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-router_proxy.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-cloud-setup-bonding.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-kill_copy_process.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_sr.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_file.sh=..,0755,/opt/cloudstack/bin
-check_heartbeat.sh=..,0755,/opt/cloudstack/bin
-xenheartbeat.sh=..,0755,/opt/cloudstack/bin
-launch_hb.sh=..,0755,/opt/cloudstack/bin
-vhd-util=..,0755,/opt/cloudstack/bin
-vmopspremium=..,0755,/etc/xapi.d/plugins
-create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloudstack/bin
-upgrade_snapshot.sh=..,0755,/opt/cloudstack/bin
-cloud-clean-vlan.sh=..,0755,/opt/cloudstack/bin
-cloud-prepare-upgrade.sh=..,0755,/opt/cloudstack/bin
-bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-swift=..,0755,/opt/cloudstack/bin
-swiftxen=..,0755,/etc/xapi.d/plugins
-s3xen=..,0755,/etc/xapi.d/plugins
-add_to_vcpus_params_live.sh=..,0755,/opt/cloudstack/bin
-ovs-pvlan=..,0755,/etc/xapi.d/plugins
-ovs-pvlan-dhcp-host.sh=../../../network,0755,/opt/cloudstack/bin
-ovs-pvlan-vm.sh=../../../network,0755,/opt/cloudstack/bin
-ovs-pvlan-cleanup.sh=../../../network,0755,/opt/cloudstack/bin
-ovs-get-dhcp-iface.sh=..,0755,/opt/cloudstack/bin
-ovs-get-bridge.sh=..,0755,/opt/cloudstack/bin
-cloudstacklog=..,0644,/etc/logrotate.d
+network_info.sh=..,0755,/opt/cloud/bin
+setupxenserver.sh=..,0755,/opt/cloud/bin
+make_migratable.sh=..,0755,/opt/cloud/bin
+createipAlias.sh=..,0755,/opt/cloud/bin
+deleteipAlias.sh=..,0755,/opt/cloud/bin
+setup_iscsi.sh=..,0755,/opt/cloud/bin
+pingtest.sh=../../..,0755,/opt/cloud/bin
+dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloud/bin
+save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_firewall.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloud/bin
+router_proxy.sh=../../../../network/domr/,0755,/opt/cloud/bin
+cloud-setup-bonding.sh=..,0755,/opt/cloud/bin
+copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloud/bin
+copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloud/bin
+kill_copy_process.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_sr.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_file.sh=..,0755,/opt/cloud/bin
+check_heartbeat.sh=..,0755,/opt/cloud/bin
+xenheartbeat.sh=..,0755,/opt/cloud/bin
+launch_hb.sh=..,0755,/opt/cloud/bin
+vhd-util=..,0755,/opt/cloud/bin
+create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloud/bin
+upgrade_snapshot.sh=..,0755,/opt/cloud/bin
+cloud-clean-vlan.sh=..,0755,/opt/cloud/bin
+cloud-prepare-upgrade.sh=..,0755,/opt/cloud/bin
+bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloud/bin
+swift=..,0755,/opt/cloud/bin
+cloud-plugin-swiftxen=..,0755,/etc/xapi.d/plugins
+cloud-plugin-s3xen=..,0755,/etc/xapi.d/plugins
+add_to_vcpus_params_live.sh=..,0755,/opt/cloud/bin
+cloud-plugin-ovs-pvlan=..,0755,/etc/xapi.d/plugins
+ovs-pvlan-dhcp-host.sh=../../../network,0755,/opt/cloud/bin
+ovs-pvlan-vm.sh=../../../network,0755,/opt/cloud/bin
+ovs-pvlan-cleanup.sh=../../../network,0755,/opt/cloud/bin
+ovs-get-dhcp-iface.sh=..,0755,/opt/cloud/bin
+ovs-get-bridge.sh=..,0755,/opt/cloud/bin
+cloudlog=..,0644,/etc/logrotate.d

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xenserver62/create_privatetemplate_from_snapshot.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver62/create_privatetemplate_from_snapshot.sh b/scripts/vm/hypervisor/xenserver/xenserver62/create_privatetemplate_from_snapshot.sh
new file mode 100644
index 0000000..fff6820
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/xenserver62/create_privatetemplate_from_snapshot.sh
@@ -0,0 +1,138 @@
+#!/bin/bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+#set -x
+ 
+usage() {
+  printf "Usage: %s [vhd file in secondary storage] [template directory in secondary storage] [template local dir] \n" $(basename $0) 
+}
+options='tcp,soft,timeo=133,retrans=1'
+cleanup()
+{
+  if [ ! -z $snapshotdir ]; then 
+    umount $snapshotdir
+    if [ $? -eq 0 ];  then
+      rmdir $snapshotdir
+    fi
+  fi
+  if [ ! -z $templatedir ]; then 
+    umount $templatedir
+    if [ $? -eq 0 ];  then
+      rmdir $templatedir
+    fi
+  fi
+}
+
+if [ -z $1 ]; then
+  usage
+  echo "2#no vhd file path"
+  exit 0
+else
+  snapshoturl=${1%/*}
+  vhdfilename=${1##*/}
+fi
+
+if [ -z $2 ]; then
+  usage
+  echo "3#no template path"
+  exit 0
+else
+  templateurl=$2
+fi
+
+if [ -z $3 ]; then
+  usage
+  echo "3#no template local dir"
+  exit 0
+else
+  tmpltLocalDir=$3
+fi
+
+
+snapshotdir=/var/run/cloud_mount/$(uuidgen -r)
+mkdir -p $snapshotdir
+if [ $? -ne 0 ]; then
+  echo "4#cann't make dir $snapshotdir"
+  exit 0
+fi
+
+mount -o $options $snapshoturl $snapshotdir
+if [ $? -ne 0 ]; then
+  rmdir $snapshotdir
+  echo "5#can not mount $snapshoturl to $snapshotdir"
+  exit 0
+fi
+
+templatedir=/var/run/cloud_mount/$tmpltLocalDir
+mkdir -p $templatedir
+if [ $? -ne 0 ]; then
+  templatedir=""
+  cleanup
+  echo "6#cann't make dir $templatedir"
+  exit 0
+fi
+
+mount -o $options $templateurl $templatedir
+if [ $? -ne 0 ]; then
+  rmdir $templatedir
+  templatedir=""
+  cleanup
+  echo "7#can not mount $templateurl to $templatedir"
+  exit 0
+fi
+
+VHDUTIL="/usr/bin/vhd-util"
+
+copyvhd()
+{
+  local desvhd=$1
+  local srcvhd=$2
+  local parent=
+  parent=`$VHDUTIL query -p -n $srcvhd`
+  if [ $? -ne 0 ]; then
+    echo "30#failed to query $srcvhd"
+    cleanup
+    exit 0
+  fi
+  if [[ "${parent}"  =~ " no parent" ]]; then
+    dd if=$srcvhd of=$desvhd bs=2M     
+    if [ $? -ne 0 ]; then
+      echo "31#failed to dd $srcvhd to $desvhd"
+      cleanup
+      exit 0
+    fi
+  else
+    copyvhd $desvhd $parent
+    $VHDUTIL coalesce -p $desvhd -n $srcvhd
+    if [ $? -ne 0 ]; then
+      echo "32#failed to coalesce  $desvhd to $srcvhd"
+      cleanup
+      exit 0
+    fi
+  fi
+}
+
+templateuuid=$(uuidgen -r)
+desvhd=$templatedir/$templateuuid.vhd
+srcvhd=$snapshotdir/$vhdfilename
+copyvhd $desvhd $srcvhd
+virtualSize=`$VHDUTIL query -v -n $desvhd`
+physicalSize=`ls -l $desvhd | awk '{print $5}'`
+cleanup
+echo "0#$templateuuid#$physicalSize#$virtualSize"
+exit 0

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xenserver62/patch
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver62/patch b/scripts/vm/hypervisor/xenserver/xenserver62/patch
index a98ca86..e8a4ea2 100644
--- a/scripts/vm/hypervisor/xenserver/xenserver62/patch
+++ b/scripts/vm/hypervisor/xenserver/xenserver62/patch
@@ -21,56 +21,62 @@
 # [Name of file]=[source path],[file permission],[destination path]
 # [destination path] is required.
 # If [file permission] is missing, 755 is assumed.
-# If [source path] is missing, it looks in the same
-# directory as the patch file.
+# If [source path] is missing, it looks in the same directory as the patch file.
 # If [source path] starts with '/', then it is absolute path.
 # If [source path] starts with '~', then it is path relative to management server home directory.
 # If [source path] does not start with '/' or '~', then it is relative path to the location of the patch file. 
-vmops=..,0755,/etc/xapi.d/plugins
+# 
+# The following specifies the paths to deposit files
+# /etc/xapi.d/plugins - All XAPI plugins.  Every file placed here should start with "cloud-".
+# /etc/cloud - Configuration files for scripts.
+# /opt/cloud/bin - All scripts used in the normal operation.
+# /opt/cloud/tools/bin - All scripts used for testing/support that are meant to be run by hand.
+# /etc/logrotate.d - All cloud log rotation configuration files.  Every file placed here must start with "cloud-".
+#
+# All log files should be placed in /var/log/cloud
+cloud-plugin-generic=..,0755,/etc/xapi.d/plugins
 xen-ovs-vif-flows.rules=..,0644,/etc/udev/rules.d
 ovs-vif-flows.py=..,0755,/etc/xapi.d/plugins
-cloudstack_plugins.conf=..,0644,/etc/xensource
-cloudstack_pluginlib.py=..,0755,/etc/xapi.d/plugins
-ovstunnel=..,0755,/etc/xapi.d/plugins
-vmopsSnapshot=..,0755,/etc/xapi.d/plugins
+cloud-plugin-lib.py=..,0755,/etc/xapi.d/plugins
+cloud-plugin-ovstunnel=..,0755,/etc/xapi.d/plugins
+cloud-plugin-snapshot=..,0755,/etc/xapi.d/plugins
+cloud-plugin-swiftxen=..,0755,/etc/xapi.d/plugins
+cloud-plugin-s3xen=..,0755,/etc/xapi.d/plugins
+cloud-plugins.conf=..,0644,/etc/cloud
+cloud-plugin-ovs-pvlan=..,0755,/etc/xapi.d/plugins
 systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
 id_rsa.cloud=../../../systemvm,0600,/root/.ssh
-network_info.sh=..,0755,/opt/cloudstack/bin
-setupxenserver.sh=..,0755,/opt/cloudstack/bin
-make_migratable.sh=..,0755,/opt/cloudstack/bin
-createipAlias.sh=..,0755,/opt/cloudstack/bin
-deleteipAlias.sh=..,0755,/opt/cloudstack/bin
-setup_iscsi.sh=..,0755,/opt/cloudstack/bin
-pingtest.sh=../../..,0755,/opt/cloudstack/bin
-dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_firewall.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-router_proxy.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-cloud-setup-bonding.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloudstack/bin
-kill_copy_process.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_sr.sh=..,0755,/opt/cloudstack/bin
-setup_heartbeat_file.sh=..,0755,/opt/cloudstack/bin
-check_heartbeat.sh=..,0755,/opt/cloudstack/bin
-xenheartbeat.sh=..,0755,/opt/cloudstack/bin
-launch_hb.sh=..,0755,/opt/cloudstack/bin
-vhd-util=..,0755,/opt/cloudstack/bin
-vmopspremium=..,0755,/etc/xapi.d/plugins
-create_privatetemplate_from_snapshot.sh=..,0755,/opt/cloudstack/bin
-upgrade_snapshot.sh=..,0755,/opt/cloudstack/bin
-cloud-clean-vlan.sh=..,0755,/opt/cloudstack/bin
-cloud-prepare-upgrade.sh=..,0755,/opt/cloudstack/bin
-bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloudstack/bin
-swift=..,0755,/opt/cloudstack/bin
-swiftxen=..,0755,/etc/xapi.d/plugins
-s3xen=..,0755,/etc/xapi.d/plugins
-add_to_vcpus_params_live.sh=..,0755,/opt/cloudstack/bin
-ovs-pvlan=..,0755,/etc/xapi.d/plugins
-ovs-pvlan-dhcp-host.sh=../../../network,0755,/opt/cloudstack/bin
-ovs-pvlan-vm.sh=../../../network,0755,/opt/cloudstack/bin
-ovs-pvlan-cleanup.sh=../../../network,0755,/opt/cloudstack/bin
-ovs-get-dhcp-iface.sh=..,0755,/opt/cloudstack/bin
-ovs-get-bridge.sh=..,0755,/opt/cloudstack/bin
-cloudstacklog=..,0644,/etc/logrotate.d
+network_info.sh=..,0755,/opt/cloud/bin
+setupxenserver.sh=..,0755,/opt/cloud/bin
+make_migratable.sh=..,0755,/opt/cloud/bin
+createipAlias.sh=..,0755,/opt/cloud/bin
+deleteipAlias.sh=..,0755,/opt/cloud/bin
+setup_iscsi.sh=..,0755,/opt/cloud/bin
+pingtest.sh=../../..,0755,/opt/cloud/bin
+dhcp_entry.sh=../../../../network/domr/,0755,/opt/cloud/bin
+save_password_to_domr.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_firewall.sh=../../../../network/domr/,0755,/opt/cloud/bin
+call_loadbalancer.sh=../../../../network/domr/,0755,/opt/cloud/bin
+router_proxy.sh=../../../../network/domr/,0755,/opt/cloud/bin
+cloud-setup-bonding.sh=..,0755,/opt/cloud/bin
+copy_vhd_to_secondarystorage.sh=..,0755,/opt/cloud/bin
+copy_vhd_from_secondarystorage.sh=..,0755,/opt/cloud/bin
+kill_copy_process.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_sr.sh=..,0755,/opt/cloud/bin
+setup_heartbeat_file.sh=..,0755,/opt/cloud/bin
+check_heartbeat.sh=..,0755,/opt/cloud/bin
+xenheartbeat.sh=..,0755,/opt/cloud/bin
+launch_hb.sh=..,0755,/opt/cloud/bin
+create_privatetemplate_from_snapshot.sh=,0755,/opt/cloud/bin
+upgrade_snapshot.sh=,0755,/opt/cloud/bin
+cloud-clean-vlan.sh=..,0755,/opt/cloud/bin
+cloud-prepare-upgrade.sh=..,0755,/opt/cloud/bin
+bumpUpPriority.sh=../../../../network/domr/,0755,/opt/cloud/bin
+swift=..,0755,/opt/cloud/bin
+add_to_vcpus_params_live.sh=..,0755,/opt/cloud/bin
+ovs-pvlan-dhcp-host.sh=../../../network,0755,/opt/cloud/bin
+ovs-pvlan-vm.sh=../../../network,0755,/opt/cloud/bin
+ovs-pvlan-cleanup.sh=../../../network,0755,/opt/cloud/bin
+ovs-get-dhcp-iface.sh=..,0755,/opt/cloud/bin
+ovs-get-bridge.sh=..,0755,/opt/cloud/bin
+cloudlog=..,0644,/etc/logrotate.d

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xenserver62/upgrade_snapshot.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver62/upgrade_snapshot.sh b/scripts/vm/hypervisor/xenserver/xenserver62/upgrade_snapshot.sh
new file mode 100644
index 0000000..c892495
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/xenserver62/upgrade_snapshot.sh
@@ -0,0 +1,133 @@
+#!/bin/bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+#set -x
+ 
+usage() {
+  printf "Usage: %s [vhd file in secondary storage] [template directory in secondary storage] \n" $(basename $0) 
+}
+
+cleanup()
+{
+  if [ ! -z $snapshotdir ]; then 
+    umount $snapshotdir
+    if [ $? -eq 0 ];  then
+      rmdir $snapshotdir
+    fi
+  fi
+  if [ ! -z $templatedir ]; then 
+    umount $templatedir
+    if [ $? -eq 0 ];  then
+      rmdir $templatedir
+    fi
+  fi
+}
+
+if [ -z $1 ]; then
+  usage
+  echo "2#no vhd file path"
+  exit 0
+else
+  snapshoturl=${1%/*}
+  vhdfilename=${1##*/}
+fi
+
+if [ -z $2 ]; then
+  usage
+  echo "3#no template path"
+  exit 0
+else
+  templateurl=$2
+fi
+
+snapshotdir=/var/run/cloud_mount/$(uuidgen -r)
+mkdir -p $snapshotdir
+if [ $? -ne 0 ]; then
+  echo "4#cann't make dir $snapshotdir"
+  exit 0
+fi
+
+mount -o tcp $snapshoturl $snapshotdir
+if [ $? -ne 0 ]; then
+  rmdir $snapshotdir
+  echo "5#can not mount $snapshoturl to $snapshotdir"
+  exit 0
+fi
+
+templatedir=/var/run/cloud_mount/$(uuidgen -r)
+mkdir -p $templatedir
+if [ $? -ne 0 ]; then
+  templatedir=""
+  cleanup
+  echo "6#cann't make dir $templatedir"
+  exit 0
+fi
+
+mount -o tcp $templateurl $templatedir
+if [ $? -ne 0 ]; then
+  rmdir $templatedir
+  templatedir=""
+  cleanup
+  echo "7#can not mount $templateurl to $templatedir"
+  exit 0
+fi
+
+VHDUTIL="/usr/bin/vhd-util"
+
+upgradeSnapshot()
+{
+  local ssvhd=$1
+  local parent=`$VHDUTIL query -p -n $ssvhd`
+  if [ $? -ne 0 ]; then
+    echo "30#failed to query $ssvhd"
+    cleanup
+    exit 0
+  fi
+  if [ "${parent##*vhd has}" = " no parent" ]; then
+    dd if=$templatevhd of=$snapshotdir/$templatefilename bs=2M 
+    if [ $? -ne 0 ]; then
+      echo "31#failed to dd $templatevhd to $snapshotdir/$templatefilenamed"
+      cleanup
+      exit 0
+    fi
+
+    $VHDUTIL modify -p $snapshotdir/$templatefilename -n $ssvhd
+    if [ $? -ne 0 ]; then
+      echo "32#failed to set parent of $ssvhd to $snapshotdir/$templatefilenamed"
+      cleanup
+      exit 0
+    fi
+
+    rm -f $parent
+  else
+    upgradeSnapshot $parent
+  fi
+}
+
+templatevhd=$(ls $templatedir/*.vhd)
+if [ $? -ne 0 ]; then
+  echo "8#template vhd doesn't exist for $templateurl"
+  cleanup
+  exit 0
+fi
+templatefilename=${templatevhd##*/}
+snapshotvhd=$snapshotdir/$vhdfilename
+upgradeSnapshot $snapshotvhd
+cleanup
+echo "0#success"
+exit 0


[8/8] git commit: updated refs/heads/4.2-workplace to 5d13a07

Posted by ah...@apache.org.
Python can't have - so it has to be _


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/5d13a07d
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/5d13a07d
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/5d13a07d

Branch: refs/heads/4.2-workplace
Commit: 5d13a07db819ea18645a65b7da1d4ada9dd2fd1f
Parents: f2bb1ac
Author: Alex Huang <al...@citrix.com>
Authored: Fri Nov 15 10:23:15 2013 -0800
Committer: Alex Huang <al...@citrix.com>
Committed: Fri Nov 15 10:23:15 2013 -0800

----------------------------------------------------------------------
 .../xen/resource/CitrixResourceBase.java        |   5 +-
 .../hypervisor/xenserver/cloud-plugin-generic   |   2 +-
 .../vm/hypervisor/xenserver/cloud-plugin-lib.py | 221 ---------------
 .../hypervisor/xenserver/cloud-plugin-ovs-pvlan |   2 +-
 .../hypervisor/xenserver/cloud-plugin-snapshot  |   2 +-
 .../vm/hypervisor/xenserver/cloud_plugin_lib.py | 221 +++++++++++++++
 .../vm/hypervisor/xenserver/ovs-vif-flows.py    |   2 +-
 .../hypervisor/xenserver/xenserver60/NFSSR.py   | 282 -------------------
 .../vm/hypervisor/xenserver/xenserver60/patch   |   3 +-
 .../vm/hypervisor/xenserver/xenserver62/patch   |   2 +-
 10 files changed, 230 insertions(+), 512 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
index c27df61..e6453df 100644
--- a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
+++ b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
@@ -5179,10 +5179,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
                 com.trilead.ssh2.Session session = sshConnection.openSession();
                 
-                String cmd = "mkdir -p /opt/cloudstack/bin";
+                String cmd = "mkdir -p /opt/cloud/bin; mkdir -p /opt/cloud/tools; mkdir -p /var/log/cloud; mkdir -p /etc/cloud";
                 if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
-                    throw new CloudRuntimeException("Cannot create directory /opt/cloudstack/bin on XenServer hosts");
+                    throw new CloudRuntimeException("Cannot create directory /opt/cloud/bin on XenServer hosts");
                 }
+
                 SCPClient scp = new SCPClient(sshConnection);
 
                 List<File> files = getPatchFiles();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/cloud-plugin-generic
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-generic b/scripts/vm/hypervisor/xenserver/cloud-plugin-generic
index 32c03de..16f2a50 100644
--- a/scripts/vm/hypervisor/xenserver/cloud-plugin-generic
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-generic
@@ -30,7 +30,7 @@ import tempfile
 import util
 import subprocess
 import zlib
-import cloud-plugin-lib as lib
+import cloud_plugin_lib as lib
 import logging
 from util import CommandException
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/cloud-plugin-lib.py
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-lib.py b/scripts/vm/hypervisor/xenserver/cloud-plugin-lib.py
deleted file mode 100644
index 6b17d0b..0000000
--- a/scripts/vm/hypervisor/xenserver/cloud-plugin-lib.py
+++ /dev/null
@@ -1,221 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Common function for Cloudstack's XenAPI plugins
-
-import ConfigParser
-import logging
-import os
-import subprocess
-
-from time import localtime, asctime
-
-DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s"
-DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
-DEFAULT_LOG_FILE = "/var/log/cloud/cloud-plugins.log"
-
-PLUGIN_CONFIG_PATH = "/etc/cloud/cloud-plugins.conf"
-OVSDB_PID_PATH = "/var/run/openvswitch/ovsdb-server.pid"
-OVSDB_DAEMON_PATH = "ovsdb-server"
-OVS_PID_PATH = "/var/run/openvswitch/ovs-vswitchd.pid"
-OVS_DAEMON_PATH = "ovs-vswitchd"
-VSCTL_PATH = "/usr/bin/ovs-vsctl"
-OFCTL_PATH = "/usr/bin/ovs-ofctl"
-XE_PATH = "/opt/xensource/bin/xe"
-
-
-class PluginError(Exception):
-    """Base Exception class for all plugin errors."""
-    def __init__(self, *args):
-        Exception.__init__(self, *args)
-
-
-def setup_logging(log_file=None):
-    debug = False
-    verbose = False
-    log_format = DEFAULT_LOG_FORMAT
-    log_date_format = DEFAULT_LOG_DATE_FORMAT
-    # try to read plugin configuration file
-    if os.path.exists(PLUGIN_CONFIG_PATH):
-        config = ConfigParser.ConfigParser()
-        config.read(PLUGIN_CONFIG_PATH)
-        try:
-            options = config.options('LOGGING')
-            if 'debug' in options:
-                debug = config.getboolean('LOGGING', 'debug')
-            if 'verbose' in options:
-                verbose = config.getboolean('LOGGING', 'verbose')
-            if 'format' in options:
-                log_format = config.get('LOGGING', 'format')
-            if 'date_format' in options:
-                log_date_format = config.get('LOGGING', 'date_format')
-            if 'file' in options:
-                log_file_2 = config.get('LOGGING', 'file')
-        except ValueError:
-            # configuration file contained invalid attributes
-            # ignore them
-            pass
-        except ConfigParser.NoSectionError:
-            # Missing 'Logging' section in configuration file
-            pass
-
-    root_logger = logging.root
-    if debug:
-        root_logger.setLevel(logging.DEBUG)
-    elif verbose:
-        root_logger.setLevel(logging.INFO)
-    else:
-        root_logger.setLevel(logging.WARNING)
-    formatter = logging.Formatter(log_format, log_date_format)
-
-    log_filename = log_file or log_file_2 or DEFAULT_LOG_FILE
-
-    logfile_handler = logging.FileHandler(log_filename)
-    logfile_handler.setFormatter(formatter)
-    root_logger.addHandler(logfile_handler)
-
-
-def do_cmd(cmd):
-    """Abstracts out the basics of issuing system commands. If the command
-    returns anything in stderr, a PluginError is raised with that information.
-    Otherwise, the output from stdout is returned.
-    """
-
-    pipe = subprocess.PIPE
-    logging.debug("Executing:%s", cmd)
-    proc = subprocess.Popen(cmd, shell=False, stdin=pipe, stdout=pipe,
-                            stderr=pipe, close_fds=True)
-    ret_code = proc.wait()
-    err = proc.stderr.read()
-    if ret_code:
-        logging.debug("The command exited with the error code: " +
-                      "%s (stderr output:%s)" % (ret_code, err))
-        raise PluginError(err)
-    output = proc.stdout.read()
-    if output.endswith('\n'):
-        output = output[:-1]
-    return output
-
-
-def _is_process_run(pidFile, name):
-    try:
-        fpid = open(pidFile, "r")
-        pid = fpid.readline()
-        fpid.close()
-    except IOError, e:
-        return -1
-
-    pid = pid[:-1]
-    ps = os.popen("ps -ae")
-    for l in ps:
-        if pid in l and name in l:
-            ps.close()
-            return 0
-
-    ps.close()
-    return -2
-
-
-def _is_tool_exist(name):
-    if os.path.exists(name):
-        return 0
-    return -1
-
-
-def check_switch():
-    global result
-
-    ret = _is_process_run(OVSDB_PID_PATH, OVSDB_DAEMON_PATH)
-    if ret < 0:
-        if ret == -1:
-            return "NO_DB_PID_FILE"
-        if ret == -2:
-            return "DB_NOT_RUN"
-
-    ret = _is_process_run(OVS_PID_PATH, OVS_DAEMON_PATH)
-    if ret < 0:
-        if ret == -1:
-            return "NO_SWITCH_PID_FILE"
-        if ret == -2:
-            return "SWITCH_NOT_RUN"
-
-    if _is_tool_exist(VSCTL_PATH) < 0:
-        return "NO_VSCTL"
-
-    if _is_tool_exist(OFCTL_PATH) < 0:
-        return "NO_OFCTL"
-
-    return "SUCCESS"
-
-
-def _build_flow_expr(**kwargs):
-    is_delete_expr = kwargs.get('delete', False)
-    flow = ""
-    if not is_delete_expr:
-        flow = "hard_timeout=%s,idle_timeout=%s,priority=%s"\
-                % (kwargs.get('hard_timeout', '0'),
-                   kwargs.get('idle_timeout', '0'),
-                   kwargs.get('priority', '1'))
-    in_port = 'in_port' in kwargs and ",in_port=%s" % kwargs['in_port'] or ''
-    dl_type = 'dl_type' in kwargs and ",dl_type=%s" % kwargs['dl_type'] or ''
-    dl_src = 'dl_src' in kwargs and ",dl_src=%s" % kwargs['dl_src'] or ''
-    dl_dst = 'dl_dst' in kwargs and ",dl_dst=%s" % kwargs['dl_dst'] or ''
-    nw_src = 'nw_src' in kwargs and ",nw_src=%s" % kwargs['nw_src'] or ''
-    nw_dst = 'nw_dst' in kwargs and ",nw_dst=%s" % kwargs['nw_dst'] or ''
-    proto = 'proto' in kwargs and ",%s" % kwargs['proto'] or ''
-    ip = ('nw_src' in kwargs or 'nw_dst' in kwargs) and ',ip' or ''
-    flow = (flow + in_port + dl_type + dl_src + dl_dst +
-            (ip or proto) + nw_src + nw_dst)
-    return flow
-
-
-def add_flow(bridge, **kwargs):
-    """
-    Builds a flow expression for **kwargs and adds the flow entry
-    to an Open vSwitch instance
-    """
-    flow = _build_flow_expr(**kwargs)
-    actions = 'actions' in kwargs and ",actions=%s" % kwargs['actions'] or ''
-    flow = flow + actions
-    addflow = [OFCTL_PATH, "add-flow", bridge, flow]
-    do_cmd(addflow)
-
-
-def del_flows(bridge, **kwargs):
-    """
-    Removes flows according to criteria passed as keyword.
-    """
-    flow = _build_flow_expr(delete=True, **kwargs)
-    # out_port condition does not exist for all flow commands
-    out_port = ("out_port" in kwargs and
-                ",out_port=%s" % kwargs['out_port'] or '')
-    flow = flow + out_port
-    delFlow = [OFCTL_PATH, 'del-flows', bridge, flow]
-    do_cmd(delFlow)
-
-
-def del_all_flows(bridge):
-    delFlow = [OFCTL_PATH, "del-flows", bridge]
-    do_cmd(delFlow)
-
-    normalFlow = "priority=0 idle_timeout=0 hard_timeout=0 actions=normal"
-    add_flow(bridge, normalFlow)
-
-
-def del_port(bridge, port):
-    delPort = [VSCTL_PATH, "del-port", bridge, port]
-    do_cmd(delPort)

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan b/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan
index 32eb794..a3b7067 100644
--- a/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan
@@ -17,7 +17,7 @@
 # under the License.
 
 
-import cloud-plugin-lib as lib
+import cloud_plugin_lib as lib
 import logging
 import os
 import sys

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot b/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot
index 7ad892d..ae2f9aa 100644
--- a/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot
@@ -34,7 +34,7 @@ import xs_errors
 import cleanup
 import stat
 import random
-import cloud-plugin-lib as lib
+import cloud_plugin_lib as lib
 import logging
 
 lib.setup_logging("/var/log/cloud/cloud-plugins.log")

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/cloud_plugin_lib.py
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud_plugin_lib.py b/scripts/vm/hypervisor/xenserver/cloud_plugin_lib.py
new file mode 100644
index 0000000..6b17d0b
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud_plugin_lib.py
@@ -0,0 +1,221 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# Common function for Cloudstack's XenAPI plugins
+
+import ConfigParser
+import logging
+import os
+import subprocess
+
+from time import localtime, asctime
+
+DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s"
+DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
+DEFAULT_LOG_FILE = "/var/log/cloud/cloud-plugins.log"
+
+PLUGIN_CONFIG_PATH = "/etc/cloud/cloud-plugins.conf"
+OVSDB_PID_PATH = "/var/run/openvswitch/ovsdb-server.pid"
+OVSDB_DAEMON_PATH = "ovsdb-server"
+OVS_PID_PATH = "/var/run/openvswitch/ovs-vswitchd.pid"
+OVS_DAEMON_PATH = "ovs-vswitchd"
+VSCTL_PATH = "/usr/bin/ovs-vsctl"
+OFCTL_PATH = "/usr/bin/ovs-ofctl"
+XE_PATH = "/opt/xensource/bin/xe"
+
+
+class PluginError(Exception):
+    """Base Exception class for all plugin errors."""
+    def __init__(self, *args):
+        Exception.__init__(self, *args)
+
+
+def setup_logging(log_file=None):
+    debug = False
+    verbose = False
+    log_format = DEFAULT_LOG_FORMAT
+    log_date_format = DEFAULT_LOG_DATE_FORMAT
+    # try to read plugin configuration file
+    if os.path.exists(PLUGIN_CONFIG_PATH):
+        config = ConfigParser.ConfigParser()
+        config.read(PLUGIN_CONFIG_PATH)
+        try:
+            options = config.options('LOGGING')
+            if 'debug' in options:
+                debug = config.getboolean('LOGGING', 'debug')
+            if 'verbose' in options:
+                verbose = config.getboolean('LOGGING', 'verbose')
+            if 'format' in options:
+                log_format = config.get('LOGGING', 'format')
+            if 'date_format' in options:
+                log_date_format = config.get('LOGGING', 'date_format')
+            if 'file' in options:
+                log_file_2 = config.get('LOGGING', 'file')
+        except ValueError:
+            # configuration file contained invalid attributes
+            # ignore them
+            pass
+        except ConfigParser.NoSectionError:
+            # Missing 'Logging' section in configuration file
+            pass
+
+    root_logger = logging.root
+    if debug:
+        root_logger.setLevel(logging.DEBUG)
+    elif verbose:
+        root_logger.setLevel(logging.INFO)
+    else:
+        root_logger.setLevel(logging.WARNING)
+    formatter = logging.Formatter(log_format, log_date_format)
+
+    log_filename = log_file or log_file_2 or DEFAULT_LOG_FILE
+
+    logfile_handler = logging.FileHandler(log_filename)
+    logfile_handler.setFormatter(formatter)
+    root_logger.addHandler(logfile_handler)
+
+
+def do_cmd(cmd):
+    """Abstracts out the basics of issuing system commands. If the command
+    returns anything in stderr, a PluginError is raised with that information.
+    Otherwise, the output from stdout is returned.
+    """
+
+    pipe = subprocess.PIPE
+    logging.debug("Executing:%s", cmd)
+    proc = subprocess.Popen(cmd, shell=False, stdin=pipe, stdout=pipe,
+                            stderr=pipe, close_fds=True)
+    ret_code = proc.wait()
+    err = proc.stderr.read()
+    if ret_code:
+        logging.debug("The command exited with the error code: " +
+                      "%s (stderr output:%s)" % (ret_code, err))
+        raise PluginError(err)
+    output = proc.stdout.read()
+    if output.endswith('\n'):
+        output = output[:-1]
+    return output
+
+
+def _is_process_run(pidFile, name):
+    try:
+        fpid = open(pidFile, "r")
+        pid = fpid.readline()
+        fpid.close()
+    except IOError, e:
+        return -1
+
+    pid = pid[:-1]
+    ps = os.popen("ps -ae")
+    for l in ps:
+        if pid in l and name in l:
+            ps.close()
+            return 0
+
+    ps.close()
+    return -2
+
+
+def _is_tool_exist(name):
+    if os.path.exists(name):
+        return 0
+    return -1
+
+
+def check_switch():
+    global result
+
+    ret = _is_process_run(OVSDB_PID_PATH, OVSDB_DAEMON_PATH)
+    if ret < 0:
+        if ret == -1:
+            return "NO_DB_PID_FILE"
+        if ret == -2:
+            return "DB_NOT_RUN"
+
+    ret = _is_process_run(OVS_PID_PATH, OVS_DAEMON_PATH)
+    if ret < 0:
+        if ret == -1:
+            return "NO_SWITCH_PID_FILE"
+        if ret == -2:
+            return "SWITCH_NOT_RUN"
+
+    if _is_tool_exist(VSCTL_PATH) < 0:
+        return "NO_VSCTL"
+
+    if _is_tool_exist(OFCTL_PATH) < 0:
+        return "NO_OFCTL"
+
+    return "SUCCESS"
+
+
+def _build_flow_expr(**kwargs):
+    is_delete_expr = kwargs.get('delete', False)
+    flow = ""
+    if not is_delete_expr:
+        flow = "hard_timeout=%s,idle_timeout=%s,priority=%s"\
+                % (kwargs.get('hard_timeout', '0'),
+                   kwargs.get('idle_timeout', '0'),
+                   kwargs.get('priority', '1'))
+    in_port = 'in_port' in kwargs and ",in_port=%s" % kwargs['in_port'] or ''
+    dl_type = 'dl_type' in kwargs and ",dl_type=%s" % kwargs['dl_type'] or ''
+    dl_src = 'dl_src' in kwargs and ",dl_src=%s" % kwargs['dl_src'] or ''
+    dl_dst = 'dl_dst' in kwargs and ",dl_dst=%s" % kwargs['dl_dst'] or ''
+    nw_src = 'nw_src' in kwargs and ",nw_src=%s" % kwargs['nw_src'] or ''
+    nw_dst = 'nw_dst' in kwargs and ",nw_dst=%s" % kwargs['nw_dst'] or ''
+    proto = 'proto' in kwargs and ",%s" % kwargs['proto'] or ''
+    ip = ('nw_src' in kwargs or 'nw_dst' in kwargs) and ',ip' or ''
+    flow = (flow + in_port + dl_type + dl_src + dl_dst +
+            (ip or proto) + nw_src + nw_dst)
+    return flow
+
+
+def add_flow(bridge, **kwargs):
+    """
+    Builds a flow expression for **kwargs and adds the flow entry
+    to an Open vSwitch instance
+    """
+    flow = _build_flow_expr(**kwargs)
+    actions = 'actions' in kwargs and ",actions=%s" % kwargs['actions'] or ''
+    flow = flow + actions
+    addflow = [OFCTL_PATH, "add-flow", bridge, flow]
+    do_cmd(addflow)
+
+
+def del_flows(bridge, **kwargs):
+    """
+    Removes flows according to criteria passed as keyword.
+    """
+    flow = _build_flow_expr(delete=True, **kwargs)
+    # out_port condition does not exist for all flow commands
+    out_port = ("out_port" in kwargs and
+                ",out_port=%s" % kwargs['out_port'] or '')
+    flow = flow + out_port
+    delFlow = [OFCTL_PATH, 'del-flows', bridge, flow]
+    do_cmd(delFlow)
+
+
+def del_all_flows(bridge):
+    delFlow = [OFCTL_PATH, "del-flows", bridge]
+    do_cmd(delFlow)
+
+    normalFlow = "priority=0 idle_timeout=0 hard_timeout=0 actions=normal"
+    add_flow(bridge, normalFlow)
+
+
+def del_port(bridge, port):
+    delPort = [VSCTL_PATH, "del-port", bridge, port]
+    do_cmd(delPort)

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py b/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py
index 58e0a3d..59e9ad3 100644
--- a/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py
+++ b/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py
@@ -21,7 +21,7 @@
 import os
 import sys
 
-import cloud-plugin-lib as pluginlib
+import cloud_plugin_lib as pluginlib
 
 
 def clear_flows(bridge, this_vif_ofport, vif_ofports):

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/xenserver60/NFSSR.py
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver60/NFSSR.py b/scripts/vm/hypervisor/xenserver/xenserver60/NFSSR.py
deleted file mode 100755
index 0d6badb..0000000
--- a/scripts/vm/hypervisor/xenserver/xenserver60/NFSSR.py
+++ /dev/null
@@ -1,282 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# FileSR: local-file storage repository
-
-import SR, VDI, SRCommand, FileSR, util
-import errno
-import os, re, sys
-import xml.dom.minidom
-import xmlrpclib
-import xs_errors
-import nfs
-import vhdutil
-from lock import Lock
-import cleanup
-
-CAPABILITIES = ["SR_PROBE","SR_UPDATE", "SR_CACHING",
-                "VDI_CREATE","VDI_DELETE","VDI_ATTACH","VDI_DETACH",
-                "VDI_UPDATE", "VDI_CLONE","VDI_SNAPSHOT","VDI_RESIZE",
-                "VDI_GENERATE_CONFIG",
-                "VDI_RESET_ON_BOOT", "ATOMIC_PAUSE"]
-
-CONFIGURATION = [ [ 'server', 'hostname or IP address of NFS server (required)' ], \
-                  [ 'serverpath', 'path on remote server (required)' ] ]
-
-                  
-DRIVER_INFO = {
-    'name': 'NFS VHD',
-    'description': 'SR plugin which stores disks as VHD files on a remote NFS filesystem',
-    'vendor': 'The Apache Software Foundation',
-    'copyright': 'Copyright (c) 2012 The Apache Software Foundation',
-    'driver_version': '1.0',
-    'required_api_version': '1.0',
-    'capabilities': CAPABILITIES,
-    'configuration': CONFIGURATION
-    }
-
-DRIVER_CONFIG = {"ATTACH_FROM_CONFIG_WITH_TAPDISK": True}
-
-
-# The mountpoint for the directory when performing an sr_probe.  All probes
-# are guaranteed to be serialised by xapi, so this single mountpoint is fine.
-PROBE_MOUNTPOINT = "probe"
-NFSPORT = 2049
-DEFAULT_TRANSPORT = "tcp"
-
-
-class NFSSR(FileSR.FileSR):
-    """NFS file-based storage repository"""
-    def handles(type):
-        return type == 'nfs'
-    handles = staticmethod(handles)
-
-
-    def load(self, sr_uuid):
-        self.ops_exclusive = FileSR.OPS_EXCLUSIVE
-        self.lock = Lock(vhdutil.LOCK_TYPE_SR, self.uuid)
-        self.sr_vditype = SR.DEFAULT_TAP
-        self.driver_config = DRIVER_CONFIG
-        if not self.dconf.has_key('server'):
-            raise xs_errors.XenError('ConfigServerMissing')
-        self.remoteserver = self.dconf['server']
-        self.path = os.path.join(SR.MOUNT_BASE, sr_uuid)
-
-        # Test for the optional 'nfsoptions' dconf attribute
-        self.transport = DEFAULT_TRANSPORT
-        if self.dconf.has_key('useUDP') and self.dconf['useUDP'] == 'true':
-            self.transport = "udp"
-
-
-    def validate_remotepath(self, scan):
-        if not self.dconf.has_key('serverpath'):
-            if scan:
-                try:
-                    self.scan_exports(self.dconf['server'])
-                except:
-                    pass
-            raise xs_errors.XenError('ConfigServerPathMissing')
-        if not self._isvalidpathstring(self.dconf['serverpath']):
-            raise xs_errors.XenError('ConfigServerPathBad', \
-                  opterr='serverpath is %s' % self.dconf['serverpath'])
-
-    def check_server(self):
-        try:
-            nfs.check_server_tcp(self.remoteserver)
-        except nfs.NfsException, exc:
-            raise xs_errors.XenError('NFSVersion',
-                                     opterr=exc.errstr)
-
-
-    def mount(self, mountpoint, remotepath):
-        try:
-            nfs.soft_mount(mountpoint, self.remoteserver, remotepath, self.transport)
-        except nfs.NfsException, exc:
-            raise xs_errors.XenError('NFSMount', opterr=exc.errstr)
-
-
-    def attach(self, sr_uuid):
-        self.validate_remotepath(False)
-        #self.remotepath = os.path.join(self.dconf['serverpath'], sr_uuid)
-        self.remotepath = self.dconf['serverpath']
-        util._testHost(self.dconf['server'], NFSPORT, 'NFSTarget')
-        self.mount_remotepath(sr_uuid)
-
-
-    def mount_remotepath(self, sr_uuid):
-        if not self._checkmount():
-            self.check_server()
-            self.mount(self.path, self.remotepath)
-
-        return super(NFSSR, self).attach(sr_uuid)
-
-
-    def probe(self):
-        # Verify NFS target and port
-        util._testHost(self.dconf['server'], NFSPORT, 'NFSTarget')
-        
-        self.validate_remotepath(True)
-        self.check_server()
-
-        temppath = os.path.join(SR.MOUNT_BASE, PROBE_MOUNTPOINT)
-
-        self.mount(temppath, self.dconf['serverpath'])
-        try:
-            return nfs.scan_srlist(temppath)
-        finally:
-            try:
-                nfs.unmount(temppath, True)
-            except:
-                pass
-
-
-    def detach(self, sr_uuid):
-        """Detach the SR: Unmounts and removes the mountpoint"""
-        if not self._checkmount():
-            return
-        util.SMlog("Aborting GC/coalesce")
-        cleanup.abort(self.uuid)
-
-        # Change directory to avoid unmount conflicts
-        os.chdir(SR.MOUNT_BASE)
-
-        try:
-            nfs.unmount(self.path, True)
-        except nfs.NfsException, exc:
-            raise xs_errors.XenError('NFSUnMount', opterr=exc.errstr)
-
-        return super(NFSSR, self).detach(sr_uuid)
-        
-
-    def create(self, sr_uuid, size):
-        util._testHost(self.dconf['server'], NFSPORT, 'NFSTarget')
-        self.validate_remotepath(True)
-        if self._checkmount():
-            raise xs_errors.XenError('NFSAttached')
-
-        # Set the target path temporarily to the base dir
-        # so that we can create the target SR directory
-        self.remotepath = self.dconf['serverpath']
-        try:
-            self.mount_remotepath(sr_uuid)
-        except Exception, exn:
-            try:
-                os.rmdir(self.path)
-            except:
-                pass
-            raise exn
-
-        #newpath = os.path.join(self.path, sr_uuid)
-        #if util.ioretry(lambda: util.pathexists(newpath)):
-        #    if len(util.ioretry(lambda: util.listdir(newpath))) != 0:
-        #        self.detach(sr_uuid)
-        #        raise xs_errors.XenError('SRExists')
-        #else:
-        #    try:
-        #        util.ioretry(lambda: util.makedirs(newpath))
-        #    except util.CommandException, inst:
-        #        if inst.code != errno.EEXIST:
-        #            self.detach(sr_uuid)
-        #            raise xs_errors.XenError('NFSCreate', 
-        #                opterr='remote directory creation error is %d' 
-        #                % inst.code)
-        self.detach(sr_uuid)
-
-    def delete(self, sr_uuid):
-        # try to remove/delete non VDI contents first
-        super(NFSSR, self).delete(sr_uuid)
-        try:
-            if self._checkmount():
-                self.detach(sr_uuid)
-
-            # Set the target path temporarily to the base dir
-            # so that we can remove the target SR directory
-            self.remotepath = self.dconf['serverpath']
-            self.mount_remotepath(sr_uuid)
-            newpath = os.path.join(self.path, sr_uuid)
-
-            if util.ioretry(lambda: util.pathexists(newpath)):
-                util.ioretry(lambda: os.rmdir(newpath))
-            self.detach(sr_uuid)
-        except util.CommandException, inst:
-            self.detach(sr_uuid)
-            if inst.code != errno.ENOENT:
-                raise xs_errors.XenError('NFSDelete')
-
-    def vdi(self, uuid, loadLocked = False):
-        if not loadLocked:
-            return NFSFileVDI(self, uuid)
-        return NFSFileVDI(self, uuid)
-    
-    def _checkmount(self):
-        return util.ioretry(lambda: util.pathexists(self.path)) \
-               and util.ioretry(lambda: util.ismount(self.path))
-
-    def scan_exports(self, target):
-        util.SMlog("scanning2 (target=%s)" % target)
-        dom = nfs.scan_exports(target)
-        print >>sys.stderr,dom.toprettyxml()
-
-class NFSFileVDI(FileSR.FileVDI):
-    def attach(self, sr_uuid, vdi_uuid):
-        if self.sr.srcmd.params.has_key("vdi_ref"):
-            try:
-                vdi_ref = self.sr.srcmd.params['vdi_ref']
-                self.session.xenapi.VDI.remove_from_xenstore_data(vdi_ref, \
-                        "vdi-type")
-                self.session.xenapi.VDI.remove_from_xenstore_data(vdi_ref, \
-                        "storage-type")
-                self.session.xenapi.VDI.add_to_xenstore_data(vdi_ref, \
-                        "storage-type", "nfs")
-            except:
-                util.logException("NFSSR:attach")
-                pass
-
-        return super(NFSFileVDI, self).attach(sr_uuid, vdi_uuid)
-
-    def generate_config(self, sr_uuid, vdi_uuid):
-        util.SMlog("NFSFileVDI.generate_config")
-        if not util.pathexists(self.path):
-                raise xs_errors.XenError('VDIUnavailable')
-        resp = {}
-        resp['device_config'] = self.sr.dconf
-        resp['sr_uuid'] = sr_uuid
-        resp['vdi_uuid'] = vdi_uuid
-        resp['command'] = 'vdi_attach_from_config'
-        # Return the 'config' encoded within a normal XMLRPC response so that
-        # we can use the regular response/error parsing code.
-        config = xmlrpclib.dumps(tuple([resp]), "vdi_attach_from_config")
-        return xmlrpclib.dumps((config,), "", True)
-
-    def attach_from_config(self, sr_uuid, vdi_uuid):
-        """Used for HA State-file only. Will not just attach the VDI but
-        also start a tapdisk on the file"""
-        util.SMlog("NFSFileVDI.attach_from_config")
-        try:
-            if not util.pathexists(self.sr.path):
-                self.sr.attach(sr_uuid)
-        except:
-            util.logException("NFSFileVDI.attach_from_config")
-            raise xs_errors.XenError('SRUnavailable', \
-                        opterr='Unable to attach from config')
-
-
-if __name__ == '__main__':
-    SRCommand.run(NFSSR, DRIVER_INFO)
-else:
-    SR.registerSR(NFSSR)

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/xenserver60/patch
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver60/patch b/scripts/vm/hypervisor/xenserver/xenserver60/patch
index 04fea8f..ba3470e 100644
--- a/scripts/vm/hypervisor/xenserver/xenserver60/patch
+++ b/scripts/vm/hypervisor/xenserver/xenserver60/patch
@@ -35,12 +35,11 @@
 # /etc/logrotate.d - All cloud log rotation configuration files.  Every file placed here must start with "cloud-".
 #
 # All log files should be placed in /var/log/cloud
-NFSSR.py=/opt/xensource/sm
 cloud-plugin-generic=..,0755,/etc/xapi.d/plugins
 xen-ovs-vif-flows.rules=..,0644,/etc/udev/rules.d
 ovs-vif-flows.py=..,0755,/etc/xapi.d/plugins
 cloud-plugins.conf=..,0644,/etc/xensource
-cloud-plugin-lib.py=..,0755,/etc/xapi.d/plugins
+cloud_plugin_lib.py=..,0755,/etc/xapi.d/plugins
 cloud-plugin-ovstunnel=..,0755,/etc/xapi.d/plugins
 cloud-plugin-snapshot=..,0755,/etc/xapi.d/plugins
 systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5d13a07d/scripts/vm/hypervisor/xenserver/xenserver62/patch
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xenserver62/patch b/scripts/vm/hypervisor/xenserver/xenserver62/patch
index e8a4ea2..f32f183 100644
--- a/scripts/vm/hypervisor/xenserver/xenserver62/patch
+++ b/scripts/vm/hypervisor/xenserver/xenserver62/patch
@@ -37,7 +37,7 @@
 cloud-plugin-generic=..,0755,/etc/xapi.d/plugins
 xen-ovs-vif-flows.rules=..,0644,/etc/udev/rules.d
 ovs-vif-flows.py=..,0755,/etc/xapi.d/plugins
-cloud-plugin-lib.py=..,0755,/etc/xapi.d/plugins
+cloud_plugin-lib.py=..,0755,/etc/xapi.d/plugins
 cloud-plugin-ovstunnel=..,0755,/etc/xapi.d/plugins
 cloud-plugin-snapshot=..,0755,/etc/xapi.d/plugins
 cloud-plugin-swiftxen=..,0755,/etc/xapi.d/plugins


[7/8] git commit: updated refs/heads/4.2-workplace to 5d13a07

Posted by ah...@apache.org.
Merged vmops and vmopspremium.  Rename all xapi plugins to start with cloud-plugin-.  Rename vmops to cloud-plugin-generic.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/f2bb1ace
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/f2bb1ace
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/f2bb1ace

Branch: refs/heads/4.2-workplace
Commit: f2bb1aceed52e64c9f3cacc81c9bb0cea17b0e73
Parents: 815b11f
Author: Alex Huang <al...@citrix.com>
Authored: Fri Nov 15 04:33:23 2013 -0800
Committer: Alex Huang <al...@citrix.com>
Committed: Fri Nov 15 04:33:23 2013 -0800

----------------------------------------------------------------------
 .../xen/resource/CitrixResourceBase.java        |  166 +-
 .../hypervisor/xen/resource/XcpOssResource.java |   18 +-
 .../xen/resource/XenServer56Resource.java       |   27 +-
 .../xen/resource/XenServerStorageProcessor.java |  117 +-
 .../hypervisor/xenserver/cloud-plugin-generic   | 1768 ++++++++++++++++++
 .../vm/hypervisor/xenserver/cloud-plugin-lib.py |  221 +++
 .../hypervisor/xenserver/cloud-plugin-ovs-pvlan |  148 ++
 .../hypervisor/xenserver/cloud-plugin-ovstunnel |  261 +++
 .../vm/hypervisor/xenserver/cloud-plugin-s3xen  |  428 +++++
 .../hypervisor/xenserver/cloud-plugin-snapshot  |  597 ++++++
 .../hypervisor/xenserver/cloud-plugin-swiftxen  |   97 +
 .../vm/hypervisor/xenserver/cloud-plugins.conf  |   21 +
 .../xenserver/cloud-prepare-upgrade.sh          |    2 +-
 scripts/vm/hypervisor/xenserver/cloudlog        |   12 +
 .../xenserver/cloudstack_pluginlib.py           |  221 ---
 .../xenserver/cloudstack_plugins.conf           |   21 -
 scripts/vm/hypervisor/xenserver/cloudstacklog   |   12 -
 .../create_privatetemplate_from_snapshot.sh     |    2 +-
 scripts/vm/hypervisor/xenserver/launch_hb.sh    |    4 +-
 .../vm/hypervisor/xenserver/mockxcpplugin.py    |   66 -
 scripts/vm/hypervisor/xenserver/ovs-pvlan       |  148 --
 .../vm/hypervisor/xenserver/ovs-vif-flows.py    |    2 +-
 scripts/vm/hypervisor/xenserver/ovstunnel       |  261 ---
 scripts/vm/hypervisor/xenserver/s3xen           |  428 -----
 .../xenserver/setup_heartbeat_file.sh           |    2 +-
 .../vm/hypervisor/xenserver/setupxenserver.sh   |    2 +-
 scripts/vm/hypervisor/xenserver/storagePlugin   |   71 -
 scripts/vm/hypervisor/xenserver/swiftxen        |   97 -
 .../vm/hypervisor/xenserver/upgrade_snapshot.sh |    2 +-
 scripts/vm/hypervisor/xenserver/vmops           | 1648 ----------------
 scripts/vm/hypervisor/xenserver/vmopsSnapshot   |  597 ------
 scripts/vm/hypervisor/xenserver/vmopspremium    |  150 --
 .../xenserver/xcposs/cloud-plugin-generic       | 1596 ++++++++++++++++
 .../xenserver/xcposs/cloud-plugin-snapshot      |  601 ++++++
 scripts/vm/hypervisor/xenserver/xcposs/patch    |   79 +-
 scripts/vm/hypervisor/xenserver/xcposs/vmops    | 1480 ---------------
 .../hypervisor/xenserver/xcposs/vmopsSnapshot   |  601 ------
 .../vm/hypervisor/xenserver/xcposs/vmopspremium |  146 --
 scripts/vm/hypervisor/xenserver/xcpserver/patch |   77 +-
 scripts/vm/hypervisor/xenserver/xenheartbeat.sh |    2 +-
 .../vm/hypervisor/xenserver/xenserver56/patch   |   78 +-
 .../hypervisor/xenserver/xenserver56fp1/patch   |   78 +-
 .../vm/hypervisor/xenserver/xenserver60/patch   |   96 +-
 .../create_privatetemplate_from_snapshot.sh     |  138 ++
 .../vm/hypervisor/xenserver/xenserver62/patch   |   98 +-
 .../xenserver/xenserver62/upgrade_snapshot.sh   |  133 ++
 46 files changed, 6472 insertions(+), 6348 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
index 94e0f59..c27df61 100644
--- a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
+++ b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
@@ -44,13 +44,12 @@ import java.util.Queue;
 import java.util.Random;
 import java.util.Set;
 import java.util.UUID;
+import java.util.concurrent.TimeoutException;
 
 import javax.ejb.Local;
 import javax.naming.ConfigurationException;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
-import java.util.concurrent.TimeoutException;
-
 
 import org.apache.commons.codec.binary.Base64;
 import org.apache.log4j.Logger;
@@ -431,7 +430,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
     protected boolean pingdomr(Connection conn, String host, String port) {
         String status;
-        status = callHostPlugin(conn, "vmops", "pingdomr", "host", host, "port", port);
+        status = callHostPlugin(conn, "cloud-plugin-generic", "pingdomr", "host", host, "port", port);
 
         if (status == null || status.isEmpty()) {
             return false;
@@ -678,10 +677,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                 long utilization = 0; // max CPU cap, default is unlimited
                 utilization = (int) ((speed * 0.99 * vmSpec.getCpus()) / _host.speed * 100);
                 //vm.addToVCPUsParamsLive(conn, "cap", Long.toString(utilization)); currently xenserver doesnot support Xapi to add VCPUs params live.
-                callHostPlugin(conn, "vmops", "add_to_VCPUs_params_live", "key", "cap", "value", Long.toString(utilization), "vmname", vmSpec.getName() );
+                callHostPlugin(conn, "cloud-plugin-generic", "add_to_VCPUs_params_live", "key", "cap", "value", Long.toString(utilization), "vmname", vmSpec.getName());
             }
             //vm.addToVCPUsParamsLive(conn, "weight", Integer.toString(cpuWeight));
-            callHostPlugin(conn, "vmops", "add_to_VCPUs_params_live", "key", "weight", "value", Integer.toString(cpuWeight), "vmname", vmSpec.getName() );
+            callHostPlugin(conn, "cloud-plugin-generic", "add_to_VCPUs_params_live", "key", "weight", "value", Integer.toString(cpuWeight), "vmname", vmSpec.getName());
         }
     }
 
@@ -816,7 +815,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             String vmName, String oldVmUuid, Boolean snapshotMemory, String hostUUID)
             throws XenAPIException, XmlRpcException {
  
-        String results = callHostPluginAsync(conn, "vmopsSnapshot",
+        String results = callHostPluginAsync(conn, "cloud-plugin-snapshot",
                 "revert_memory_snapshot", 10 * 60 * 1000, "snapshotUUID",
                 vmSnapshot.getUuid(conn), "vmName", vmName, "oldVmUuid",
                 oldVmUuid, "snapshotMemory", snapshotMemory.toString(), "hostUUID", hostUUID);
@@ -998,7 +997,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             if (!configured) {
                 // Plug dom0 vif only if not done before for network and host
                 enableXenServerNetwork(conn, nw, nwName, "tunnel network for account " + key);
-                String result = callHostPlugin(conn, "ovstunnel", "setup_ovs_bridge", "bridge", bridge,
+                String result = callHostPlugin(conn, "cloud-plugin-ovstunnel", "setup_ovs_bridge", "bridge", bridge,
                         "key", String.valueOf(key),
                         "xs_nw_uuid", nw.getUuid(conn),
                         "cs_host_id", ((Long)hostId).toString());
@@ -1021,7 +1020,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         try {
             Network nw = findOrCreateTunnelNetwork(conn, key);
             String bridge = nw.getBridge(conn);
-            String result = callHostPlugin(conn, "ovstunnel", "destroy_ovs_bridge", "bridge", bridge);
+            String result = callHostPlugin(conn, "cloud-plugin-ovstunnel", "destroy_ovs_bridge", "bridge", bridge);
             String[] res = result.split(":");
             if (res.length != 2 || !res[0].equalsIgnoreCase("SUCCESS")) {
                 //TODO: Should make this error not fatal?
@@ -1627,7 +1626,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     	
     	String result = null;
     	if (cmd.getType() == PvlanSetupCommand.Type.DHCP) {
-    		result = callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-dhcp", "op", op, "nw-label", nwNameLabel,
+            result = callHostPlugin(conn, "cloud-plugin-ovs-pvlan", "setup-pvlan-dhcp", "op", op, "nw-label", nwNameLabel,
     				"primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "dhcp-name", dhcpName,
     				"dhcp-ip", dhcpIp, "dhcp-mac", dhcpMac);
     		if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
@@ -1637,7 +1636,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     			s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
     		}
     	} else if (cmd.getType() == PvlanSetupCommand.Type.VM) {
-    		result = callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-vm", "op", op, "nw-label", nwNameLabel,
+            result = callHostPlugin(conn, "cloud-plugin-ovs-pvlan", "setup-pvlan-vm", "op", op, "nw-label", nwNameLabel,
     				"primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "vm-mac", vmMac);
     		if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
     			s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
@@ -1728,7 +1727,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                         }
                     }
                     if (secGrpEnabled) {
-                        result = callHostPlugin(conn, "vmops", "default_network_rules_systemvm", "vmName", vmName);
+                        result = callHostPlugin(conn, "cloud-plugin-generic", "default_network_rules_systemvm", "vmName", vmName);
                         if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                             s_logger.warn("Failed to program default network rules for " + vmName);
                         } else {
@@ -1753,7 +1752,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                             } else {
                                 secIpsStr = "0:";
                             }
-                            result = callHostPlugin(conn, "vmops", "default_network_rules", "vmName", vmName, "vmIP", nic.getIp(), "vmMAC", nic.getMac(), "vmID", Long.toString(vmSpec.getId()), "secIps", secIpsStr);
+                            result = callHostPlugin(conn, "cloud-plugin-generic", "default_network_rules", "vmName", vmName, "vmIP", nic.getIp(), "vmMAC", nic.getMac(), "vmID",
+                                Long.toString(vmSpec.getId()), "secIps", secIpsStr);
 
                             if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                                 s_logger.warn("Failed to program default network rules for " + vmName+" on nic with ip:"+nic.getIp()+" mac:"+nic.getMac());
@@ -1790,7 +1790,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
     private boolean doPingTest(Connection conn, final String computingHostIp) {
         String args = "-h " + computingHostIp;
-        String result = callHostPlugin(conn, "vmops", "pingtest", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "pingtest", "args", args);
         if (result == null || result.isEmpty()) {
             return false;
         }
@@ -1803,7 +1803,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
     private boolean doPingTest(Connection conn, final String domRIp, final String vmIp) {
         String args = "-i " + domRIp + " -p " + vmIp;
-        String result = callHostPlugin(conn, "vmops", "pingtest", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "pingtest", "args", args);
         if (result == null || result.isEmpty()) {
             return false;
         }
@@ -1833,7 +1833,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         for (String ip : cmd.getVpnIps()) {
             args += " " + ip;
         }
-        String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
         if (result == null || result.isEmpty()) {
             return new CheckS2SVpnConnectionsAnswer(cmd, false, "CheckS2SVpnConneciontsCommand failed");
         }
@@ -1843,7 +1843,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     private CheckRouterAnswer execute(CheckRouterCommand cmd) {
         Connection conn = getConnection();
         String args = "checkrouter.sh " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
-        String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
         if (result == null || result.isEmpty()) {
             return new CheckRouterAnswer(cmd, "CheckRouterCommand failed");
         }
@@ -1853,7 +1853,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     private GetDomRVersionAnswer execute(GetDomRVersionCmd cmd) {
         Connection conn = getConnection();
         String args = "get_template_version.sh " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
-        String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
         if (result == null || result.isEmpty()) {
             return new GetDomRVersionAnswer(cmd, "getDomRVersionCmd failed");
         }
@@ -1867,7 +1867,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     private Answer execute(BumpUpPriorityCommand cmd) {
         Connection conn = getConnection();
         String args = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
-        String result = callHostPlugin(conn, "vmops", "bumpUpPriority", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "bumpUpPriority", "args", args);
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "BumpUpPriorityCommand failed");
         }
@@ -1917,7 +1917,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args.append(" -r ").append(rule.getDstIp());
             args.append(" -d ").append(rule.getStringDstPortRange());
 
-            String result = callHostPlugin(conn, "vmops", "setFirewallRule", "args", args.toString());
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "setFirewallRule", "args", args.toString());
 
             if (result == null || result.isEmpty()) {
                 results[i++] = "Failed";
@@ -1942,7 +1942,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += rule.revoked() ? " -D" : " -A";
             args += " -l " + rule.getSrcIp();
             args += " -r " + rule.getDstIp();
-            String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args.toString());
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args.toString());
 
             if (result == null || result.isEmpty()) {
                 results[i++] = "Failed";
@@ -1980,7 +1980,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args.append(" -d ").append(rule.getStringSrcPortRange());
             args.append(" -G ");
 
-            String result = callHostPlugin(conn, "vmops", "setFirewallRule", "args", args.toString());
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "setFirewallRule", "args", args.toString());
 
             if (result == null || result.isEmpty()) {
                 results[i++] = "Failed";
@@ -2009,7 +2009,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             tmpCfgFileContents += "\n";
         }
         String tmpCfgFilePath = "/etc/haproxy/haproxy.cfg.new";
-        String result = callHostPlugin(conn, "vmops", "createFileInDomr", "domrip", routerIp, "filepath", tmpCfgFilePath, "filecontents", tmpCfgFileContents);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "createFileInDomr", "domrip", routerIp, "filepath", tmpCfgFilePath, "filecontents", tmpCfgFileContents);
 
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "LoadBalancerConfigCommand failed to create HA proxy cfg file.");
@@ -2051,7 +2051,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += " -s " + sb.toString();
         }
 
-        result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
 
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "LoadBalancerConfigCommand failed");
@@ -2067,7 +2067,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         for (IpAliasTO ipaliasto : ipAliasTOs) {
             args = args + ipaliasto.getAlias_count()+":"+ipaliasto.getRouterip()+":"+ipaliasto.getNetmask()+"-";
         }
-        String result = callHostPlugin(conn, "vmops", "createipAlias", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "createipAlias", "args", args);
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "CreateIPAliasCommand failed\n");
         }
@@ -2089,7 +2089,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         for (IpAliasTO ipAliasTO : activeIpAliasTOs) {
             args = args + ipAliasTO.getAlias_count()+":"+ipAliasTO.getRouterip()+":"+ipAliasTO.getNetmask()+"-";
         }
-        String result = callHostPlugin(conn, "vmops", "deleteipAlias", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "deleteipAlias", "args", args);
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "DeleteipAliasCommand failed\n");
         }
@@ -2106,7 +2106,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args = args + dhcpTo.getRouterIp()+":"+dhcpTo.getGateway()+":"+dhcpTo.getNetmask()+":"+dhcpTo.getStartIpOfSubnet()+"-";
         }
 
-        String result = callHostPlugin(conn, "vmops", "configdnsmasq", "routerip", routerIp, "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "configdnsmasq", "routerip", routerIp, "args", args);
 
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "DnsMasqconfigCommand failed");
@@ -2137,7 +2137,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             tmpCfgFileContents += "\n";
         }
 
-        String result = callHostPlugin(conn, "vmops", "createFile", "filepath", tmpCfgFilePath, "filecontents", tmpCfgFileContents);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "createFile", "filepath", tmpCfgFilePath, "filecontents", tmpCfgFileContents);
 
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "LoadBalancerConfigCommand failed to create HA proxy cfg file.");
@@ -2178,13 +2178,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += " -s " + sb.toString();
         }
 
-        result = callHostPlugin(conn, "vmops", "setLoadBalancerRule", "args", args);
+        result = callHostPlugin(conn, "cloud-plugin-generic", "setLoadBalancerRule", "args", args);
 
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "LoadBalancerConfigCommand failed");
         }
 
-        callHostPlugin(conn, "vmops", "deleteFile", "filepath", tmpCfgFilePath);
+        callHostPlugin(conn, "cloud-plugin-generic", "deleteFile", "filepath", tmpCfgFilePath);
 
         return new Answer(cmd);
     }
@@ -2217,7 +2217,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         	args += " -z";
         }
         
-        String result = callHostPlugin(conn, "vmops", "saveDhcpEntry", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "saveDhcpEntry", "args", args);
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "DhcpEntry failed");
         }
@@ -2238,7 +2238,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += " -d ";
             args += " -s " + cmd.getVpnServerIp();
         }
-        String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "Configure VPN failed");
         }
@@ -2254,7 +2254,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             } else {
                 args += " -u " + userpwd.getUsernamePassword();
             }
-            String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
             if (result == null || result.isEmpty()) {
                 return new Answer(cmd, false, "Configure VPN user failed for user " + userpwd.getUsername());
             }
@@ -2273,7 +2273,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
         String args = "vmdata.py " + routerPrivateIpAddress + " -d " + json;
 
-        String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
 
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "vm_data failed");
@@ -2292,7 +2292,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         String args = "savepassword.sh " + routerPrivateIPAddress;
         args += " -v " + vmIpAddress;
         args += " -p " + password;
-        String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
 
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "savePassword failed");
@@ -2390,14 +2390,14 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             }
 
 
-            String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
             if (result == null || result.isEmpty()) {
                 throw new InternalErrorException("Xen plugin \"ipassoc\" failed.");
             }
 
             if (!add) {
                 args += " -d";
-                String zeroIpsRes = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+                String zeroIpsRes = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
                 if (zeroIpsRes == null || zeroIpsRes.isEmpty()) {
                     //There are no ip address set on the interface. So unplug the interface
                     // If it is not unplugged then the interface is not resuable.
@@ -2474,7 +2474,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += " -n ";
             args += NetUtils.getSubNet(ip.getPublicIp(), ip.getVlanNetmask());
 
-            String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
             if (result == null || result.isEmpty()) {
                 throw new InternalErrorException("Xen plugin \"vpc_ipassoc\" failed.");
             }
@@ -2483,7 +2483,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                 snatArgs += " -l " + ip.getPublicIp();
                 snatArgs += " -c " + "eth" + correctVif.getDevice(conn);
 
-                result = callHostPlugin(conn, "vmops", "routerProxy", "args", snatArgs);
+                result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", snatArgs);
                 if (result == null || result.isEmpty()) {
                     throw new InternalErrorException("Xen plugin \"vpc_privateGateway\" failed.");
                 }
@@ -3068,7 +3068,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     }
 
     String upgradeSnapshot(Connection conn, String templatePath, String snapshotPath) {
-        String results = callHostPluginAsync(conn, "vmopspremium", "upgrade_snapshot",
+        String results = callHostPluginAsync(conn, "cloud-plugin-generic", "upgrade_snapshot",
                 2 * 60 * 60, "templatePath", templatePath, "snapshotPath", snapshotPath);
 
         if (results == null || results.isEmpty()) {
@@ -3088,7 +3088,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
     String createTemplateFromSnapshot(Connection conn, String templatePath, String snapshotPath, int wait) {
         String tmpltLocalDir = UUID.randomUUID().toString();
-        String results = callHostPluginAsync(conn, "vmopspremium", "create_privatetemplate_from_snapshot",
+        String results = callHostPluginAsync(conn, "cloud-plugin-generic", "create_privatetemplate_from_snapshot",
                 wait, "templatePath", templatePath, "snapshotPath", snapshotPath, "tmpltLocalDir", tmpltLocalDir);
         String errMsg = null;
         if (results == null || results.isEmpty()) {
@@ -3109,7 +3109,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     }
 
     boolean killCopyProcess(Connection conn, String nameLabel) {
-        String results = callHostPluginAsync(conn, "vmops", "kill_copy_process",
+        String results = callHostPluginAsync(conn, "cloud-plugin-generic", "kill_copy_process",
                 60, "namelabel", nameLabel);
         String errMsg = null;
         if (results == null || results.equals("false")) {
@@ -3140,7 +3140,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
     String copy_vhd_from_secondarystorage(Connection conn, String mountpoint, String sruuid, int wait) {
         String nameLabel = "cloud-" + UUID.randomUUID().toString();
-        String results = callHostPluginAsync(conn, "vmopspremium", "copy_vhd_from_secondarystorage",
+        String results = callHostPluginAsync(conn, "cloud-plugin-generic", "copy_vhd_from_secondarystorage",
                 wait, "mountpoint", mountpoint, "sruuid", sruuid, "namelabel", nameLabel);
         String errMsg = null;
         if (results == null || results.isEmpty()) {
@@ -3402,7 +3402,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         // Ignore the result of the callHostPlugin. Even if unmounting the
         // snapshots dir fails, let Ready command
         // succeed.
-        callHostPlugin(conn, "vmopsSnapshot", "unmountSnapshotsDir", "dcId", dcId.toString());
+        callHostPlugin(conn, "cloud-plugin-snapshot", "unmountSnapshotsDir", "dcId", dcId.toString());
 
         setupLinkLocalNetwork(conn);
         // try to destroy CD-ROM device for all system VMs on this host
@@ -3662,7 +3662,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     void forceShutdownVM(Connection conn, VM vm) {
         try {
             Long domId = vm.getDomid(conn);
-            callHostPlugin(conn, "vmopspremium", "forceShutdownVM", "domId", domId.toString());
+            callHostPlugin(conn, "cloud-plugin-generic", "forceShutdownVM", "domId", domId.toString());
             vm.powerStateReset(conn);
             vm.destroy(conn);
         } catch (Exception e) {
@@ -3823,7 +3823,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     boolean swiftDownload(Connection conn, SwiftTO swift, String container, String rfilename, String dir, String lfilename, Boolean remote) {
         String result = null;
         try {
-            result = callHostPluginAsync(conn, "swiftxen", "swift", 60 * 60,
+            result = callHostPluginAsync(conn, "cloud-plugin-swiftxen", "swift", 60 * 60,
                     "op", "download", "url", swift.getUrl(), "account", swift.getAccount(),
                     "username", swift.getUserName(), "key", swift.getKey(), "rfilename", rfilename,
                     "dir", dir, "lfilename", lfilename, "remote", remote.toString());
@@ -3839,7 +3839,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     boolean swiftUpload(Connection conn, SwiftTO swift, String container, String ldir, String lfilename, Boolean isISCSI, int wait) {
         String result = null;
         try {
-            result = callHostPluginAsync(conn, "swiftxen", "swift", wait,
+            result = callHostPluginAsync(conn, "cloud-plugin-swiftxen", "swift", wait,
                     "op", "upload", "url", swift.getUrl(), "account", swift.getAccount(),
                     "username", swift.getUserName(), "key", swift.getKey(), "container", container,
                     "ldir", ldir, "lfilename", lfilename, "isISCSI", isISCSI.toString());
@@ -3855,7 +3855,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     boolean swiftDelete(Connection conn, SwiftTO swift, String rfilename) {
         String result = null;
         try {
-            result = callHostPlugin(conn, "swiftxen", "swift",
+            result = callHostPlugin(conn, "cloud-plugin-swiftxen", "swift",
                     "op", "delete", "url", swift.getUrl(), "account", swift.getAccount(),
                     "username", swift.getUserName(), "key", swift.getKey(), "rfilename", rfilename);
             if( result != null && result.equals("true")) {
@@ -3893,7 +3893,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         // Each argument is put in a separate line for readability.
         // Using more lines does not harm the environment.
         String backupUuid = UUID.randomUUID().toString();
-        String results = callHostPluginAsync(conn, "vmopsSnapshot", "backupSnapshot", wait,
+        String results = callHostPluginAsync(conn, "cloud-plugin-snapshot", "backupSnapshot", wait,
                 "primaryStorageSRUuid", primaryStorageSRUuid, "dcId", dcId.toString(), "accountId", accountId.toString(),
                 "volumeId", volumeId.toString(), "secondaryStorageMountPath", secondaryStorageMountPath,
                 "snapshotUuid", snapshotUuid, "prevBackupUuid", prevBackupUuid, "backupUuid", backupUuid, "isISCSI", isISCSI.toString(), "secHostId", secHostId.toString());
@@ -4029,7 +4029,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                         vm.setAffinity(conn, vm.getResidentOn(conn));
 
                         if (_canBridgeFirewall) {
-                            String result = callHostPlugin(conn, "vmops", "destroy_network_rules_for_vm", "vmName", cmd
+                            String result = callHostPlugin(conn, "cloud-plugin-generic", "destroy_network_rules_for_vm", "vmName", cmd
                                     .getVmName());
                             if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                                 s_logger.warn("Failed to remove  network rules for vm " + cmd.getVmName());
@@ -4226,7 +4226,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     }
 
     protected boolean setIptables(Connection conn) {
-        String result = callHostPlugin(conn, "vmops", "setIptables");
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "setIptables");
         if (result == null || result.isEmpty()) {
             return false;
         }
@@ -4714,7 +4714,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     private HashMap<String, Pair<Long,Long>> syncNetworkGroups(Connection conn, long id) {
         HashMap<String, Pair<Long,Long>> states = new HashMap<String, Pair<Long,Long>>();
 
-        String result = callHostPlugin(conn, "vmops", "get_rule_logs_for_vms", "host_uuid", _host.uuid);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "get_rule_logs_for_vms", "host_uuid", _host.uuid);
         s_logger.trace("syncNetworkGroups: id=" + id + " got: " + result);
         String [] rulelogs = result != null ?result.split(";"): new String [0];
         for (String rulesforvm: rulelogs){
@@ -4901,7 +4901,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             }
 
             String brName = linkLocal.getBridge(conn);
-            callHostPlugin(conn, "vmops", "setLinkLocalIP", "brName", brName);
+            callHostPlugin(conn, "cloud-plugin-generic", "setLinkLocalIP", "brName", brName);
             _host.linkLocalNetwork = linkLocal.getUuid(conn);
 
         } catch (XenAPIException e) {
@@ -5050,7 +5050,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                 s_logger.debug("multipath is already set");
             }
             if (cmd.needSetup() ) {
-                result = callHostPlugin(conn, "vmops", "setup_iscsi", "uuid", _host.uuid);
+                result = callHostPlugin(conn, "cloud-plugin-generic", "setup_iscsi", "uuid", _host.uuid);
                 if (!result.contains("> DONE <")) {
                     s_logger.warn("Unable to setup iscsi: " + result);
                     return new SetupAnswer(cmd, result);
@@ -5160,7 +5160,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
             while (it.hasNext()) {
                 String tag = it.next();
-                if (tag.startsWith("vmops-version-")) {
+                if (tag.startsWith("cloud-version-")) {
                     if (tag.contains(version)) {
                         s_logger.info(logX(host, "Host " + hr.address + " is already setup."));
                         return false;
@@ -5247,7 +5247,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             } finally {
                 sshConnection.close();
             }
-            hr.tags.add("vmops-version-" + version);
+            hr.tags.add("cloud-version-" + version);
             host.setTags(conn, hr.tags);
             return true;
         } catch (XenAPIException e) {
@@ -5496,7 +5496,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
 
     protected String callHostPluginPremium(Connection conn, String cmd, String... params) {
-        return callHostPlugin(conn, "vmopspremium", cmd, params);
+        return callHostPlugin(conn, "cloud-plugin-generic", cmd, params);
     }
 
     protected String setupHeartbeatSr(Connection conn, SR sr, boolean force) throws XenAPIException, XmlRpcException {
@@ -5520,7 +5520,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                     break;
                 }
             }
-            result = callHostPluginThroughMaster(conn, "vmopspremium", "setup_heartbeat_sr", "host", _host.uuid,
+            result = callHostPluginThroughMaster(conn, "cloud-plugin-generic", "setup_heartbeat_sr", "host", _host.uuid,
                     "sr", srUuid);
             if (result == null || !result.split("#")[1].equals("0")) {
                 throw new CloudRuntimeException("Unable to setup heartbeat sr on SR " + srUuid + " due to " + result);
@@ -5590,7 +5590,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     }
 
     protected boolean can_bridge_firewall(Connection conn) {
-        return Boolean.valueOf(callHostPlugin(conn, "vmops", "can_bridge_firewall", "host_uuid", _host.uuid, "instance", _instance));
+        return Boolean.valueOf(callHostPlugin(conn, "cloud-plugin-generic", "can_bridge_firewall", "host_uuid", _host.uuid, "instance", _instance));
     }
 
 
@@ -5619,7 +5619,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             }
 
             String bridge = nw.getBridge(conn);
-            String result = callHostPlugin(conn, "ovstunnel", "destroy_tunnel", "bridge", bridge, "in_port", cmd.getInPortName());
+            String result = callHostPlugin(conn, "cloud-plugin-ovstunnel", "destroy_tunnel", "bridge", bridge, "in_port", cmd.getInPortName());
             if (result.equalsIgnoreCase("SUCCESS")) {
                 return new Answer(cmd, true, result);
             } else {
@@ -5649,7 +5649,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
             configureTunnelNetwork(conn, cmd.getNetworkId(), cmd.getFrom(), cmd.getKey());
             bridge = nw.getBridge(conn);
-            String result = callHostPlugin(conn, "ovstunnel", "create_tunnel", "bridge", bridge, "remote_ip", cmd.getRemoteIp(),
+            String result = callHostPlugin(conn, "cloud-plugin-ovstunnel", "create_tunnel", "bridge", bridge, "remote_ip", cmd.getRemoteIp(),
                     "key", cmd.getKey().toString(), "from", cmd.getFrom().toString(), "to", cmd.getTo().toString());
             String[] res = result.split(":");
             if (res.length == 2 && res[0].equalsIgnoreCase("SUCCESS")) {
@@ -5671,7 +5671,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         try {
             Network nw = setupvSwitchNetwork(conn);
             String bridge = nw.getBridge(conn);
-            String result = callHostPlugin(conn, "ovsgre", "ovs_delete_flow", "bridge", bridge,
+            String result = callHostPlugin(conn, "cloud-plugin-ovsgre", "ovs_delete_flow", "bridge", bridge,
                     "vmName", cmd.getVmName());
 
             if (result.equalsIgnoreCase("SUCCESS")) {
@@ -5691,7 +5691,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
     private List<Pair<String, Long>> ovsFullSyncStates() {
         Connection conn = getConnection();
-        String result = callHostPlugin(conn, "ovsgre", "ovs_get_vm_log", "host_uuid", _host.uuid);
+        String result = callHostPlugin(conn, "cloud-plugin-ovsgre", "ovs_get_vm_log", "host_uuid", _host.uuid);
         String [] logs = result != null ?result.split(";"): new String [0];
         List<Pair<String, Long>> states = new ArrayList<Pair<String, Long>>();
         for (String log: logs){
@@ -5723,7 +5723,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
              * none guest network nic. don't worry, it will fail silently at host
              * plugin side
              */
-            String result = callHostPlugin(conn, "ovsgre", "ovs_set_tag_and_flow", "bridge", bridge,
+            String result = callHostPlugin(conn, "cloud-plugin-ovsgre", "ovs_set_tag_and_flow", "bridge", bridge,
                     "vmName", cmd.getVmName(), "tag", cmd.getTag(),
                     "vlans", cmd.getVlans(), "seqno", cmd.getSeqNo());
             s_logger.debug("set flow for " + cmd.getVmName() + " " + result);
@@ -5783,7 +5783,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             Network nw = setupvSwitchNetwork(conn);
             bridge = nw.getBridge(conn);
 
-            String result = callHostPlugin(conn, "ovsgre", "ovs_create_gre", "bridge", bridge,
+            String result = callHostPlugin(conn, "cloud-plugin-ovsgre", "ovs_create_gre", "bridge", bridge,
                     "remoteIP", cmd.getRemoteIp(), "greKey", cmd.getKey(), "from",
                     Long.toString(cmd.getFrom()), "to", Long.toString(cmd.getTo()));
             String[] res = result.split(":");
@@ -5820,7 +5820,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                     SecurityGroupRuleAnswer.FailureReason.CANNOT_BRIDGE_FIREWALL);
         }
 
-        String result = callHostPlugin(conn, "vmops", "network_rules",
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "network_rules",
                 "vmName", cmd.getVmName(),
                 "vmIP", cmd.getGuestIp(),
                 "vmMAC", cmd.getGuestMac(),
@@ -7489,7 +7489,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                     "bucket", s3.getBucketName(), "key", key, "https",
                     s3.isHttps() != null ? s3.isHttps().toString()
                             : "null"));
-            final String result = callHostPluginAsync(connection, "s3xen",
+            final String result = callHostPluginAsync(connection, "cloud-plugin-s3xen",
                     "s3", wait,
                     parameters.toArray(new String[parameters.size()]));
 
@@ -7711,12 +7711,12 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
 
 
     protected boolean createSecondaryStorageFolder(Connection conn, String remoteMountPath, String newFolder) {
-        String result = callHostPlugin(conn, "vmopsSnapshot", "create_secondary_storage_folder", "remoteMountPath", remoteMountPath, "newFolder", newFolder);
+        String result = callHostPlugin(conn, "cloud-plugin-snapshot", "create_secondary_storage_folder", "remoteMountPath", remoteMountPath, "newFolder", newFolder);
         return (result != null);
     }
 
     protected boolean deleteSecondaryStorageFolder(Connection conn, String remoteMountPath, String folder) {
-        String details = callHostPlugin(conn, "vmopsSnapshot", "delete_secondary_storage_folder", "remoteMountPath", remoteMountPath, "folder", folder);
+        String details = callHostPlugin(conn, "cloud-plugin-snapshot", "delete_secondary_storage_folder", "remoteMountPath", remoteMountPath, "folder", folder);
         return (details != null && details.equals("1"));
     }
 
@@ -7730,7 +7730,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             checksum = "";
         }
 
-        String result = callHostPlugin(conn, "vmopsSnapshot", "post_create_private_template", "templatePath", templatePath, "templateFilename", tmpltFilename, "templateName", templateName, "templateDescription", templateDescription,
+        String result = callHostPlugin(conn, "cloud-plugin-snapshot", "post_create_private_template", "templatePath", templatePath, "templateFilename", tmpltFilename,
+            "templateName", templateName, "templateDescription", templateDescription,
                 "checksum", checksum, "size", String.valueOf(size), "virtualSize", String.valueOf(virtualSize), "templateId", String.valueOf(templateId));
 
         boolean success = false;
@@ -7749,7 +7750,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     }
 
     protected String getVhdParent(Connection conn, String primaryStorageSRUuid, String snapshotUuid, Boolean isISCSI) {
-        String parentUuid = callHostPlugin(conn, "vmopsSnapshot", "getVhdParent", "primaryStorageSRUuid", primaryStorageSRUuid,
+        String parentUuid = callHostPlugin(conn, "cloud-plugin-snapshot", "getVhdParent", "primaryStorageSRUuid", primaryStorageSRUuid,
                 "snapshotUuid", snapshotUuid, "isISCSI", isISCSI.toString());
 
         if (parentUuid == null || parentUuid.isEmpty() || parentUuid.equalsIgnoreCase("None")) {
@@ -7784,7 +7785,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
     protected String deleteSnapshotBackup(Connection conn, Long dcId, Long accountId, Long volumeId, String secondaryStorageMountPath, String backupUUID) {
 
         // If anybody modifies the formatting below again, I'll skin them
-        String result = callHostPlugin(conn, "vmopsSnapshot", "deleteSnapshotBackup", "backupUUID", backupUUID, "dcId", dcId.toString(), "accountId", accountId.toString(),
+        String result = callHostPlugin(conn, "cloud-plugin-snapshot", "deleteSnapshotBackup", "backupUUID", backupUUID, "dcId", dcId.toString(), "accountId", accountId.toString(),
                 "volumeId", volumeId.toString(), "secondaryStorageMountPath", secondaryStorageMountPath);
 
         return result;
@@ -7878,7 +7879,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             return new Answer(cmd, true, null);
         }
         Connection conn = getConnection();
-        String result = callHostPlugin(conn, "vmops","cleanup_rules", "instance", _instance);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "cleanup_rules", "instance", _instance);
         int numCleaned = Integer.parseInt(result);
         if (result == null || result.isEmpty() || (numCleaned < 0)) {
             s_logger.warn("Failed to cleanup rules for host " + _host.ip);
@@ -7994,7 +7995,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         boolean success = true;
         Connection conn = getConnection();
         if (cmd.getType() != VirtualMachine.Type.User) {
-            String result = callHostPlugin(conn, "vmops", "default_network_rules_systemvm", "vmName", cmd.getVmName());
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "default_network_rules_systemvm", "vmName", cmd.getVmName());
             if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                 success = false;
             }
@@ -8007,7 +8008,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
         boolean success = true;
         Connection conn = getConnection();
 
-        String result = callHostPlugin(conn, "vmops", "network_rules_vmSecondaryIp", "vmName", cmd.getVmName(), "vmMac", cmd.getVmMac(), "vmSecIp", cmd.getVmSecIp(), "action",
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "network_rules_vmSecondaryIp", "vmName", cmd.getVmName(), "vmMac", cmd.getVmMac(), "vmSecIp",
+            cmd.getVmSecIp(), "action",
                 cmd.getAction());
         if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
             success = false;
@@ -8050,7 +8052,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += " -a " + sb.toString();
         }
 
-        callResult = callHostPlugin(conn, "vmops", "setFirewallRule", "args", args);
+        callResult = callHostPlugin(conn, "cloud-plugin-generic", "setFirewallRule", "args", args);
 
         if (callResult == null || callResult.isEmpty()) {
             //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails
@@ -8348,7 +8350,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             if ( domainName != null && !domainName.isEmpty() ) {
                 args += " -e " + domainName;
             }
-            String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
             if (result == null || result.isEmpty()) {
                 return new SetupGuestNetworkAnswer(cmd, false, "creating guest network failed due to " + ((result == null)? "null":result));
             }
@@ -8421,7 +8423,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += " -N ";
             args += cmd.getPeerGuestCidrList();
         }
-        String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
         if (result == null || result.isEmpty()) {
             return new Answer(cmd, false, "Configure site to site VPN failed! ");
         }
@@ -8447,7 +8449,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += " -c ";
             args += "eth" + correctVif.getDevice(conn);
 
-            String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
             if (result == null || result.isEmpty()) {
                 throw new InternalErrorException("Xen plugin \"vpc_snat\" failed.");
             }
@@ -8489,7 +8491,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                 args += " -d " + "eth" + vif.getDevice(conn);
                 args += " -a " + sb.toString();
 
-                callResult = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+                callResult = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
                 if (callResult == null || callResult.isEmpty()) {
                     //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails
                     for (int i=0; i < results.length; i++) {
@@ -8504,7 +8506,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                 args += " -m " + Long.toString(NetUtils.getCidrSize(nic.getNetmask()));
                 args += " -a " + sb.toString();
 
-                callResult = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+                callResult = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
                 if (callResult == null || callResult.isEmpty()) {
                     //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails
                     for (int i=0; i < results.length; i++) {
@@ -8538,7 +8540,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             args += " -r " + rule.getDstIp();
             args += " -d " + rule.getStringDstPortRange().replace(":", "-");
 
-            String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args.toString());
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args.toString());
 
             if (result == null || result.isEmpty()) {
                 results[i++] = "Failed";
@@ -8565,7 +8567,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
             }
             String args = "vpc_staticroute.sh " + routerIp;
             args += " -a " + sb.toString();
-            callResult = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+            callResult = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
             if (callResult == null || callResult.isEmpty()) {
                 //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails
                 for (int i=0; i < results.length; i++) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XcpOssResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XcpOssResource.java b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XcpOssResource.java
index cb885c3..e53a911 100644
--- a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XcpOssResource.java
+++ b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XcpOssResource.java
@@ -27,6 +27,12 @@ import javax.ejb.Local;
 import org.apache.log4j.Logger;
 import org.apache.xmlrpc.XmlRpcException;
 
+import com.xensource.xenapi.Connection;
+import com.xensource.xenapi.Types;
+import com.xensource.xenapi.Types.XenAPIException;
+import com.xensource.xenapi.VBD;
+import com.xensource.xenapi.VDI;
+import com.xensource.xenapi.VM;
 
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.Command;
@@ -46,12 +52,6 @@ import com.cloud.storage.resource.StorageSubsystemCommandHandlerBase;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.script.Script;
 import com.cloud.vm.VirtualMachine;
-import com.xensource.xenapi.Connection;
-import com.xensource.xenapi.Types;
-import com.xensource.xenapi.Types.XenAPIException;
-import com.xensource.xenapi.VBD;
-import com.xensource.xenapi.VDI;
-import com.xensource.xenapi.VM;
 
 
 @Local(value=ServerResource.class)
@@ -91,7 +91,7 @@ public class XcpOssResource extends CitrixResourceBase {
     protected VBD createPatchVbd(Connection conn, String vmName, VM vm) throws XmlRpcException, XenAPIException {
         if (_host.localSRuuid != null) {
             //create an iso vdi on it
-            String result = callHostPlugin(conn, "vmops", "createISOVHD", "uuid", _host.localSRuuid);
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "createISOVHD", "uuid", _host.localSRuuid);
             if (result == null || result.equalsIgnoreCase("Failed")) {
                 throw new CloudRuntimeException("can not create systemvm vdi");
             }
@@ -157,7 +157,7 @@ public class XcpOssResource extends CitrixResourceBase {
                     publicIp = nic.getIp();
                 }
             }
-            callHostPlugin(conn, "vmops", "setDNATRule", "ip", publicIp, "port", "8443", "add", "true");
+            callHostPlugin(conn, "cloud-plugin-generic", "setDNATRule", "ip", publicIp, "port", "8443", "add", "true");
         }
 
         return answer;
@@ -169,7 +169,7 @@ public class XcpOssResource extends CitrixResourceBase {
         String vmName = cmd.getVmName();
         if (vmName.startsWith("v-")) {
             Connection conn = getConnection();
-            callHostPlugin(conn, "vmops", "setDNATRule", "add", "false");
+            callHostPlugin(conn, "cloud-plugin-generic", "setDNATRule", "add", "false");
         }
         return answer;
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java
index 24329e6..33a8796 100644
--- a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java
+++ b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java
@@ -11,7 +11,7 @@
 // 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 
+// KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
 package com.cloud.hypervisor.xen.resource;
@@ -20,10 +20,21 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
+
 import javax.ejb.Local;
+
 import org.apache.log4j.Logger;
 import org.apache.xmlrpc.XmlRpcException;
 
+import com.xensource.xenapi.Connection;
+import com.xensource.xenapi.Host;
+import com.xensource.xenapi.Network;
+import com.xensource.xenapi.PIF;
+import com.xensource.xenapi.Types.IpConfigurationMode;
+import com.xensource.xenapi.Types.XenAPIException;
+import com.xensource.xenapi.VLAN;
+import com.xensource.xenapi.VM;
+
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.CheckOnHostAnswer;
 import com.cloud.agent.api.CheckOnHostCommand;
@@ -36,14 +47,6 @@ import com.cloud.agent.api.StartupCommand;
 import com.cloud.resource.ServerResource;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.script.Script;
-import com.xensource.xenapi.Connection;
-import com.xensource.xenapi.Host;
-import com.xensource.xenapi.Network;
-import com.xensource.xenapi.PIF;
-import com.xensource.xenapi.Types.IpConfigurationMode;
-import com.xensource.xenapi.Types.XenAPIException;
-import com.xensource.xenapi.VLAN;
-import com.xensource.xenapi.VM;
 
 @Local(value = ServerResource.class)
 public class XenServer56Resource extends CitrixResourceBase {
@@ -142,7 +145,7 @@ public class XenServer56Resource extends CitrixResourceBase {
             args += vif;
         }
 
-        return callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+        return callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
     }
 
     protected NetworkUsageAnswer VPCNetworkUsage(NetworkUsageCommand cmd) {
@@ -169,7 +172,7 @@ public class XenServer56Resource extends CitrixResourceBase {
                 return new NetworkUsageAnswer(cmd, "success", 0L, 0L);
             }
 
-            String result = callHostPlugin(conn, "vmops", "routerProxy", "args", args);
+            String result = callHostPlugin(conn, "cloud-plugin-generic", "routerProxy", "args", args);
             if (option.equals("get") || option.equals("vpn")) {
                 long[] stats = new long[2];
                 if (result != null) {
@@ -208,7 +211,7 @@ public class XenServer56Resource extends CitrixResourceBase {
             return answer;
         } catch (Exception ex) {
             s_logger.warn("Failed to get network usage stats due to ", ex);
-            return new NetworkUsageAnswer(cmd, ex); 
+            return new NetworkUsageAnswer(cmd, ex);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServerStorageProcessor.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServerStorageProcessor.java b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServerStorageProcessor.java
index a3932e4..f5d5256 100644
--- a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServerStorageProcessor.java
+++ b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServerStorageProcessor.java
@@ -30,6 +30,22 @@ import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
+import org.apache.log4j.Logger;
+import org.apache.xmlrpc.XmlRpcException;
+
+import com.xensource.xenapi.Connection;
+import com.xensource.xenapi.Host;
+import com.xensource.xenapi.PBD;
+import com.xensource.xenapi.Pool;
+import com.xensource.xenapi.SR;
+import com.xensource.xenapi.Types;
+import com.xensource.xenapi.Types.BadServerResponse;
+import com.xensource.xenapi.Types.XenAPIException;
+import com.xensource.xenapi.VBD;
+import com.xensource.xenapi.VDI;
+import com.xensource.xenapi.VM;
+import com.xensource.xenapi.VMGuestMetrics;
+
 import org.apache.cloudstack.storage.command.AttachAnswer;
 import org.apache.cloudstack.storage.command.AttachCommand;
 import org.apache.cloudstack.storage.command.AttachPrimaryDataStoreAnswer;
@@ -46,8 +62,6 @@ import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
 import org.apache.cloudstack.storage.to.SnapshotObjectTO;
 import org.apache.cloudstack.storage.to.TemplateObjectTO;
 import org.apache.cloudstack.storage.to.VolumeObjectTO;
-import org.apache.log4j.Logger;
-import org.apache.xmlrpc.XmlRpcException;
 
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.CreateStoragePoolCommand;
@@ -70,18 +84,6 @@ import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.storage.encoding.DecodedDataObject;
 import com.cloud.utils.storage.encoding.DecodedDataStore;
 import com.cloud.utils.storage.encoding.Decoder;
-import com.xensource.xenapi.Connection;
-import com.xensource.xenapi.Host;
-import com.xensource.xenapi.PBD;
-import com.xensource.xenapi.Pool;
-import com.xensource.xenapi.SR;
-import com.xensource.xenapi.Types;
-import com.xensource.xenapi.Types.BadServerResponse;
-import com.xensource.xenapi.Types.XenAPIException;
-import com.xensource.xenapi.VBD;
-import com.xensource.xenapi.VDI;
-import com.xensource.xenapi.VM;
-import com.xensource.xenapi.VMGuestMetrics;
 
 public class XenServerStorageProcessor implements StorageProcessor {
     private static final Logger s_logger = Logger.getLogger(XenServerStorageProcessor.class);
@@ -89,7 +91,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
     private String BaseMountPointOnHost = "/var/run/cloud_mount";
 
     public XenServerStorageProcessor(CitrixResourceBase resource) {
-        this.hypervisorResource = resource;
+        hypervisorResource = resource;
     }
 
     public void setBaseMountPointOnHost(String baseMountPointOnHost) {
@@ -117,14 +119,14 @@ public class XenServerStorageProcessor implements StorageProcessor {
 
         String vmName = cmd.getVmName();
         try {
-            Connection conn = this.hypervisorResource.getConnection();
+            Connection conn = hypervisorResource.getConnection();
 
             VBD isoVBD = null;
 
             // Find the VM
-            VM vm = this.hypervisorResource.getVM(conn, vmName);
+            VM vm = hypervisorResource.getVM(conn, vmName);
             // Find the ISO VDI
-            VDI isoVDI = this.hypervisorResource.getIsoVDIByURL(conn, vmName, isoURL);
+            VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, vmName, isoURL);
 
             // Find the VM's CD-ROM VBD
             Set<VBD> vbds = vm.getVBDs(conn);
@@ -168,20 +170,20 @@ public class XenServerStorageProcessor implements StorageProcessor {
         DataTO data = disk.getData();
 
         try {
-            Connection conn = this.hypervisorResource.getConnection();
+            Connection conn = hypervisorResource.getConnection();
             // Look up the VDI
             VDI vdi = null;
 
             if (cmd.isManaged()) {
-                vdi = this.hypervisorResource.handleSrAndVdiAttach(cmd.get_iScsiName(), cmd.getStorageHost(),
+                vdi = hypervisorResource.handleSrAndVdiAttach(cmd.get_iScsiName(), cmd.getStorageHost(),
                         cmd.getChapInitiatorUsername(), cmd.getChapInitiatorPassword());
             }
             else {
-                vdi = this.hypervisorResource.mount(conn, null, null, data.getPath());
+                vdi = hypervisorResource.mount(conn, null, null, data.getPath());
             }
 
             // Look up the VM
-            VM vm = this.hypervisorResource.getVM(conn, vmName);
+            VM vm = hypervisorResource.getVM(conn, vmName);
             /* For HVM guest, if no pv driver installed, no attach/detach */
             boolean isHVM;
             if (vm.getPVBootloader(conn).equalsIgnoreCase("")) {
@@ -191,7 +193,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
             }
             VMGuestMetrics vgm = vm.getGuestMetrics(conn);
             boolean pvDrvInstalled = false;
-            if (!this.hypervisorResource.isRefNull(vgm) && vgm.getPVDriversUpToDate(conn)) {
+            if (!hypervisorResource.isRefNull(vgm) && vgm.getPVDriversUpToDate(conn)) {
                 pvDrvInstalled = true;
             }
             if (isHVM && !pvDrvInstalled) {
@@ -207,13 +209,13 @@ public class XenServerStorageProcessor implements StorageProcessor {
                     String msg = "Device 3 is reserved for CD-ROM, choose other device";
                     return new AttachAnswer(msg);
                 }
-                if(this.hypervisorResource.isDeviceUsed(conn, vm, deviceId)) {
+                if(hypervisorResource.isDeviceUsed(conn, vm, deviceId)) {
                     String msg = "Device " + deviceId + " is used in VM " + vmName;
                     return new AttachAnswer(msg);
                 }
                 diskNumber = deviceId.toString();
             } else {
-                diskNumber = this.hypervisorResource.getUnusedDeviceNum(conn, vm);
+                diskNumber = hypervisorResource.getUnusedDeviceNum(conn, vm);
             }
             // Create a new VBD
             VBD.Record vbdr = new VBD.Record();
@@ -265,13 +267,13 @@ public class XenServerStorageProcessor implements StorageProcessor {
         }
 
         try {
-            Connection conn = this.hypervisorResource.getConnection();
+            Connection conn = hypervisorResource.getConnection();
             // Find the VM
-            VM vm = this.hypervisorResource.getVM(conn, cmd.getVmName());
+            VM vm = hypervisorResource.getVM(conn, cmd.getVmName());
             String vmUUID = vm.getUuid(conn);
 
             // Find the ISO VDI
-            VDI isoVDI = this.hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), isoURL);
+            VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), isoURL);
 
             SR sr = isoVDI.getSR(conn);
 
@@ -294,7 +296,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
             }
 
             if (!sr.getNameLabel(conn).startsWith("XenServer Tools")) {
-                this.hypervisorResource.removeSR(conn, sr);
+                hypervisorResource.removeSR(conn, sr);
             }
 
             return new DettachAnswer(disk);
@@ -316,11 +318,11 @@ public class XenServerStorageProcessor implements StorageProcessor {
         DiskTO disk = cmd.getDisk();
         DataTO data = disk.getData();
         try {
-            Connection conn = this.hypervisorResource.getConnection();
+            Connection conn = hypervisorResource.getConnection();
             // Look up the VDI
-            VDI vdi = this.hypervisorResource.mount(conn, null, null, data.getPath());
+            VDI vdi = hypervisorResource.mount(conn, null, null, data.getPath());
             // Look up the VM
-            VM vm = this.hypervisorResource.getVM(conn, vmName);
+            VM vm = hypervisorResource.getVM(conn, vmName);
             /* For HVM guest, if no pv driver installed, no attach/detach */
             boolean isHVM;
             if (vm.getPVBootloader(conn).equalsIgnoreCase("")) {
@@ -330,7 +332,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
             }
             VMGuestMetrics vgm = vm.getGuestMetrics(conn);
             boolean pvDrvInstalled = false;
-            if (!this.hypervisorResource.isRefNull(vgm) && vgm.getPVDriversUpToDate(conn)) {
+            if (!hypervisorResource.isRefNull(vgm) && vgm.getPVDriversUpToDate(conn)) {
                 pvDrvInstalled = true;
             }
             if (isHVM && !pvDrvInstalled) {
@@ -356,10 +358,10 @@ public class XenServerStorageProcessor implements StorageProcessor {
             // Update the VDI's label to be "detached"
             vdi.setNameLabel(conn, "detached");
 
-            this.hypervisorResource.umount(conn, vdi);
+            hypervisorResource.umount(conn, vdi);
 
             if (cmd.isManaged()) {
-                this.hypervisorResource.handleSrAndVdiDetach(cmd.get_iScsiName());
+                hypervisorResource.handleSrAndVdiDetach(cmd.get_iScsiName());
             }
 
             return new DettachAnswer(disk);
@@ -565,13 +567,13 @@ public class XenServerStorageProcessor implements StorageProcessor {
                     }
                     if (target.equals(dc.get("target")) && targetiqn.equals(dc.get("targetIQN")) && lunid.equals(dc.get("lunid"))) {
                         throw new CloudRuntimeException("There is a SR using the same configuration target:" + dc.get("target") +  ",  targetIQN:"
-                                + dc.get("targetIQN")  + ", lunid:" + dc.get("lunid") + " for pool " + pool.getUuid() + "on host:" + this.hypervisorResource.getHost().uuid);
+                                + dc.get("targetIQN")  + ", lunid:" + dc.get("lunid") + " for pool " + pool.getUuid() + "on host:" + hypervisorResource.getHost().uuid);
                     }
                 }
                 deviceConfig.put("target", target);
                 deviceConfig.put("targetIQN", targetiqn);
 
-                Host host = Host.getByUuid(conn, this.hypervisorResource.getHost().uuid);
+                Host host = Host.getByUuid(conn, hypervisorResource.getHost().uuid);
                 Map<String, String> smConfig = new HashMap<String, String>();
                 String type = SRType.LVMOISCSI.toString();
                 String poolId = Long.toString(pool.getId());
@@ -644,7 +646,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
         }
     }
     protected Answer execute(CreateStoragePoolCommand cmd) {
-        Connection conn = this.hypervisorResource.getConnection();
+        Connection conn = hypervisorResource.getConnection();
         StorageFilerTO pool = cmd.getPool();
         try {
             if (pool.getType() == StoragePoolType.NetworkFilesystem) {
@@ -657,7 +659,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
             }
             return new Answer(cmd, true, "success");
         } catch (Exception e) {
-            String msg = "Catch Exception " + e.getClass().getName() + ", create StoragePool failed due to " + e.toString() + " on host:" + this.hypervisorResource.getHost().uuid + " pool: " + pool.getHost() + pool.getPath();
+            String msg = "Catch Exception " + e.getClass().getName() + ", create StoragePool failed due to " + e.toString() + " on host:" + hypervisorResource.getHost().uuid + " pool: " + pool.getHost() + pool.getPath();
             s_logger.warn(msg, e);
             return new Answer(cmd, false, msg);
         }
@@ -771,7 +773,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
 
     private String copy_vhd_from_secondarystorage(Connection conn, String mountpoint, String sruuid, int wait) {
         String nameLabel = "cloud-" + UUID.randomUUID().toString();
-        String results = hypervisorResource.callHostPluginAsync(conn, "vmopspremium", "copy_vhd_from_secondarystorage",
+        String results = hypervisorResource.callHostPluginAsync(conn, "cloud-plugin-generic", "copy_vhd_from_secondarystorage",
                 wait, "mountpoint", mountpoint, "sruuid", sruuid, "namelabel", nameLabel);
         String errMsg = null;
         if (results == null || results.isEmpty()) {
@@ -821,7 +823,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
     }
 
     protected String getVhdParent(Connection conn, String primaryStorageSRUuid, String snapshotUuid, Boolean isISCSI) {
-        String parentUuid = hypervisorResource.callHostPlugin(conn, "vmopsSnapshot", "getVhdParent", "primaryStorageSRUuid", primaryStorageSRUuid,
+        String parentUuid = hypervisorResource.callHostPlugin(conn, "cloud-plugin-snapshot", "getVhdParent", "primaryStorageSRUuid", primaryStorageSRUuid,
                 "snapshotUuid", snapshotUuid, "isISCSI", isISCSI.toString());
 
         if (parentUuid == null || parentUuid.isEmpty() || parentUuid.equalsIgnoreCase("None")) {
@@ -1031,7 +1033,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
     boolean swiftUpload(Connection conn, SwiftTO swift, String container, String ldir, String lfilename, Boolean isISCSI, int wait) {
         String result = null;
         try {
-            result = hypervisorResource.callHostPluginAsync(conn, "swiftxen", "swift", wait,
+            result = hypervisorResource.callHostPluginAsync(conn, "cloud-plugin-swiftxen", "swift", wait,
                     "op", "upload", "url", swift.getUrl(), "account", swift.getAccount(),
                     "username", swift.getUserName(), "key", swift.getKey(), "container", container,
                     "ldir", ldir, "lfilename", lfilename, "isISCSI", isISCSI.toString());
@@ -1047,7 +1049,8 @@ public class XenServerStorageProcessor implements StorageProcessor {
     protected String deleteSnapshotBackup(Connection conn, String localMountPoint, String path, String secondaryStorageMountPath, String backupUUID) {
 
         // If anybody modifies the formatting below again, I'll skin them
-        String result = hypervisorResource.callHostPlugin(conn, "vmopsSnapshot", "deleteSnapshotBackup", "backupUUID", backupUUID, "path", path, "secondaryStorageMountPath", secondaryStorageMountPath, "localMountPoint", localMountPoint);
+        String result = hypervisorResource.callHostPlugin(conn, "cloud-plugin-snapshot", "deleteSnapshotBackup", "backupUUID", backupUUID, "path", path,
+            "secondaryStorageMountPath", secondaryStorageMountPath, "localMountPoint", localMountPoint);
 
         return result;
     }
@@ -1087,7 +1090,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
                     iSCSIFlag.toString(), "bucket", s3.getBucketName(),
                     "key", key, "https", s3.isHttps() != null ? s3.isHttps().toString()
                             : "null", "maxSingleUploadSizeInBytes", String.valueOf(s3.getMaxSingleUploadSizeInBytes())));
-            final String result = hypervisorResource.callHostPluginAsync(connection, "s3xen",
+            final String result = hypervisorResource.callHostPluginAsync(connection, "cloud-plugin-s3xen",
                     "s3", wait,
                     parameters.toArray(new String[parameters.size()]));
 
@@ -1116,7 +1119,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
         // Each argument is put in a separate line for readability.
         // Using more lines does not harm the environment.
         String backupUuid = UUID.randomUUID().toString();
-        String results = hypervisorResource.callHostPluginAsync(conn, "vmopsSnapshot", "backupSnapshot", wait,
+        String results = hypervisorResource.callHostPluginAsync(conn, "cloud-plugin-snapshot", "backupSnapshot", wait,
                 "primaryStorageSRUuid", primaryStorageSRUuid, "path", path, "secondaryStorageMountPath", secondaryStorageMountPath,
                 "snapshotUuid", snapshotUuid, "prevBackupUuid", prevBackupUuid, "backupUuid", backupUuid, "isISCSI", isISCSI.toString(), "localMountPoint", localMountPoint);
         String errMsg = null;
@@ -1336,7 +1339,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
 
     @Override
     public Answer createTemplateFromVolume(CopyCommand cmd) {
-        Connection conn = this.hypervisorResource.getConnection();
+        Connection conn = hypervisorResource.getConnection();
         VolumeObjectTO volume = (VolumeObjectTO)cmd.getSrcTO();
         TemplateObjectTO template = (TemplateObjectTO)cmd.getDestTO();
         NfsTO destStore = (NfsTO)cmd.getDestTO().getDataStore();
@@ -1357,7 +1360,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
             URI uri = new URI(secondaryStoragePoolURL);
             secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
             installPath = template.getPath();
-            if( !this.hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
+            if( !hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
                 details = " Filed to create folder " + installPath + " in secondary storage";
                 s_logger.warn(details);
                 return new CopyCmdAnswer(details);
@@ -1366,10 +1369,10 @@ public class XenServerStorageProcessor implements StorageProcessor {
             VDI vol = getVDIbyUuid(conn, volumeUUID);
             // create template SR
             URI tmpltURI = new URI(secondaryStoragePoolURL + "/" + installPath);
-            tmpltSR = this.hypervisorResource.createNfsSRbyURI(conn, tmpltURI, false);
+            tmpltSR = hypervisorResource.createNfsSRbyURI(conn, tmpltURI, false);
 
             // copy volume to template SR
-            VDI tmpltVDI = this.hypervisorResource.cloudVDIcopy(conn, vol, tmpltSR, wait);
+            VDI tmpltVDI = hypervisorResource.cloudVDIcopy(conn, vol, tmpltSR, wait);
             // scan makes XenServer pick up VDI physicalSize
             tmpltSR.scan(conn);
             if (userSpecifiedName != null) {
@@ -1382,12 +1385,12 @@ public class XenServerStorageProcessor implements StorageProcessor {
             long physicalSize = tmpltVDI.getPhysicalUtilisation(conn);
             // create the template.properties file
             String templatePath = secondaryStorageMountPath + "/" + installPath;
-            result = this.hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId());
+            result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId());
             if (!result) {
                 throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + tmpltURI);
             }
             installPath = installPath + "/" + tmpltFilename;
-            this.hypervisorResource.removeSR(conn, tmpltSR);
+            hypervisorResource.removeSR(conn, tmpltSR);
             tmpltSR = null;
             TemplateObjectTO newTemplate = new TemplateObjectTO();
             newTemplate.setPath(installPath);
@@ -1399,10 +1402,10 @@ public class XenServerStorageProcessor implements StorageProcessor {
             return answer;
         } catch (Exception e) {
             if (tmpltSR != null) {
-                this.hypervisorResource.removeSR(conn, tmpltSR);
+                hypervisorResource.removeSR(conn, tmpltSR);
             }
             if ( secondaryStorageMountPath != null) {
-                this.hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
+                hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
             }
             details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString();
             s_logger.error(details, e);
@@ -1417,7 +1420,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
 
     @Override
     public Answer createVolumeFromSnapshot(CopyCommand cmd) {
-        Connection conn = this.hypervisorResource.getConnection();
+        Connection conn = hypervisorResource.getConnection();
         DataTO srcData = cmd.getSrcTO();
         SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData;
         DataTO destData = cmd.getDestTO();
@@ -1442,7 +1445,7 @@ public class XenServerStorageProcessor implements StorageProcessor {
             return new CopyCmdAnswer(details);
         }
         try {
-            SR primaryStorageSR = this.hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
+            SR primaryStorageSR = hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
             if (primaryStorageSR == null) {
                 throw new InternalErrorException("Could not create volume from snapshot because the primary Storage SR could not be created from the name label: "
                         + primaryStorageNameLabel);
@@ -1487,14 +1490,14 @@ public class XenServerStorageProcessor implements StorageProcessor {
         SnapshotObjectTO snapshot = (SnapshotObjectTO)cmd.getData();
         DataStoreTO store = snapshot.getDataStore();
         if (store.getRole() == DataStoreRole.Primary) {
-            Connection conn = this.hypervisorResource.getConnection();
+            Connection conn = hypervisorResource.getConnection();
             VDI snapshotVdi = getVDIbyUuid(conn, snapshot.getPath());
             if (snapshotVdi == null) {
                 return new Answer(null);
             }
             String errMsg = null;
             try {
-                this.deleteVDI(conn, snapshotVdi);
+                deleteVDI(conn, snapshotVdi);
             } catch (BadServerResponse e) {
                 s_logger.debug("delete snapshot failed:" + e.toString());
                 errMsg = e.toString();


[3/8] Merged vmops and vmopspremium. Rename all xapi plugins to start with cloud-plugin-. Rename vmops to cloud-plugin-generic.

Posted by ah...@apache.org.
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/vmopsSnapshot
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/vmopsSnapshot b/scripts/vm/hypervisor/xenserver/vmopsSnapshot
deleted file mode 100755
index 0f5fbc6..0000000
--- a/scripts/vm/hypervisor/xenserver/vmopsSnapshot
+++ /dev/null
@@ -1,597 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Version @VERSION@
-#
-# A plugin for executing script needed by vmops cloud 
-
-import os, sys, time
-import XenAPIPlugin
-sys.path.append("/opt/xensource/sm/")
-import SR, VDI, SRCommand, util, lvutil
-from util import CommandException
-import vhdutil
-import shutil
-import lvhdutil
-import errno
-import subprocess
-import xs_errors
-import cleanup
-import stat
-import random
-import cloudstack_pluginlib as lib
-import logging
-
-lib.setup_logging("/var/log/vmops.log")
-
-VHD_UTIL = '/opt/cloudstack/bin/vhd-util'
-VHD_PREFIX = 'VHD-'
-CLOUD_DIR = '/var/run/cloud_mount'
-
-def echo(fn):
-    def wrapped(*v, **k):
-        name = fn.__name__
-        logging.debug("#### VMOPS enter  %s ####" % name )
-        res = fn(*v, **k)
-        logging.debug("#### VMOPS exit  %s ####" % name )
-        return res
-    return wrapped
-
-
-@echo
-def create_secondary_storage_folder(session, args):
-    local_mount_path = None
-
-    logging.debug("create_secondary_storage_folder, args: " + str(args))
-
-    try:
-        try:
-            # Mount the remote resource folder locally
-            remote_mount_path = args["remoteMountPath"]
-            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
-            mount(remote_mount_path, local_mount_path)
-
-            # Create the new folder
-            new_folder = local_mount_path + "/" + args["newFolder"]
-            if not os.path.isdir(new_folder):
-                current_umask = os.umask(0)
-                os.makedirs(new_folder)
-                os.umask(current_umask)
-        except OSError, (errno, strerror):
-            errMsg = "create_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
-            logging.debug(errMsg)
-            raise xs_errors.XenError(errMsg)
-        except:
-            errMsg = "create_secondary_storage_folder failed."
-            logging.debug(errMsg)
-            raise xs_errors.XenError(errMsg)
-    finally:
-        if local_mount_path != None:
-            # Unmount the local folder
-            umount(local_mount_path)
-            # Remove the local folder
-            os.system("rmdir " + local_mount_path)
-        
-    return "1"
-
-@echo
-def delete_secondary_storage_folder(session, args):
-    local_mount_path = None
-
-    logging.debug("delete_secondary_storage_folder, args: " + str(args))
-
-    try:
-        try:
-            # Mount the remote resource folder locally
-            remote_mount_path = args["remoteMountPath"]
-            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
-            mount(remote_mount_path, local_mount_path)
-
-            # Delete the specified folder
-            folder = local_mount_path + "/" + args["folder"]
-            if os.path.isdir(folder):
-                os.system("rm -f " + folder + "/*")
-                os.system("rmdir " + folder)
-        except OSError, (errno, strerror):
-            errMsg = "delete_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
-            logging.debug(errMsg)
-            raise xs_errors.XenError(errMsg)
-        except:
-            errMsg = "delete_secondary_storage_folder failed."
-            logging.debug(errMsg)
-            raise xs_errors.XenError(errMsg)
-    finally:
-        if local_mount_path != None:
-            # Unmount the local folder
-            umount(local_mount_path)
-            # Remove the local folder
-            os.system("rmdir " + local_mount_path)
-        
-    return "1"
-     
-@echo
-def post_create_private_template(session, args):
-    local_mount_path = None
-    try:
-        try:
-            # get local template folder 
-            templatePath = args["templatePath"]
-            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
-            mount(templatePath, local_mount_path)
-            # Retrieve args
-            filename = args["templateFilename"]
-            name = args["templateName"]
-            description = args["templateDescription"]
-            checksum = args["checksum"]
-            file_size = args["size"]
-            virtual_size = args["virtualSize"]
-            template_id = args["templateId"]
-           
-            # Create the template.properties file
-            template_properties_install_path = local_mount_path + "/template.properties"
-            f = open(template_properties_install_path, "w")
-            f.write("filename=" + filename + "\n")
-            f.write("vhd=true\n")
-            f.write("id=" + template_id + "\n")
-            f.write("vhd.filename=" + filename + "\n")
-            f.write("public=false\n")
-            f.write("uniquename=" + name + "\n")
-            f.write("vhd.virtualsize=" + virtual_size + "\n")
-            f.write("virtualsize=" + virtual_size + "\n")
-            f.write("checksum=" + checksum + "\n")
-            f.write("hvm=true\n")
-            f.write("description=" + description + "\n")
-            f.write("vhd.size=" + str(file_size) + "\n")
-            f.write("size=" + str(file_size) + "\n")
-            f.close()
-            logging.debug("Created template.properties file")
-           
-            # Set permissions
-            permissions = stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH
-            os.chmod(template_properties_install_path, permissions)
-            logging.debug("Set permissions on template and template.properties")
-
-        except:
-            errMsg = "post_create_private_template failed."
-            logging.debug(errMsg)
-            raise xs_errors.XenError(errMsg)
-
-    finally:
-        if local_mount_path != None:
-            # Unmount the local folder
-            umount(local_mount_path)
-            # Remove the local folder
-            os.system("rmdir " + local_mount_path)
-    return "1" 
-  
-def isfile(path, isISCSI):
-    errMsg = ''
-    exists = True
-    if isISCSI:
-        exists = checkVolumeAvailablility(path)
-    else:
-        exists = os.path.isfile(path)
-        
-    if not exists:
-        errMsg = "File " + path + " does not exist."
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return errMsg
-
-def copyfile(fromFile, toFile, isISCSI):
-    logging.debug("Starting to copy " + fromFile + " to " + toFile)
-    errMsg = ''
-    try:
-        cmd = ['dd', 'if=' + fromFile, 'of=' + toFile, 'bs=4M']
-        txt = util.pread2(cmd)
-    except:
-        try:
-            os.system("rm -f " + toFile)
-        except:
-            txt = ''
-        txt = ''
-        errMsg = "Error while copying " + fromFile + " to " + toFile + " in secondary storage"
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-
-    logging.debug("Successfully copied " + fromFile + " to " + toFile)
-    return errMsg
-
-def chdir(path):
-    try:
-        os.chdir(path)
-    except OSError, (errno, strerror):
-        errMsg = "Unable to chdir to " + path + " because of OSError with errno: " + str(errno) + " and strerr: " + strerror
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    logging.debug("Chdired to " + path)
-    return
-
-def scanParent(path):
-    # Do a scan for the parent for ISCSI volumes
-    # Note that the parent need not be visible on the XenServer
-    parentUUID = ''
-    try:
-        lvName = os.path.basename(path)
-        dirname = os.path.dirname(path)
-        vgName = os.path.basename(dirname) 
-        vhdInfo = vhdutil.getVHDInfoLVM(lvName, lvhdutil.extractUuid, vgName)
-        parentUUID = vhdInfo.parentUuid
-    except:
-        errMsg = "Could not get vhd parent of " + path
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return parentUUID
-
-def getParent(path, isISCSI):
-    parentUUID = ''
-    try :
-        if isISCSI:
-            parentUUID = vhdutil.getParent(path, lvhdutil.extractUuid)
-        else:
-            parentUUID = vhdutil.getParent(path, cleanup.FileVDI.extractUuid)
-    except:
-        errMsg = "Could not get vhd parent of " + path
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return parentUUID
-
-def getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI):
-    snapshotVHD    = getVHD(snapshotUuid, isISCSI)
-    snapshotPath   = os.path.join(primarySRPath, snapshotVHD)
-
-    baseCopyUuid = ''
-    if isISCSI:
-        checkVolumeAvailablility(snapshotPath)
-        baseCopyUuid = scanParent(snapshotPath)
-    else:
-        baseCopyUuid = getParent(snapshotPath, isISCSI)
-    
-    logging.debug("Base copy of snapshotUuid: " + snapshotUuid + " is " + baseCopyUuid)
-    return baseCopyUuid
-
-def setParent(parent, child):
-    try:
-        cmd = [VHD_UTIL, "modify", "-p", parent, "-n", child]
-        txt = util.pread2(cmd)
-    except:
-        errMsg = "Unexpected error while trying to set parent of " + child + " to " + parent 
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    logging.debug("Successfully set parent of " + child + " to " + parent)
-    return
-
-def rename(originalVHD, newVHD):
-    try:
-        os.rename(originalVHD, newVHD)
-    except OSError, (errno, strerror):
-        errMsg = "OSError while renaming " + origiinalVHD + " to " + newVHD + "with errno: " + str(errno) + " and strerr: " + strerror
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return
-
-def makedirs(path):
-    if not os.path.isdir(path):
-        try:
-            os.makedirs(path)
-        except OSError, (errno, strerror):
-            umount(path)
-            if os.path.isdir(path):
-                return
-            errMsg = "OSError while creating " + path + " with errno: " + str(errno) + " and strerr: " + strerror
-            logging.debug(errMsg)
-            raise xs_errors.XenError(errMsg)
-    return
-
-def mount(remoteDir, localDir):
-    makedirs(localDir)
-    options = "soft,tcp,timeo=133,retrans=1"
-    try: 
-        cmd = ['mount', '-o', options, remoteDir, localDir]
-        txt = util.pread2(cmd)
-    except:
-        txt = ''
-        errMsg = "Unexpected error while trying to mount " + remoteDir + " to " + localDir 
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    logging.debug("Successfully mounted " + remoteDir + " to " + localDir)
-
-    return
-
-def umount(localDir):
-    try: 
-        cmd = ['umount', localDir]
-        util.pread2(cmd)
-    except CommandException:
-        errMsg = "CommandException raised while trying to umount " + localDir 
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-
-    logging.debug("Successfully unmounted " + localDir)
-    return
-
-def mountSnapshotsDir(secondaryStorageMountPath, localMountPointPath, path):
-    # The aim is to mount secondaryStorageMountPath on 
-    # And create <accountId>/<instanceId> dir on it, if it doesn't exist already.
-    # Assuming that secondaryStorageMountPath  exists remotely
-
-    # Just mount secondaryStorageMountPath/<relativeDir>/SecondaryStorageHost/ everytime
-    # Never unmount.
-    # path is like "snapshots/account/volumeId", we mount secondary_storage:/snapshots
-    relativeDir = path.split("/")[0]
-    restDir = "/".join(path.split("/")[1:])
-    snapshotsDir = os.path.join(secondaryStorageMountPath, relativeDir)
-
-    makedirs(localMountPointPath)
-    # if something is not mounted already on localMountPointPath,
-    # mount secondaryStorageMountPath on localMountPath
-    if os.path.ismount(localMountPointPath):
-        # There is more than one secondary storage per zone.
-        # And we are mounting each sec storage under a zone-specific directory
-        # So two secondary storage snapshot dirs will never get mounted on the same point on the same XenServer.
-        logging.debug("The remote snapshots directory has already been mounted on " + localMountPointPath)
-    else:
-        mount(snapshotsDir, localMountPointPath)
-
-    # Create accountId/instanceId dir on localMountPointPath, if it doesn't exist
-    backupsDir = os.path.join(localMountPointPath, restDir)
-    makedirs(backupsDir)
-    return backupsDir
-
-def unmountAll(path):
-    try:
-        for dir in os.listdir(path):
-            if dir.isdigit():
-                logging.debug("Unmounting Sub-Directory: " + dir)
-                localMountPointPath = os.path.join(path, dir)
-                umount(localMountPointPath)
-    except:
-        logging.debug("Ignoring the error while trying to unmount the snapshots dir")
-
-@echo
-def unmountSnapshotsDir(session, args):
-    dcId = args['dcId']
-    localMountPointPath = os.path.join(CLOUD_DIR, dcId)
-    localMountPointPath = os.path.join(localMountPointPath, "snapshots")
-    unmountAll(localMountPointPath)
-    try:
-        umount(localMountPointPath)
-    except:
-        logging.debug("Ignoring the error while trying to unmount the snapshots dir.")
-
-    return "1"
-
-def getPrimarySRPath(primaryStorageSRUuid, isISCSI):
-    if isISCSI:
-        primarySRDir = lvhdutil.VG_PREFIX + primaryStorageSRUuid
-        return os.path.join(lvhdutil.VG_LOCATION, primarySRDir)
-    else:
-        return os.path.join(SR.MOUNT_BASE, primaryStorageSRUuid)
-
-def getBackupVHD(UUID):
-    return UUID + '.' + SR.DEFAULT_TAP
-
-def getVHD(UUID, isISCSI):
-    if isISCSI:
-        return VHD_PREFIX + UUID
-    else:
-        return UUID + '.' + SR.DEFAULT_TAP
-
-def getIsTrueString(stringValue):
-    booleanValue = False
-    if (stringValue and stringValue == 'true'):
-        booleanValue = True
-    return booleanValue 
-
-def makeUnavailable(uuid, primarySRPath, isISCSI):
-    if not isISCSI:
-        return
-    VHD = getVHD(uuid, isISCSI)
-    path = os.path.join(primarySRPath, VHD)
-    manageAvailability(path, '-an')
-    return
-
-def manageAvailability(path, value):
-    if path.__contains__("/var/run/sr-mount"):
-        return
-    logging.debug("Setting availability of " + path + " to " + value)
-    try:
-        cmd = ['/usr/sbin/lvchange', value, path]
-        util.pread2(cmd)
-    except: #CommandException, (rc, cmdListStr, stderr):
-        #errMsg = "CommandException thrown while executing: " + cmdListStr + " with return code: " + str(rc) + " and stderr: " + stderr
-        errMsg = "Unexpected exception thrown by lvchange"
-        logging.debug(errMsg)
-        if value == "-ay":
-            # Raise an error only if we are trying to make it available.
-            # Just warn if we are trying to make it unavailable after the 
-            # snapshot operation is done.
-            raise xs_errors.XenError(errMsg)
-    return
-
-
-def checkVolumeAvailablility(path):
-    try:
-        if not isVolumeAvailable(path):
-            # The VHD file is not available on XenSever. The volume is probably
-            # inactive or detached.
-            # Do lvchange -ay to make it available on XenServer
-            manageAvailability(path, '-ay')
-    except:
-        errMsg = "Could not determine status of ISCSI path: " + path
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    
-    success = False
-    i = 0
-    while i < 6:
-        i = i + 1
-        # Check if the vhd is actually visible by checking for the link
-        # set isISCSI to true
-        success = isVolumeAvailable(path)
-        if success:
-            logging.debug("Made vhd: " + path + " available and confirmed that it is visible")
-            break
-
-        # Sleep for 10 seconds before checking again.
-        time.sleep(10)
-
-    # If not visible within 1 min fail
-    if not success:
-        logging.debug("Could not make vhd: " +  path + " available despite waiting for 1 minute. Does it exist?")
-
-    return success
-
-def isVolumeAvailable(path):
-    # Check if iscsi volume is available on this XenServer.
-    status = "0"
-    try:
-        p = subprocess.Popen(["/bin/bash", "-c", "if [ -L " + path + " ]; then echo 1; else echo 0;fi"], stdout=subprocess.PIPE)
-        status = p.communicate()[0].strip("\n")
-    except:
-        errMsg = "Could not determine status of ISCSI path: " + path
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-
-    return (status == "1")  
-
-def getVhdParent(session, args):
-    logging.debug("getParent with " + str(args))
-    primaryStorageSRUuid      = args['primaryStorageSRUuid']
-    snapshotUuid              = args['snapshotUuid']
-    isISCSI                   = getIsTrueString(args['isISCSI']) 
-
-    primarySRPath = getPrimarySRPath(primaryStorageSRUuid, isISCSI)
-    logging.debug("primarySRPath: " + primarySRPath)
-
-    baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
-
-    return  baseCopyUuid
-
-
-def backupSnapshot(session, args):
-    logging.debug("Called backupSnapshot with " + str(args))
-    primaryStorageSRUuid      = args['primaryStorageSRUuid']
-    secondaryStorageMountPath = args['secondaryStorageMountPath']
-    snapshotUuid              = args['snapshotUuid']
-    prevBackupUuid            = args['prevBackupUuid']
-    backupUuid                = args['backupUuid']
-    isISCSI                   = getIsTrueString(args['isISCSI'])
-    path = args['path']
-    localMountPoint = args['localMountPoint']
-    primarySRPath = getPrimarySRPath(primaryStorageSRUuid, isISCSI)
-    logging.debug("primarySRPath: " + primarySRPath)
-
-    baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
-    baseCopyVHD  = getVHD(baseCopyUuid, isISCSI)
-    baseCopyPath = os.path.join(primarySRPath, baseCopyVHD)
-    logging.debug("Base copy path: " + baseCopyPath)
-
-
-    # Mount secondary storage mount path on XenServer along the path
-    # /var/run/sr-mount/<dcId>/snapshots/ and create <accountId>/<volumeId> dir
-    # on it.
-    backupsDir = mountSnapshotsDir(secondaryStorageMountPath, localMountPoint, path)
-    logging.debug("Backups dir " + backupsDir)
-    prevBackupUuid = prevBackupUuid.split("/")[-1]
-    # Check existence of snapshot on primary storage
-    isfile(baseCopyPath, isISCSI)
-    if prevBackupUuid:
-        # Check existence of prevBackupFile
-        prevBackupVHD = getBackupVHD(prevBackupUuid)
-        prevBackupFile = os.path.join(backupsDir, prevBackupVHD)
-        isfile(prevBackupFile, False)
-
-    # copy baseCopyPath to backupsDir with new uuid
-    backupVHD = getBackupVHD(backupUuid)  
-    backupFile = os.path.join(backupsDir, backupVHD)
-    logging.debug("Back up " + baseCopyUuid + " to Secondary Storage as " + backupUuid)
-    copyfile(baseCopyPath, backupFile, isISCSI)
-    vhdutil.setHidden(backupFile, False)
-
-    # Because the primary storage is always scanned, the parent of this base copy is always the first base copy.
-    # We don't want that, we want a chain of VHDs each of which is a delta from the previous.
-    # So set the parent of the current baseCopyVHD to prevBackupVHD 
-    if prevBackupUuid:
-        # If there was a previous snapshot
-        setParent(prevBackupFile, backupFile)
-
-    txt = "1#" + backupUuid
-    return txt
-
-@echo
-def deleteSnapshotBackup(session, args):
-    logging.debug("Calling deleteSnapshotBackup with " + str(args))
-    secondaryStorageMountPath = args['secondaryStorageMountPath']
-    backupUUID                = args['backupUUID']
-    path = args['path']
-    localMountPoint = args['localMountPoint']
-
-    backupsDir = mountSnapshotsDir(secondaryStorageMountPath, localMountPoint, path)
-    # chdir to the backupsDir for convenience
-    chdir(backupsDir)
-
-    backupVHD = getBackupVHD(backupUUID)
-    logging.debug("checking existence of " + backupVHD)
-
-    # The backupVHD is on secondary which is NFS and not ISCSI.
-    if not os.path.isfile(backupVHD):
-        logging.debug("backupVHD " + backupVHD + "does not exist. Not trying to delete it")
-        return "1"
-    logging.debug("backupVHD " + backupVHD + " exists.")
-        
-    # Just delete the backupVHD
-    try:
-        os.remove(backupVHD)
-    except OSError, (errno, strerror):
-        errMsg = "OSError while removing " + backupVHD + " with errno: " + str(errno) + " and strerr: " + strerror
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-
-    return "1"
-   
-@echo
-def revert_memory_snapshot(session, args):
-    logging.debug("Calling revert_memory_snapshot with " + str(args))
-    vmName = args['vmName']
-    snapshotUUID = args['snapshotUUID']
-    oldVmUuid = args['oldVmUuid']
-    snapshotMemory = args['snapshotMemory']
-    hostUUID = args['hostUUID']
-    try:
-        cmd = '''xe vbd-list vm-uuid=%s | grep 'vdi-uuid' | grep -v 'not in database' | sed -e 's/vdi-uuid ( RO)://g' ''' % oldVmUuid
-        vdiUuids = os.popen(cmd).read().split()
-        cmd2 = '''xe vm-param-get param-name=power-state uuid=''' + oldVmUuid
-        if os.popen(cmd2).read().split()[0] != 'halted':
-            os.system("xe vm-shutdown force=true vm=" + vmName)
-        os.system("xe vm-destroy uuid=" + oldVmUuid)
-        os.system("xe snapshot-revert snapshot-uuid=" + snapshotUUID)
-        if snapshotMemory == 'true':
-            os.system("xe vm-resume vm=" + vmName + " on=" + hostUUID)
-        for vdiUuid in vdiUuids:
-            os.system("xe vdi-destroy uuid=" + vdiUuid)
-    except OSError, (errno, strerror):
-        errMsg = "OSError while reverting vm " + vmName + " to snapshot " + snapshotUUID + " with errno: " + str(errno) + " and strerr: " + strerror
-        logging.debug(errMsg)
-        raise xs_errors.XenError(errMsg)
-    return "0"
-
-if __name__ == "__main__":
-    XenAPIPlugin.dispatch({"getVhdParent":getVhdParent,  "create_secondary_storage_folder":create_secondary_storage_folder, "delete_secondary_storage_folder":delete_secondary_storage_folder, "post_create_private_template":post_create_private_template, "backupSnapshot": backupSnapshot, "deleteSnapshotBackup": deleteSnapshotBackup, "unmountSnapshotsDir": unmountSnapshotsDir, "revert_memory_snapshot":revert_memory_snapshot})
-    
-

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/vmopspremium
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/vmopspremium b/scripts/vm/hypervisor/xenserver/vmopspremium
deleted file mode 100755
index cd495fd..0000000
--- a/scripts/vm/hypervisor/xenserver/vmopspremium
+++ /dev/null
@@ -1,150 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Version @VERSION@
-#
-# A plugin for executing script needed by vmops cloud 
-
-import os, sys, time
-import XenAPIPlugin
-sys.path.append("/opt/xensource/sm/")
-import util
-import socket
-import cloudstack_pluginlib as lib
-import logging
-
-lib.setup_logging("/var/log/vmops.log")
-
-CS_DIR="/opt/cloudstack/bin/"
-
-def echo(fn):
-    def wrapped(*v, **k):
-        name = fn.__name__
-        logging.debug("#### VMOPS enter  %s ####" % name )
-        res = fn(*v, **k)
-        logging.debug("#### VMOPS exit  %s ####" % name )
-        return res
-    return wrapped
-
-@echo
-def forceShutdownVM(session, args):
-    domId = args['domId']
-    try:
-        cmd = ["/opt/xensource/debug/xenops", "destroy_domain", "-domid", domId]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-
-@echo
-def create_privatetemplate_from_snapshot(session, args):
-    templatePath = args['templatePath']
-    snapshotPath = args['snapshotPath']
-    tmpltLocalDir = args['tmpltLocalDir']
-    try:
-        cmd = ["bash", CS_DIR + "create_privatetemplate_from_snapshot.sh",snapshotPath, templatePath, tmpltLocalDir]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-@echo
-def upgrade_snapshot(session, args):
-    templatePath = args['templatePath']
-    snapshotPath = args['snapshotPath']
-    try:
-        cmd = ["bash", CS_DIR + "upgrate_snapshot.sh",snapshotPath, templatePath]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-@echo
-def copy_vhd_to_secondarystorage(session, args):
-    mountpoint = args['mountpoint']
-    vdiuuid = args['vdiuuid']
-    sruuid = args['sruuid']
-    try:
-        cmd = ["bash", CS_DIR + "copy_vhd_to_secondarystorage.sh", mountpoint, vdiuuid, sruuid]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-@echo
-def copy_vhd_from_secondarystorage(session, args):
-    mountpoint = args['mountpoint']
-    sruuid = args['sruuid']
-    namelabel = args['namelabel']
-    try:
-        cmd = ["bash", CS_DIR + "copy_vhd_from_secondarystorage.sh", mountpoint, sruuid, namelabel]
-        txt = util.pread2(cmd)
-    except:
-        txt = '10#failed'
-    return txt
-
-@echo
-def setup_heartbeat_sr(session, args):
-    host = args['host']
-    sr = args['sr']
-    try:
-        cmd = ["bash", CS_DIR + "setup_heartbeat_sr.sh", host, sr]
-        txt = util.pread2(cmd)
-    except:
-        txt = ''
-    return txt
-
-@echo
-def setup_heartbeat_file(session, args):
-    host = args['host']
-    sr = args['sr']
-    add = args['add']
-    try:
-        cmd = ["bash", CS_DIR + "setup_heartbeat_file.sh", host, sr, add]
-        txt = util.pread2(cmd)
-    except:
-        txt = ''
-    return txt
-
-@echo
-def check_heartbeat(session, args):
-    host = args['host']
-    interval = args['interval']
-    try:
-       cmd = ["bash", CS_DIR + "check_heartbeat.sh", host, interval]
-       txt = util.pread2(cmd)
-    except:
-       txt=''
-    return txt
-    
-   
-@echo
-def heartbeat(session, args):
-    host = args['host']
-    interval = args['interval']
-    try: 
-       cmd = ["/bin/bash", CS_DIR + "launch_hb.sh", host, interval]
-       txt = util.pread2(cmd)
-    except:
-       txt='fail'
-    return txt
-
-if __name__ == "__main__":
-    XenAPIPlugin.dispatch({"forceShutdownVM":forceShutdownVM, "upgrade_snapshot":upgrade_snapshot, "create_privatetemplate_from_snapshot":create_privatetemplate_from_snapshot, "copy_vhd_to_secondarystorage":copy_vhd_to_secondarystorage, "copy_vhd_from_secondarystorage":copy_vhd_from_secondarystorage, "setup_heartbeat_sr":setup_heartbeat_sr, "setup_heartbeat_file":setup_heartbeat_file, "check_heartbeat":check_heartbeat, "heartbeat": heartbeat})
-

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xcposs/cloud-plugin-generic
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xcposs/cloud-plugin-generic b/scripts/vm/hypervisor/xenserver/xcposs/cloud-plugin-generic
new file mode 100644
index 0000000..aca0fe1
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/xcposs/cloud-plugin-generic
@@ -0,0 +1,1596 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# Version @VERSION@
+#
+# A plugin for executing script needed by CloudStack cloud 
+
+import os, sys, time
+import XenAPIPlugin
+sys.path.extend(["/usr/lib/xcp/sm/", "/usr/local/sbin/", "/sbin/"])
+import base64
+import socket
+import stat
+import tempfile
+import util
+import subprocess
+import zlib
+from util import CommandException
+
+def echo(fn):
+    def wrapped(*v, **k):
+        name = fn.__name__
+        util.SMlog("#### CLOUD enter  %s ####" % name )
+        res = fn(*v, **k)
+        util.SMlog("#### CLOUD exit  %s ####" % name )
+        return res
+    return wrapped
+
+@echo
+def setup_iscsi(session, args):
+   uuid=args['uuid']
+   try:
+       cmd = ["bash", "/usr/lib/cloud/bin/setup_iscsi.sh", uuid]
+       txt = util.pread2(cmd)
+   except:
+       txt = ''
+   return '> DONE <'
+ 
+
+@echo
+def getgateway(session, args):
+    mgmt_ip = args['mgmtIP']
+    try:
+        cmd = ["bash", "/usr/lib/cloud/bin/network_info.sh", "-g", mgmt_ip]
+        txt = util.pread2(cmd)
+    except:
+        txt = ''
+
+    return txt
+    
+@echo
+def preparemigration(session, args):
+    uuid = args['uuid']
+    try:
+        cmd = ["/usr/lib/cloud/bin/make_migratable.sh", uuid]
+        util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog("Catch prepare migration exception" )
+        txt = ''
+
+    return txt
+
+@echo
+def setIptables(session, args):
+    try:
+        '''cmd = ["/bin/bash", "/usr/lib/cloud/bin/setupxenserver.sh"]
+        txt = util.pread2(cmd)'''
+        txt = 'success'
+    except:
+        util.SMlog("  setIptables execution failed "  )
+        txt = '' 
+
+    return txt
+ 
+@echo
+def pingdomr(session, args):
+    host = args['host']
+    port = args['port']
+    socket.setdefaulttimeout(3)
+    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    try:
+        s.connect((host,int(port)))
+        txt = 'success'
+    except:
+        txt = ''
+    
+    s.close()
+
+    return txt
+
+@echo
+def kill_copy_process(session, args):
+    namelabel = args['namelabel']
+    try:
+        cmd = ["bash", "/usr/lib/cloud/bin/kill_copy_process.sh", namelabel]
+        txt = util.pread2(cmd)
+    except:
+        txt = 'false'
+    return txt
+
+@echo
+def pingxenserver(session, args):
+    txt = 'success'
+    return txt
+
+@echo
+def ipassoc(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/ipassoc.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog("  ip associate failed "  )
+        txt = '' 
+
+    return txt
+
+def pingtest(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/pingtest.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog("  pingtest failed "  )
+        txt = ''
+
+    return txt
+
+@echo
+def savePassword(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/save_password_to_domr.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog("  save password to domr failed "  )
+        txt = '' 
+
+    return txt
+
+@echo
+def saveDhcpEntry(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/dhcp_entry.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog(" save dhcp entry failed "  )
+        txt = '' 
+
+    return txt
+    
+@echo
+def lt2p_vpn(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/l2tp_vpn.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog("l2tp vpn failed "  )
+        txt = '' 
+
+    return txt    
+
+@echo
+def setLinkLocalIP(session, args):
+    brName = args['brName']
+    try:
+        cmd = ["ip", "route", "del", "169.254.0.0/16"]
+        txt = util.pread2(cmd)
+    except:
+        txt = '' 
+    try:
+        cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
+        txt = util.pread2(cmd)
+    except:
+
+        try:
+            cmd = ["brctl", "addbr", brName]
+            txt = util.pread2(cmd)
+        except:
+            pass
+ 
+        try:
+            cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
+            txt = util.pread2(cmd)
+        except:
+            pass
+    try:
+        cmd = ["ip", "route", "add", "169.254.0.0/16", "dev", brName, "src", "169.254.0.1"]
+        txt = util.pread2(cmd)
+    except:
+        txt = '' 
+    txt = 'success'
+    return txt
+    
+@echo
+def setFirewallRule(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/call_firewall.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog(" set firewall rule failed "  )
+        txt = '' 
+
+    return txt
+
+@echo
+def setLoadBalancerRule(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/call_loadbalancer.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog(" set loadbalancer rule failed "  )
+        txt = '' 
+
+    return txt
+    
+@echo
+def createFile(session, args):
+    file_path = args['filepath']
+    file_contents = args['filecontents']
+
+    try:
+        f = open(file_path, "w")
+        f.write(file_contents)
+        f.close()
+        txt = 'success'
+    except:
+        util.SMlog(" failed to create HA proxy cfg file ")
+        txt = ''
+
+    return txt
+
+@echo
+def deleteFile(session, args):
+    file_path = args["filepath"]
+
+    try:
+        if os.path.isfile(file_path):
+            os.remove(file_path)
+        txt = 'success'
+    except:
+        util.SMlog(" failed to remove HA proxy cfg file ")
+        txt = ''
+
+    return txt
+
+
+@echo
+def networkUsage(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/networkUsage.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+    except:
+        util.SMlog("  network usage error "  )
+        txt = '' 
+
+    return txt
+    
+def get_private_nic(session, args):
+    vms = session.xenapi.VM.get_all()
+    host_uuid = args.get('host_uuid')
+    host = session.xenapi.host.get_by_uuid(host_uuid)
+    piflist = session.xenapi.host.get_PIFs(host)
+    mgmtnic = 'eth0'
+    for pif in piflist:
+        pifrec = session.xenapi.PIF.get_record(pif)
+        network = pifrec.get('network')
+        nwrec = session.xenapi.network.get_record(network)
+        if nwrec.get('name_label') == 'cloud-guest':
+            return pifrec.get('device')
+        if pifrec.get('management'):
+            mgmtnic = pifrec.get('device')
+    
+    return mgmtnic
+
+def chain_name(vm_name):
+    if vm_name.startswith('i-') or vm_name.startswith('r-'):
+        if vm_name.endswith('untagged'):
+            return '-'.join(vm_name.split('-')[:-1])
+    return vm_name
+
+def chain_name_def(vm_name):
+    if vm_name.startswith('i-'):
+        if vm_name.endswith('untagged'):
+            return '-'.join(vm_name.split('-')[:-2]) + "-def"
+        return '-'.join(vm_name.split('-')[:-1]) + "-def"
+    return vm_name
+  
+def egress_chain_name(vm_name):
+    return chain_name(vm_name) + "-eg"
+      
+@echo
+def can_bridge_firewall(session, args):
+    try:
+        util.pread2(['ebtables', '-V'])
+        util.pread2(['ipset', '-V'])
+    except:
+        return 'false'
+
+    host_uuid = args.get('host_uuid')
+    try:
+        util.pread2(['iptables', '-N', 'BRIDGE-FIREWALL'])
+        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-D', 'FORWARD',  '-j', 'RH-Firewall-1-INPUT'])
+    except:
+        util.SMlog('Chain BRIDGE-FIREWALL already exists')
+    privnic = get_private_nic(session, args)
+    result = 'true'
+    try:
+        util.pread2(['/bin/bash', '-c', 'iptables -n -L FORWARD | grep BRIDGE-FIREWALL'])
+    except:
+        try:
+            util.pread2(['iptables', '-I', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '-j', 'BRIDGE-FIREWALL'])
+            util.pread2(['iptables', '-A', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', privnic, '-j', 'ACCEPT'])
+            util.pread2(['iptables', '-A', 'FORWARD', '-j', 'DROP'])
+        except:
+            return 'false'
+    default_ebtables_rules()
+    allow_egress_traffic(session)
+    if not os.path.exists('/var/run/cloud'):
+        os.makedirs('/var/run/cloud')
+    if not os.path.exists('/var/cache/cloud'):
+        os.makedirs('/var/cache/cloud')
+    #get_ipset_keyword()
+ 
+    cleanup_rules_for_dead_vms(session)
+    cleanup_rules(session, args)
+    
+    return result
+
+@echo
+def default_ebtables_rules():
+    try:
+        util.pread2(['ebtables', '-N',  'DEFAULT_EBTABLES'])
+        util.pread2(['ebtables', '-A', 'FORWARD', '-j'  'DEFAULT_EBTABLES'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'ACCEPT'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Request', '-j', 'ACCEPT'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Reply', '-j', 'ACCEPT'])
+        # deny mac broadcast and multicast
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Broadcast', '-j', 'DROP']) 
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Multicast', '-j', 'DROP']) 
+        # deny ip broadcast and multicast
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'DROP'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '224.0.0.0/4', '-j', 'DROP'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-j', 'RETURN'])
+        # deny ipv6
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv6', '-j', 'DROP'])
+        # deny vlan
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', '802_1Q', '-j', 'DROP'])
+        # deny all others (e.g., 802.1d, CDP)
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES',  '-j', 'DROP'])
+    except:
+        util.SMlog('Chain DEFAULT_EBTABLES already exists')
+
+
+@echo
+def allow_egress_traffic(session):
+    devs = []
+    for pif in session.xenapi.PIF.get_all():
+        pif_rec = session.xenapi.PIF.get_record(pif)
+        vlan = pif_rec.get('VLAN')
+        dev = pif_rec.get('device')
+        if vlan == '-1':
+            devs.append(dev)
+        else:
+            devs.append(dev + "." + vlan)
+    for d in devs:
+        try:
+            util.pread2(['/bin/bash', '-c', "iptables -n -L FORWARD | grep '%s '" % d])
+        except:
+            try:
+                util.pread2(['iptables', '-I', 'FORWARD', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', d, '-j', 'ACCEPT'])
+            except:
+                util.SMlog("Failed to add FORWARD rule through to %s" % d)
+                return 'false'
+    return 'true'
+
+
+def ipset(ipsetname, proto, start, end, ips):
+    try:
+        util.pread2(['ipset', '-N', ipsetname, 'iptreemap'])
+    except:
+        util.SMlog("ipset chain already exists" + ipsetname)
+
+    result = True
+    ipsettmp = ''.join(''.join(ipsetname.split('-')).split('_')) + str(int(time.time()) % 1000)
+
+    try: 
+        util.pread2(['ipset', '-N', ipsettmp, 'iptreemap']) 
+    except:
+        util.SMlog("Failed to create temp ipset, reusing old name= " + ipsettmp)
+        try: 
+            util.pread2(['ipset', '-F', ipsettmp]) 
+        except:
+            util.SMlog("Failed to clear old temp ipset name=" + ipsettmp)
+            return False
+        
+    try: 
+        for ip in ips:
+            try:
+                util.pread2(['ipset', '-A', ipsettmp, ip])
+            except CommandException, cex:
+                if cex.reason.rfind('already in set') == -1:
+                   raise
+    except:
+        util.SMlog("Failed to program ipset " + ipsetname)
+        util.pread2(['ipset', '-F', ipsettmp]) 
+        util.pread2(['ipset', '-X', ipsettmp]) 
+        return False
+
+    try: 
+        util.pread2(['ipset', '-W', ipsettmp, ipsetname]) 
+    except:
+        util.SMlog("Failed to swap ipset " + ipsetname)
+        result = False
+
+    try: 
+        util.pread2(['ipset', '-F', ipsettmp]) 
+        util.pread2(['ipset', '-X', ipsettmp]) 
+    except:
+        # if the temporary name clashes next time we'll just reuse it
+        util.SMlog("Failed to delete temp ipset " + ipsettmp)
+
+    return result
+
+@echo 
+def destroy_network_rules_for_vm(session, args):
+    vm_name = args.pop('vmName')
+    vmchain = chain_name(vm_name)
+    vmchain_egress = egress_chain_name(vm_name)
+    vmchain_default = chain_name_def(vm_name)
+    
+    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
+    if vm_name.startswith('i-') or vm_name.startswith('r-') or vm_name.startswith('l-'):
+        try:
+            util.pread2(['iptables', '-F', vmchain_default])
+            util.pread2(['iptables', '-X', vmchain_default])
+        except:
+            util.SMlog("Ignoring failure to delete  chain " + vmchain_default)
+    
+    destroy_ebtables_rules(vmchain)
+    
+    try:
+        util.pread2(['iptables', '-F', vmchain])
+        util.pread2(['iptables', '-X', vmchain])
+    except:
+        util.SMlog("Ignoring failure to delete ingress chain " + vmchain)
+        
+   
+    try:
+        util.pread2(['iptables', '-F', vmchain_egress])
+        util.pread2(['iptables', '-X', vmchain_egress])
+    except:
+        util.SMlog("Ignoring failure to delete egress chain " + vmchain_egress)
+    
+    remove_rule_log_for_vm(vm_name)
+    
+    if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
+        return 'true'
+    
+    try:
+        setscmd = "ipset --save | grep " +  vmchain + " | grep '^-N' | awk '{print $2}'"
+        setsforvm = util.pread2(['/bin/bash', '-c', setscmd]).split('\n')
+        for set in setsforvm:
+            if set != '':
+                util.pread2(['ipset', '-F', set])       
+                util.pread2(['ipset', '-X', set])       
+    except:
+        util.SMlog("Failed to destroy ipsets for %" % vm_name)
+    
+    
+    return 'true'
+
+@echo
+def destroy_ebtables_rules(vm_chain):
+    
+    delcmd = "ebtables-save | grep " +  vm_chain + " | sed 's/-A/-D/'"
+    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
+    delcmds.pop()
+    for cmd in delcmds:
+        try:
+            dc = cmd.split(' ')
+            dc.insert(0, 'ebtables')
+            util.pread2(dc)
+        except:
+            util.SMlog("Ignoring failure to delete ebtables rules for vm " + vm_chain)
+    try:
+        util.pread2(['ebtables', '-F', vm_chain])
+        util.pread2(['ebtables', '-X', vm_chain])
+    except:
+            util.SMlog("Ignoring failure to delete ebtables chain for vm " + vm_chain)   
+
+@echo
+def destroy_arptables_rules(vm_chain):
+    delcmd = "arptables -vL FORWARD | grep " + vm_chain + " | sed 's/-i any//' | sed 's/-o any//' | awk '{print $1,$2,$3,$4}' "
+    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
+    delcmds.pop()
+    for cmd in delcmds:
+        try:
+            dc = cmd.split(' ')
+            dc.insert(0, 'arptables')
+            dc.insert(1, '-D')
+            dc.insert(2, 'FORWARD')
+            util.pread2(dc)
+        except:
+            util.SMlog("Ignoring failure to delete arptables rules for vm " + vm_chain)
+    
+    try:
+        util.pread2(['arptables', '-F', vm_chain])
+        util.pread2(['arptables', '-X', vm_chain])
+    except:
+        util.SMlog("Ignoring failure to delete arptables chain for vm " + vm_chain) 
+              
+@echo
+def default_ebtables_antispoof_rules(vm_chain, vifs, vm_ip, vm_mac):
+    if vm_mac == 'ff:ff:ff:ff:ff:ff':
+        util.SMlog("Ignoring since mac address is not valid")
+        return 'true'
+    
+    try:
+        util.pread2(['ebtables', '-N', vm_chain])
+    except:
+        try:
+            util.pread2(['ebtables', '-F', vm_chain])
+        except:
+            util.SMlog("Failed to create ebtables antispoof chain, skipping")
+            return 'true'
+
+    # note all rules for packets into the bridge (-i) precede all output rules (-o)
+    # always start after the first rule in the FORWARD chain that jumps to DEFAULT_EBTABLES chain
+    try:
+        for vif in vifs:
+            util.pread2(['ebtables', '-I', 'FORWARD', '2', '-i',  vif,  '-j', vm_chain])
+            util.pread2(['ebtables', '-A', 'FORWARD', '-o',  vif, '-j', vm_chain])
+    except:
+        util.SMlog("Failed to program default ebtables FORWARD rules for %s" % vm_chain)
+        return 'false'
+
+    try:
+        for vif in vifs:
+            # only allow source mac that belongs to the vm
+	    util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-s', '!', vm_mac,  '-j', 'DROP'])
+            # do not allow fake dhcp responses
+            util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '68', '-j', 'DROP'])
+            # do not allow snooping of dhcp requests
+            util.pread2(['ebtables', '-A', vm_chain, '-o', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'DROP'])
+    except:
+        util.SMlog("Failed to program default ebtables antispoof rules for %s" % vm_chain)
+        return 'false'
+
+    return 'true'
+
+@echo
+def default_arp_antispoof(vm_chain, vifs, vm_ip, vm_mac):
+    if vm_mac == 'ff:ff:ff:ff:ff:ff':
+        util.SMlog("Ignoring since mac address is not valid")
+        return 'true'
+
+    try:
+        util.pread2(['arptables',  '-N', vm_chain])
+    except:
+        try:
+            util.pread2(['arptables', '-F', vm_chain])
+        except:
+            util.SMlog("Failed to create arptables rule, skipping")
+            return 'true'
+
+    # note all rules for packets into the bridge (-i) precede all output rules (-o)
+    try:
+        for vif in vifs:
+           util.pread2(['arptables',  '-I', 'FORWARD', '-i',  vif, '-j', vm_chain])
+           util.pread2(['arptables',  '-A', 'FORWARD', '-o',  vif, '-j', vm_chain])
+    except:
+        util.SMlog("Failed to program default arptables rules in FORWARD chain vm=" + vm_chain)
+        return 'false'
+    
+    try:
+        for vif in vifs:
+            #accept arp replies into the bridge as long as the source mac and ips match the vm
+            util.pread2(['arptables',  '-A', vm_chain, '-i', vif, '--opcode', 'Reply', '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'ACCEPT'])
+            #accept any arp requests from this vm. In the future this can be restricted to deny attacks on hosts
+            #also important to restrict source ip and src mac in these requests as they can be used to update arp tables on destination
+            util.pread2(['arptables',  '-A', vm_chain, '-i', vif, '--opcode', 'Request',  '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'RETURN'])
+            #accept any arp requests to this vm as long as the request is for this vm's ip
+            util.pread2(['arptables',  '-A', vm_chain, '-o', vif, '--opcode', 'Request', '--destination-ip', vm_ip, '-j', 'ACCEPT'])   
+            #accept any arp replies to this vm as long as the mac and ip matches
+            util.pread2(['arptables',  '-A', vm_chain, '-o', vif, '--opcode', 'Reply', '--destination-mac', vm_mac, '--destination-ip', vm_ip, '-j', 'ACCEPT'])   
+        util.pread2(['arptables',  '-A', vm_chain,  '-j', 'DROP'])
+
+    except:
+        util.SMlog("Failed to program default arptables  rules")
+        return 'false'
+
+    return 'true'
+
+@echo
+def default_network_rules_systemvm(session, args):
+    vm_name = args.pop('vmName')
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vm_name)
+        if len(vm) != 1:
+             return 'false'
+        vm_rec = session.xenapi.VM.get_record(vm[0])
+        vm_vifs = vm_rec.get('VIFs')
+        vifnums = [session.xenapi.VIF.get_record(vif).get('device') for vif in vm_vifs]
+        domid = vm_rec.get('domid')
+    except:
+        util.SMlog("### Failed to get domid or vif list for vm  ##" + vm_name)
+        return 'false'
+    
+    if domid == '-1':
+        util.SMlog("### Failed to get domid for vm (-1):  " + vm_name)
+        return 'false'
+
+    vifs = ["vif" + domid + "." + v for v in vifnums]
+    #vm_name =  '-'.join(vm_name.split('-')[:-1])
+    vmchain = chain_name(vm_name)
+   
+ 
+    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
+  
+    try:
+        util.pread2(['iptables', '-N', vmchain])
+    except:
+        util.pread2(['iptables', '-F', vmchain])
+    
+    allow_egress_traffic(session)
+  
+    for vif in vifs:
+        try:
+            util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', vif, '-j', vmchain])
+            util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '4', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', vmchain])
+            util.pread2(['iptables', '-I', vmchain, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', 'RETURN'])
+        except:
+            util.SMlog("Failed to program default rules")
+            return 'false'
+	
+	
+    util.pread2(['iptables', '-A', vmchain, '-j', 'ACCEPT'])
+    
+    if write_rule_log_for_vm(vm_name, '-1', '_ignore_', domid, '_initial_', '-1') == False:
+        util.SMlog("Failed to log default network rules for systemvm, ignoring")
+    return 'true'
+
+
+@echo
+def default_network_rules(session, args):
+    vm_name = args.pop('vmName')
+    vm_ip = args.pop('vmIP')
+    vm_id = args.pop('vmID')
+    vm_mac = args.pop('vmMAC')
+    
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vm_name)
+        if len(vm) != 1:
+             util.SMlog("### Failed to get record for vm  " + vm_name)
+             return 'false'
+        vm_rec = session.xenapi.VM.get_record(vm[0])
+        domid = vm_rec.get('domid')
+    except:
+        util.SMlog("### Failed to get domid for vm " + vm_name)
+        return 'false'
+    if domid == '-1':     
+        util.SMlog("### Failed to get domid for vm (-1):  " + vm_name)
+        return 'false'
+    
+    vif = "vif" + domid + ".0"
+    tap = "tap" + domid + ".0"
+    vifs = [vif]
+    try:
+        util.pread2(['ifconfig', tap])
+        vifs.append(tap)
+    except:
+        pass
+
+    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
+
+     
+    vmchain =  chain_name(vm_name)
+    vmchain_egress =  egress_chain_name(vm_name)
+    vmchain_default = chain_name_def(vm_name)
+    
+    destroy_ebtables_rules(vmchain)
+    
+
+    try:
+        util.pread2(['iptables', '-N', vmchain])
+    except:
+        util.pread2(['iptables', '-F', vmchain])
+    
+    try:
+        util.pread2(['iptables', '-N', vmchain_egress])
+    except:
+        util.pread2(['iptables', '-F', vmchain_egress])
+        
+    try:
+        util.pread2(['iptables', '-N', vmchain_default])
+    except:
+        util.pread2(['iptables', '-F', vmchain_default])        
+
+    try:
+        for v in vifs:
+            util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
+            util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
+        util.pread2(['iptables', '-A', vmchain_default, '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
+        #allow dhcp
+        for v in vifs:
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
+
+        #don't let vm spoof its ip address
+        for v in vifs:
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip,'-p', 'udp', '--dport', '53', '-j', 'RETURN'])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', '!', vm_ip, '-j', 'DROP'])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '--destination', '!', vm_ip, '-j', 'DROP'])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip, '-j', vmchain_egress])
+        
+        for v in vifs:
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v,  '-j', vmchain])
+    except:
+        util.SMlog("Failed to program default rules for vm " + vm_name)
+        return 'false'
+    
+    default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
+    default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
+    
+    if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, '_initial_', '-1', vm_mac) == False:
+        util.SMlog("Failed to log default network rules, ignoring")
+        
+    util.SMlog("Programmed default rules for vm " + vm_name)
+    return 'true'
+
+@echo
+def check_domid_changed(session, vmName):
+    curr_domid = '-1'
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vmName)
+        if len(vm) != 1:
+             util.SMlog("### Could not get record for vm ## " + vmName)
+        else:
+            vm_rec = session.xenapi.VM.get_record(vm[0])
+            curr_domid = vm_rec.get('domid')
+    except:
+        util.SMlog("### Failed to get domid for vm  ## " + vmName)
+        
+    
+    logfilename = "/var/run/cloud/" + vmName +".log"
+    if not os.path.exists(logfilename):
+        return ['-1', curr_domid]
+    
+    lines = (line.rstrip() for line in open(logfilename))
+    
+    [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno, _vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
+    for line in lines:
+        try:
+            [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno,_vmMac] = line.split(',')
+        except ValueError,v:
+            [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno] = line.split(',')
+        break
+    
+    return [curr_domid, old_domid]
+
+@echo
+def delete_rules_for_vm_in_bridge_firewall_chain(vmName):
+    vm_name = vmName
+    vmchain = chain_name_def(vm_name)
+    
+    delcmd = "iptables-save | grep '\-A BRIDGE-FIREWALL' | grep " +  vmchain + " | sed 's/-A/-D/'"
+    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
+    delcmds.pop()
+    for cmd in delcmds:
+        try:
+            dc = cmd.split(' ')
+            dc.insert(0, 'iptables')
+            dc.pop()
+            util.pread2(filter(None, dc))
+        except:
+              util.SMlog("Ignoring failure to delete rules for vm " + vmName)
+
+  
+@echo
+def network_rules_for_rebooted_vm(session, vmName):
+    vm_name = vmName
+    [curr_domid, old_domid] = check_domid_changed(session, vm_name)
+    
+    if curr_domid == old_domid:
+        return True
+    
+    if old_domid == '-1':
+        return True
+    
+    if curr_domid == '-1':
+        return True
+    
+    util.SMlog("Found a rebooted VM -- reprogramming rules for  " + vm_name)
+    
+    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
+    if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
+        default_network_rules_systemvm(session, {"vmName":vm_name})
+        return True
+    
+    vif = "vif" + curr_domid + ".0"
+    tap = "tap" + curr_domid + ".0"
+    vifs = [vif]
+    try:
+        util.pread2(['ifconfig', tap])
+        vifs.append(tap)
+    except:
+        pass
+    vmchain = chain_name(vm_name)
+    vmchain_default = chain_name_def(vm_name)
+
+    for v in vifs:
+        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
+        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
+
+    #change antispoof rule in vmchain
+    try:
+        delcmd = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-in | sed 's/-A/-D/'"
+        delcmd2 = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-out | sed 's/-A/-D/'"
+        inscmd = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-in | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' "
+        inscmd2 = "iptables-save| grep '\-A " +  vmchain_default + "' | grep  physdev-in | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/' "
+        inscmd3 = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-out | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' "
+        inscmd4 = "iptables-save| grep '\-A " +  vmchain_default + "' | grep  physdev-out | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/' "
+        
+        ipts = []
+        for cmd in [delcmd, delcmd2, inscmd, inscmd2, inscmd3, inscmd4]:
+            cmds = util.pread2(['/bin/bash', '-c', cmd]).split('\n')
+            cmds.pop()
+            for c in cmds:
+                    ipt = c.split(' ')
+                    ipt.insert(0, 'iptables')
+                    ipt.pop()
+                    ipts.append(ipt)
+        
+        for ipt in ipts:
+            try:
+                util.pread2(filter(None,ipt))
+            except:
+                util.SMlog("Failed to rewrite antispoofing rules for vm " + vm_name)
+        
+        util.pread2(['/bin/bash', '-c', 'iptables -D ' + vmchain_default + " -j " + vmchain])
+        util.pread2(['/bin/bash', '-c', 'iptables -A ' + vmchain_default + " -j " + vmchain])
+    except:
+        util.SMlog("No rules found for vm " + vm_name)
+
+    destroy_ebtables_rules(vmchain)
+    destroy_arptables_rules(vmchain)
+    [vm_ip, vm_mac] = get_vm_mac_ip_from_log(vmchain)
+    default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
+    default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
+    rewrite_rule_log_for_vm(vm_name, curr_domid)
+    return True
+
+def rewrite_rule_log_for_vm(vm_name, new_domid):
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    if not os.path.exists(logfilename):
+        return 
+    lines = (line.rstrip() for line in open(logfilename))
+    
+    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
+    for line in lines:
+        try:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
+            break
+        except ValueError,v:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
+    
+    write_rule_log_for_vm(_vmName, _vmID, _vmIP, new_domid, _signature, '-1', _vmMac)
+
+def get_rule_log_for_vm(session, vmName):
+    vm_name = vmName;
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    if not os.path.exists(logfilename):
+        return ''
+    
+    lines = (line.rstrip() for line in open(logfilename))
+    
+    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
+    for line in lines:
+        try:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
+            break
+        except ValueError,v:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
+    
+    return ','.join([_vmName, _vmID, _vmIP, _domID, _signature, _seqno])
+
+@echo
+def get_vm_mac_ip_from_log(vm_name):
+    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '0.0.0.0', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    if not os.path.exists(logfilename):
+        return ['_', '_']
+    
+    lines = (line.rstrip() for line in open(logfilename))
+    for line in lines:
+        try:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
+            break
+        except ValueError,v:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
+    
+    return [ _vmIP, _vmMac]
+
+@echo
+def get_rule_logs_for_vms(session, args):
+    host_uuid = args.pop('host_uuid')
+    try:
+        thishost = session.xenapi.host.get_by_uuid(host_uuid)
+        hostrec = session.xenapi.host.get_record(thishost)
+        vms = hostrec.get('resident_VMs')
+    except:
+        util.SMlog("Failed to get host from uuid " + host_uuid)
+        return ' '
+    
+    result = []
+    try:
+        for name in [session.xenapi.VM.get_name_label(x) for x in vms]:
+            if 1 not in [ name.startswith(c) for c in ['r-', 's-', 'v-', 'i-', 'l-'] ]:
+                continue
+            network_rules_for_rebooted_vm(session, name)
+            if name.startswith('i-'):
+                log = get_rule_log_for_vm(session, name)
+                result.append(log)
+    except:
+        util.SMlog("Failed to get rule logs, better luck next time!")
+        
+    return ";".join(result)
+
+@echo
+def cleanup_rules_for_dead_vms(session):
+  try:
+    vms = session.xenapi.VM.get_all()
+    cleaned = 0
+    for vm_name in [session.xenapi.VM.get_name_label(x) for x in vms]:
+        if 1 in [ vm_name.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-'] ]:
+            vm = session.xenapi.VM.get_by_name_label(vm_name)
+            if len(vm) != 1:
+                continue
+            vm_rec = session.xenapi.VM.get_record(vm[0])
+            state = vm_rec.get('power_state')
+            if state != 'Running' and state != 'Paused':
+                util.SMlog("vm " + vm_name + " is not running, cleaning up")
+                destroy_network_rules_for_vm(session, {'vmName':vm_name})
+                cleaned = cleaned+1
+                
+    util.SMlog("Cleaned up rules for " + str(cleaned) + " vms")
+  except:
+    util.SMlog("Failed to cleanup rules for dead vms!")
+        
+
+@echo
+def cleanup_rules(session, args):
+  instance = args.get('instance')
+  if not instance:
+    instance = 'VM'
+  resident_vms = []
+  try:
+    hostname = util.pread2(['/bin/bash', '-c', 'hostname']).split('\n')
+    if len(hostname) < 1:
+       raise Exception('Could not find hostname of this host')
+    thishost = session.xenapi.host.get_by_name_label(hostname[0])
+    if len(thishost) < 1:
+       raise Exception("Could not find host record from hostname %s of this host"%hostname[0])
+    hostrec = session.xenapi.host.get_record(thishost[0])
+    vms = hostrec.get('resident_VMs')
+    resident_vms = [session.xenapi.VM.get_name_label(x) for x in vms]
+    util.SMlog('cleanup_rules: found %s resident vms on this host %s' % (len(resident_vms)-1, hostname[0]))
+ 
+    chainscmd = "iptables-save | grep '^:' | awk '{print $1}' | cut -d':' -f2 | sed 's/-def/-%s/'| sed 's/-eg//' | sort|uniq" % instance
+    chains = util.pread2(['/bin/bash', '-c', chainscmd]).split('\n')
+    vmchains = [ch  for ch in chains if 1 in [ ch.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-']]]
+    util.SMlog('cleanup_rules: found %s iptables chains for vms on this host %s' % (len(vmchains), hostname[0]))
+    cleaned = 0
+    cleanup = []
+    for chain in vmchains:
+        vm = session.xenapi.VM.get_by_name_label(chain)
+        if len(vm) != 1:
+            vm = session.xenapi.VM.get_by_name_label(chain + "-untagged")
+            if len(vm) != 1:
+                util.SMlog("chain " + chain + " does not correspond to a vm, cleaning up")
+                cleanup.append(chain)
+                continue
+        if chain not in resident_vms:
+            util.SMlog("vm " + chain + " is not running, cleaning up")
+            cleanup.append(chain)
+                
+    for vm_name in cleanup:
+        destroy_network_rules_for_vm(session, {'vmName':vm_name})
+                    
+    util.SMlog("Cleaned up rules for " + str(len(cleanup)) + " chains")
+    return str(len(cleanup))                
+  except Exception, ex:
+    util.SMlog("Failed to cleanup rules, reason= " + str(ex))
+    return '-1';
+
+@echo
+def check_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno):
+    vm_name = vmName;
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    if not os.path.exists(logfilename):
+        util.SMlog("Failed to find logfile %s" %logfilename)
+        return [True, True, True]
+        
+    lines = (line.rstrip() for line in open(logfilename))
+    
+    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
+    try:
+        for line in lines:
+            try:
+                [_vmName,_vmID,_vmIP,_domID,_signature,_seqno, _vmMac] = line.split(',')
+            except ValueError,v:
+                [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
+            break
+    except:
+        util.SMlog("Failed to parse log file for vm " + vmName)
+        remove_rule_log_for_vm(vmName)
+        return [True, True, True]
+    
+    reprogramDefault = False
+    if (domID != _domID) or (vmID != _vmID) or (vmIP != _vmIP):
+        util.SMlog("Change in default info set of vm %s" % vmName)
+        return [True, True, True]
+    else:
+        util.SMlog("No change in default info set of vm %s" % vmName)
+    
+    reprogramChain = False
+    rewriteLog = True
+    if (int(seqno) > int(_seqno)):
+        if (_signature != signature):
+            reprogramChain = True
+            util.SMlog("Seqno increased from %s to %s: reprogamming "\
+                        "ingress rules for vm %s" % (_seqno, seqno, vmName))
+        else:
+            util.SMlog("Seqno increased from %s to %s: but no change "\
+                        "in signature for vm: skip programming ingress "\
+                        "rules %s" % (_seqno, seqno, vmName))
+    elif (int(seqno) < int(_seqno)):
+        util.SMlog("Seqno decreased from %s to %s: ignoring these "\
+                        "ingress rules for vm %s" % (_seqno, seqno, vmName))
+        rewriteLog = False
+    elif (signature != _signature):
+        util.SMlog("Seqno %s stayed the same but signature changed from "\
+                    "%s to %s for vm %s" % (seqno, _signature, signature, vmName))
+        rewriteLog = True
+        reprogramChain = True
+    else:
+        util.SMlog("Seqno and signature stayed the same: %s : ignoring these "\
+                        "ingress rules for vm %s" % (seqno, vmName))
+        rewriteLog = False
+        
+    return [reprogramDefault, reprogramChain, rewriteLog]
+    
+
+@echo
+def write_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno, vmMac='ff:ff:ff:ff:ff:ff'):
+    vm_name = vmName
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    util.SMlog("Writing log to " + logfilename)
+    logf = open(logfilename, 'w')
+    output = ','.join([vmName, vmID, vmIP, domID, signature, seqno, vmMac])
+    result = True
+    try:
+        logf.write(output)
+        logf.write('\n')
+    except:
+        util.SMlog("Failed to write to rule log file " + logfilename)
+        result = False
+        
+    logf.close()
+    
+    return result
+
+@echo
+def remove_rule_log_for_vm(vmName):
+    vm_name = vmName
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+
+    result = True
+    try:
+        os.remove(logfilename)
+    except:
+        util.SMlog("Failed to delete rule log file " + logfilename)
+        result = False
+    
+    return result
+
+@echo
+def inflate_rules (zipped):
+   return zlib.decompress(base64.b64decode(zipped))
+
+@echo
+def cache_ipset_keyword():
+    tmpname = 'ipsetqzvxtmp'
+    try:
+        util.pread2(['/bin/bash', '-c', 'ipset -N ' + tmpname + ' iptreemap'])
+    except:
+        util.pread2(['/bin/bash', '-c', 'ipset -F ' + tmpname])
+
+    try:
+        util.pread2(['/bin/bash', '-c', 'iptables -A INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
+        util.pread2(['/bin/bash', '-c', 'iptables -D INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
+        keyword = 'set'
+    except:
+        keyword = 'match-set'
+    
+    try:
+       util.pread2(['/bin/bash', '-c', 'ipset -X ' + tmpname])
+    except:
+       pass
+       
+    cachefile = "/var/cache/cloud/ipset.keyword"
+    util.SMlog("Writing ipset keyword to " + cachefile)
+    cachef = open(cachefile, 'w')
+    try:
+        cachef.write(keyword)
+        cachef.write('\n')
+    except:
+        util.SMlog("Failed to write to cache file " + cachef)
+        
+    cachef.close()
+    return keyword
+    
+@echo
+def get_ipset_keyword():
+    cachefile = "/var/cache/cloud/ipset.keyword"
+    keyword = 'match-set'
+    
+    if not os.path.exists(cachefile):
+        util.SMlog("Failed to find ipset keyword cachefile %s" %cachefile)
+        keyword = cache_ipset_keyword()
+    else:
+        lines = (line.rstrip() for line in open(cachefile))
+        for line in lines:
+            keyword = line
+            break
+
+    return keyword
+
+@echo
+def network_rules(session, args):
+  try:
+    vm_name = args.get('vmName')
+    vm_ip = args.get('vmIP')
+    vm_id = args.get('vmID')
+    vm_mac = args.get('vmMAC')
+    signature = args.pop('signature')
+    seqno = args.pop('seqno')
+    deflated = 'false'
+    if 'deflated' in args:
+        deflated = args.pop('deflated')
+    
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vm_name)
+        if len(vm) != 1:
+             util.SMlog("### Could not get record for vm ## " + vm_name)
+             return 'false'
+        vm_rec = session.xenapi.VM.get_record(vm[0])
+        domid = vm_rec.get('domid')
+    except:
+        util.SMlog("### Failed to get domid for vm  ## " + vm_name)
+        return 'false'
+    if domid == '-1':
+        util.SMlog("### Failed to get domid for vm (-1):  " + vm_name)
+        return 'false'
+   
+    vif = "vif" + domid + ".0"
+    tap = "tap" + domid + ".0"
+    vifs = [vif]
+    try:
+        util.pread2(['ifconfig', tap])
+        vifs.append(tap)
+    except:
+        pass
+   
+
+    reason = 'seqno_change_or_sig_change'
+    [reprogramDefault, reprogramChain, rewriteLog] = \
+             check_rule_log_for_vm (vm_name, vm_id, vm_ip, domid, signature, seqno)
+    
+    if not reprogramDefault and not reprogramChain:
+        util.SMlog("No changes detected between current state and received state")
+        reason = 'seqno_same_sig_same'
+        if rewriteLog:
+            reason = 'seqno_increased_sig_same'
+            write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac)
+        util.SMlog("Programming network rules for vm  %s seqno=%s signature=%s guestIp=%s,"\
+               " do nothing, reason=%s" % (vm_name, seqno, signature, vm_ip, reason))
+        return 'true'
+           
+    if not reprogramChain:
+        util.SMlog("###Not programming any ingress rules since no changes detected?")
+        return 'true'
+
+    if reprogramDefault:
+        util.SMlog("Change detected in vmId or vmIp or domId, resetting default rules")
+        default_network_rules(session, args)
+        reason = 'domid_change'
+    
+    rules = args.pop('rules')
+    if deflated.lower() == 'true':
+       rules = inflate_rules (rules)
+    keyword = '--' + get_ipset_keyword() 
+    lines = rules.split(' ')
+
+    util.SMlog("Programming network rules for vm  %s seqno=%s numrules=%s signature=%s guestIp=%s,"\
+              " update iptables, reason=%s" % (vm_name, seqno, len(lines), signature, vm_ip, reason))
+    
+    cmds = []
+    egressrules = 0
+    for line in lines:
+        tokens = line.split(':')
+        if len(tokens) != 5:
+          continue
+        type = tokens[0]
+        protocol = tokens[1]
+        start = tokens[2]
+        end = tokens[3]
+        cidrs = tokens.pop();
+        ips = cidrs.split(",")
+        ips.pop()
+        allow_any = False
+
+        if type == 'E':
+            vmchain = egress_chain_name(vm_name)
+            action = "RETURN"
+            direction = "dst"
+            egressrules = egressrules + 1
+        else:
+            vmchain = chain_name(vm_name)
+            action = "ACCEPT"
+            direction = "src"
+        if  '0.0.0.0/0' in ips:
+            i = ips.index('0.0.0.0/0')
+            del ips[i]
+            allow_any = True
+        range = start + ":" + end
+        if ips:    
+            ipsetname = vmchain + "_" + protocol + "_" + start + "_" + end
+            if start == "-1":
+                ipsetname = vmchain + "_" + protocol + "_any"
+
+            if ipset(ipsetname, protocol, start, end, ips) == False:
+                util.SMlog(" failed to create ipset for rule " + str(tokens))
+
+            if protocol == 'all':
+                iptables = ['iptables', '-I', vmchain, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
+            elif protocol != 'icmp':
+                iptables = ['iptables', '-I', vmchain, '-p',  protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
+            else:
+                range = start + "/" + end
+                if start == "-1":
+                    range = "any"
+                iptables = ['iptables', '-I', vmchain, '-p',  'icmp', '--icmp-type',  range,  '-m', 'set', keyword, ipsetname, direction, '-j', action]
+                
+            cmds.append(iptables)
+            util.SMlog(iptables)
+        
+        if allow_any and protocol != 'all':
+            if protocol != 'icmp':
+                iptables = ['iptables', '-I', vmchain, '-p',  protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-j', action]
+            else:
+                range = start + "/" + end
+                if start == "-1":
+                    range = "any"
+                iptables = ['iptables', '-I', vmchain, '-p',  'icmp', '--icmp-type',  range, '-j', action]
+            cmds.append(iptables)
+            util.SMlog(iptables)
+      
+    vmchain = chain_name(vm_name)        
+    util.pread2(['iptables', '-F', vmchain])
+    egress_vmchain = egress_chain_name(vm_name)        
+    util.pread2(['iptables', '-F', egress_vmchain])
+    
+    for cmd in cmds:
+        util.pread2(cmd)
+        
+    if egressrules == 0 :
+        util.pread2(['iptables', '-A', egress_vmchain, '-j', 'RETURN'])
+    else:
+        util.pread2(['iptables', '-A', egress_vmchain, '-j', 'DROP'])
+   
+    util.pread2(['iptables', '-A', vmchain, '-j', 'DROP'])
+
+    if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac) == False:
+        return 'false'
+    
+    return 'true'
+  except:
+    util.SMlog("Failed to network rule !")
+
+@echo
+def checkRouter(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/getRouterStatus.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+    except:
+        util.SMlog("  check router status fail! ")
+        txt = '' 
+
+    return txt
+
+@echo
+def bumpUpPriority(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/bumpUpPriority.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        util.SMlog("bump up priority fail! ")
+        txt = ''
+
+    return txt
+    
+@echo
+def setDNATRule(session, args):
+    add = args["add"]
+    if add == "false":
+        util.pread2(["iptables", "-t", "nat", "-F"])
+    else:
+        ip = args["ip"]
+        port = args["port"]
+        util.pread2(["iptables", "-t", "nat", "-F"])
+        util.pread2(["iptables", "-t", "nat", "-A", "PREROUTING", "-i", "xenbr0", "-p", "tcp", "--dport", port, "-m", "state", "--state", "NEW", "-j", "DNAT", "--to-destination", ip +":443"])
+    return ""
+
+@echo
+def createISOVHD(session, args):
+    #hack for XCP on ubuntu 12.04, as can't attach iso to a vm
+    vdis = session.xenapi.VDI.get_by_name_label("systemvm-vdi");
+    util.SMlog(vdis)
+    if len(vdis) > 0:
+        vdi_record = session.xenapi.VDI.get_record(vdis[0])
+        vdi_uuid = vdi_record['uuid']
+        return vdi_uuid
+    localsrUUid = args['uuid'];
+    sr = session.xenapi.SR.get_by_uuid(localsrUUid)
+    data = {'name_label': "systemvm-vdi",
+            'SR': sr,
+            'virtual_size': '50000000',
+            'type': 'user',
+            'sharable':False,
+            'read_only':False,
+            'other_config':{},
+            }
+    vdi = session.xenapi.VDI.create(data);
+    vdi_record = session.xenapi.VDI.get_record(vdi)
+
+    vdi_uuid = vdi_record['uuid']
+
+    vms = session.xenapi.VM.get_all()
+    ctrldom = None
+    for vm in vms:
+        dom0 = session.xenapi.VM.get_is_control_domain(vm)
+        if dom0 is False:
+            continue
+        else:
+            ctrldom = vm
+
+    if ctrldom is None:
+        return "Failed"
+
+    vbds = session.xenapi.VM.get_VBDs(ctrldom)
+    if len(vbds) == 0:
+        vbd = session.xenapi.VBD.create({"VDI": vdi, "VM": ctrldom, "type":"Disk", "device": "xvda4",  "bootable": False, "mode": "RW", "userdevice": "4", "empty":False,
+                              "other_config":{}, "qos_algorithm_type":"", "qos_algorithm_params":{}})
+    else:
+        vbd = vbds[0]
+
+    vbdr = session.xenapi.VBD.get_record(vbd)
+    if session.xenapi.VBD.get_currently_attached(vbd) is False:
+        session.xenapi.VBD.plug(vbd)
+        vbdr = session.xenapi.VBD.get_record(vbd)
+    util.pread2(["dd", "if=/usr/share/xcp/packages/iso/systemvm.iso", "of=" + "/dev/" + vbdr["device"]])
+    session.xenapi.VBD.unplug(vbd)
+    session.xenapi.VBD.destroy(vbd)
+    return vdi_uuid
+
+@echo
+def routerProxy(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/router_proxy.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        if txt is None or len(txt) == 0 :
+            txt = 'success'
+    except:
+        util.SMlog("routerProxy command " + sargs + " failed "  )
+        txt = ''
+
+    return txt
+
+@echo
+def getDomRVersion(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, "/usr/lib/cloud/bin/getDomRVersion.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+    except:
+        util.SMlog("  get domR version fail! ")
+        txt = '' 
+
+    return txt
+	
+@echo
+def forceShutdownVM(session, args):
+    domId = args['domId']
+    try:
+        cmd = ["/usr/lib/xcp/debug/xenops", "destroy_domain", "-domid", domId]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+
+@echo
+def create_privatetemplate_from_snapshot(session, args):
+    templatePath = args['templatePath']
+    snapshotPath = args['snapshotPath']
+    tmpltLocalDir = args['tmpltLocalDir']
+    try:
+        cmd = ["bash", "/usr/lib/cloud/bin/create_privatetemplate_from_snapshot.sh",snapshotPath, templatePath, tmpltLocalDir]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+@echo
+def upgrade_snapshot(session, args):
+    templatePath = args['templatePath']
+    snapshotPath = args['snapshotPath']
+    try:
+        cmd = ["bash", "/usr/lib/cloud/bin/upgrate_snapshot.sh",snapshotPath, templatePath]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+@echo
+def copy_vhd_to_secondarystorage(session, args):
+    mountpoint = args['mountpoint']
+    vdiuuid = args['vdiuuid']
+    sruuid = args['sruuid']
+    try:
+        cmd = ["bash", "/usr/lib/cloud/bin/copy_vhd_to_secondarystorage.sh", mountpoint, vdiuuid, sruuid]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+@echo
+def copy_vhd_from_secondarystorage(session, args):
+    mountpoint = args['mountpoint']
+    sruuid = args['sruuid']
+    namelabel = args['namelabel']
+    try:
+        cmd = ["bash", "/usr/lib/cloud/bin/copy_vhd_from_secondarystorage.sh", mountpoint, sruuid, namelabel]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+@echo
+def setup_heartbeat_sr(session, args):
+    host = args['host']
+    sr = args['sr']
+    try:
+        cmd = ["bash", "/usr/lib/cloud/bin/setup_heartbeat_sr.sh", host, sr]
+        txt = util.pread2(cmd)
+    except:
+        txt = ''
+    return txt
+
+@echo
+def setup_heartbeat_file(session, args):
+    host = args['host']
+    sr = args['sr']
+    add = args['add']
+    try:
+        cmd = ["bash", "/usr/lib/cloud/bin/setup_heartbeat_file.sh", host, sr, add]
+        txt = util.pread2(cmd)
+    except:
+        txt = ''
+    return txt
+
+@echo
+def check_heartbeat(session, args):
+    host = args['host']
+    interval = args['interval']
+    try:
+       cmd = ["bash", "/usr/lib/cloud/bin/check_heartbeat.sh", host, interval]
+       txt = util.pread2(cmd)
+    except:
+       txt=''
+    return txt
+    
+   
+@echo
+def heartbeat(session, args):
+    '''
+    host = args['host']
+    interval = args['interval']
+    try: 
+       cmd = ["/bin/bash", "/usr/lib/cloud/bin/launch_hb.sh", host, interval]
+       txt = util.pread2(cmd)
+    except:
+       txt='fail'
+    '''
+    return '> DONE <'
+
+if __name__ == "__main__":
+     XenAPIPlugin.dispatch({"forceShutdownVM":forceShutdownVM, 
+	                        "upgrade_snapshot":upgrade_snapshot, 
+							"create_privatetemplate_from_snapshot":create_privatetemplate_from_snapshot,
+							"copy_vhd_to_secondarystorage":copy_vhd_to_secondarystorage, 
+							"copy_vhd_from_secondarystorage":copy_vhd_from_secondarystorage, 
+							"setup_heartbeat_sr":setup_heartbeat_sr, 
+							"setup_heartbeat_file":setup_heartbeat_file, 
+							"check_heartbeat":check_heartbeat, 
+							"heartbeat": heartbeat, 
+	                        "pingtest": pingtest, 
+							"setup_iscsi":setup_iscsi,  
+                            "getgateway": getgateway, "preparemigration": preparemigration, 
+                            "setIptables": setIptables, "pingdomr": pingdomr, "pingxenserver": pingxenserver,  
+                            "ipassoc": ipassoc, "savePassword": savePassword, 
+                            "saveDhcpEntry": saveDhcpEntry, "setFirewallRule": setFirewallRule, 
+                            "setLoadBalancerRule": setLoadBalancerRule, "createFile": createFile, "deleteFile": deleteFile, 
+                            "networkUsage": networkUsage, "network_rules":network_rules, 
+                            "can_bridge_firewall":can_bridge_firewall, "default_network_rules":default_network_rules,
+                            "destroy_network_rules_for_vm":destroy_network_rules_for_vm, 
+                            "default_network_rules_systemvm":default_network_rules_systemvm, 
+                            "get_rule_logs_for_vms":get_rule_logs_for_vms, 
+                            "setLinkLocalIP":setLinkLocalIP, "lt2p_vpn":lt2p_vpn,
+                            "cleanup_rules":cleanup_rules, "checkRouter":checkRouter,
+                            "bumpUpPriority":bumpUpPriority, "getDomRVersion":getDomRVersion,
+                            "kill_copy_process":kill_copy_process,
+                            "createISOVHD":createISOVHD,
+                            "routerProxy":routerProxy,
+                            "setDNATRule":setDNATRule})


[4/8] Merged vmops and vmopspremium. Rename all xapi plugins to start with cloud-plugin-. Rename vmops to cloud-plugin-generic.

Posted by ah...@apache.org.
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/storagePlugin
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/storagePlugin b/scripts/vm/hypervisor/xenserver/storagePlugin
deleted file mode 100755
index bb03379..0000000
--- a/scripts/vm/hypervisor/xenserver/storagePlugin
+++ /dev/null
@@ -1,71 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Version @VERSION@
-#
-# A plugin for executing script needed by vmops cloud 
-
-import os, sys, time
-import XenAPIPlugin
-sys.path.extend(["/opt/xensource/sm/", "/usr/lib/xcp/sm/", "/usr/local/sbin/", "/sbin/"])
-import util
-import base64
-import socket
-import stat
-import tempfile
-import subprocess
-import zlib
-import urllib2
-import traceback
-
-def echo(fn):
-    def wrapped(*v, **k):
-        name = fn.__name__
-        util.SMlog("#### xen plugin enter  %s ####" % name )
-        res = fn(*v, **k)
-        util.SMlog("#### xen plugin exit  %s ####" % name )
-        return res
-    return wrapped
-
-@echo
-def downloadTemplateFromUrl(session, args):
-    destPath = args["destPath"]
-    srcUrl = args["srcUrl"]
-    try:
-        template = urllib2.urlopen(srcUrl)
-        destFile = open(destPath, "wb")
-        destFile.write(template.read())
-        destFile.close()
-        return "success"
-    except:
-        util.SMlog("exception: " + str(sys.exc_info()))
-        return ""
-    
-@echo
-def getTemplateSize(session, args):
-   srcUrl = args["srcUrl"]
-   try:
-       template = urllib2.urlopen(srcUrl)
-       headers = template.info()
-       return str(headers["Content-Length"])
-   except:
-       return ""
-if __name__ == "__main__":
-    XenAPIPlugin.dispatch({"downloadTemplateFromUrl": downloadTemplateFromUrl
-                           ,"getTemplateSize": getTemplateSize
-                          })

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/swiftxen
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/swiftxen b/scripts/vm/hypervisor/xenserver/swiftxen
deleted file mode 100644
index b9ede19..0000000
--- a/scripts/vm/hypervisor/xenserver/swiftxen
+++ /dev/null
@@ -1,97 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# 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/cloudstack/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})

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/upgrade_snapshot.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/upgrade_snapshot.sh b/scripts/vm/hypervisor/xenserver/upgrade_snapshot.sh
index b2e8685..4cb2e30 100755
--- a/scripts/vm/hypervisor/xenserver/upgrade_snapshot.sh
+++ b/scripts/vm/hypervisor/xenserver/upgrade_snapshot.sh
@@ -87,7 +87,7 @@ if [ $? -ne 0 ]; then
   exit 0
 fi
 
-VHDUTIL="/opt/cloudstack/bin/vhd-util"
+VHDUTIL="/opt/cloud/bin/vhd-util"
 
 upgradeSnapshot()
 {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/vmops
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/vmops b/scripts/vm/hypervisor/xenserver/vmops
deleted file mode 100755
index 742cd95..0000000
--- a/scripts/vm/hypervisor/xenserver/vmops
+++ /dev/null
@@ -1,1648 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Version @VERSION@
-#
-# A plugin for executing script needed by vmops cloud 
-
-import os, sys, time
-import XenAPIPlugin
-sys.path.extend(["/opt/xensource/sm/", "/usr/local/sbin/", "/sbin/"])
-import base64
-import socket
-import stat
-import tempfile
-import util
-import subprocess
-import zlib
-import cloudstack_pluginlib as lib
-import logging
-from util import CommandException
-
-lib.setup_logging("/var/log/vmops.log")
-
-CS_DIR="/opt/cloudstack/bin/"
-
-def echo(fn):
-    def wrapped(*v, **k):
-        name = fn.__name__
-        logging.debug("#### VMOPS enter  %s ####" % name )
-        res = fn(*v, **k)
-        logging.debug("#### VMOPS exit  %s ####" % name )
-        return res
-    return wrapped
-
-@echo
-def add_to_VCPUs_params_live(session, args):
-    key = args['key']
-    value = args['value']
-    vmname = args['vmname']
-    try:
-        cmd = ["bash", CS_DIR + "add_to_vcpus_params_live.sh", vmname, key, value]
-        txt = util.pread2(cmd)
-    except:
-        return 'false'
-    return 'true'
-
-@echo
-def setup_iscsi(session, args):
-   uuid=args['uuid']
-   try:
-       cmd = ["bash", CS_DIR + "setup_iscsi.sh", uuid]
-       txt = util.pread2(cmd)
-   except:
-       txt = ''
-   return txt
- 
-
-@echo
-def getgateway(session, args):
-    mgmt_ip = args['mgmtIP']
-    try:
-        cmd = ["bash", CS_DIR + "network_info.sh", "-g", mgmt_ip]
-        txt = util.pread2(cmd)
-    except:
-        txt = ''
-
-    return txt
-    
-@echo
-def preparemigration(session, args):
-    uuid = args['uuid']
-    try:
-        cmd = [CS_DIR + "make_migratable.sh", uuid]
-        util.pread2(cmd)
-        txt = 'success'
-    except:
-        logging.debug("Catch prepare migration exception" )
-        txt = ''
-
-    return txt
-
-@echo
-def setIptables(session, args):
-    try:
-        cmd = ["/bin/bash", CS_DIR + "setupxenserver.sh"]
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        logging.debug("  setIptables execution failed "  )
-        txt = '' 
-
-    return txt
- 
-@echo
-def pingdomr(session, args):
-    host = args['host']
-    port = args['port']
-    socket.setdefaulttimeout(3)
-    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    try:
-        s.connect((host,int(port)))
-        txt = 'success'
-    except:
-        txt = ''
-    
-    s.close()
-
-    return txt
-
-@echo
-def kill_copy_process(session, args):
-    namelabel = args['namelabel']
-    try:
-        cmd = ["bash", CS_DIR + "kill_copy_process.sh", namelabel]
-        txt = util.pread2(cmd)
-    except:
-        txt = 'false'
-    return txt
-
-@echo
-def pingxenserver(session, args):
-    txt = 'success'
-    return txt
-
-def pingtest(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, CS_DIR + "pingtest.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        logging.debug("  pingtest failed "  )
-        txt = ''
-
-    return txt
-
-@echo
-def savePassword(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, CS_DIR + "save_password_to_domr.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        logging.debug("  save password to domr failed "  )
-        txt = '' 
-
-    return txt
-
-@echo
-def saveDhcpEntry(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, CS_DIR + "dhcp_entry.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        logging.debug(" save dhcp entry failed "  )
-        txt = '' 
-
-    return txt
-    
-@echo
-def setLinkLocalIP(session, args):
-    brName = args['brName']
-    try:
-        cmd = ["ip", "route", "del", "169.254.0.0/16"]
-        txt = util.pread2(cmd)
-    except:
-        txt = '' 
-    try:
-        cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
-        txt = util.pread2(cmd)
-    except:
-        try:
-            cmd = ['cat', '/etc/xensource/network.conf']
-            result = util.pread2(cmd)
-        except:
-            return 'can not cat network.conf'
-
-        if result.lower().strip() == "bridge":
-            try:
-                cmd = ["brctl", "addbr", brName]
-                txt = util.pread2(cmd)
-            except:
-                pass
-
-        else:
-            try:
-                cmd = ["ovs-vsctl", "add-br", brName]
-                txt = util.pread2(cmd)
-            except:
-                pass
-        
-        try:
-            cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
-            txt = util.pread2(cmd)
-        except:
-            pass
-    try:
-        cmd = ["ip", "route", "add", "169.254.0.0/16", "dev", brName, "src", "169.254.0.1"]
-        txt = util.pread2(cmd)
-    except:
-        txt = '' 
-    txt = 'success'
-    return txt
-
-
-    
-@echo
-def setFirewallRule(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, CS_DIR + "call_firewall.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        logging.debug(" set firewall rule failed "  )
-        txt = '' 
-
-    return txt
-    
-@echo
-def routerProxy(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, CS_DIR + "router_proxy.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        if txt is None or len(txt) == 0 :
-            txt = 'success'
-    except:
-        logging.debug("routerProxy command " + sargs + " failed "  )
-        txt = '' 
-
-    return txt
-
-
-
-@echo
-def setLoadBalancerRule(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, CS_DIR + "call_loadbalancer.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        logging.debug(" set loadbalancer rule failed "  )
-        txt = '' 
-
-    return txt
-@echo
-def configdnsmasq(session, args):
-    routerip = args['routerip']
-    args = args['args']
-    target   = "root@"+routerip
-    try:
-       util.pread2(['ssh','-p','3922','-q','-o','StrictHostKeyChecking=no','-i','/root/.ssh/id_rsa.cloud',target,'/root/dnsmasq.sh',args])
-       txt='success'
-    except:
-       logging.debug("failed to config dnsmasq server")
-       txt=''
-    return txt
-
-@echo
-def createipAlias(session, args):
-    args = args['args']
-    cmd = args.split(' ')
-    cmd.insert(0, CS_DIR + "createipAlias.sh")
-    cmd.insert(0, "bin/bash")
-    try:
-       txt=util.pread2(cmd)
-       txt='success'
-    except:
-       logging.debug("failed to create ip alias on router vm")
-       txt=''
-    return txt
-
-@echo
-def deleteipAlias(session, args):
-    args = args['args']
-    cmd = args.split(' ')
-    cmd.insert(0, CS_DIR + "deleteipAlias.sh")
-    cmd.insert(0, "bin/bash")
-    try:
-       txt=util.pread2(cmd)
-       txt='success'
-    except:
-       logging.debug("failed to create ip alias on router vm")
-       txt=''
-    return txt
-
-@echo
-def createFile(session, args):
-    file_path = args['filepath']
-    file_contents = args['filecontents']
-
-    try:
-        f = open(file_path, "w")
-        f.write(file_contents)
-        f.close()
-        txt = 'success'
-    except:
-        logging.debug(" failed to create HA proxy cfg file ")
-        txt = ''
-
-    return txt
-
-@echo
-def createFileInDomr(session, args):
-    file_path = args['filepath']
-    file_contents = args['filecontents']
-    domrip = args['domrip']
-    try:
-        tmpfile = util.pread2(['mktemp']).strip()
-        f = open(tmpfile, "w")
-        f.write(file_contents)
-        f.close()
-        target = "root@" + domrip + ":" + file_path
-        util.pread2(['scp','-P','3922','-q','-o','StrictHostKeyChecking=no','-i','/root/.ssh/id_rsa.cloud',tmpfile, target])
-        util.pread2(['rm',tmpfile])
-        txt = 'success'
-    except:
-        logging.debug(" failed to create HA proxy cfg file ")
-        txt = ''
-
-    return txt
-
-@echo
-def deleteFile(session, args):
-    file_path = args["filepath"]
-
-    try:
-        if os.path.isfile(file_path):
-            os.remove(file_path)
-        txt = 'success'
-    except:
-        logging.debug(" failed to remove HA proxy cfg file ")
-        txt = ''
-
-    return txt
-
-
-    
-def get_private_nic(session, args):
-    vms = session.xenapi.VM.get_all()
-    host_uuid = args.get('host_uuid')
-    host = session.xenapi.host.get_by_uuid(host_uuid)
-    piflist = session.xenapi.host.get_PIFs(host)
-    mgmtnic = 'eth0'
-    for pif in piflist:
-        pifrec = session.xenapi.PIF.get_record(pif)
-        network = pifrec.get('network')
-        nwrec = session.xenapi.network.get_record(network)
-        if nwrec.get('name_label') == 'cloud-guest':
-            return pifrec.get('device')
-        if pifrec.get('management'):
-            mgmtnic = pifrec.get('device')
-    
-    return mgmtnic
-
-def chain_name(vm_name):
-    if vm_name.startswith('i-') or vm_name.startswith('r-'):
-        if vm_name.endswith('untagged'):
-            return '-'.join(vm_name.split('-')[:-1])
-    return vm_name
-
-def chain_name_def(vm_name):
-    if vm_name.startswith('i-'):
-        if vm_name.endswith('untagged'):
-            return '-'.join(vm_name.split('-')[:-2]) + "-def"
-        return '-'.join(vm_name.split('-')[:-1]) + "-def"
-    return vm_name
-  
-def egress_chain_name(vm_name):
-    return chain_name(vm_name) + "-eg"
-      
-@echo
-def can_bridge_firewall(session, args):
-    try:
-        util.pread2(['ebtables', '-V'])
-        util.pread2(['ipset', '-V'])
-        cmd = ['cat', '/etc/xensource/network.conf']
-        result = util.pread2(cmd)
-        if result.lower().strip() != "bridge":
-            return 'false'
-
-    except:
-        return 'false'
-
-    host_uuid = args.get('host_uuid')
-    try:
-        util.pread2(['iptables', '-N', 'BRIDGE-FIREWALL'])
-        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-D', 'FORWARD',  '-j', 'RH-Firewall-1-INPUT'])
-    except:
-        logging.debug('Chain BRIDGE-FIREWALL already exists')
-
-    try:
-        util.pread2(['iptables', '-N', 'BRIDGE-DEFAULT-FIREWALL'])
-        util.pread2(['iptables', '-A', 'BRIDGE-DEFAULT-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-A', 'BRIDGE-DEFAULT-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-A', 'BRIDGE-DEFAULT-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '-j', 'BRIDGE-DEFAULT-FIREWALL'])
-        util.pread2(['iptables', '-D', 'BRIDGE-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-D', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-D', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
-    except:
-        logging.debug('Chain BRIDGE-DEFAULT-FIREWALL already exists')
-
-    privnic = get_private_nic(session, args)
-    result = 'true'
-    try:
-        util.pread2(['/bin/bash', '-c', 'iptables -n -L FORWARD | grep BRIDGE-FIREWALL'])
-    except:
-        try:
-            util.pread2(['iptables', '-I', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '-j', 'BRIDGE-FIREWALL'])
-            util.pread2(['iptables', '-A', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', privnic, '-j', 'ACCEPT'])
-            util.pread2(['iptables', '-A', 'FORWARD', '-j', 'DROP'])
-        except:
-            return 'false'
-    default_ebtables_rules()
-    allow_egress_traffic(session)
-    if not os.path.exists('/var/run/cloud'):
-        os.makedirs('/var/run/cloud')
-    if not os.path.exists('/var/cache/cloud'):
-        os.makedirs('/var/cache/cloud')
-    #get_ipset_keyword()
- 
-    cleanup_rules_for_dead_vms(session)
-    cleanup_rules(session, args)
-    
-    return result
-
-@echo
-def default_ebtables_rules():
-    try:
-        util.pread2(['ebtables', '-N',  'DEFAULT_EBTABLES'])
-        util.pread2(['ebtables', '-A', 'FORWARD', '-j'  'DEFAULT_EBTABLES'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'ACCEPT'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '--ip-proto', 'udp', '--ip-dport', '68', '-j', 'ACCEPT'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Request', '-j', 'ACCEPT'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Reply', '-j', 'ACCEPT'])
-        # deny mac broadcast and multicast
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Broadcast', '-j', 'DROP']) 
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Multicast', '-j', 'DROP']) 
-        # deny ip broadcast and multicast
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'DROP'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '224.0.0.0/4', '-j', 'DROP'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-j', 'RETURN'])
-        # deny ipv6
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv6', '-j', 'DROP'])
-        # deny vlan
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', '802_1Q', '-j', 'DROP'])
-        # deny all others (e.g., 802.1d, CDP)
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES',  '-j', 'DROP'])
-    except:
-        logging.debug('Chain DEFAULT_EBTABLES already exists')
-
-
-@echo
-def allow_egress_traffic(session):
-    devs = []
-    for pif in session.xenapi.PIF.get_all():
-        pif_rec = session.xenapi.PIF.get_record(pif)
-        dev = pif_rec.get('device')
-        devs.append(dev + "+")
-    for d in devs:
-        try:
-            util.pread2(['/bin/bash', '-c', "iptables -n -L FORWARD | grep '%s '" % d])
-        except:
-            try:
-                util.pread2(['iptables', '-I', 'FORWARD', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', d, '-j', 'ACCEPT'])
-            except:
-                logging.debug("Failed to add FORWARD rule through to %s" % d)
-                return 'false'
-    return 'true'
-
-
-def ipset(ipsetname, proto, start, end, ips):
-    try:
-        util.pread2(['ipset', '-N', ipsetname, 'iptreemap'])
-    except:
-        logging.debug("ipset chain already exists" + ipsetname)
-
-    result = True
-    ipsettmp = ''.join(''.join(ipsetname.split('-')).split('_')) + str(int(time.time()) % 1000)
-
-    try: 
-        util.pread2(['ipset', '-N', ipsettmp, 'iptreemap']) 
-    except:
-        logging.debug("Failed to create temp ipset, reusing old name= " + ipsettmp)
-        try: 
-            util.pread2(['ipset', '-F', ipsettmp]) 
-        except:
-            logging.debug("Failed to clear old temp ipset name=" + ipsettmp)
-            return False
-        
-    try: 
-        for ip in ips:
-            try:
-                util.pread2(['ipset', '-A', ipsettmp, ip])
-            except CommandException, cex:
-                if cex.reason.rfind('already in set') == -1:
-                   raise
-    except:
-        logging.debug("Failed to program ipset " + ipsetname)
-        util.pread2(['ipset', '-F', ipsettmp]) 
-        util.pread2(['ipset', '-X', ipsettmp]) 
-        return False
-
-    try: 
-        util.pread2(['ipset', '-W', ipsettmp, ipsetname]) 
-    except:
-        logging.debug("Failed to swap ipset " + ipsetname)
-        result = False
-
-    try: 
-        util.pread2(['ipset', '-F', ipsettmp]) 
-        util.pread2(['ipset', '-X', ipsettmp]) 
-    except:
-        # if the temporary name clashes next time we'll just reuse it
-        logging.debug("Failed to delete temp ipset " + ipsettmp)
-
-    return result
-
-@echo 
-def destroy_network_rules_for_vm(session, args):
-    vm_name = args.pop('vmName')
-    vmchain = chain_name(vm_name)
-    vmchain_egress = egress_chain_name(vm_name)
-    vmchain_default = chain_name_def(vm_name)
-    
-    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
-    if vm_name.startswith('i-') or vm_name.startswith('r-') or vm_name.startswith('l-'):
-        try:
-            util.pread2(['iptables', '-F', vmchain_default])
-            util.pread2(['iptables', '-X', vmchain_default])
-        except:
-            logging.debug("Ignoring failure to delete  chain " + vmchain_default)
-    
-    destroy_ebtables_rules(vmchain)
-    destroy_arptables_rules(vmchain)
-    
-    try:
-        util.pread2(['iptables', '-F', vmchain])
-        util.pread2(['iptables', '-X', vmchain])
-    except:
-        logging.debug("Ignoring failure to delete ingress chain " + vmchain)
-        
-   
-    try:
-        util.pread2(['iptables', '-F', vmchain_egress])
-        util.pread2(['iptables', '-X', vmchain_egress])
-    except:
-        logging.debug("Ignoring failure to delete egress chain " + vmchain_egress)
-    
-    remove_rule_log_for_vm(vm_name)
-    remove_secip_log_for_vm(vm_name)
-    
-    if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
-        return 'true'
-    
-    try:
-        setscmd = "ipset --save | grep " +  vmchain + " | grep '^-N' | awk '{print $2}'"
-        setsforvm = util.pread2(['/bin/bash', '-c', setscmd]).split('\n')
-        for set in setsforvm:
-            if set != '':
-                util.pread2(['ipset', '-F', set])       
-                util.pread2(['ipset', '-X', set])       
-    except:
-        logging.debug("Failed to destroy ipsets for %" % vm_name)
-    
-    
-    return 'true'
-
-@echo
-def destroy_ebtables_rules(vm_chain):
-    
-    delcmd = "ebtables-save | grep " +  vm_chain + " | sed 's/-A/-D/'"
-    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
-    delcmds.pop()
-    for cmd in delcmds:
-        try:
-            dc = cmd.split(' ')
-            dc.insert(0, 'ebtables')
-            util.pread2(dc)
-        except:
-            logging.debug("Ignoring failure to delete ebtables rules for vm " + vm_chain)
-    try:
-        util.pread2(['ebtables', '-F', vm_chain])
-        util.pread2(['ebtables', '-X', vm_chain])
-    except:
-            logging.debug("Ignoring failure to delete ebtables chain for vm " + vm_chain)   
-
-@echo
-def destroy_arptables_rules(vm_chain):
-    delcmd = "arptables -vL FORWARD | grep " + vm_chain + " | sed 's/-i any//' | sed 's/-o any//' | awk '{print $1,$2,$3,$4}' "
-    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
-    delcmds.pop()
-    for cmd in delcmds:
-        try:
-            dc = cmd.split(' ')
-            dc.insert(0, 'arptables')
-            dc.insert(1, '-D')
-            dc.insert(2, 'FORWARD')
-            util.pread2(dc)
-        except:
-            logging.debug("Ignoring failure to delete arptables rules for vm " + vm_chain)
-    
-    try:
-        util.pread2(['arptables', '-F', vm_chain])
-        util.pread2(['arptables', '-X', vm_chain])
-    except:
-        logging.debug("Ignoring failure to delete arptables chain for vm " + vm_chain) 
-              
-@echo
-def default_ebtables_antispoof_rules(vm_chain, vifs, vm_ip, vm_mac):
-    if vm_mac == 'ff:ff:ff:ff:ff:ff':
-        logging.debug("Ignoring since mac address is not valid")
-        return 'true'
-    
-    try:
-        util.pread2(['ebtables', '-N', vm_chain])
-    except:
-        try:
-            util.pread2(['ebtables', '-F', vm_chain])
-        except:
-            logging.debug("Failed to create ebtables antispoof chain, skipping")
-            return 'true'
-
-    # note all rules for packets into the bridge (-i) precede all output rules (-o)
-    # always start after the first rule in the FORWARD chain that jumps to DEFAULT_EBTABLES chain
-    try:
-        for vif in vifs:
-            util.pread2(['ebtables', '-I', 'FORWARD', '2', '-i',  vif,  '-j', vm_chain])
-            util.pread2(['ebtables', '-A', 'FORWARD', '-o',  vif, '-j', vm_chain])
-    except:
-        logging.debug("Failed to program default ebtables FORWARD rules for %s" % vm_chain)
-        return 'false'
-
-    try:
-        for vif in vifs:
-            # only allow source mac that belongs to the vm
-            try:
-                util.pread2(['ebtables', '-t', 'nat', '-I', 'PREROUTING', '-i', vif, '-s', '!' , vm_mac, '-j', 'DROP'])
-            except:
-                util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-s', '!', vm_mac,  '-j', 'DROP'])
-
-            # do not allow fake dhcp responses
-            util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '68', '-j', 'DROP'])
-            # do not allow snooping of dhcp requests
-            util.pread2(['ebtables', '-A', vm_chain, '-o', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'DROP'])
-    except:
-        logging.debug("Failed to program default ebtables antispoof rules for %s" % vm_chain)
-        return 'false'
-
-    return 'true'
-
-@echo
-def default_arp_antispoof(vm_chain, vifs, vm_ip, vm_mac):
-    if vm_mac == 'ff:ff:ff:ff:ff:ff':
-        logging.debug("Ignoring since mac address is not valid")
-        return 'true'
-
-    try:
-        util.pread2(['arptables',  '-N', vm_chain])
-    except:
-        try:
-            util.pread2(['arptables', '-F', vm_chain])
-        except:
-            logging.debug("Failed to create arptables rule, skipping")
-            return 'true'
-
-    # note all rules for packets into the bridge (-i) precede all output rules (-o)
-    try:
-        for vif in vifs:
-           util.pread2(['arptables',  '-I', 'FORWARD', '-i',  vif, '-j', vm_chain])
-           util.pread2(['arptables',  '-A', 'FORWARD', '-o',  vif, '-j', vm_chain])
-    except:
-        logging.debug("Failed to program default arptables rules in FORWARD chain vm=" + vm_chain)
-        return 'false'
-    
-    try:
-        for vif in vifs:
-            #accept arp replies into the bridge as long as the source mac and ips match the vm
-            util.pread2(['arptables',  '-A', vm_chain, '-i', vif, '--opcode', 'Reply', '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'ACCEPT'])
-            #accept any arp requests from this vm. In the future this can be restricted to deny attacks on hosts
-            #also important to restrict source ip and src mac in these requests as they can be used to update arp tables on destination
-            util.pread2(['arptables',  '-A', vm_chain, '-i', vif, '--opcode', 'Request',  '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'RETURN'])
-            #accept any arp requests to this vm as long as the request is for this vm's ip
-            util.pread2(['arptables',  '-A', vm_chain, '-o', vif, '--opcode', 'Request', '--destination-ip', vm_ip, '-j', 'ACCEPT'])   
-            #accept any arp replies to this vm as long as the mac and ip matches
-            util.pread2(['arptables',  '-A', vm_chain, '-o', vif, '--opcode', 'Reply', '--destination-mac', vm_mac, '--destination-ip', vm_ip, '-j', 'ACCEPT'])   
-        util.pread2(['arptables',  '-A', vm_chain,  '-j', 'DROP'])
-
-    except:
-        logging.debug("Failed to program default arptables  rules")
-        return 'false'
-
-    return 'true'
-
-
-@echo
-def network_rules_vmSecondaryIp(session, args):
-    vm_name = args.pop('vmName')
-    vm_mac = args.pop('vmMac')
-    ip_secondary = args.pop('vmSecIp')
-    action = args.pop('action')
-    logging.debug("vmMac = "+ vm_mac)
-    logging.debug("vmName = "+ vm_name)
-    #action = "-A"
-    logging.debug("action = "+ action)
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vm_name)
-        if len(vm) != 1:
-             return 'false'
-        vm_rec = session.xenapi.VM.get_record(vm[0])
-        vm_vifs = vm_rec.get('VIFs')
-        vifnums = [session.xenapi.VIF.get_record(vif).get('device') for vif in vm_vifs]
-        domid = vm_rec.get('domid')
-    except:
-        logging.debug("### Failed to get domid or vif list for vm  ##" + vm_name)
-        return 'false'
-
-    if domid == '-1':
-        logging.debug("### Failed to get domid for vm (-1):  " + vm_name)
-        return 'false'
-
-    vifs = ["vif" + domid + "." + v for v in vifnums]
-    #vm_name =  '-'.join(vm_name.split('-')[:-1])
-    vmchain = chain_name(vm_name)
-    add_to_ipset(vmchain, [ip_secondary], action)
-
-    #add arptables rules for the secondary ip
-    arp_rules_vmip(vmchain, vifs, [ip_secondary], vm_mac, action)
-
-    return 'true'
-
-@echo
-def default_network_rules_systemvm(session, args):
-    vm_name = args.pop('vmName')
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vm_name)
-        if len(vm) != 1:
-             return 'false'
-        vm_rec = session.xenapi.VM.get_record(vm[0])
-        vm_vifs = vm_rec.get('VIFs')
-        vifnums = [session.xenapi.VIF.get_record(vif).get('device') for vif in vm_vifs]
-        domid = vm_rec.get('domid')
-    except:
-        logging.debug("### Failed to get domid or vif list for vm  ##" + vm_name)
-        return 'false'
-    
-    if domid == '-1':
-        logging.debug("### Failed to get domid for vm (-1):  " + vm_name)
-        return 'false'
-
-    vifs = ["vif" + domid + "." + v for v in vifnums]
-    #vm_name =  '-'.join(vm_name.split('-')[:-1])
-    vmchain = chain_name(vm_name)
-   
- 
-    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
-  
-    try:
-        util.pread2(['iptables', '-N', vmchain])
-    except:
-        util.pread2(['iptables', '-F', vmchain])
-    
-    for vif in vifs:
-        try:
-            util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', vif, '-j', vmchain])
-            util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', vmchain])
-            util.pread2(['iptables', '-I', vmchain, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', 'RETURN'])
-        except:
-            logging.debug("Failed to program default rules")
-            return 'false'
-	
-	
-    util.pread2(['iptables', '-A', vmchain, '-j', 'ACCEPT'])
-    
-    if write_rule_log_for_vm(vm_name, '-1', '_ignore_', domid, '_initial_', '-1') == False:
-        logging.debug("Failed to log default network rules for systemvm, ignoring")
-    return 'true'
-
-@echo
-def create_ipset_forvm (ipsetname):
-    result = True
-    try:
-        logging.debug("Creating ipset chain .... " + ipsetname)
-        util.pread2(['ipset', '-F', ipsetname])
-        util.pread2(['ipset', '-X', ipsetname])
-        util.pread2(['ipset', '-N', ipsetname, 'iphash'])
-    except:
-        logging.debug("ipset chain not exists creating.... " + ipsetname)
-        util.pread2(['ipset', '-N', ipsetname, 'iphash'])
-
-    return result
-
-@echo
-def add_to_ipset(ipsetname, ips, action):
-    result = True
-    for ip in ips:
-        try:
-            logging.debug("vm ip " + ip)
-            util.pread2(['ipset', action, ipsetname, ip])
-        except:
-            logging.debug("vm ip alreday in ip set" + ip)
-            continue
-
-    return result
-
-@echo
-def arp_rules_vmip (vm_chain, vifs, ips, vm_mac, action):
-    try:
-        if action == "-A":
-            action = "-I"
-        for vif in vifs:
-            for vm_ip in ips:
-                #accept any arp requests to this vm as long as the request is for this vm's ip
-                util.pread2(['arptables',  action, vm_chain, '-o', vif, '--opcode', 'Request', '--destination-ip', vm_ip, '-j', 'ACCEPT'])
-                #accept any arp replies to this vm as long as the mac and ip matches
-                util.pread2(['arptables',  action, vm_chain, '-o', vif, '--opcode', 'Reply', '--destination-mac', vm_mac, '--destination-ip', vm_ip, '-j', 'ACCEPT'])
-                #accept arp replies into the bridge as long as the source mac and ips match the vm
-                util.pread2(['arptables',  action, vm_chain, '-i', vif, '--opcode', 'Reply', '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'ACCEPT'])
-                #accept any arp requests from this vm. In the future this can be restricted to deny attacks on hosts
-                #also important to restrict source ip and src mac in these requests as they can be used to update arp tables on destination
-                util.pread2(['arptables',  action, vm_chain, '-i', vif, '--opcode', 'Request',  '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'RETURN'])
-    except:
-        logging.debug("Failed to program arptables  rules for ip")
-        return 'false'
-
-    return 'true'
-
-
-@echo
-def default_network_rules(session, args):
-    vm_name = args.pop('vmName')
-    vm_ip = args.pop('vmIP')
-    vm_id = args.pop('vmID')
-    vm_mac = args.pop('vmMAC')
-    sec_ips = args.pop("secIps")
-    action = "-A"
-    
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vm_name)
-        if len(vm) != 1:
-             logging.debug("### Failed to get record for vm  " + vm_name)
-             return 'false'
-        vm_rec = session.xenapi.VM.get_record(vm[0])
-        domid = vm_rec.get('domid')
-    except:
-        logging.debug("### Failed to get domid for vm " + vm_name)
-        return 'false'
-    if domid == '-1':     
-        logging.debug("### Failed to get domid for vm (-1):  " + vm_name)
-        return 'false'
-    
-    vif = "vif" + domid + ".0"
-    tap = "tap" + domid + ".0"
-    vifs = [vif]
-    try:
-        util.pread2(['ifconfig', tap])
-        vifs.append(tap)
-    except:
-        pass
-
-    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
-
-     
-    vmchain =  chain_name(vm_name)
-    vmchain_egress =  egress_chain_name(vm_name)
-    vmchain_default = chain_name_def(vm_name)
-    
-    destroy_ebtables_rules(vmchain)
-    
-
-    try:
-        util.pread2(['iptables', '-N', vmchain])
-    except:
-        util.pread2(['iptables', '-F', vmchain])
-    
-    try:
-        util.pread2(['iptables', '-N', vmchain_egress])
-    except:
-        util.pread2(['iptables', '-F', vmchain_egress])
-        
-    try:
-        util.pread2(['iptables', '-N', vmchain_default])
-    except:
-        util.pread2(['iptables', '-F', vmchain_default])        
-
-    vmipset = vm_name
-    #create ipset and add vm ips to that ip set
-    if create_ipset_forvm(vmipset) == False:
-       logging.debug(" failed to create ipset for rule " + str(tokens))
-       return 'false'
-
-    #add primary nic ip to ipset
-    if add_to_ipset(vmipset, [vm_ip], action ) == False:
-       logging.debug(" failed to add vm " + vm_ip + " ip to set ")
-       return 'false'
-
-    #add secodnary nic ips to ipset
-    secIpSet = "1"
-    ips = sec_ips.split(':')
-    ips.pop()
-    if ips[0] == "0":
-        secIpSet = "0";
-
-    if secIpSet == "1":
-        logging.debug("Adding ipset for secondary ips")
-        add_to_ipset(vmipset, ips, action)
-        if write_secip_log_for_vm(vm_name, sec_ips, vm_id) == False:
-            logging.debug("Failed to log default network rules, ignoring")
-
-    keyword = '--' + get_ipset_keyword()
-
-    try:
-        for v in vifs:
-            util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
-            util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
-
-        #don't let vm spoof its ip address
-        for v in vifs:
-            #util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip,'-p', 'udp', '--dport', '53', '-j', 'RETURN'])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-m', 'set', keyword, vmipset, 'src', '-p', 'udp', '--dport', '53', '-j', 'RETURN'])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-m', 'set', '!', keyword, vmipset, 'src', '-j', 'DROP'])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-m', 'set', '!', keyword, vmipset, 'dst', '-j', 'DROP'])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-m', 'set', keyword, vmipset, 'src', '-j', vmchain_egress])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v,  '-j', vmchain])
-    except:
-        logging.debug("Failed to program default rules for vm " + vm_name)
-        return 'false'
-    
-    default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
-    #add default arp rules for secondary ips;
-    if secIpSet == "1":
-        logging.debug("Adding arp rules for sec ip")
-        arp_rules_vmip(vmchain, vifs, ips, vm_mac, action)
-
-    default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
-    
-    if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, '_initial_', '-1', vm_mac) == False:
-        logging.debug("Failed to log default network rules, ignoring")
-        
-    logging.debug("Programmed default rules for vm " + vm_name)
-    return 'true'
-
-@echo
-def check_domid_changed(session, vmName):
-    curr_domid = '-1'
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vmName)
-        if len(vm) != 1:
-             logging.debug("### Could not get record for vm ## " + vmName)
-        else:
-            vm_rec = session.xenapi.VM.get_record(vm[0])
-            curr_domid = vm_rec.get('domid')
-    except:
-        logging.debug("### Failed to get domid for vm  ## " + vmName)
-        
-    
-    logfilename = "/var/run/cloud/" + vmName +".log"
-    if not os.path.exists(logfilename):
-        return ['-1', curr_domid]
-    
-    lines = (line.rstrip() for line in open(logfilename))
-    
-    [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno, _vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
-    for line in lines:
-        try:
-            [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno,_vmMac] = line.split(',')
-        except ValueError,v:
-            [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno] = line.split(',')
-        break
-    
-    return [curr_domid, old_domid]
-
-@echo
-def delete_rules_for_vm_in_bridge_firewall_chain(vmName):
-    vm_name = vmName
-    vmchain = chain_name_def(vm_name)
-    
-    delcmd = "iptables-save | grep '\-A BRIDGE-FIREWALL' | grep " +  vmchain + " | sed 's/-A/-D/'"
-    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
-    delcmds.pop()
-    for cmd in delcmds:
-        try:
-            dc = cmd.split(' ')
-            dc.insert(0, 'iptables')
-            dc.pop()
-            util.pread2(filter(None, dc))
-        except:
-              logging.debug("Ignoring failure to delete rules for vm " + vmName)
-
-  
-@echo
-def network_rules_for_rebooted_vm(session, vmName):
-    vm_name = vmName
-    [curr_domid, old_domid] = check_domid_changed(session, vm_name)
-    
-    if curr_domid == old_domid:
-        return True
-    
-    if old_domid == '-1':
-        return True
-    
-    if curr_domid == '-1':
-        return True
-    
-    logging.debug("Found a rebooted VM -- reprogramming rules for  " + vm_name)
-    
-    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
-    if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
-        default_network_rules_systemvm(session, {"vmName":vm_name})
-        return True
-    
-    vif = "vif" + curr_domid + ".0"
-    tap = "tap" + curr_domid + ".0"
-    vifs = [vif]
-    try:
-        util.pread2(['ifconfig', tap])
-        vifs.append(tap)
-    except:
-        pass
-    vmchain = chain_name(vm_name)
-    vmchain_default = chain_name_def(vm_name)
-
-    for v in vifs:
-        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
-        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
-
-    #change antispoof rule in vmchain
-    try:
-        delcmd = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-in | sed 's/!--set/! --set/' | sed 's/-A/-D/'"
-        delcmd2 = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-out | sed 's/!--set/! --set/'| sed 's/-A/-D/'"
-        inscmd = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-in | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' | sed 's/!--set/! --set/'"
-        inscmd2 = "iptables-save| grep '\-A " +  vmchain_default + "' | grep  physdev-in | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/' | sed 's/!--set/! --set/'"
-        inscmd3 = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-out | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' | sed 's/!--set/! --set/'"
-        inscmd4 = "iptables-save| grep '\-A " +  vmchain_default + "' | grep  physdev-out | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/'  | sed 's/!--set/! --set/'"
-        
-        ipts = []
-        for cmd in [delcmd, delcmd2, inscmd, inscmd2, inscmd3, inscmd4]:
-            cmds = util.pread2(['/bin/bash', '-c', cmd]).split('\n')
-            cmds.pop()
-            for c in cmds:
-                    ipt = c.split(' ')
-                    ipt.insert(0, 'iptables')
-                    ipt.pop()
-                    ipts.append(ipt)
-        
-        for ipt in ipts:
-            try:
-                util.pread2(filter(None,ipt))
-            except:
-                logging.debug("Failed to rewrite antispoofing rules for vm " + vm_name)
-    except:
-        logging.debug("No rules found for vm " + vm_name)
-
-    destroy_ebtables_rules(vmchain)
-    destroy_arptables_rules(vmchain)
-    [vm_ip, vm_mac] = get_vm_mac_ip_from_log(vmchain)
-    default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
-
-    #check wether the vm has secondary ips
-    if is_secondary_ips_set(vm_name) == True:
-        vmips = get_vm_sec_ips(vm_name)
-        #add arp rules for the secondaryp ip
-        for ip in vmips:
-            arp_rules_vmip(vmchain, vifs, [ip], vm_mac, "-A")
-
-
-    default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
-    rewrite_rule_log_for_vm(vm_name, curr_domid)
-    return True
-
-
-
-@echo
-def get_vm_sec_ips(vm_name):
-    logfilename = "/var/run/cloud/" + vm_name +".ip"
-
-    lines = (line.rstrip() for line in open(logfilename))
-    for line in lines:
-        try:
-            [_vmName,_vmIP,_vmID] = line.split(',')
-            break
-        except ValueError,v:
-            [_vmName,_vmIP,_vmID] = line.split(',')
-
-    _vmIPS = _vmIP.split(":")[:-1]
-    return _vmIPS
-
-@echo
-def is_secondary_ips_set(vm_name):
-    logfilename = "/var/run/cloud/" + vm_name +".ip"
-    if not os.path.exists(logfilename):
-        return False
-
-    return True
-
-@echo
-def rewrite_rule_log_for_vm(vm_name, new_domid):
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    if not os.path.exists(logfilename):
-        return 
-    lines = (line.rstrip() for line in open(logfilename))
-    
-    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
-    for line in lines:
-        try:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
-            break
-        except ValueError,v:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
-    
-    write_rule_log_for_vm(_vmName, _vmID, _vmIP, new_domid, _signature, '-1', _vmMac)
-
-def get_rule_log_for_vm(session, vmName):
-    vm_name = vmName;
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    if not os.path.exists(logfilename):
-        return ''
-    
-    lines = (line.rstrip() for line in open(logfilename))
-    
-    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
-    for line in lines:
-        try:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
-            break
-        except ValueError,v:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
-    
-    return ','.join([_vmName, _vmID, _vmIP, _domID, _signature, _seqno])
-
-@echo
-def get_vm_mac_ip_from_log(vm_name):
-    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '0.0.0.0', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    if not os.path.exists(logfilename):
-        return ['_', '_']
-    
-    lines = (line.rstrip() for line in open(logfilename))
-    for line in lines:
-        try:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
-            break
-        except ValueError,v:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
-    
-    return [ _vmIP, _vmMac]
-
-@echo
-def get_rule_logs_for_vms(session, args):
-    host_uuid = args.pop('host_uuid')
-    try:
-        thishost = session.xenapi.host.get_by_uuid(host_uuid)
-        hostrec = session.xenapi.host.get_record(thishost)
-        vms = hostrec.get('resident_VMs')
-    except:
-        logging.debug("Failed to get host from uuid " + host_uuid)
-        return ' '
-    
-    result = []
-    try:
-        for name in [session.xenapi.VM.get_name_label(x) for x in vms]:
-            if 1 not in [ name.startswith(c) for c in ['r-', 's-', 'v-', 'i-', 'l-'] ]:
-                continue
-            network_rules_for_rebooted_vm(session, name)
-            if name.startswith('i-'):
-                log = get_rule_log_for_vm(session, name)
-                result.append(log)
-    except:
-        logging.debug("Failed to get rule logs, better luck next time!")
-        
-    return ";".join(result)
-
-@echo
-def cleanup_rules_for_dead_vms(session):
-  try:
-    vms = session.xenapi.VM.get_all()
-    cleaned = 0
-    for vm_name in [session.xenapi.VM.get_name_label(x) for x in vms]:
-        if 1 in [ vm_name.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-'] ]:
-            vm = session.xenapi.VM.get_by_name_label(vm_name)
-            if len(vm) != 1:
-                continue
-            vm_rec = session.xenapi.VM.get_record(vm[0])
-            state = vm_rec.get('power_state')
-            if state != 'Running' and state != 'Paused':
-                logging.debug("vm " + vm_name + " is not running, cleaning up")
-                destroy_network_rules_for_vm(session, {'vmName':vm_name})
-                cleaned = cleaned+1
-                
-    logging.debug("Cleaned up rules for " + str(cleaned) + " vms")
-  except:
-    logging.debug("Failed to cleanup rules for dead vms!")
-        
-
-@echo
-def cleanup_rules(session, args):
-  instance = args.get('instance')
-  if not instance:
-    instance = 'VM'
-  resident_vms = []
-  try:
-    hostname = util.pread2(['/bin/bash', '-c', 'hostname']).split('\n')
-    if len(hostname) < 1:
-       raise Exception('Could not find hostname of this host')
-    thishost = session.xenapi.host.get_by_name_label(hostname[0])
-    if len(thishost) < 1:
-       raise Exception("Could not find host record from hostname %s of this host"%hostname[0])
-    hostrec = session.xenapi.host.get_record(thishost[0])
-    vms = hostrec.get('resident_VMs')
-    resident_vms = [session.xenapi.VM.get_name_label(x) for x in vms]
-    logging.debug('cleanup_rules: found %s resident vms on this host %s' % (len(resident_vms)-1, hostname[0]))
- 
-    chainscmd = "iptables-save | grep '^:' | awk '{print $1}' | cut -d':' -f2 | sed 's/-def/-%s/'| sed 's/-eg//' | sort|uniq" % instance
-    chains = util.pread2(['/bin/bash', '-c', chainscmd]).split('\n')
-    vmchains = [ch  for ch in chains if 1 in [ ch.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-']]]
-    logging.debug('cleanup_rules: found %s iptables chains for vms on this host %s' % (len(vmchains), hostname[0]))
-    cleaned = 0
-    cleanup = []
-    for chain in vmchains:
-        vmname = chain
-        if vmname not in resident_vms:
-            vmname = chain + "-untagged"
-            if vmname not in resident_vms:
-                logging.debug("vm " + chain + " is not running on this host, cleaning up")
-                cleanup.append(chain)
-
-    for vm_name in cleanup:
-        destroy_network_rules_for_vm(session, {'vmName':vm_name})
-                    
-    logging.debug("Cleaned up rules for " + str(len(cleanup)) + " chains")
-    return str(len(cleanup))                
-  except Exception, ex:
-    logging.debug("Failed to cleanup rules, reason= " + str(ex))
-    return '-1';
-
-@echo
-def check_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno):
-    vm_name = vmName;
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    if not os.path.exists(logfilename):
-        logging.debug("Failed to find logfile %s" %logfilename)
-        return [True, True, True]
-        
-    lines = (line.rstrip() for line in open(logfilename))
-    
-    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
-    try:
-        for line in lines:
-            try:
-                [_vmName,_vmID,_vmIP,_domID,_signature,_seqno, _vmMac] = line.split(',')
-            except ValueError,v:
-                [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
-            break
-    except:
-        logging.debug("Failed to parse log file for vm " + vmName)
-        remove_rule_log_for_vm(vmName)
-        return [True, True, True]
-    
-    reprogramDefault = False
-    if (domID != _domID) or (vmID != _vmID) or (vmIP != _vmIP):
-        logging.debug("Change in default info set of vm %s" % vmName)
-        return [True, True, True]
-    else:
-        logging.debug("No change in default info set of vm %s" % vmName)
-    
-    reprogramChain = False
-    rewriteLog = True
-    if (int(seqno) > int(_seqno)):
-        if (_signature != signature):
-            reprogramChain = True
-            logging.debug("Seqno increased from %s to %s: reprogamming "\
-                        "ingress rules for vm %s" % (_seqno, seqno, vmName))
-        else:
-            logging.debug("Seqno increased from %s to %s: but no change "\
-                        "in signature for vm: skip programming ingress "\
-                        "rules %s" % (_seqno, seqno, vmName))
-    elif (int(seqno) < int(_seqno)):
-        logging.debug("Seqno decreased from %s to %s: ignoring these "\
-                        "ingress rules for vm %s" % (_seqno, seqno, vmName))
-        rewriteLog = False
-    elif (signature != _signature):
-        logging.debug("Seqno %s stayed the same but signature changed from "\
-                    "%s to %s for vm %s" % (seqno, _signature, signature, vmName))
-        rewriteLog = True
-        reprogramChain = True
-    else:
-        logging.debug("Seqno and signature stayed the same: %s : ignoring these "\
-                        "ingress rules for vm %s" % (seqno, vmName))
-        rewriteLog = False
-        
-    return [reprogramDefault, reprogramChain, rewriteLog]
-    
-@echo
-def write_secip_log_for_vm (vmName, secIps, vmId):
-    vm_name = vmName
-    logfilename = "/var/run/cloud/"+vm_name+".ip"
-    logging.debug("Writing log to " + logfilename)
-    logf = open(logfilename, 'w')
-    output = ','.join([vmName, secIps, vmId])
-    result = True
-
-    try:
-        logf.write(output)
-        logf.write('\n')
-    except:
-        logging.debug("Failed to write to rule log file " + logfilename)
-        result = False
-
-    logf.close()
-
-    return result
-
-@echo
-def remove_secip_log_for_vm(vmName):
-    vm_name = vmName
-    logfilename = "/var/run/cloud/"+vm_name+".ip"
-
-    result = True
-    try:
-        os.remove(logfilename)
-    except:
-        logging.debug("Failed to delete rule log file " + logfilename)
-        result = False
-
-    return result
-
-@echo
-def write_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno, vmMac='ff:ff:ff:ff:ff:ff'):
-    vm_name = vmName
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    logging.debug("Writing log to " + logfilename)
-    logf = open(logfilename, 'w')
-    output = ','.join([vmName, vmID, vmIP, domID, signature, seqno, vmMac])
-    result = True
-    try:
-        logf.write(output)
-        logf.write('\n')
-    except:
-        logging.debug("Failed to write to rule log file " + logfilename)
-        result = False
-        
-    logf.close()
-    
-    return result
-
-@echo
-def remove_rule_log_for_vm(vmName):
-    vm_name = vmName
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-
-    result = True
-    try:
-        os.remove(logfilename)
-    except:
-        logging.debug("Failed to delete rule log file " + logfilename)
-        result = False
-    
-    return result
-
-@echo
-def inflate_rules (zipped):
-   return zlib.decompress(base64.b64decode(zipped))
-
-@echo
-def cache_ipset_keyword():
-    tmpname = 'ipsetqzvxtmp'
-    try:
-        util.pread2(['/bin/bash', '-c', 'ipset -N ' + tmpname + ' iptreemap'])
-    except:
-        util.pread2(['/bin/bash', '-c', 'ipset -F ' + tmpname])
-
-    try:
-        util.pread2(['/bin/bash', '-c', 'iptables -A INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
-        util.pread2(['/bin/bash', '-c', 'iptables -D INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
-        keyword = 'set'
-    except:
-        keyword = 'match-set'
-    
-    try:
-       util.pread2(['/bin/bash', '-c', 'ipset -X ' + tmpname])
-    except:
-       pass
-       
-    cachefile = "/var/cache/cloud/ipset.keyword"
-    logging.debug("Writing ipset keyword to " + cachefile)
-    cachef = open(cachefile, 'w')
-    try:
-        cachef.write(keyword)
-        cachef.write('\n')
-    except:
-        logging.debug("Failed to write to cache file " + cachef)
-        
-    cachef.close()
-    return keyword
-    
-@echo
-def get_ipset_keyword():
-    cachefile = "/var/cache/cloud/ipset.keyword"
-    keyword = 'match-set'
-    
-    if not os.path.exists(cachefile):
-        logging.debug("Failed to find ipset keyword cachefile %s" %cachefile)
-        keyword = cache_ipset_keyword()
-    else:
-        lines = (line.rstrip() for line in open(cachefile))
-        for line in lines:
-            keyword = line
-            break
-
-    return keyword
-
-@echo
-def network_rules(session, args):
-  try:
-    vm_name = args.get('vmName')
-    vm_ip = args.get('vmIP')
-    vm_id = args.get('vmID')
-    vm_mac = args.get('vmMAC')
-    signature = args.pop('signature')
-    seqno = args.pop('seqno')
-    sec_ips = args.get("secIps")
-    deflated = 'false'
-    if 'deflated' in args:
-        deflated = args.pop('deflated')
-    
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vm_name)
-        if len(vm) != 1:
-             logging.debug("### Could not get record for vm ## " + vm_name)
-             return 'false'
-        vm_rec = session.xenapi.VM.get_record(vm[0])
-        domid = vm_rec.get('domid')
-    except:
-        logging.debug("### Failed to get domid for vm  ## " + vm_name)
-        return 'false'
-    if domid == '-1':
-        logging.debug("### Failed to get domid for vm (-1):  " + vm_name)
-        return 'false'
-   
-    vif = "vif" + domid + ".0"
-    tap = "tap" + domid + ".0"
-    vifs = [vif]
-    try:
-        util.pread2(['ifconfig', tap])
-        vifs.append(tap)
-    except:
-        pass
-   
-
-    reason = 'seqno_change_or_sig_change'
-    [reprogramDefault, reprogramChain, rewriteLog] = \
-             check_rule_log_for_vm (vm_name, vm_id, vm_ip, domid, signature, seqno)
-    
-    if not reprogramDefault and not reprogramChain:
-        logging.debug("No changes detected between current state and received state")
-        reason = 'seqno_same_sig_same'
-        if rewriteLog:
-            reason = 'seqno_increased_sig_same'
-            write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac)
-        logging.debug("Programming network rules for vm  %s seqno=%s signature=%s guestIp=%s,"\
-               " do nothing, reason=%s" % (vm_name, seqno, signature, vm_ip, reason))
-        return 'true'
-           
-    if not reprogramChain:
-        logging.debug("###Not programming any ingress rules since no changes detected?")
-        return 'true'
-
-    if reprogramDefault:
-        logging.debug("Change detected in vmId or vmIp or domId, resetting default rules")
-        default_network_rules(session, args)
-        reason = 'domid_change'
-    
-    rules = args.pop('rules')
-    if deflated.lower() == 'true':
-       rules = inflate_rules (rules)
-    keyword = '--' + get_ipset_keyword() 
-    lines = rules.split(' ')
-
-    logging.debug("Programming network rules for vm  %s seqno=%s numrules=%s signature=%s guestIp=%s,"\
-              " update iptables, reason=%s" % (vm_name, seqno, len(lines), signature, vm_ip, reason))
-    
-    cmds = []
-    egressrules = 0
-    for line in lines:
-        tokens = line.split(':')
-        if len(tokens) != 5:
-          continue
-        type = tokens[0]
-        protocol = tokens[1]
-        start = tokens[2]
-        end = tokens[3]
-        cidrs = tokens.pop();
-        ips = cidrs.split(",")
-        ips.pop()
-        allow_any = False
-
-        if type == 'E':
-            vmchain = egress_chain_name(vm_name)
-            action = "RETURN"
-            direction = "dst"
-            egressrules = egressrules + 1
-        else:
-            vmchain = chain_name(vm_name)
-            action = "ACCEPT"
-            direction = "src"
-        if  '0.0.0.0/0' in ips:
-            i = ips.index('0.0.0.0/0')
-            del ips[i]
-            allow_any = True
-        range = start + ":" + end
-        if ips:    
-            ipsetname = vmchain + "_" + protocol + "_" + start + "_" + end
-            if start == "-1":
-                ipsetname = vmchain + "_" + protocol + "_any"
-
-            if ipset(ipsetname, protocol, start, end, ips) == False:
-                logging.debug(" failed to create ipset for rule " + str(tokens))
-
-            if protocol == 'all':
-                iptables = ['iptables', '-I', vmchain, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
-            elif protocol != 'icmp':
-                iptables = ['iptables', '-I', vmchain, '-p',  protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
-            else:
-                range = start + "/" + end
-                if start == "-1":
-                    range = "any"
-                iptables = ['iptables', '-I', vmchain, '-p',  'icmp', '--icmp-type',  range,  '-m', 'set', keyword, ipsetname, direction, '-j', action]
-                
-            cmds.append(iptables)
-            logging.debug(iptables)
-        
-        if allow_any and protocol != 'all':
-            if protocol != 'icmp':
-                iptables = ['iptables', '-I', vmchain, '-p',  protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-j', action]
-            else:
-                range = start + "/" + end
-                if start == "-1":
-                    range = "any"
-                iptables = ['iptables', '-I', vmchain, '-p',  'icmp', '--icmp-type',  range, '-j', action]
-            cmds.append(iptables)
-            logging.debug(iptables)
-      
-    vmchain = chain_name(vm_name)
-    try:
-        util.pread2(['iptables', '-F', vmchain])
-    except:
-        logging.debug("Ignoring failure to delete chain " + vmchain)
-        util.pread2(['iptables', '-N', vmchain])
-
-    egress_vmchain = egress_chain_name(vm_name)
-    try:
-        util.pread2(['iptables', '-F', egress_vmchain])
-    except:
-        logging.debug("Ignoring failure to delete chain " + egress_vmchain)
-        util.pread2(['iptables', '-N', egress_vmchain])
-
-    
-    for cmd in cmds:
-        util.pread2(cmd)
-        
-    if egressrules == 0 :
-        util.pread2(['iptables', '-A', egress_vmchain, '-j', 'RETURN'])
-    else:
-        util.pread2(['iptables', '-A', egress_vmchain, '-j', 'DROP'])
-   
-    util.pread2(['iptables', '-A', vmchain, '-j', 'DROP'])
-
-    if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac) == False:
-        return 'false'
-    
-    return 'true'
-  except:
-    logging.debug("Failed to network rule !")
-
-@echo
-def bumpUpPriority(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, CS_DIR + "bumpUpPriority.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        logging.debug("bump up priority fail! ")
-        txt = ''
-
-    return txt
-
-
-if __name__ == "__main__":
-     XenAPIPlugin.dispatch({"pingtest": pingtest, "setup_iscsi":setup_iscsi,  
-                            "getgateway": getgateway, "preparemigration": preparemigration, 
-                            "setIptables": setIptables, "pingdomr": pingdomr, "pingxenserver": pingxenserver,  
-                            "savePassword": savePassword, 
-                            "saveDhcpEntry": saveDhcpEntry, "setFirewallRule": setFirewallRule, "routerProxy": routerProxy, 
-                            "setLoadBalancerRule": setLoadBalancerRule, "createFile": createFile, "deleteFile": deleteFile, 
-                            "network_rules":network_rules, 
-                            "can_bridge_firewall":can_bridge_firewall, "default_network_rules":default_network_rules,
-                            "destroy_network_rules_for_vm":destroy_network_rules_for_vm, 
-                            "default_network_rules_systemvm":default_network_rules_systemvm, 
-                            "network_rules_vmSecondaryIp":network_rules_vmSecondaryIp,
-                            "createipAlias":createipAlias,
-                            "configdnsmasq":configdnsmasq,
-                            "deleteipAlias":deleteipAlias,
-                            "get_rule_logs_for_vms":get_rule_logs_for_vms, 
-			    "add_to_VCPUs_params_live":add_to_VCPUs_params_live,
-                            "setLinkLocalIP":setLinkLocalIP,
-                            "cleanup_rules":cleanup_rules,
-                            "bumpUpPriority":bumpUpPriority,
-                            "createFileInDomr":createFileInDomr,
-                            "kill_copy_process":kill_copy_process})


[5/8] Merged vmops and vmopspremium. Rename all xapi plugins to start with cloud-plugin-. Rename vmops to cloud-plugin-generic.

Posted by ah...@apache.org.
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-plugin-s3xen
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-s3xen b/scripts/vm/hypervisor/xenserver/cloud-plugin-s3xen
new file mode 100644
index 0000000..9830897
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-s3xen
@@ -0,0 +1,428 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# Version @VERSION@
+#
+# A plugin for executing script needed by CloudStack
+from copy import copy
+from datetime import datetime
+from httplib import *
+from string import join
+from string import split
+
+import os
+import sys
+import time
+import md5 as md5mod
+import sha
+import base64
+import hmac
+import traceback
+import urllib2
+from xml.dom.minidom import parseString
+
+import XenAPIPlugin
+sys.path.extend(["/opt/xensource/sm/"])
+import util
+
+NULL = 'null'
+
+# Value conversion utility functions ...
+
+
+def to_none(value):
+
+    if value is None:
+        return None
+    if isinstance(value, basestring) and value.strip().lower() == NULL:
+        return None
+    return value
+
+
+def to_bool(value):
+
+    if to_none(value) is None:
+        return False
+    if isinstance(value, basestring) and value.strip().lower() == 'true':
+        return True
+    if isinstance(value, int) and value:
+        return True
+    return False
+
+
+def to_integer(value, default):
+
+    if to_none(value) is None or not isinstance(value, int):
+        return default
+    return int(value)
+
+
+def optional_str_value(value, default):
+
+    if is_not_blank(value):
+        return value
+    return default
+
+
+def is_blank(value):
+
+    return not is_not_blank(value)
+
+
+def is_not_blank(value):
+
+    if to_none(value) is None or not isinstance(value, basestring):
+        return True
+    if value.strip == '':
+        return False
+    return True
+
+
+def get_optional_key(map, key, default=''):
+
+    if key in map:
+        return map[key]
+    return default
+
+
+def log(message):
+
+    util.SMlog('#### CLOUD %s ####' % message)
+
+
+def echo(fn):
+    def wrapped(*v, **k):
+        name = fn.__name__
+        log("enter %s ####" % name)
+        res = fn(*v, **k)
+        log("exit %s with result %s" % (name, res))
+        return res
+    return wrapped
+
+
+def require_str_value(value, error_message):
+
+    if is_not_blank(value):
+        return value
+
+    raise ValueError(error_message)
+
+
+def retry(max_attempts, fn):
+
+    attempts = 1
+    while attempts <= max_attempts:
+        log("Attempting execution " + str(attempts) + "/" + str(
+            max_attempts) + " of " + fn.__name__)
+        try:
+            return fn()
+        except:
+            if (attempts >= max_attempts):
+                raise
+            attempts = attempts + 1
+
+
+def compute_md5(filename, buffer_size=8192):
+
+    hasher = md5mod.md5()
+
+    file = open(filename, 'rb')
+    try:
+
+        data = file.read(buffer_size)
+        while data != "":
+            hasher.update(data)
+            data = file.read(buffer_size)
+
+        return base64.encodestring(hasher.digest())[:-1]
+
+    finally:
+
+        file.close()
+
+
+class S3Client(object):
+
+    DEFAULT_END_POINT = 's3.amazonaws.com'
+    DEFAULT_CONNECTION_TIMEOUT = 50000
+    DEFAULT_SOCKET_TIMEOUT = 50000
+    DEFAULT_MAX_ERROR_RETRY = 3
+
+    HEADER_CONTENT_MD5 = 'Content-MD5'
+    HEADER_CONTENT_TYPE = 'Content-Type'
+    HEADER_CONTENT_LENGTH = 'Content-Length'
+
+    def __init__(self, access_key, secret_key, end_point=None,
+                 https_flag=None, connection_timeout=None, socket_timeout=None,
+                 max_error_retry=None):
+
+        self.access_key = require_str_value(
+            access_key, 'An access key must be specified.')
+        self.secret_key = require_str_value(
+            secret_key, 'A secret key must be specified.')
+        self.end_point = optional_str_value(end_point, self.DEFAULT_END_POINT)
+        self.https_flag = to_bool(https_flag)
+        self.connection_timeout = to_integer(
+            connection_timeout, self.DEFAULT_CONNECTION_TIMEOUT)
+        self.socket_timeout = to_integer(
+            socket_timeout, self.DEFAULT_SOCKET_TIMEOUT)
+        self.max_error_retry = to_integer(
+            max_error_retry, self.DEFAULT_MAX_ERROR_RETRY)
+
+    def build_canocialized_resource(self, bucket, key):
+        if not key.startswith("/"):
+            uri = bucket + "/" + key
+        else:
+            uri = bucket + key
+
+        return "/" + uri
+
+    def noop_send_body(connection):
+        pass
+
+    def noop_read(response):
+        return response.read()
+
+    def do_operation(
+        self, method, bucket, key, input_headers={},
+            fn_send_body=noop_send_body, fn_read=noop_read):
+
+        headers = copy(input_headers)
+        headers['Expect'] = '100-continue'
+
+        uri = self.build_canocialized_resource(bucket, key)
+        signature, request_date = self.sign_request(method, uri, headers)
+        headers['Authorization'] = "AWS " + self.access_key + ":" + signature
+        headers['Date'] = request_date
+
+        def perform_request():
+            connection = None
+            if self.https_flag:
+                connection = HTTPSConnection(self.end_point)
+            else:
+                connection = HTTPConnection(self.end_point)
+
+            try:
+                connection.timeout = self.socket_timeout
+                connection.putrequest(method, uri)
+
+                for k, v in headers.items():
+                    connection.putheader(k, v)
+                connection.endheaders()
+
+                fn_send_body(connection)
+
+                response = connection.getresponse()
+                log("Sent " + method + " request to " + self.end_point +
+                    uri + " with headers " + str(headers) +
+                    ".  Received response status " + str(response.status) +
+                    ": " + response.reason)
+
+                return fn_read(response)
+
+            finally:
+                connection.close()
+
+        return retry(self.max_error_retry, perform_request)
+
+    '''
+    See http://bit.ly/MMC5de for more information regarding the creation of
+    AWS authorization tokens and header signing
+    '''
+    def sign_request(self, operation, canocialized_resource, headers):
+
+        request_date = datetime.utcnow(
+        ).strftime('%a, %d %b %Y %H:%M:%S +0000')
+
+        content_hash = get_optional_key(headers, self.HEADER_CONTENT_MD5)
+        content_type = get_optional_key(headers, self.HEADER_CONTENT_TYPE)
+
+        string_to_sign = join(
+            [operation, content_hash, content_type, request_date,
+                canocialized_resource], '\n')
+
+        signature = base64.encodestring(
+            hmac.new(self.secret_key, string_to_sign.encode('utf8'),
+                     sha).digest())[:-1]
+
+        return signature, request_date
+        
+    def getText(self, nodelist):
+        rc = []
+        for node in nodelist:
+            if node.nodeType == node.TEXT_NODE:
+                rc.append(node.data)
+        return ''.join(rc)
+
+    def multiUpload(self, bucket, key, src_fileName, chunkSize=5 * 1024 * 1024):
+        uploadId={}
+        def readInitalMultipart(response):
+           data = response.read()
+           xmlResult = parseString(data) 
+           result = xmlResult.getElementsByTagName("InitiateMultipartUploadResult")[0]
+           upload = result.getElementsByTagName("UploadId")[0]
+           uploadId["0"] = upload.childNodes[0].data
+       
+        self.do_operation('POST', bucket, key + "?uploads", fn_read=readInitalMultipart) 
+
+        fileSize = os.path.getsize(src_fileName) 
+        parts = fileSize / chunkSize + ((fileSize % chunkSize) and 1)
+        part = 1
+        srcFile = open(src_fileName, 'rb')
+        etags = []
+        while part <= parts:
+            offset = part - 1
+            size = min(fileSize - offset * chunkSize, chunkSize)
+            headers = {
+                self.HEADER_CONTENT_LENGTH: size
+            }
+            def send_body(connection): 
+               srcFile.seek(offset * chunkSize)
+               block = srcFile.read(size)
+               connection.send(block)
+            def read_multiPart(response):
+               etag = response.getheader('ETag') 
+               etags.append((part, etag))
+            self.do_operation("PUT", bucket, "%s?partNumber=%s&uploadId=%s"%(key, part, uploadId["0"]), headers, send_body, read_multiPart)
+            part = part + 1
+        srcFile.close()
+
+        data = [] 
+        partXml = "<Part><PartNumber>%i</PartNumber><ETag>%s</ETag></Part>"
+        for etag in etags:
+            data.append(partXml%etag)
+        msg = "<CompleteMultipartUpload>%s</CompleteMultipartUpload>"%("".join(data))
+        size = len(msg)
+        headers = {
+            self.HEADER_CONTENT_LENGTH: size
+        }
+        def send_complete_multipart(connection):
+            connection.send(msg) 
+        self.do_operation("POST", bucket, "%s?uploadId=%s"%(key, uploadId["0"]), headers, send_complete_multipart)
+
+    def put(self, bucket, key, src_filename, maxSingleUpload):
+
+        if not os.path.isfile(src_filename):
+            raise Exception(
+                "Attempt to put " + src_filename + " that does not exist.")
+
+        size = os.path.getsize(src_filename)
+        if size > maxSingleUpload or maxSingleUpload == 0:
+            return self.multiUpload(bucket, key, src_filename)
+           
+        headers = {
+            self.HEADER_CONTENT_MD5: compute_md5(src_filename),
+        
+            self.HEADER_CONTENT_TYPE: 'application/octet-stream',
+            self.HEADER_CONTENT_LENGTH: str(os.stat(src_filename).st_size),
+        }
+
+        def send_body(connection):
+            src_file = open(src_filename, 'rb')
+            try:
+                while True:
+                    block = src_file.read(8192)
+                    if not block:
+                        break
+                    connection.send(block)
+
+            except:
+                src_file.close()
+
+        self.do_operation('PUT', bucket, key, headers, send_body)
+
+    def get(self, bucket, key, target_filename):
+
+        def read(response):
+
+            file = open(target_filename, 'wb')
+
+            try:
+
+                while True:
+                    block = response.read(8192)
+                    if not block:
+                        break
+                    file.write(block)
+            except:
+
+                file.close()
+
+        return self.do_operation('GET', bucket, key, fn_read=read)
+
+    def delete(self, bucket, key):
+
+        return self.do_operation('DELETE', bucket, key)
+
+
+def parseArguments(args):
+
+    # The keys in the args map will correspond to the properties defined on
+    # the com.cloud.utils.S3Utils#ClientOptions interface
+    client = S3Client(
+        args['accessKey'], args['secretKey'], args['endPoint'],
+        args['https'], args['connectionTimeout'], args['socketTimeout'])
+
+    operation = args['operation']
+    bucket = args['bucket']
+    key = args['key']
+    filename = args['filename']
+    maxSingleUploadBytes = int(args["maxSingleUploadSizeInBytes"])
+
+    if is_blank(operation):
+        raise ValueError('An operation must be specified.')
+
+    if is_blank(bucket):
+        raise ValueError('A bucket must be specified.')
+
+    if is_blank(key):
+        raise ValueError('A value must be specified.')
+
+    if is_blank(filename):
+        raise ValueError('A filename must be specified.')
+
+    return client, operation, bucket, key, filename, maxSingleUploadBytes
+
+
+@echo
+def s3(session, args):
+
+    client, operation, bucket, key, filename, maxSingleUploadBytes = parseArguments(args)
+
+    try:
+
+        if operation == 'put':
+            client.put(bucket, key, filename, maxSingleUploadBytes)
+        elif operation == 'get':
+            client.get(bucket, key, filename)
+        elif operation == 'delete':
+            client.delete(bucket, key, filename)
+        else:
+            raise RuntimeError(
+                "S3 plugin does not support operation " + operation)
+
+        return 'true'
+
+    except:
+        log("Operation " + operation + " on file " + filename +
+            " from/in bucket " + bucket + " key " + key)
+        log(traceback.format_exc())
+        return 'false'
+
+if __name__ == "__main__":
+    XenAPIPlugin.dispatch({"s3": s3})

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot b/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot
new file mode 100644
index 0000000..7ad892d
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-snapshot
@@ -0,0 +1,597 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# Version @VERSION@
+#
+# A plugin for executing script needed by CloudStack 
+
+import os, sys, time
+import XenAPIPlugin
+sys.path.append("/opt/xensource/sm/")
+import SR, VDI, SRCommand, util, lvutil
+from util import CommandException
+import vhdutil
+import shutil
+import lvhdutil
+import errno
+import subprocess
+import xs_errors
+import cleanup
+import stat
+import random
+import cloud-plugin-lib as lib
+import logging
+
+lib.setup_logging("/var/log/cloud/cloud-plugins.log")
+
+VHD_UTIL = '/usr/bin/vhd-util'
+VHD_PREFIX = 'VHD-'
+CLOUD_DIR = '/var/run/cloud_mount'
+
+def echo(fn):
+    def wrapped(*v, **k):
+        name = fn.__name__
+        logging.debug("#### CLOUD enter  %s ####" % name )
+        res = fn(*v, **k)
+        logging.debug("#### CLOUD exit  %s ####" % name )
+        return res
+    return wrapped
+
+
+@echo
+def create_secondary_storage_folder(session, args):
+    local_mount_path = None
+
+    logging.debug("create_secondary_storage_folder, args: " + str(args))
+
+    try:
+        try:
+            # Mount the remote resource folder locally
+            remote_mount_path = args["remoteMountPath"]
+            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
+            mount(remote_mount_path, local_mount_path)
+
+            # Create the new folder
+            new_folder = local_mount_path + "/" + args["newFolder"]
+            if not os.path.isdir(new_folder):
+                current_umask = os.umask(0)
+                os.makedirs(new_folder)
+                os.umask(current_umask)
+        except OSError, (errno, strerror):
+            errMsg = "create_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
+            logging.debug(errMsg)
+            raise xs_errors.XenError(errMsg)
+        except:
+            errMsg = "create_secondary_storage_folder failed."
+            logging.debug(errMsg)
+            raise xs_errors.XenError(errMsg)
+    finally:
+        if local_mount_path != None:
+            # Unmount the local folder
+            umount(local_mount_path)
+            # Remove the local folder
+            os.system("rmdir " + local_mount_path)
+        
+    return "1"
+
+@echo
+def delete_secondary_storage_folder(session, args):
+    local_mount_path = None
+
+    logging.debug("delete_secondary_storage_folder, args: " + str(args))
+
+    try:
+        try:
+            # Mount the remote resource folder locally
+            remote_mount_path = args["remoteMountPath"]
+            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
+            mount(remote_mount_path, local_mount_path)
+
+            # Delete the specified folder
+            folder = local_mount_path + "/" + args["folder"]
+            if os.path.isdir(folder):
+                os.system("rm -f " + folder + "/*")
+                os.system("rmdir " + folder)
+        except OSError, (errno, strerror):
+            errMsg = "delete_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
+            logging.debug(errMsg)
+            raise xs_errors.XenError(errMsg)
+        except:
+            errMsg = "delete_secondary_storage_folder failed."
+            logging.debug(errMsg)
+            raise xs_errors.XenError(errMsg)
+    finally:
+        if local_mount_path != None:
+            # Unmount the local folder
+            umount(local_mount_path)
+            # Remove the local folder
+            os.system("rmdir " + local_mount_path)
+        
+    return "1"
+     
+@echo
+def post_create_private_template(session, args):
+    local_mount_path = None
+    try:
+        try:
+            # get local template folder 
+            templatePath = args["templatePath"]
+            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
+            mount(templatePath, local_mount_path)
+            # Retrieve args
+            filename = args["templateFilename"]
+            name = args["templateName"]
+            description = args["templateDescription"]
+            checksum = args["checksum"]
+            file_size = args["size"]
+            virtual_size = args["virtualSize"]
+            template_id = args["templateId"]
+           
+            # Create the template.properties file
+            template_properties_install_path = local_mount_path + "/template.properties"
+            f = open(template_properties_install_path, "w")
+            f.write("filename=" + filename + "\n")
+            f.write("vhd=true\n")
+            f.write("id=" + template_id + "\n")
+            f.write("vhd.filename=" + filename + "\n")
+            f.write("public=false\n")
+            f.write("uniquename=" + name + "\n")
+            f.write("vhd.virtualsize=" + virtual_size + "\n")
+            f.write("virtualsize=" + virtual_size + "\n")
+            f.write("checksum=" + checksum + "\n")
+            f.write("hvm=true\n")
+            f.write("description=" + description + "\n")
+            f.write("vhd.size=" + str(file_size) + "\n")
+            f.write("size=" + str(file_size) + "\n")
+            f.close()
+            logging.debug("Created template.properties file")
+           
+            # Set permissions
+            permissions = stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH
+            os.chmod(template_properties_install_path, permissions)
+            logging.debug("Set permissions on template and template.properties")
+
+        except:
+            errMsg = "post_create_private_template failed."
+            logging.debug(errMsg)
+            raise xs_errors.XenError(errMsg)
+
+    finally:
+        if local_mount_path != None:
+            # Unmount the local folder
+            umount(local_mount_path)
+            # Remove the local folder
+            os.system("rmdir " + local_mount_path)
+    return "1" 
+  
+def isfile(path, isISCSI):
+    errMsg = ''
+    exists = True
+    if isISCSI:
+        exists = checkVolumeAvailablility(path)
+    else:
+        exists = os.path.isfile(path)
+        
+    if not exists:
+        errMsg = "File " + path + " does not exist."
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return errMsg
+
+def copyfile(fromFile, toFile, isISCSI):
+    logging.debug("Starting to copy " + fromFile + " to " + toFile)
+    errMsg = ''
+    try:
+        cmd = ['dd', 'if=' + fromFile, 'of=' + toFile, 'bs=4M']
+        txt = util.pread2(cmd)
+    except:
+        try:
+            os.system("rm -f " + toFile)
+        except:
+            txt = ''
+        txt = ''
+        errMsg = "Error while copying " + fromFile + " to " + toFile + " in secondary storage"
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+
+    logging.debug("Successfully copied " + fromFile + " to " + toFile)
+    return errMsg
+
+def chdir(path):
+    try:
+        os.chdir(path)
+    except OSError, (errno, strerror):
+        errMsg = "Unable to chdir to " + path + " because of OSError with errno: " + str(errno) + " and strerr: " + strerror
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    logging.debug("Chdired to " + path)
+    return
+
+def scanParent(path):
+    # Do a scan for the parent for ISCSI volumes
+    # Note that the parent need not be visible on the XenServer
+    parentUUID = ''
+    try:
+        lvName = os.path.basename(path)
+        dirname = os.path.dirname(path)
+        vgName = os.path.basename(dirname) 
+        vhdInfo = vhdutil.getVHDInfoLVM(lvName, lvhdutil.extractUuid, vgName)
+        parentUUID = vhdInfo.parentUuid
+    except:
+        errMsg = "Could not get vhd parent of " + path
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return parentUUID
+
+def getParent(path, isISCSI):
+    parentUUID = ''
+    try :
+        if isISCSI:
+            parentUUID = vhdutil.getParent(path, lvhdutil.extractUuid)
+        else:
+            parentUUID = vhdutil.getParent(path, cleanup.FileVDI.extractUuid)
+    except:
+        errMsg = "Could not get vhd parent of " + path
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return parentUUID
+
+def getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI):
+    snapshotVHD    = getVHD(snapshotUuid, isISCSI)
+    snapshotPath   = os.path.join(primarySRPath, snapshotVHD)
+
+    baseCopyUuid = ''
+    if isISCSI:
+        checkVolumeAvailablility(snapshotPath)
+        baseCopyUuid = scanParent(snapshotPath)
+    else:
+        baseCopyUuid = getParent(snapshotPath, isISCSI)
+    
+    logging.debug("Base copy of snapshotUuid: " + snapshotUuid + " is " + baseCopyUuid)
+    return baseCopyUuid
+
+def setParent(parent, child):
+    try:
+        cmd = [VHD_UTIL, "modify", "-p", parent, "-n", child]
+        txt = util.pread2(cmd)
+    except:
+        errMsg = "Unexpected error while trying to set parent of " + child + " to " + parent 
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    logging.debug("Successfully set parent of " + child + " to " + parent)
+    return
+
+def rename(originalVHD, newVHD):
+    try:
+        os.rename(originalVHD, newVHD)
+    except OSError, (errno, strerror):
+        errMsg = "OSError while renaming " + origiinalVHD + " to " + newVHD + "with errno: " + str(errno) + " and strerr: " + strerror
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return
+
+def makedirs(path):
+    if not os.path.isdir(path):
+        try:
+            os.makedirs(path)
+        except OSError, (errno, strerror):
+            umount(path)
+            if os.path.isdir(path):
+                return
+            errMsg = "OSError while creating " + path + " with errno: " + str(errno) + " and strerr: " + strerror
+            logging.debug(errMsg)
+            raise xs_errors.XenError(errMsg)
+    return
+
+def mount(remoteDir, localDir):
+    makedirs(localDir)
+    options = "soft,tcp,timeo=133,retrans=1"
+    try: 
+        cmd = ['mount', '-o', options, remoteDir, localDir]
+        txt = util.pread2(cmd)
+    except:
+        txt = ''
+        errMsg = "Unexpected error while trying to mount " + remoteDir + " to " + localDir 
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    logging.debug("Successfully mounted " + remoteDir + " to " + localDir)
+
+    return
+
+def umount(localDir):
+    try: 
+        cmd = ['umount', localDir]
+        util.pread2(cmd)
+    except CommandException:
+        errMsg = "CommandException raised while trying to umount " + localDir 
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+
+    logging.debug("Successfully unmounted " + localDir)
+    return
+
+def mountSnapshotsDir(secondaryStorageMountPath, localMountPointPath, path):
+    # The aim is to mount secondaryStorageMountPath on 
+    # And create <accountId>/<instanceId> dir on it, if it doesn't exist already.
+    # Assuming that secondaryStorageMountPath  exists remotely
+
+    # Just mount secondaryStorageMountPath/<relativeDir>/SecondaryStorageHost/ everytime
+    # Never unmount.
+    # path is like "snapshots/account/volumeId", we mount secondary_storage:/snapshots
+    relativeDir = path.split("/")[0]
+    restDir = "/".join(path.split("/")[1:])
+    snapshotsDir = os.path.join(secondaryStorageMountPath, relativeDir)
+
+    makedirs(localMountPointPath)
+    # if something is not mounted already on localMountPointPath,
+    # mount secondaryStorageMountPath on localMountPath
+    if os.path.ismount(localMountPointPath):
+        # There is more than one secondary storage per zone.
+        # And we are mounting each sec storage under a zone-specific directory
+        # So two secondary storage snapshot dirs will never get mounted on the same point on the same XenServer.
+        logging.debug("The remote snapshots directory has already been mounted on " + localMountPointPath)
+    else:
+        mount(snapshotsDir, localMountPointPath)
+
+    # Create accountId/instanceId dir on localMountPointPath, if it doesn't exist
+    backupsDir = os.path.join(localMountPointPath, restDir)
+    makedirs(backupsDir)
+    return backupsDir
+
+def unmountAll(path):
+    try:
+        for dir in os.listdir(path):
+            if dir.isdigit():
+                logging.debug("Unmounting Sub-Directory: " + dir)
+                localMountPointPath = os.path.join(path, dir)
+                umount(localMountPointPath)
+    except:
+        logging.debug("Ignoring the error while trying to unmount the snapshots dir")
+
+@echo
+def unmountSnapshotsDir(session, args):
+    dcId = args['dcId']
+    localMountPointPath = os.path.join(CLOUD_DIR, dcId)
+    localMountPointPath = os.path.join(localMountPointPath, "snapshots")
+    unmountAll(localMountPointPath)
+    try:
+        umount(localMountPointPath)
+    except:
+        logging.debug("Ignoring the error while trying to unmount the snapshots dir.")
+
+    return "1"
+
+def getPrimarySRPath(primaryStorageSRUuid, isISCSI):
+    if isISCSI:
+        primarySRDir = lvhdutil.VG_PREFIX + primaryStorageSRUuid
+        return os.path.join(lvhdutil.VG_LOCATION, primarySRDir)
+    else:
+        return os.path.join(SR.MOUNT_BASE, primaryStorageSRUuid)
+
+def getBackupVHD(UUID):
+    return UUID + '.' + SR.DEFAULT_TAP
+
+def getVHD(UUID, isISCSI):
+    if isISCSI:
+        return VHD_PREFIX + UUID
+    else:
+        return UUID + '.' + SR.DEFAULT_TAP
+
+def getIsTrueString(stringValue):
+    booleanValue = False
+    if (stringValue and stringValue == 'true'):
+        booleanValue = True
+    return booleanValue 
+
+def makeUnavailable(uuid, primarySRPath, isISCSI):
+    if not isISCSI:
+        return
+    VHD = getVHD(uuid, isISCSI)
+    path = os.path.join(primarySRPath, VHD)
+    manageAvailability(path, '-an')
+    return
+
+def manageAvailability(path, value):
+    if path.__contains__("/var/run/sr-mount"):
+        return
+    logging.debug("Setting availability of " + path + " to " + value)
+    try:
+        cmd = ['/usr/sbin/lvchange', value, path]
+        util.pread2(cmd)
+    except: #CommandException, (rc, cmdListStr, stderr):
+        #errMsg = "CommandException thrown while executing: " + cmdListStr + " with return code: " + str(rc) + " and stderr: " + stderr
+        errMsg = "Unexpected exception thrown by lvchange"
+        logging.debug(errMsg)
+        if value == "-ay":
+            # Raise an error only if we are trying to make it available.
+            # Just warn if we are trying to make it unavailable after the 
+            # snapshot operation is done.
+            raise xs_errors.XenError(errMsg)
+    return
+
+
+def checkVolumeAvailablility(path):
+    try:
+        if not isVolumeAvailable(path):
+            # The VHD file is not available on XenSever. The volume is probably
+            # inactive or detached.
+            # Do lvchange -ay to make it available on XenServer
+            manageAvailability(path, '-ay')
+    except:
+        errMsg = "Could not determine status of ISCSI path: " + path
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    
+    success = False
+    i = 0
+    while i < 6:
+        i = i + 1
+        # Check if the vhd is actually visible by checking for the link
+        # set isISCSI to true
+        success = isVolumeAvailable(path)
+        if success:
+            logging.debug("Made vhd: " + path + " available and confirmed that it is visible")
+            break
+
+        # Sleep for 10 seconds before checking again.
+        time.sleep(10)
+
+    # If not visible within 1 min fail
+    if not success:
+        logging.debug("Could not make vhd: " +  path + " available despite waiting for 1 minute. Does it exist?")
+
+    return success
+
+def isVolumeAvailable(path):
+    # Check if iscsi volume is available on this XenServer.
+    status = "0"
+    try:
+        p = subprocess.Popen(["/bin/bash", "-c", "if [ -L " + path + " ]; then echo 1; else echo 0;fi"], stdout=subprocess.PIPE)
+        status = p.communicate()[0].strip("\n")
+    except:
+        errMsg = "Could not determine status of ISCSI path: " + path
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+
+    return (status == "1")  
+
+def getVhdParent(session, args):
+    logging.debug("getParent with " + str(args))
+    primaryStorageSRUuid      = args['primaryStorageSRUuid']
+    snapshotUuid              = args['snapshotUuid']
+    isISCSI                   = getIsTrueString(args['isISCSI']) 
+
+    primarySRPath = getPrimarySRPath(primaryStorageSRUuid, isISCSI)
+    logging.debug("primarySRPath: " + primarySRPath)
+
+    baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
+
+    return  baseCopyUuid
+
+
+def backupSnapshot(session, args):
+    logging.debug("Called backupSnapshot with " + str(args))
+    primaryStorageSRUuid      = args['primaryStorageSRUuid']
+    secondaryStorageMountPath = args['secondaryStorageMountPath']
+    snapshotUuid              = args['snapshotUuid']
+    prevBackupUuid            = args['prevBackupUuid']
+    backupUuid                = args['backupUuid']
+    isISCSI                   = getIsTrueString(args['isISCSI'])
+    path = args['path']
+    localMountPoint = args['localMountPoint']
+    primarySRPath = getPrimarySRPath(primaryStorageSRUuid, isISCSI)
+    logging.debug("primarySRPath: " + primarySRPath)
+
+    baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
+    baseCopyVHD  = getVHD(baseCopyUuid, isISCSI)
+    baseCopyPath = os.path.join(primarySRPath, baseCopyVHD)
+    logging.debug("Base copy path: " + baseCopyPath)
+
+
+    # Mount secondary storage mount path on XenServer along the path
+    # /var/run/sr-mount/<dcId>/snapshots/ and create <accountId>/<volumeId> dir
+    # on it.
+    backupsDir = mountSnapshotsDir(secondaryStorageMountPath, localMountPoint, path)
+    logging.debug("Backups dir " + backupsDir)
+    prevBackupUuid = prevBackupUuid.split("/")[-1]
+    # Check existence of snapshot on primary storage
+    isfile(baseCopyPath, isISCSI)
+    if prevBackupUuid:
+        # Check existence of prevBackupFile
+        prevBackupVHD = getBackupVHD(prevBackupUuid)
+        prevBackupFile = os.path.join(backupsDir, prevBackupVHD)
+        isfile(prevBackupFile, False)
+
+    # copy baseCopyPath to backupsDir with new uuid
+    backupVHD = getBackupVHD(backupUuid)  
+    backupFile = os.path.join(backupsDir, backupVHD)
+    logging.debug("Back up " + baseCopyUuid + " to Secondary Storage as " + backupUuid)
+    copyfile(baseCopyPath, backupFile, isISCSI)
+    vhdutil.setHidden(backupFile, False)
+
+    # Because the primary storage is always scanned, the parent of this base copy is always the first base copy.
+    # We don't want that, we want a chain of VHDs each of which is a delta from the previous.
+    # So set the parent of the current baseCopyVHD to prevBackupVHD 
+    if prevBackupUuid:
+        # If there was a previous snapshot
+        setParent(prevBackupFile, backupFile)
+
+    txt = "1#" + backupUuid
+    return txt
+
+@echo
+def deleteSnapshotBackup(session, args):
+    logging.debug("Calling deleteSnapshotBackup with " + str(args))
+    secondaryStorageMountPath = args['secondaryStorageMountPath']
+    backupUUID                = args['backupUUID']
+    path = args['path']
+    localMountPoint = args['localMountPoint']
+
+    backupsDir = mountSnapshotsDir(secondaryStorageMountPath, localMountPoint, path)
+    # chdir to the backupsDir for convenience
+    chdir(backupsDir)
+
+    backupVHD = getBackupVHD(backupUUID)
+    logging.debug("checking existence of " + backupVHD)
+
+    # The backupVHD is on secondary which is NFS and not ISCSI.
+    if not os.path.isfile(backupVHD):
+        logging.debug("backupVHD " + backupVHD + "does not exist. Not trying to delete it")
+        return "1"
+    logging.debug("backupVHD " + backupVHD + " exists.")
+        
+    # Just delete the backupVHD
+    try:
+        os.remove(backupVHD)
+    except OSError, (errno, strerror):
+        errMsg = "OSError while removing " + backupVHD + " with errno: " + str(errno) + " and strerr: " + strerror
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+
+    return "1"
+   
+@echo
+def revert_memory_snapshot(session, args):
+    logging.debug("Calling revert_memory_snapshot with " + str(args))
+    vmName = args['vmName']
+    snapshotUUID = args['snapshotUUID']
+    oldVmUuid = args['oldVmUuid']
+    snapshotMemory = args['snapshotMemory']
+    hostUUID = args['hostUUID']
+    try:
+        cmd = '''xe vbd-list vm-uuid=%s | grep 'vdi-uuid' | grep -v 'not in database' | sed -e 's/vdi-uuid ( RO)://g' ''' % oldVmUuid
+        vdiUuids = os.popen(cmd).read().split()
+        cmd2 = '''xe vm-param-get param-name=power-state uuid=''' + oldVmUuid
+        if os.popen(cmd2).read().split()[0] != 'halted':
+            os.system("xe vm-shutdown force=true vm=" + vmName)
+        os.system("xe vm-destroy uuid=" + oldVmUuid)
+        os.system("xe snapshot-revert snapshot-uuid=" + snapshotUUID)
+        if snapshotMemory == 'true':
+            os.system("xe vm-resume vm=" + vmName + " on=" + hostUUID)
+        for vdiUuid in vdiUuids:
+            os.system("xe vdi-destroy uuid=" + vdiUuid)
+    except OSError, (errno, strerror):
+        errMsg = "OSError while reverting vm " + vmName + " to snapshot " + snapshotUUID + " with errno: " + str(errno) + " and strerr: " + strerror
+        logging.debug(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return "0"
+
+if __name__ == "__main__":
+    XenAPIPlugin.dispatch({"getVhdParent":getVhdParent,  "create_secondary_storage_folder":create_secondary_storage_folder, "delete_secondary_storage_folder":delete_secondary_storage_folder, "post_create_private_template":post_create_private_template, "backupSnapshot": backupSnapshot, "deleteSnapshotBackup": deleteSnapshotBackup, "unmountSnapshotsDir": unmountSnapshotsDir, "revert_memory_snapshot":revert_memory_snapshot})
+    
+

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-plugin-swiftxen
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-swiftxen b/scripts/vm/hypervisor/xenserver/cloud-plugin-swiftxen
new file mode 100644
index 0000000..f7ce3b7
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-swiftxen
@@ -0,0 +1,97 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# 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("#### CLOUD enter  %s ####" % name )
+        res = fn(*v, **k)
+        util.SMlog("#### CLOUD exit  %s ####" % name )
+        return res
+    return wrapped
+
+SWIFT = "/opt/cloud/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("#### CLOUD 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})

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-plugins.conf
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugins.conf b/scripts/vm/hypervisor/xenserver/cloud-plugins.conf
new file mode 100644
index 0000000..960a873
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugins.conf
@@ -0,0 +1,21 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+[LOGGING]
+# Logging options for CloudStack plugins
+debug = True
+verbose = True

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-prepare-upgrade.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-prepare-upgrade.sh b/scripts/vm/hypervisor/xenserver/cloud-prepare-upgrade.sh
index 4f80f72..3db628d 100755
--- a/scripts/vm/hypervisor/xenserver/cloud-prepare-upgrade.sh
+++ b/scripts/vm/hypervisor/xenserver/cloud-prepare-upgrade.sh
@@ -83,7 +83,7 @@ fake_pv_driver() {
     return 0
   fi
   host=$(xe vm-param-get uuid=$vm param-name=resident-on)
-  xe host-call-plugin host-uuid=$host plugin=vmops fn=preparemigration args:uuid=$vm
+  xe host-call-plugin host-uuid=$host plugin=cloud-plugin-generic fn=preparemigration args:uuid=$vm
 }
 
 vms=$(xe vm-list is-control-domain=false| grep ^uuid | awk '{print $NF}')

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloudlog
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloudlog b/scripts/vm/hypervisor/xenserver/cloudlog
new file mode 100644
index 0000000..9202253
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloudlog
@@ -0,0 +1,12 @@
+/var/log/cloud-plugins.log {
+    daily
+    size 1M
+    rotate 20
+}
+
+/var/log/cloud-ovstunnel.log /var/log/cloud-ovs-pvlan.log {
+    daily
+    size 1M
+    rotate 2
+}
+

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloudstack_pluginlib.py
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloudstack_pluginlib.py b/scripts/vm/hypervisor/xenserver/cloudstack_pluginlib.py
deleted file mode 100644
index 422111f..0000000
--- a/scripts/vm/hypervisor/xenserver/cloudstack_pluginlib.py
+++ /dev/null
@@ -1,221 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Common function for Cloudstack's XenAPI plugins
-
-import ConfigParser
-import logging
-import os
-import subprocess
-
-from time import localtime, asctime
-
-DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s"
-DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
-DEFAULT_LOG_FILE = "/var/log/cloudstack_plugins.log"
-
-PLUGIN_CONFIG_PATH = "/etc/xensource/cloudstack_plugins.conf"
-OVSDB_PID_PATH = "/var/run/openvswitch/ovsdb-server.pid"
-OVSDB_DAEMON_PATH = "ovsdb-server"
-OVS_PID_PATH = "/var/run/openvswitch/ovs-vswitchd.pid"
-OVS_DAEMON_PATH = "ovs-vswitchd"
-VSCTL_PATH = "/usr/bin/ovs-vsctl"
-OFCTL_PATH = "/usr/bin/ovs-ofctl"
-XE_PATH = "/opt/xensource/bin/xe"
-
-
-class PluginError(Exception):
-    """Base Exception class for all plugin errors."""
-    def __init__(self, *args):
-        Exception.__init__(self, *args)
-
-
-def setup_logging(log_file=None):
-    debug = False
-    verbose = False
-    log_format = DEFAULT_LOG_FORMAT
-    log_date_format = DEFAULT_LOG_DATE_FORMAT
-    # try to read plugin configuration file
-    if os.path.exists(PLUGIN_CONFIG_PATH):
-        config = ConfigParser.ConfigParser()
-        config.read(PLUGIN_CONFIG_PATH)
-        try:
-            options = config.options('LOGGING')
-            if 'debug' in options:
-                debug = config.getboolean('LOGGING', 'debug')
-            if 'verbose' in options:
-                verbose = config.getboolean('LOGGING', 'verbose')
-            if 'format' in options:
-                log_format = config.get('LOGGING', 'format')
-            if 'date_format' in options:
-                log_date_format = config.get('LOGGING', 'date_format')
-            if 'file' in options:
-                log_file_2 = config.get('LOGGING', 'file')
-        except ValueError:
-            # configuration file contained invalid attributes
-            # ignore them
-            pass
-        except ConfigParser.NoSectionError:
-            # Missing 'Logging' section in configuration file
-            pass
-
-    root_logger = logging.root
-    if debug:
-        root_logger.setLevel(logging.DEBUG)
-    elif verbose:
-        root_logger.setLevel(logging.INFO)
-    else:
-        root_logger.setLevel(logging.WARNING)
-    formatter = logging.Formatter(log_format, log_date_format)
-
-    log_filename = log_file or log_file_2 or DEFAULT_LOG_FILE
-
-    logfile_handler = logging.FileHandler(log_filename)
-    logfile_handler.setFormatter(formatter)
-    root_logger.addHandler(logfile_handler)
-
-
-def do_cmd(cmd):
-    """Abstracts out the basics of issuing system commands. If the command
-    returns anything in stderr, a PluginError is raised with that information.
-    Otherwise, the output from stdout is returned.
-    """
-
-    pipe = subprocess.PIPE
-    logging.debug("Executing:%s", cmd)
-    proc = subprocess.Popen(cmd, shell=False, stdin=pipe, stdout=pipe,
-                            stderr=pipe, close_fds=True)
-    ret_code = proc.wait()
-    err = proc.stderr.read()
-    if ret_code:
-        logging.debug("The command exited with the error code: " +
-                      "%s (stderr output:%s)" % (ret_code, err))
-        raise PluginError(err)
-    output = proc.stdout.read()
-    if output.endswith('\n'):
-        output = output[:-1]
-    return output
-
-
-def _is_process_run(pidFile, name):
-    try:
-        fpid = open(pidFile, "r")
-        pid = fpid.readline()
-        fpid.close()
-    except IOError, e:
-        return -1
-
-    pid = pid[:-1]
-    ps = os.popen("ps -ae")
-    for l in ps:
-        if pid in l and name in l:
-            ps.close()
-            return 0
-
-    ps.close()
-    return -2
-
-
-def _is_tool_exist(name):
-    if os.path.exists(name):
-        return 0
-    return -1
-
-
-def check_switch():
-    global result
-
-    ret = _is_process_run(OVSDB_PID_PATH, OVSDB_DAEMON_PATH)
-    if ret < 0:
-        if ret == -1:
-            return "NO_DB_PID_FILE"
-        if ret == -2:
-            return "DB_NOT_RUN"
-
-    ret = _is_process_run(OVS_PID_PATH, OVS_DAEMON_PATH)
-    if ret < 0:
-        if ret == -1:
-            return "NO_SWITCH_PID_FILE"
-        if ret == -2:
-            return "SWITCH_NOT_RUN"
-
-    if _is_tool_exist(VSCTL_PATH) < 0:
-        return "NO_VSCTL"
-
-    if _is_tool_exist(OFCTL_PATH) < 0:
-        return "NO_OFCTL"
-
-    return "SUCCESS"
-
-
-def _build_flow_expr(**kwargs):
-    is_delete_expr = kwargs.get('delete', False)
-    flow = ""
-    if not is_delete_expr:
-        flow = "hard_timeout=%s,idle_timeout=%s,priority=%s"\
-                % (kwargs.get('hard_timeout', '0'),
-                   kwargs.get('idle_timeout', '0'),
-                   kwargs.get('priority', '1'))
-    in_port = 'in_port' in kwargs and ",in_port=%s" % kwargs['in_port'] or ''
-    dl_type = 'dl_type' in kwargs and ",dl_type=%s" % kwargs['dl_type'] or ''
-    dl_src = 'dl_src' in kwargs and ",dl_src=%s" % kwargs['dl_src'] or ''
-    dl_dst = 'dl_dst' in kwargs and ",dl_dst=%s" % kwargs['dl_dst'] or ''
-    nw_src = 'nw_src' in kwargs and ",nw_src=%s" % kwargs['nw_src'] or ''
-    nw_dst = 'nw_dst' in kwargs and ",nw_dst=%s" % kwargs['nw_dst'] or ''
-    proto = 'proto' in kwargs and ",%s" % kwargs['proto'] or ''
-    ip = ('nw_src' in kwargs or 'nw_dst' in kwargs) and ',ip' or ''
-    flow = (flow + in_port + dl_type + dl_src + dl_dst +
-            (ip or proto) + nw_src + nw_dst)
-    return flow
-
-
-def add_flow(bridge, **kwargs):
-    """
-    Builds a flow expression for **kwargs and adds the flow entry
-    to an Open vSwitch instance
-    """
-    flow = _build_flow_expr(**kwargs)
-    actions = 'actions' in kwargs and ",actions=%s" % kwargs['actions'] or ''
-    flow = flow + actions
-    addflow = [OFCTL_PATH, "add-flow", bridge, flow]
-    do_cmd(addflow)
-
-
-def del_flows(bridge, **kwargs):
-    """
-    Removes flows according to criteria passed as keyword.
-    """
-    flow = _build_flow_expr(delete=True, **kwargs)
-    # out_port condition does not exist for all flow commands
-    out_port = ("out_port" in kwargs and
-                ",out_port=%s" % kwargs['out_port'] or '')
-    flow = flow + out_port
-    delFlow = [OFCTL_PATH, 'del-flows', bridge, flow]
-    do_cmd(delFlow)
-
-
-def del_all_flows(bridge):
-    delFlow = [OFCTL_PATH, "del-flows", bridge]
-    do_cmd(delFlow)
-
-    normalFlow = "priority=0 idle_timeout=0 hard_timeout=0 actions=normal"
-    add_flow(bridge, normalFlow)
-
-
-def del_port(bridge, port):
-    delPort = [VSCTL_PATH, "del-port", bridge, port]
-    do_cmd(delPort)

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloudstack_plugins.conf
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloudstack_plugins.conf b/scripts/vm/hypervisor/xenserver/cloudstack_plugins.conf
deleted file mode 100644
index 8335893..0000000
--- a/scripts/vm/hypervisor/xenserver/cloudstack_plugins.conf
+++ /dev/null
@@ -1,21 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-[LOGGING]
-# Logging options for Cloudstack plugins
-debug = True
-verbose = True

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloudstacklog
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloudstacklog b/scripts/vm/hypervisor/xenserver/cloudstacklog
deleted file mode 100644
index 2bae5ab..0000000
--- a/scripts/vm/hypervisor/xenserver/cloudstacklog
+++ /dev/null
@@ -1,12 +0,0 @@
-/var/log/vmops.log {
-    daily
-    size 1M
-    rotate 20
-}
-
-/var/log/ovstunnel.log /var/log/ovs-pvlan.log {
-    daily
-    size 1M
-    rotate 2
-}
-

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh b/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh
index f170f69..d39fc6e 100755
--- a/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh
+++ b/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh
@@ -96,7 +96,7 @@ if [ $? -ne 0 ]; then
   exit 0
 fi
 
-VHDUTIL="/opt/cloudstack/bin/vhd-util"
+VHDUTIL="/opt/cloud/bin/vhd-util"
 
 copyvhd()
 {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/launch_hb.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/launch_hb.sh b/scripts/vm/hypervisor/xenserver/launch_hb.sh
index bde7fca..289eb5f 100755
--- a/scripts/vm/hypervisor/xenserver/launch_hb.sh
+++ b/scripts/vm/hypervisor/xenserver/launch_hb.sh
@@ -33,7 +33,7 @@ if [ -z $2 ]; then
   exit 3
 fi
 
-if [ ! -f /opt/cloudstack/bin/xenheartbeat.sh ]; then
+if [ ! -f /opt/cloud/bin/xenheartbeat.sh ]; then
   printf "Error: Unable to find xenheartbeat.sh to launch\n"
   exit 4
 fi
@@ -42,5 +42,5 @@ for psid in `ps -ef | grep xenheartbeat | grep -v grep | awk '{print $2}'`; do
   kill $psid
 done
 
-nohup /opt/cloudstack/bin/xenheartbeat.sh $1 $2 >/dev/null 2>/dev/null &
+nohup /opt/cloud/bin/xenheartbeat.sh $1 $2 >/dev/null 2>/dev/null &
 echo "======> DONE <======"

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/mockxcpplugin.py
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/mockxcpplugin.py b/scripts/vm/hypervisor/xenserver/mockxcpplugin.py
deleted file mode 100644
index 0de24ca..0000000
--- a/scripts/vm/hypervisor/xenserver/mockxcpplugin.py
+++ /dev/null
@@ -1,66 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# This is for test purpose, to test xcp plugin
-
-import sys
-import XenAPI
-import os.path
-import traceback
-import socket
-def getHost():
-    hostname = socket.gethostname()
-    url = "http://localhost"
-    session = XenAPI.Session(url)
-    session.xenapi.login_with_password("root", "password")
-    host = session.xenapi.host
-    hosts = session.xenapi.host.get_by_name_label(hostname)
-    if len(hosts) != 1:
-        print "can't find host:" + hostname
-        sys.exit(1)
-    localhost = hosts[0]
-    return [host, localhost]
-
-def callPlugin(pluginName, func, params):
-    hostPair = getHost()
-    host = hostPair[0]
-    localhost = hostPair[1]
-    return host.call_plugin(localhost, pluginName, func, params)
-
-def main():
-    if len(sys.argv) < 3:
-        print "args: pluginName funcName params"
-        sys.exit(1)
-
-    pluginName = sys.argv[1]
-    funcName = sys.argv[2]
-
-    paramList = sys.argv[3:]
-    if (len(paramList) % 2) != 0:
-        print "params must be name/value pair"
-        sys.exit(2)
-    params = {}
-    pos = 0;
-    for i in range(len(paramList) / 2):
-        params[str(paramList[pos])] = str(paramList[pos+1])
-        pos = pos + 2
-    print "call: " + pluginName + " " + funcName + ", with params: " + str(params)
-    print "return: " +  callPlugin(pluginName, funcName, params)
-
-if __name__ == "__main__":
-    main()

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/ovs-pvlan
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/ovs-pvlan b/scripts/vm/hypervisor/xenserver/ovs-pvlan
deleted file mode 100755
index 31d60d0..0000000
--- a/scripts/vm/hypervisor/xenserver/ovs-pvlan
+++ /dev/null
@@ -1,148 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-
-import cloudstack_pluginlib as lib
-import logging
-import os
-import sys
-import subprocess
-import time
-import XenAPIPlugin
-
-sys.path.append("/opt/xensource/sm/")
-import util
-
-from time import localtime as _localtime, asctime as _asctime
-
-CS_DIR = "/opt/cloudstack/bin/"
-
-xePath = "/opt/xensource/bin/xe"
-dhcpSetupPath = CS_DIR + "ovs-pvlan-dhcp-host.sh"
-vmSetupPath = CS_DIR + "ovs-pvlan-vm.sh"
-getDhcpIfacePath = CS_DIR + "ovs-get-dhcp-iface.sh"
-pvlanCleanupPath = CS_DIR + "ovs-pvlan-cleanup.sh"
-getBridgePath = CS_DIR + "ovs-get-bridge.sh"
-
-lib.setup_logging("/var/log/ovs-pvlan.log")
-
-def echo(fn):
-    def wrapped(*v, **k):
-        name = fn.__name__
-        logging.debug("#### VMOPS enter  %s ####" % name)
-        res = fn(*v, **k)
-        logging.debug("#### VMOPS exit  %s ####" % name)
-        return res
-    return wrapped
-
-@echo
-def setup_pvlan_dhcp(session, args):
-    op = args.pop("op")
-    nw_label = args.pop("nw-label")
-    primary = args.pop("primary-pvlan")
-    isolated = args.pop("isolated-pvlan")
-    dhcp_name = args.pop("dhcp-name")
-    dhcp_ip = args.pop("dhcp-ip")
-    dhcp_mac = args.pop("dhcp-mac")
-
-    res = lib.check_switch()
-    if res != "SUCCESS":
-        return "FAILURE:%s" % res
-
-    logging.debug("Network is:%s" % (nw_label))
-    bridge = lib.do_cmd([getBridgePath, nw_label])
-    logging.debug("Determine bridge/switch is :%s" % (bridge))
-
-    if op == "add":
-        logging.debug("Try to get dhcp vm %s port on the switch:%s" % (dhcp_name, bridge))
-        dhcp_iface = lib.do_cmd([getDhcpIfacePath, bridge, dhcp_name])
-        logging.debug("About to setup dhcp vm on the switch:%s" % bridge)
-        res = lib.do_cmd([dhcpSetupPath, "-A", "-b", bridge, "-p", primary,
-            "-i", isolated, "-n", dhcp_name, "-d", dhcp_ip, "-m", dhcp_mac,
-            "-I", dhcp_iface])
-	if res:
-	    result = "FAILURE:%s" % res
-	    return result;
-	logging.debug("Setup dhcp vm on switch program done")
-    elif op == "delete":
-        logging.debug("About to remove dhcp the switch:%s" % bridge)
-        res = lib.do_cmd([dhcpSetupPath, "-D", "-b", bridge, "-p", primary,
-            "-i", isolated, "-n", dhcp_name, "-d", dhcp_ip, "-m", dhcp_mac])
-	if res:
-	    result = "FAILURE:%s" % res
-	    return result;
-	logging.debug("Remove DHCP on switch program done")
-    
-    result = "true"
-    logging.debug("Setup_pvlan_dhcp completed with result:%s" % result)
-    return result
-
-@echo
-def setup_pvlan_vm(session, args):
-    op = args.pop("op")
-    nw_label = args.pop("nw-label")
-    primary = args.pop("primary-pvlan")
-    isolated = args.pop("isolated-pvlan")
-    vm_mac = args.pop("vm-mac")
-    trunk_port = 1
-
-    res = lib.check_switch()
-    if res != "SUCCESS":
-        return "FAILURE:%s" % res
-
-    bridge = lib.do_cmd([getBridgePath, nw_label])
-    logging.debug("Determine bridge/switch is :%s" % (bridge))
-
-    if op == "add":
-        logging.debug("About to setup vm on the switch:%s" % bridge)
-        res = lib.do_cmd([vmSetupPath, "-A", "-b", bridge, "-p", primary, "-i", isolated, "-v", vm_mac])
-	if res:
-	    result = "FAILURE:%s" % res
-	    return result;
-	logging.debug("Setup vm on switch program done")
-    elif op == "delete":
-        logging.debug("About to remove vm on the switch:%s" % bridge)
-        res = lib.do_cmd([vmSetupPath, "-D", "-b", bridge, "-p", primary, "-i", isolated, "-v", vm_mac])
-	if res:
-	    result = "FAILURE:%s" % res
-	    return result;
-	logging.debug("Remove vm on switch program done")
-
-    result = "true"
-    logging.debug("Setup_pvlan_vm_alone completed with result:%s" % result)
-    return result
-
-@echo
-def cleanup(session, args):
-    res = lib.check_switch()
-    if res != "SUCCESS":
-        return "FAILURE:%s" % res
-
-    res = lib.do_cmd([pvlanCleanUpPath])
-    if res:
-        result = "FAILURE:%s" % res
-        return result;
-
-    result = "true"
-    logging.debug("Setup_pvlan_vm_dhcp completed with result:%s" % result)
-    return result
-
-if __name__ == "__main__":
-    XenAPIPlugin.dispatch({"setup-pvlan-dhcp": setup_pvlan_dhcp,
-                           "setup-pvlan-vm": setup_pvlan_vm,
-                           "cleanup":cleanup})

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py b/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py
index 8452dae..58e0a3d 100644
--- a/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py
+++ b/scripts/vm/hypervisor/xenserver/ovs-vif-flows.py
@@ -21,7 +21,7 @@
 import os
 import sys
 
-import cloudstack_pluginlib as pluginlib
+import cloud-plugin-lib as pluginlib
 
 
 def clear_flows(bridge, this_vif_ofport, vif_ofports):

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/ovstunnel
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/ovstunnel b/scripts/vm/hypervisor/xenserver/ovstunnel
deleted file mode 100755
index 1ff7e82..0000000
--- a/scripts/vm/hypervisor/xenserver/ovstunnel
+++ /dev/null
@@ -1,261 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-
-# Creates a tunnel mesh across xenserver hosts
-# Enforces broadcast drop rules on ingress GRE tunnels
-
-import cloudstack_pluginlib as lib
-import logging
-import os
-import sys
-import subprocess
-import time
-import XenAPIPlugin
-
-sys.path.append("/opt/xensource/sm/")
-import util
-
-from time import localtime as _localtime, asctime as _asctime
-
-xePath = "/opt/xensource/bin/xe"
-lib.setup_logging("/var/log/ovstunnel.log")
-
-
-def block_ipv6_v5(bridge):
-    lib.add_flow(bridge, priority=65000, dl_type='0x86dd', actions='drop')
-
-
-def block_ipv6_v6(bridge):
-    lib.add_flow(bridge, priority=65000, proto='ipv6', actions='drop')
-
-
-block_ipv6_handlers = {
-        '5': block_ipv6_v5,
-        '6': block_ipv6_v6}
-
-
-def echo(fn):
-    def wrapped(*v, **k):
-        name = fn.__name__
-        logging.debug("#### VMOPS enter  %s ####" % name)
-        res = fn(*v, **k)
-        logging.debug("#### VMOPS exit  %s ####" % name)
-        return res
-    return wrapped
-
-
-@echo
-def setup_ovs_bridge(session, args):
-    bridge = args.pop("bridge")
-    key = args.pop("key")
-    xs_nw_uuid = args.pop("xs_nw_uuid")
-    cs_host_id = args.pop("cs_host_id")
-
-    res = lib.check_switch()
-    if res != "SUCCESS":
-        return "FAILURE:%s" % res
-
-    logging.debug("About to manually create the bridge:%s" % bridge)
-    # create a bridge with the same name as the xapi network
-    # also associate gre key in other config attribute
-    res = lib.do_cmd([lib.VSCTL_PATH, "--", "--may-exist", "add-br", bridge,
-                                     "--", "set", "bridge", bridge,
-                                     "other_config:gre_key=%s" % key])
-    logging.debug("Bridge has been manually created:%s" % res)
-    # TODO: Make sure xs-network-uuid is set into external_ids
-    lib.do_cmd([lib.VSCTL_PATH, "set", "Bridge", bridge,
-                            "external_ids:xs-network-uuid=%s" % xs_nw_uuid])
-    # Non empty result means something went wrong
-    if res:
-        result = "FAILURE:%s" % res
-    else:
-        # Verify the bridge actually exists, with the gre_key properly set
-        res = lib.do_cmd([lib.VSCTL_PATH, "get", "bridge",
-                                          bridge, "other_config:gre_key"])
-        if key in res:
-            result = "SUCCESS:%s" % bridge
-        else:
-            result = "FAILURE:%s" % res
-        # Finally note in the xenapi network object that the network has
-        # been configured
-        xs_nw_uuid = lib.do_cmd([lib.XE_PATH, "network-list",
-                                "bridge=%s" % bridge, "--minimal"])
-        lib.do_cmd([lib.XE_PATH, "network-param-set", "uuid=%s" % xs_nw_uuid,
-                   "other-config:is-ovs-tun-network=True"])
-        conf_hosts = lib.do_cmd([lib.XE_PATH, "network-param-get",
-                                "uuid=%s" % xs_nw_uuid,
-                                "param-name=other-config",
-                                "param-key=ovs-host-setup", "--minimal"])
-        conf_hosts = cs_host_id + (conf_hosts and ',%s' % conf_hosts or '')
-        lib.do_cmd([lib.XE_PATH, "network-param-set", "uuid=%s" % xs_nw_uuid,
-                   "other-config:ovs-host-setup=%s" % conf_hosts])
-
-        # BLOCK IPv6 - Flow spec changes with ovs version
-        host_list_cmd = [lib.XE_PATH, 'host-list', '--minimal']
-        host_list_str = lib.do_cmd(host_list_cmd)
-        host_uuid = host_list_str.split(',')[0].strip()
-        version_cmd = [lib.XE_PATH, 'host-param-get', 'uuid=%s' % host_uuid,
-                                   'param-name=software-version',
-                                   'param-key=product_version']
-        version = lib.do_cmd(version_cmd).split('.')[0]
-        block_ipv6_handlers[version](bridge)
-    logging.debug("Setup_ovs_bridge completed with result:%s" % result)
-    return result
-
-
-@echo
-def destroy_ovs_bridge(session, args):
-    bridge = args.pop("bridge")
-    res = lib.check_switch()
-    if res != "SUCCESS":
-        return res
-    res = lib.do_cmd([lib.VSCTL_PATH, "del-br", bridge])
-    logging.debug("Bridge has been manually removed:%s" % res)
-    if res:
-        result = "FAILURE:%s" % res
-    else:
-        # Note that the bridge has been removed on xapi network object
-        xs_nw_uuid = lib.do_cmd([xePath, "network-list",
-                                "bridge=%s" % bridge, "--minimal"])
-        #FIXME: WOW, this an error
-        #lib.do_cmd([xePath,"network-param-set", "uuid=%s" % xs_nw_uuid,
-        #                  "other-config:ovs-setup=False"])
-        result = "SUCCESS:%s" % bridge
-
-    logging.debug("Destroy_ovs_bridge completed with result:%s" % result)
-    return result
-
-
-@echo
-def create_tunnel(session, args):
-    bridge = args.pop("bridge")
-    remote_ip = args.pop("remote_ip")
-    gre_key = args.pop("key")
-    src_host = args.pop("from")
-    dst_host = args.pop("to")
-
-    logging.debug("Entering create_tunnel")
-
-    res = lib.check_switch()
-    if res != "SUCCESS":
-        logging.debug("Openvswitch running: NO")
-        return "FAILURE:%s" % res
-
-    # We need to keep the name below 14 characters
-    # src and target are enough - consider a fixed length hash
-    name = "t%s-%s-%s" % (gre_key, src_host, dst_host)
-
-    # Verify the xapi bridge to be created
-    # NOTE: Timeout should not be necessary anymore
-    wait = [lib.VSCTL_PATH, "--timeout=30", "wait-until", "bridge",
-                    bridge, "--", "get", "bridge", bridge, "name"]
-    res = lib.do_cmd(wait)
-    if bridge not in res:
-        logging.debug("WARNING:Can't find bridge %s for creating " +
-                                  "tunnel!" % bridge)
-        return "FAILURE:NO_BRIDGE"
-    logging.debug("bridge %s for creating tunnel - VERIFIED" % bridge)
-    tunnel_setup = False
-    drop_flow_setup = False
-    try:
-        # Create a port and configure the tunnel interface for it
-        add_tunnel = [lib.VSCTL_PATH, "add-port", bridge,
-                                  name, "--", "set", "interface",
-                                  name, "type=gre", "options:key=%s" % gre_key,
-                                  "options:remote_ip=%s" % remote_ip]
-        lib.do_cmd(add_tunnel)
-        tunnel_setup = True
-        # verify port
-        verify_port = [lib.VSCTL_PATH, "get", "port", name, "interfaces"]
-        res = lib.do_cmd(verify_port)
-        # Expecting python-style list as output
-        iface_list = []
-        if len(res) > 2:
-            iface_list = res.strip()[1:-1].split(',')
-        if len(iface_list) != 1:
-            logging.debug("WARNING: Unexpected output while verifying " +
-                                      "port %s on bridge %s" % (name, bridge))
-            return "FAILURE:VERIFY_PORT_FAILED"
-
-        # verify interface
-        iface_uuid = iface_list[0]
-        verify_interface_key = [lib.VSCTL_PATH, "get", "interface",
-                                iface_uuid, "options:key"]
-        verify_interface_ip = [lib.VSCTL_PATH, "get", "interface",
-                               iface_uuid, "options:remote_ip"]
-
-        key_validation = lib.do_cmd(verify_interface_key)
-        ip_validation = lib.do_cmd(verify_interface_ip)
-
-        if not gre_key in key_validation or not remote_ip in ip_validation:
-            logging.debug("WARNING: Unexpected output while verifying " +
-                          "interface %s on bridge %s" % (name, bridge))
-            return "FAILURE:VERIFY_INTERFACE_FAILED"
-        logging.debug("Tunnel interface validated:%s" % verify_interface_ip)
-        cmd_tun_ofport = [lib.VSCTL_PATH, "get", "interface",
-                                          iface_uuid, "ofport"]
-        tun_ofport = lib.do_cmd(cmd_tun_ofport)
-        # Ensure no trailing LF
-        if tun_ofport.endswith('\n'):
-            tun_ofport = tun_ofport[:-1]
-        # add flow entryies for dropping broadcast coming in from gre tunnel
-        lib.add_flow(bridge, priority=1000, in_port=tun_ofport,
-                         dl_dst='ff:ff:ff:ff:ff:ff', actions='drop')
-        lib.add_flow(bridge, priority=1000, in_port=tun_ofport,
-                     nw_dst='224.0.0.0/24', actions='drop')
-        drop_flow_setup = True
-        logging.debug("Broadcast drop rules added")
-        return "SUCCESS:%s" % name
-    except:
-        logging.debug("An unexpected error occured. Rolling back")
-        if tunnel_setup:
-            logging.debug("Deleting GRE interface")
-            # Destroy GRE port and interface
-            lib.del_port(bridge, name)
-        if drop_flow_setup:
-            # Delete flows
-            logging.debug("Deleting flow entries from GRE interface")
-            lib.del_flows(bridge, in_port=tun_ofport)
-        # This will not cancel the original exception
-        raise
-
-
-@echo
-def destroy_tunnel(session, args):
-    bridge = args.pop("bridge")
-    iface_name = args.pop("in_port")
-    logging.debug("Destroying tunnel at port %s for bridge %s"
-                            % (iface_name, bridge))
-    ofport = get_field_of_interface(iface_name, "ofport")
-    lib.del_flows(bridge, in_port=ofport)
-    lib.del_port(bridge, iface_name)
-    return "SUCCESS"
-
-
-def get_field_of_interface(iface_name, field):
-    get_iface_cmd = [lib.VSCTL_PATH, "get", "interface", iface_name, field]
-    res = lib.do_cmd(get_iface_cmd)
-    return res
-
-
-if __name__ == "__main__":
-    XenAPIPlugin.dispatch({"create_tunnel": create_tunnel,
-                           "destroy_tunnel": destroy_tunnel,
-                           "setup_ovs_bridge": setup_ovs_bridge,
-                           "destroy_ovs_bridge": destroy_ovs_bridge})

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/s3xen
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/s3xen b/scripts/vm/hypervisor/xenserver/s3xen
deleted file mode 100644
index bf81bbd..0000000
--- a/scripts/vm/hypervisor/xenserver/s3xen
+++ /dev/null
@@ -1,428 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Version @VERSION@
-#
-# A plugin for executing script needed by CloudStack
-from copy import copy
-from datetime import datetime
-from httplib import *
-from string import join
-from string import split
-
-import os
-import sys
-import time
-import md5 as md5mod
-import sha
-import base64
-import hmac
-import traceback
-import urllib2
-from xml.dom.minidom import parseString
-
-import XenAPIPlugin
-sys.path.extend(["/opt/xensource/sm/"])
-import util
-
-NULL = 'null'
-
-# Value conversion utility functions ...
-
-
-def to_none(value):
-
-    if value is None:
-        return None
-    if isinstance(value, basestring) and value.strip().lower() == NULL:
-        return None
-    return value
-
-
-def to_bool(value):
-
-    if to_none(value) is None:
-        return False
-    if isinstance(value, basestring) and value.strip().lower() == 'true':
-        return True
-    if isinstance(value, int) and value:
-        return True
-    return False
-
-
-def to_integer(value, default):
-
-    if to_none(value) is None or not isinstance(value, int):
-        return default
-    return int(value)
-
-
-def optional_str_value(value, default):
-
-    if is_not_blank(value):
-        return value
-    return default
-
-
-def is_blank(value):
-
-    return not is_not_blank(value)
-
-
-def is_not_blank(value):
-
-    if to_none(value) is None or not isinstance(value, basestring):
-        return True
-    if value.strip == '':
-        return False
-    return True
-
-
-def get_optional_key(map, key, default=''):
-
-    if key in map:
-        return map[key]
-    return default
-
-
-def log(message):
-
-    util.SMlog('#### VMOPS %s ####' % message)
-
-
-def echo(fn):
-    def wrapped(*v, **k):
-        name = fn.__name__
-        log("enter %s ####" % name)
-        res = fn(*v, **k)
-        log("exit %s with result %s" % (name, res))
-        return res
-    return wrapped
-
-
-def require_str_value(value, error_message):
-
-    if is_not_blank(value):
-        return value
-
-    raise ValueError(error_message)
-
-
-def retry(max_attempts, fn):
-
-    attempts = 1
-    while attempts <= max_attempts:
-        log("Attempting execution " + str(attempts) + "/" + str(
-            max_attempts) + " of " + fn.__name__)
-        try:
-            return fn()
-        except:
-            if (attempts >= max_attempts):
-                raise
-            attempts = attempts + 1
-
-
-def compute_md5(filename, buffer_size=8192):
-
-    hasher = md5mod.md5()
-
-    file = open(filename, 'rb')
-    try:
-
-        data = file.read(buffer_size)
-        while data != "":
-            hasher.update(data)
-            data = file.read(buffer_size)
-
-        return base64.encodestring(hasher.digest())[:-1]
-
-    finally:
-
-        file.close()
-
-
-class S3Client(object):
-
-    DEFAULT_END_POINT = 's3.amazonaws.com'
-    DEFAULT_CONNECTION_TIMEOUT = 50000
-    DEFAULT_SOCKET_TIMEOUT = 50000
-    DEFAULT_MAX_ERROR_RETRY = 3
-
-    HEADER_CONTENT_MD5 = 'Content-MD5'
-    HEADER_CONTENT_TYPE = 'Content-Type'
-    HEADER_CONTENT_LENGTH = 'Content-Length'
-
-    def __init__(self, access_key, secret_key, end_point=None,
-                 https_flag=None, connection_timeout=None, socket_timeout=None,
-                 max_error_retry=None):
-
-        self.access_key = require_str_value(
-            access_key, 'An access key must be specified.')
-        self.secret_key = require_str_value(
-            secret_key, 'A secret key must be specified.')
-        self.end_point = optional_str_value(end_point, self.DEFAULT_END_POINT)
-        self.https_flag = to_bool(https_flag)
-        self.connection_timeout = to_integer(
-            connection_timeout, self.DEFAULT_CONNECTION_TIMEOUT)
-        self.socket_timeout = to_integer(
-            socket_timeout, self.DEFAULT_SOCKET_TIMEOUT)
-        self.max_error_retry = to_integer(
-            max_error_retry, self.DEFAULT_MAX_ERROR_RETRY)
-
-    def build_canocialized_resource(self, bucket, key):
-        if not key.startswith("/"):
-            uri = bucket + "/" + key
-        else:
-            uri = bucket + key
-
-        return "/" + uri
-
-    def noop_send_body(connection):
-        pass
-
-    def noop_read(response):
-        return response.read()
-
-    def do_operation(
-        self, method, bucket, key, input_headers={},
-            fn_send_body=noop_send_body, fn_read=noop_read):
-
-        headers = copy(input_headers)
-        headers['Expect'] = '100-continue'
-
-        uri = self.build_canocialized_resource(bucket, key)
-        signature, request_date = self.sign_request(method, uri, headers)
-        headers['Authorization'] = "AWS " + self.access_key + ":" + signature
-        headers['Date'] = request_date
-
-        def perform_request():
-            connection = None
-            if self.https_flag:
-                connection = HTTPSConnection(self.end_point)
-            else:
-                connection = HTTPConnection(self.end_point)
-
-            try:
-                connection.timeout = self.socket_timeout
-                connection.putrequest(method, uri)
-
-                for k, v in headers.items():
-                    connection.putheader(k, v)
-                connection.endheaders()
-
-                fn_send_body(connection)
-
-                response = connection.getresponse()
-                log("Sent " + method + " request to " + self.end_point +
-                    uri + " with headers " + str(headers) +
-                    ".  Received response status " + str(response.status) +
-                    ": " + response.reason)
-
-                return fn_read(response)
-
-            finally:
-                connection.close()
-
-        return retry(self.max_error_retry, perform_request)
-
-    '''
-    See http://bit.ly/MMC5de for more information regarding the creation of
-    AWS authorization tokens and header signing
-    '''
-    def sign_request(self, operation, canocialized_resource, headers):
-
-        request_date = datetime.utcnow(
-        ).strftime('%a, %d %b %Y %H:%M:%S +0000')
-
-        content_hash = get_optional_key(headers, self.HEADER_CONTENT_MD5)
-        content_type = get_optional_key(headers, self.HEADER_CONTENT_TYPE)
-
-        string_to_sign = join(
-            [operation, content_hash, content_type, request_date,
-                canocialized_resource], '\n')
-
-        signature = base64.encodestring(
-            hmac.new(self.secret_key, string_to_sign.encode('utf8'),
-                     sha).digest())[:-1]
-
-        return signature, request_date
-        
-    def getText(self, nodelist):
-        rc = []
-        for node in nodelist:
-            if node.nodeType == node.TEXT_NODE:
-                rc.append(node.data)
-        return ''.join(rc)
-
-    def multiUpload(self, bucket, key, src_fileName, chunkSize=5 * 1024 * 1024):
-        uploadId={}
-        def readInitalMultipart(response):
-           data = response.read()
-           xmlResult = parseString(data) 
-           result = xmlResult.getElementsByTagName("InitiateMultipartUploadResult")[0]
-           upload = result.getElementsByTagName("UploadId")[0]
-           uploadId["0"] = upload.childNodes[0].data
-       
-        self.do_operation('POST', bucket, key + "?uploads", fn_read=readInitalMultipart) 
-
-        fileSize = os.path.getsize(src_fileName) 
-        parts = fileSize / chunkSize + ((fileSize % chunkSize) and 1)
-        part = 1
-        srcFile = open(src_fileName, 'rb')
-        etags = []
-        while part <= parts:
-            offset = part - 1
-            size = min(fileSize - offset * chunkSize, chunkSize)
-            headers = {
-                self.HEADER_CONTENT_LENGTH: size
-            }
-            def send_body(connection): 
-               srcFile.seek(offset * chunkSize)
-               block = srcFile.read(size)
-               connection.send(block)
-            def read_multiPart(response):
-               etag = response.getheader('ETag') 
-               etags.append((part, etag))
-            self.do_operation("PUT", bucket, "%s?partNumber=%s&uploadId=%s"%(key, part, uploadId["0"]), headers, send_body, read_multiPart)
-            part = part + 1
-        srcFile.close()
-
-        data = [] 
-        partXml = "<Part><PartNumber>%i</PartNumber><ETag>%s</ETag></Part>"
-        for etag in etags:
-            data.append(partXml%etag)
-        msg = "<CompleteMultipartUpload>%s</CompleteMultipartUpload>"%("".join(data))
-        size = len(msg)
-        headers = {
-            self.HEADER_CONTENT_LENGTH: size
-        }
-        def send_complete_multipart(connection):
-            connection.send(msg) 
-        self.do_operation("POST", bucket, "%s?uploadId=%s"%(key, uploadId["0"]), headers, send_complete_multipart)
-
-    def put(self, bucket, key, src_filename, maxSingleUpload):
-
-        if not os.path.isfile(src_filename):
-            raise Exception(
-                "Attempt to put " + src_filename + " that does not exist.")
-
-        size = os.path.getsize(src_filename)
-        if size > maxSingleUpload or maxSingleUpload == 0:
-            return self.multiUpload(bucket, key, src_filename)
-           
-        headers = {
-            self.HEADER_CONTENT_MD5: compute_md5(src_filename),
-        
-            self.HEADER_CONTENT_TYPE: 'application/octet-stream',
-            self.HEADER_CONTENT_LENGTH: str(os.stat(src_filename).st_size),
-        }
-
-        def send_body(connection):
-            src_file = open(src_filename, 'rb')
-            try:
-                while True:
-                    block = src_file.read(8192)
-                    if not block:
-                        break
-                    connection.send(block)
-
-            except:
-                src_file.close()
-
-        self.do_operation('PUT', bucket, key, headers, send_body)
-
-    def get(self, bucket, key, target_filename):
-
-        def read(response):
-
-            file = open(target_filename, 'wb')
-
-            try:
-
-                while True:
-                    block = response.read(8192)
-                    if not block:
-                        break
-                    file.write(block)
-            except:
-
-                file.close()
-
-        return self.do_operation('GET', bucket, key, fn_read=read)
-
-    def delete(self, bucket, key):
-
-        return self.do_operation('DELETE', bucket, key)
-
-
-def parseArguments(args):
-
-    # The keys in the args map will correspond to the properties defined on
-    # the com.cloud.utils.S3Utils#ClientOptions interface
-    client = S3Client(
-        args['accessKey'], args['secretKey'], args['endPoint'],
-        args['https'], args['connectionTimeout'], args['socketTimeout'])
-
-    operation = args['operation']
-    bucket = args['bucket']
-    key = args['key']
-    filename = args['filename']
-    maxSingleUploadBytes = int(args["maxSingleUploadSizeInBytes"])
-
-    if is_blank(operation):
-        raise ValueError('An operation must be specified.')
-
-    if is_blank(bucket):
-        raise ValueError('A bucket must be specified.')
-
-    if is_blank(key):
-        raise ValueError('A value must be specified.')
-
-    if is_blank(filename):
-        raise ValueError('A filename must be specified.')
-
-    return client, operation, bucket, key, filename, maxSingleUploadBytes
-
-
-@echo
-def s3(session, args):
-
-    client, operation, bucket, key, filename, maxSingleUploadBytes = parseArguments(args)
-
-    try:
-
-        if operation == 'put':
-            client.put(bucket, key, filename, maxSingleUploadBytes)
-        elif operation == 'get':
-            client.get(bucket, key, filename)
-        elif operation == 'delete':
-            client.delete(bucket, key, filename)
-        else:
-            raise RuntimeError(
-                "S3 plugin does not support operation " + operation)
-
-        return 'true'
-
-    except:
-        log("Operation " + operation + " on file " + filename +
-            " from/in bucket " + bucket + " key " + key)
-        log(traceback.format_exc())
-        return 'false'
-
-if __name__ == "__main__":
-    XenAPIPlugin.dispatch({"s3": s3})

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/setup_heartbeat_file.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/setup_heartbeat_file.sh b/scripts/vm/hypervisor/xenserver/setup_heartbeat_file.sh
index fdb3099..e6425d8 100755
--- a/scripts/vm/hypervisor/xenserver/setup_heartbeat_file.sh
+++ b/scripts/vm/hypervisor/xenserver/setup_heartbeat_file.sh
@@ -58,7 +58,7 @@ if [ `xe pbd-list sr-uuid=$2 | grep -B 1 $1 | wc -l` -eq 0 ]; then
   exit 0
 fi
 
-hbfile=/opt/cloudstack/bin/heartbeat
+hbfile=/etc/cloud/heartbeat
 
 if [ "$3" = "true" ]; then
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/setupxenserver.sh
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/setupxenserver.sh b/scripts/vm/hypervisor/xenserver/setupxenserver.sh
index 61ce90b..14a1d46 100755
--- a/scripts/vm/hypervisor/xenserver/setupxenserver.sh
+++ b/scripts/vm/hypervisor/xenserver/setupxenserver.sh
@@ -55,7 +55,7 @@ mv -n /etc/cron.daily/logrotate /etc/cron.hourly 2>&1
 echo 1048576 >/proc/sys/fs/aio-max-nr
 
 # empty heartbeat
-cat /dev/null > /opt/cloudstack/bin/heartbeat
+cat /dev/null > /etc/cloud/heartbeat
 # empty knownhost
 cat /dev/null > /root/.ssh/known_hosts
 


[6/8] Merged vmops and vmopspremium. Rename all xapi plugins to start with cloud-plugin-. Rename vmops to cloud-plugin-generic.

Posted by ah...@apache.org.
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-plugin-generic
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-generic b/scripts/vm/hypervisor/xenserver/cloud-plugin-generic
new file mode 100644
index 0000000..32c03de
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-generic
@@ -0,0 +1,1768 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# Version @VERSION@
+#
+# A plugin for executing script needed by CloudStack 
+
+import os, sys, time
+import XenAPIPlugin
+sys.path.extend(["/opt/xensource/sm/", "/usr/local/sbin/", "/sbin/"])
+import base64
+import socket
+import stat
+import tempfile
+import util
+import subprocess
+import zlib
+import cloud-plugin-lib as lib
+import logging
+from util import CommandException
+
+lib.setup_logging("/var/log/cloud/cloud-plugins.log")
+
+CS_DIR="/opt/cloud/bin/"
+
+def echo(fn):
+    def wrapped(*v, **k):
+        name = fn.__name__
+        logging.debug("#### CLOUD enter  %s ####" % name )
+        res = fn(*v, **k)
+        logging.debug("#### CLOUD exit  %s ####" % name )
+        return res
+    return wrapped
+
+@echo
+def add_to_VCPUs_params_live(session, args):
+    key = args['key']
+    value = args['value']
+    vmname = args['vmname']
+    try:
+        cmd = ["bash", CS_DIR + "add_to_vcpus_params_live.sh", vmname, key, value]
+        txt = util.pread2(cmd)
+    except:
+        return 'false'
+    return 'true'
+
+@echo
+def setup_iscsi(session, args):
+   uuid=args['uuid']
+   try:
+       cmd = ["bash", CS_DIR + "setup_iscsi.sh", uuid]
+       txt = util.pread2(cmd)
+   except:
+       txt = ''
+   return txt
+ 
+
+@echo
+def getgateway(session, args):
+    mgmt_ip = args['mgmtIP']
+    try:
+        cmd = ["bash", CS_DIR + "network_info.sh", "-g", mgmt_ip]
+        txt = util.pread2(cmd)
+    except:
+        txt = ''
+
+    return txt
+    
+@echo
+def preparemigration(session, args):
+    uuid = args['uuid']
+    try:
+        cmd = [CS_DIR + "make_migratable.sh", uuid]
+        util.pread2(cmd)
+        txt = 'success'
+    except:
+        logging.debug("Catch prepare migration exception" )
+        txt = ''
+
+    return txt
+
+@echo
+def setIptables(session, args):
+    try:
+        cmd = ["/bin/bash", CS_DIR + "setupxenserver.sh"]
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        logging.debug("  setIptables execution failed "  )
+        txt = '' 
+
+    return txt
+ 
+@echo
+def pingdomr(session, args):
+    host = args['host']
+    port = args['port']
+    socket.setdefaulttimeout(3)
+    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    try:
+        s.connect((host,int(port)))
+        txt = 'success'
+    except:
+        txt = ''
+    
+    s.close()
+
+    return txt
+
+@echo
+def kill_copy_process(session, args):
+    namelabel = args['namelabel']
+    try:
+        cmd = ["bash", CS_DIR + "kill_copy_process.sh", namelabel]
+        txt = util.pread2(cmd)
+    except:
+        txt = 'false'
+    return txt
+
+@echo
+def pingxenserver(session, args):
+    txt = 'success'
+    return txt
+
+def pingtest(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, CS_DIR + "pingtest.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        logging.debug("  pingtest failed "  )
+        txt = ''
+
+    return txt
+
+@echo
+def savePassword(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, CS_DIR + "save_password_to_domr.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        logging.debug("  save password to domr failed "  )
+        txt = '' 
+
+    return txt
+
+@echo
+def saveDhcpEntry(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, CS_DIR + "dhcp_entry.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        logging.debug(" save dhcp entry failed "  )
+        txt = '' 
+
+    return txt
+    
+@echo
+def setLinkLocalIP(session, args):
+    brName = args['brName']
+    try:
+        cmd = ["ip", "route", "del", "169.254.0.0/16"]
+        txt = util.pread2(cmd)
+    except:
+        txt = '' 
+    try:
+        cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
+        txt = util.pread2(cmd)
+    except:
+        try:
+            cmd = ['cat', '/etc/xensource/network.conf']
+            result = util.pread2(cmd)
+        except:
+            return 'can not cat network.conf'
+
+        if result.lower().strip() == "bridge":
+            try:
+                cmd = ["brctl", "addbr", brName]
+                txt = util.pread2(cmd)
+            except:
+                pass
+
+        else:
+            try:
+                cmd = ["ovs-vsctl", "add-br", brName]
+                txt = util.pread2(cmd)
+            except:
+                pass
+        
+        try:
+            cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
+            txt = util.pread2(cmd)
+        except:
+            pass
+    try:
+        cmd = ["ip", "route", "add", "169.254.0.0/16", "dev", brName, "src", "169.254.0.1"]
+        txt = util.pread2(cmd)
+    except:
+        txt = '' 
+    txt = 'success'
+    return txt
+
+
+    
+@echo
+def setFirewallRule(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, CS_DIR + "call_firewall.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        logging.debug(" set firewall rule failed "  )
+        txt = '' 
+
+    return txt
+    
+@echo
+def routerProxy(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, CS_DIR + "router_proxy.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        if txt is None or len(txt) == 0 :
+            txt = 'success'
+    except:
+        logging.debug("routerProxy command " + sargs + " failed "  )
+        txt = '' 
+
+    return txt
+
+
+
+@echo
+def setLoadBalancerRule(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, CS_DIR + "call_loadbalancer.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        logging.debug(" set loadbalancer rule failed "  )
+        txt = '' 
+
+    return txt
+@echo
+def configdnsmasq(session, args):
+    routerip = args['routerip']
+    args = args['args']
+    target   = "root@"+routerip
+    try:
+       util.pread2(['ssh','-p','3922','-q','-o','StrictHostKeyChecking=no','-i','/root/.ssh/id_rsa.cloud',target,'/root/dnsmasq.sh',args])
+       txt='success'
+    except:
+       logging.debug("failed to config dnsmasq server")
+       txt=''
+    return txt
+
+@echo
+def createipAlias(session, args):
+    args = args['args']
+    cmd = args.split(' ')
+    cmd.insert(0, CS_DIR + "createipAlias.sh")
+    cmd.insert(0, "bin/bash")
+    try:
+       txt=util.pread2(cmd)
+       txt='success'
+    except:
+       logging.debug("failed to create ip alias on router vm")
+       txt=''
+    return txt
+
+@echo
+def deleteipAlias(session, args):
+    args = args['args']
+    cmd = args.split(' ')
+    cmd.insert(0, CS_DIR + "deleteipAlias.sh")
+    cmd.insert(0, "bin/bash")
+    try:
+       txt=util.pread2(cmd)
+       txt='success'
+    except:
+       logging.debug("failed to create ip alias on router vm")
+       txt=''
+    return txt
+
+@echo
+def createFile(session, args):
+    file_path = args['filepath']
+    file_contents = args['filecontents']
+
+    try:
+        f = open(file_path, "w")
+        f.write(file_contents)
+        f.close()
+        txt = 'success'
+    except:
+        logging.debug(" failed to create HA proxy cfg file ")
+        txt = ''
+
+    return txt
+
+@echo
+def createFileInDomr(session, args):
+    file_path = args['filepath']
+    file_contents = args['filecontents']
+    domrip = args['domrip']
+    try:
+        tmpfile = util.pread2(['mktemp']).strip()
+        f = open(tmpfile, "w")
+        f.write(file_contents)
+        f.close()
+        target = "root@" + domrip + ":" + file_path
+        util.pread2(['scp','-P','3922','-q','-o','StrictHostKeyChecking=no','-i','/root/.ssh/id_rsa.cloud',tmpfile, target])
+        util.pread2(['rm',tmpfile])
+        txt = 'success'
+    except:
+        logging.debug(" failed to create HA proxy cfg file ")
+        txt = ''
+
+    return txt
+
+@echo
+def deleteFile(session, args):
+    file_path = args["filepath"]
+
+    try:
+        if os.path.isfile(file_path):
+            os.remove(file_path)
+        txt = 'success'
+    except:
+        logging.debug(" failed to remove HA proxy cfg file ")
+        txt = ''
+
+    return txt
+
+
+    
+def get_private_nic(session, args):
+    vms = session.xenapi.VM.get_all()
+    host_uuid = args.get('host_uuid')
+    host = session.xenapi.host.get_by_uuid(host_uuid)
+    piflist = session.xenapi.host.get_PIFs(host)
+    mgmtnic = 'eth0'
+    for pif in piflist:
+        pifrec = session.xenapi.PIF.get_record(pif)
+        network = pifrec.get('network')
+        nwrec = session.xenapi.network.get_record(network)
+        if nwrec.get('name_label') == 'cloud-guest':
+            return pifrec.get('device')
+        if pifrec.get('management'):
+            mgmtnic = pifrec.get('device')
+    
+    return mgmtnic
+
+def chain_name(vm_name):
+    if vm_name.startswith('i-') or vm_name.startswith('r-'):
+        if vm_name.endswith('untagged'):
+            return '-'.join(vm_name.split('-')[:-1])
+    return vm_name
+
+def chain_name_def(vm_name):
+    if vm_name.startswith('i-'):
+        if vm_name.endswith('untagged'):
+            return '-'.join(vm_name.split('-')[:-2]) + "-def"
+        return '-'.join(vm_name.split('-')[:-1]) + "-def"
+    return vm_name
+  
+def egress_chain_name(vm_name):
+    return chain_name(vm_name) + "-eg"
+      
+@echo
+def can_bridge_firewall(session, args):
+    try:
+        util.pread2(['ebtables', '-V'])
+        util.pread2(['ipset', '-V'])
+        cmd = ['cat', '/etc/xensource/network.conf']
+        result = util.pread2(cmd)
+        if result.lower().strip() != "bridge":
+            return 'false'
+
+    except:
+        return 'false'
+
+    host_uuid = args.get('host_uuid')
+    try:
+        util.pread2(['iptables', '-N', 'BRIDGE-FIREWALL'])
+        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-D', 'FORWARD',  '-j', 'RH-Firewall-1-INPUT'])
+    except:
+        logging.debug('Chain BRIDGE-FIREWALL already exists')
+
+    try:
+        util.pread2(['iptables', '-N', 'BRIDGE-DEFAULT-FIREWALL'])
+        util.pread2(['iptables', '-A', 'BRIDGE-DEFAULT-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-A', 'BRIDGE-DEFAULT-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-A', 'BRIDGE-DEFAULT-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '-j', 'BRIDGE-DEFAULT-FIREWALL'])
+        util.pread2(['iptables', '-D', 'BRIDGE-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-D', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
+        util.pread2(['iptables', '-D', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
+    except:
+        logging.debug('Chain BRIDGE-DEFAULT-FIREWALL already exists')
+
+    privnic = get_private_nic(session, args)
+    result = 'true'
+    try:
+        util.pread2(['/bin/bash', '-c', 'iptables -n -L FORWARD | grep BRIDGE-FIREWALL'])
+    except:
+        try:
+            util.pread2(['iptables', '-I', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '-j', 'BRIDGE-FIREWALL'])
+            util.pread2(['iptables', '-A', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', privnic, '-j', 'ACCEPT'])
+            util.pread2(['iptables', '-A', 'FORWARD', '-j', 'DROP'])
+        except:
+            return 'false'
+    default_ebtables_rules()
+    allow_egress_traffic(session)
+    if not os.path.exists('/var/run/cloud'):
+        os.makedirs('/var/run/cloud')
+    if not os.path.exists('/var/cache/cloud'):
+        os.makedirs('/var/cache/cloud')
+    #get_ipset_keyword()
+ 
+    cleanup_rules_for_dead_vms(session)
+    cleanup_rules(session, args)
+    
+    return result
+
+@echo
+def default_ebtables_rules():
+    try:
+        util.pread2(['ebtables', '-N',  'DEFAULT_EBTABLES'])
+        util.pread2(['ebtables', '-A', 'FORWARD', '-j'  'DEFAULT_EBTABLES'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'ACCEPT'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '--ip-proto', 'udp', '--ip-dport', '68', '-j', 'ACCEPT'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Request', '-j', 'ACCEPT'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Reply', '-j', 'ACCEPT'])
+        # deny mac broadcast and multicast
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Broadcast', '-j', 'DROP']) 
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Multicast', '-j', 'DROP']) 
+        # deny ip broadcast and multicast
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'DROP'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '224.0.0.0/4', '-j', 'DROP'])
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-j', 'RETURN'])
+        # deny ipv6
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv6', '-j', 'DROP'])
+        # deny vlan
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', '802_1Q', '-j', 'DROP'])
+        # deny all others (e.g., 802.1d, CDP)
+        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES',  '-j', 'DROP'])
+    except:
+        logging.debug('Chain DEFAULT_EBTABLES already exists')
+
+
+@echo
+def allow_egress_traffic(session):
+    devs = []
+    for pif in session.xenapi.PIF.get_all():
+        pif_rec = session.xenapi.PIF.get_record(pif)
+        dev = pif_rec.get('device')
+        devs.append(dev + "+")
+    for d in devs:
+        try:
+            util.pread2(['/bin/bash', '-c', "iptables -n -L FORWARD | grep '%s '" % d])
+        except:
+            try:
+                util.pread2(['iptables', '-I', 'FORWARD', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', d, '-j', 'ACCEPT'])
+            except:
+                logging.debug("Failed to add FORWARD rule through to %s" % d)
+                return 'false'
+    return 'true'
+
+
+def ipset(ipsetname, proto, start, end, ips):
+    try:
+        util.pread2(['ipset', '-N', ipsetname, 'iptreemap'])
+    except:
+        logging.debug("ipset chain already exists" + ipsetname)
+
+    result = True
+    ipsettmp = ''.join(''.join(ipsetname.split('-')).split('_')) + str(int(time.time()) % 1000)
+
+    try: 
+        util.pread2(['ipset', '-N', ipsettmp, 'iptreemap']) 
+    except:
+        logging.debug("Failed to create temp ipset, reusing old name= " + ipsettmp)
+        try: 
+            util.pread2(['ipset', '-F', ipsettmp]) 
+        except:
+            logging.debug("Failed to clear old temp ipset name=" + ipsettmp)
+            return False
+        
+    try: 
+        for ip in ips:
+            try:
+                util.pread2(['ipset', '-A', ipsettmp, ip])
+            except CommandException, cex:
+                if cex.reason.rfind('already in set') == -1:
+                   raise
+    except:
+        logging.debug("Failed to program ipset " + ipsetname)
+        util.pread2(['ipset', '-F', ipsettmp]) 
+        util.pread2(['ipset', '-X', ipsettmp]) 
+        return False
+
+    try: 
+        util.pread2(['ipset', '-W', ipsettmp, ipsetname]) 
+    except:
+        logging.debug("Failed to swap ipset " + ipsetname)
+        result = False
+
+    try: 
+        util.pread2(['ipset', '-F', ipsettmp]) 
+        util.pread2(['ipset', '-X', ipsettmp]) 
+    except:
+        # if the temporary name clashes next time we'll just reuse it
+        logging.debug("Failed to delete temp ipset " + ipsettmp)
+
+    return result
+
+@echo 
+def destroy_network_rules_for_vm(session, args):
+    vm_name = args.pop('vmName')
+    vmchain = chain_name(vm_name)
+    vmchain_egress = egress_chain_name(vm_name)
+    vmchain_default = chain_name_def(vm_name)
+    
+    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
+    if vm_name.startswith('i-') or vm_name.startswith('r-') or vm_name.startswith('l-'):
+        try:
+            util.pread2(['iptables', '-F', vmchain_default])
+            util.pread2(['iptables', '-X', vmchain_default])
+        except:
+            logging.debug("Ignoring failure to delete  chain " + vmchain_default)
+    
+    destroy_ebtables_rules(vmchain)
+    destroy_arptables_rules(vmchain)
+    
+    try:
+        util.pread2(['iptables', '-F', vmchain])
+        util.pread2(['iptables', '-X', vmchain])
+    except:
+        logging.debug("Ignoring failure to delete ingress chain " + vmchain)
+        
+   
+    try:
+        util.pread2(['iptables', '-F', vmchain_egress])
+        util.pread2(['iptables', '-X', vmchain_egress])
+    except:
+        logging.debug("Ignoring failure to delete egress chain " + vmchain_egress)
+    
+    remove_rule_log_for_vm(vm_name)
+    remove_secip_log_for_vm(vm_name)
+    
+    if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
+        return 'true'
+    
+    try:
+        setscmd = "ipset --save | grep " +  vmchain + " | grep '^-N' | awk '{print $2}'"
+        setsforvm = util.pread2(['/bin/bash', '-c', setscmd]).split('\n')
+        for set in setsforvm:
+            if set != '':
+                util.pread2(['ipset', '-F', set])       
+                util.pread2(['ipset', '-X', set])       
+    except:
+        logging.debug("Failed to destroy ipsets for %" % vm_name)
+    
+    
+    return 'true'
+
+@echo
+def destroy_ebtables_rules(vm_chain):
+    
+    delcmd = "ebtables-save | grep " +  vm_chain + " | sed 's/-A/-D/'"
+    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
+    delcmds.pop()
+    for cmd in delcmds:
+        try:
+            dc = cmd.split(' ')
+            dc.insert(0, 'ebtables')
+            util.pread2(dc)
+        except:
+            logging.debug("Ignoring failure to delete ebtables rules for vm " + vm_chain)
+    try:
+        util.pread2(['ebtables', '-F', vm_chain])
+        util.pread2(['ebtables', '-X', vm_chain])
+    except:
+            logging.debug("Ignoring failure to delete ebtables chain for vm " + vm_chain)   
+
+@echo
+def destroy_arptables_rules(vm_chain):
+    delcmd = "arptables -vL FORWARD | grep " + vm_chain + " | sed 's/-i any//' | sed 's/-o any//' | awk '{print $1,$2,$3,$4}' "
+    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
+    delcmds.pop()
+    for cmd in delcmds:
+        try:
+            dc = cmd.split(' ')
+            dc.insert(0, 'arptables')
+            dc.insert(1, '-D')
+            dc.insert(2, 'FORWARD')
+            util.pread2(dc)
+        except:
+            logging.debug("Ignoring failure to delete arptables rules for vm " + vm_chain)
+    
+    try:
+        util.pread2(['arptables', '-F', vm_chain])
+        util.pread2(['arptables', '-X', vm_chain])
+    except:
+        logging.debug("Ignoring failure to delete arptables chain for vm " + vm_chain) 
+              
+@echo
+def default_ebtables_antispoof_rules(vm_chain, vifs, vm_ip, vm_mac):
+    if vm_mac == 'ff:ff:ff:ff:ff:ff':
+        logging.debug("Ignoring since mac address is not valid")
+        return 'true'
+    
+    try:
+        util.pread2(['ebtables', '-N', vm_chain])
+    except:
+        try:
+            util.pread2(['ebtables', '-F', vm_chain])
+        except:
+            logging.debug("Failed to create ebtables antispoof chain, skipping")
+            return 'true'
+
+    # note all rules for packets into the bridge (-i) precede all output rules (-o)
+    # always start after the first rule in the FORWARD chain that jumps to DEFAULT_EBTABLES chain
+    try:
+        for vif in vifs:
+            util.pread2(['ebtables', '-I', 'FORWARD', '2', '-i',  vif,  '-j', vm_chain])
+            util.pread2(['ebtables', '-A', 'FORWARD', '-o',  vif, '-j', vm_chain])
+    except:
+        logging.debug("Failed to program default ebtables FORWARD rules for %s" % vm_chain)
+        return 'false'
+
+    try:
+        for vif in vifs:
+            # only allow source mac that belongs to the vm
+            try:
+                util.pread2(['ebtables', '-t', 'nat', '-I', 'PREROUTING', '-i', vif, '-s', '!' , vm_mac, '-j', 'DROP'])
+            except:
+                util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-s', '!', vm_mac,  '-j', 'DROP'])
+
+            # do not allow fake dhcp responses
+            util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '68', '-j', 'DROP'])
+            # do not allow snooping of dhcp requests
+            util.pread2(['ebtables', '-A', vm_chain, '-o', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'DROP'])
+    except:
+        logging.debug("Failed to program default ebtables antispoof rules for %s" % vm_chain)
+        return 'false'
+
+    return 'true'
+
+@echo
+def default_arp_antispoof(vm_chain, vifs, vm_ip, vm_mac):
+    if vm_mac == 'ff:ff:ff:ff:ff:ff':
+        logging.debug("Ignoring since mac address is not valid")
+        return 'true'
+
+    try:
+        util.pread2(['arptables',  '-N', vm_chain])
+    except:
+        try:
+            util.pread2(['arptables', '-F', vm_chain])
+        except:
+            logging.debug("Failed to create arptables rule, skipping")
+            return 'true'
+
+    # note all rules for packets into the bridge (-i) precede all output rules (-o)
+    try:
+        for vif in vifs:
+           util.pread2(['arptables',  '-I', 'FORWARD', '-i',  vif, '-j', vm_chain])
+           util.pread2(['arptables',  '-A', 'FORWARD', '-o',  vif, '-j', vm_chain])
+    except:
+        logging.debug("Failed to program default arptables rules in FORWARD chain vm=" + vm_chain)
+        return 'false'
+    
+    try:
+        for vif in vifs:
+            #accept arp replies into the bridge as long as the source mac and ips match the vm
+            util.pread2(['arptables',  '-A', vm_chain, '-i', vif, '--opcode', 'Reply', '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'ACCEPT'])
+            #accept any arp requests from this vm. In the future this can be restricted to deny attacks on hosts
+            #also important to restrict source ip and src mac in these requests as they can be used to update arp tables on destination
+            util.pread2(['arptables',  '-A', vm_chain, '-i', vif, '--opcode', 'Request',  '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'RETURN'])
+            #accept any arp requests to this vm as long as the request is for this vm's ip
+            util.pread2(['arptables',  '-A', vm_chain, '-o', vif, '--opcode', 'Request', '--destination-ip', vm_ip, '-j', 'ACCEPT'])   
+            #accept any arp replies to this vm as long as the mac and ip matches
+            util.pread2(['arptables',  '-A', vm_chain, '-o', vif, '--opcode', 'Reply', '--destination-mac', vm_mac, '--destination-ip', vm_ip, '-j', 'ACCEPT'])   
+        util.pread2(['arptables',  '-A', vm_chain,  '-j', 'DROP'])
+
+    except:
+        logging.debug("Failed to program default arptables  rules")
+        return 'false'
+
+    return 'true'
+
+
+@echo
+def network_rules_vmSecondaryIp(session, args):
+    vm_name = args.pop('vmName')
+    vm_mac = args.pop('vmMac')
+    ip_secondary = args.pop('vmSecIp')
+    action = args.pop('action')
+    logging.debug("vmMac = "+ vm_mac)
+    logging.debug("vmName = "+ vm_name)
+    #action = "-A"
+    logging.debug("action = "+ action)
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vm_name)
+        if len(vm) != 1:
+             return 'false'
+        vm_rec = session.xenapi.VM.get_record(vm[0])
+        vm_vifs = vm_rec.get('VIFs')
+        vifnums = [session.xenapi.VIF.get_record(vif).get('device') for vif in vm_vifs]
+        domid = vm_rec.get('domid')
+    except:
+        logging.debug("### Failed to get domid or vif list for vm  ##" + vm_name)
+        return 'false'
+
+    if domid == '-1':
+        logging.debug("### Failed to get domid for vm (-1):  " + vm_name)
+        return 'false'
+
+    vifs = ["vif" + domid + "." + v for v in vifnums]
+    #vm_name =  '-'.join(vm_name.split('-')[:-1])
+    vmchain = chain_name(vm_name)
+    add_to_ipset(vmchain, [ip_secondary], action)
+
+    #add arptables rules for the secondary ip
+    arp_rules_vmip(vmchain, vifs, [ip_secondary], vm_mac, action)
+
+    return 'true'
+
+@echo
+def default_network_rules_systemvm(session, args):
+    vm_name = args.pop('vmName')
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vm_name)
+        if len(vm) != 1:
+             return 'false'
+        vm_rec = session.xenapi.VM.get_record(vm[0])
+        vm_vifs = vm_rec.get('VIFs')
+        vifnums = [session.xenapi.VIF.get_record(vif).get('device') for vif in vm_vifs]
+        domid = vm_rec.get('domid')
+    except:
+        logging.debug("### Failed to get domid or vif list for vm  ##" + vm_name)
+        return 'false'
+    
+    if domid == '-1':
+        logging.debug("### Failed to get domid for vm (-1):  " + vm_name)
+        return 'false'
+
+    vifs = ["vif" + domid + "." + v for v in vifnums]
+    #vm_name =  '-'.join(vm_name.split('-')[:-1])
+    vmchain = chain_name(vm_name)
+   
+ 
+    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
+  
+    try:
+        util.pread2(['iptables', '-N', vmchain])
+    except:
+        util.pread2(['iptables', '-F', vmchain])
+    
+    for vif in vifs:
+        try:
+            util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', vif, '-j', vmchain])
+            util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', vmchain])
+            util.pread2(['iptables', '-I', vmchain, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', 'RETURN'])
+        except:
+            logging.debug("Failed to program default rules")
+            return 'false'
+	
+	
+    util.pread2(['iptables', '-A', vmchain, '-j', 'ACCEPT'])
+    
+    if write_rule_log_for_vm(vm_name, '-1', '_ignore_', domid, '_initial_', '-1') == False:
+        logging.debug("Failed to log default network rules for systemvm, ignoring")
+    return 'true'
+
+@echo
+def create_ipset_forvm (ipsetname):
+    result = True
+    try:
+        logging.debug("Creating ipset chain .... " + ipsetname)
+        util.pread2(['ipset', '-F', ipsetname])
+        util.pread2(['ipset', '-X', ipsetname])
+        util.pread2(['ipset', '-N', ipsetname, 'iphash'])
+    except:
+        logging.debug("ipset chain not exists creating.... " + ipsetname)
+        util.pread2(['ipset', '-N', ipsetname, 'iphash'])
+
+    return result
+
+@echo
+def add_to_ipset(ipsetname, ips, action):
+    result = True
+    for ip in ips:
+        try:
+            logging.debug("vm ip " + ip)
+            util.pread2(['ipset', action, ipsetname, ip])
+        except:
+            logging.debug("vm ip alreday in ip set" + ip)
+            continue
+
+    return result
+
+@echo
+def arp_rules_vmip (vm_chain, vifs, ips, vm_mac, action):
+    try:
+        if action == "-A":
+            action = "-I"
+        for vif in vifs:
+            for vm_ip in ips:
+                #accept any arp requests to this vm as long as the request is for this vm's ip
+                util.pread2(['arptables',  action, vm_chain, '-o', vif, '--opcode', 'Request', '--destination-ip', vm_ip, '-j', 'ACCEPT'])
+                #accept any arp replies to this vm as long as the mac and ip matches
+                util.pread2(['arptables',  action, vm_chain, '-o', vif, '--opcode', 'Reply', '--destination-mac', vm_mac, '--destination-ip', vm_ip, '-j', 'ACCEPT'])
+                #accept arp replies into the bridge as long as the source mac and ips match the vm
+                util.pread2(['arptables',  action, vm_chain, '-i', vif, '--opcode', 'Reply', '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'ACCEPT'])
+                #accept any arp requests from this vm. In the future this can be restricted to deny attacks on hosts
+                #also important to restrict source ip and src mac in these requests as they can be used to update arp tables on destination
+                util.pread2(['arptables',  action, vm_chain, '-i', vif, '--opcode', 'Request',  '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'RETURN'])
+    except:
+        logging.debug("Failed to program arptables  rules for ip")
+        return 'false'
+
+    return 'true'
+
+
+@echo
+def default_network_rules(session, args):
+    vm_name = args.pop('vmName')
+    vm_ip = args.pop('vmIP')
+    vm_id = args.pop('vmID')
+    vm_mac = args.pop('vmMAC')
+    sec_ips = args.pop("secIps")
+    action = "-A"
+    
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vm_name)
+        if len(vm) != 1:
+             logging.debug("### Failed to get record for vm  " + vm_name)
+             return 'false'
+        vm_rec = session.xenapi.VM.get_record(vm[0])
+        domid = vm_rec.get('domid')
+    except:
+        logging.debug("### Failed to get domid for vm " + vm_name)
+        return 'false'
+    if domid == '-1':     
+        logging.debug("### Failed to get domid for vm (-1):  " + vm_name)
+        return 'false'
+    
+    vif = "vif" + domid + ".0"
+    tap = "tap" + domid + ".0"
+    vifs = [vif]
+    try:
+        util.pread2(['ifconfig', tap])
+        vifs.append(tap)
+    except:
+        pass
+
+    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
+
+     
+    vmchain =  chain_name(vm_name)
+    vmchain_egress =  egress_chain_name(vm_name)
+    vmchain_default = chain_name_def(vm_name)
+    
+    destroy_ebtables_rules(vmchain)
+    
+
+    try:
+        util.pread2(['iptables', '-N', vmchain])
+    except:
+        util.pread2(['iptables', '-F', vmchain])
+    
+    try:
+        util.pread2(['iptables', '-N', vmchain_egress])
+    except:
+        util.pread2(['iptables', '-F', vmchain_egress])
+        
+    try:
+        util.pread2(['iptables', '-N', vmchain_default])
+    except:
+        util.pread2(['iptables', '-F', vmchain_default])        
+
+    vmipset = vm_name
+    #create ipset and add vm ips to that ip set
+    if create_ipset_forvm(vmipset) == False:
+       logging.debug(" failed to create ipset for rule " + str(tokens))
+       return 'false'
+
+    #add primary nic ip to ipset
+    if add_to_ipset(vmipset, [vm_ip], action ) == False:
+       logging.debug(" failed to add vm " + vm_ip + " ip to set ")
+       return 'false'
+
+    #add secodnary nic ips to ipset
+    secIpSet = "1"
+    ips = sec_ips.split(':')
+    ips.pop()
+    if ips[0] == "0":
+        secIpSet = "0";
+
+    if secIpSet == "1":
+        logging.debug("Adding ipset for secondary ips")
+        add_to_ipset(vmipset, ips, action)
+        if write_secip_log_for_vm(vm_name, sec_ips, vm_id) == False:
+            logging.debug("Failed to log default network rules, ignoring")
+
+    keyword = '--' + get_ipset_keyword()
+
+    try:
+        for v in vifs:
+            util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
+            util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
+
+        #don't let vm spoof its ip address
+        for v in vifs:
+            #util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip,'-p', 'udp', '--dport', '53', '-j', 'RETURN'])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-m', 'set', keyword, vmipset, 'src', '-p', 'udp', '--dport', '53', '-j', 'RETURN'])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-m', 'set', '!', keyword, vmipset, 'src', '-j', 'DROP'])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-m', 'set', '!', keyword, vmipset, 'dst', '-j', 'DROP'])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-m', 'set', keyword, vmipset, 'src', '-j', vmchain_egress])
+            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v,  '-j', vmchain])
+    except:
+        logging.debug("Failed to program default rules for vm " + vm_name)
+        return 'false'
+    
+    default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
+    #add default arp rules for secondary ips;
+    if secIpSet == "1":
+        logging.debug("Adding arp rules for sec ip")
+        arp_rules_vmip(vmchain, vifs, ips, vm_mac, action)
+
+    default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
+    
+    if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, '_initial_', '-1', vm_mac) == False:
+        logging.debug("Failed to log default network rules, ignoring")
+        
+    logging.debug("Programmed default rules for vm " + vm_name)
+    return 'true'
+
+@echo
+def check_domid_changed(session, vmName):
+    curr_domid = '-1'
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vmName)
+        if len(vm) != 1:
+             logging.debug("### Could not get record for vm ## " + vmName)
+        else:
+            vm_rec = session.xenapi.VM.get_record(vm[0])
+            curr_domid = vm_rec.get('domid')
+    except:
+        logging.debug("### Failed to get domid for vm  ## " + vmName)
+        
+    
+    logfilename = "/var/run/cloud/" + vmName +".log"
+    if not os.path.exists(logfilename):
+        return ['-1', curr_domid]
+    
+    lines = (line.rstrip() for line in open(logfilename))
+    
+    [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno, _vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
+    for line in lines:
+        try:
+            [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno,_vmMac] = line.split(',')
+        except ValueError,v:
+            [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno] = line.split(',')
+        break
+    
+    return [curr_domid, old_domid]
+
+@echo
+def delete_rules_for_vm_in_bridge_firewall_chain(vmName):
+    vm_name = vmName
+    vmchain = chain_name_def(vm_name)
+    
+    delcmd = "iptables-save | grep '\-A BRIDGE-FIREWALL' | grep " +  vmchain + " | sed 's/-A/-D/'"
+    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
+    delcmds.pop()
+    for cmd in delcmds:
+        try:
+            dc = cmd.split(' ')
+            dc.insert(0, 'iptables')
+            dc.pop()
+            util.pread2(filter(None, dc))
+        except:
+              logging.debug("Ignoring failure to delete rules for vm " + vmName)
+
+  
+@echo
+def network_rules_for_rebooted_vm(session, vmName):
+    vm_name = vmName
+    [curr_domid, old_domid] = check_domid_changed(session, vm_name)
+    
+    if curr_domid == old_domid:
+        return True
+    
+    if old_domid == '-1':
+        return True
+    
+    if curr_domid == '-1':
+        return True
+    
+    logging.debug("Found a rebooted VM -- reprogramming rules for  " + vm_name)
+    
+    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
+    if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
+        default_network_rules_systemvm(session, {"vmName":vm_name})
+        return True
+    
+    vif = "vif" + curr_domid + ".0"
+    tap = "tap" + curr_domid + ".0"
+    vifs = [vif]
+    try:
+        util.pread2(['ifconfig', tap])
+        vifs.append(tap)
+    except:
+        pass
+    vmchain = chain_name(vm_name)
+    vmchain_default = chain_name_def(vm_name)
+
+    for v in vifs:
+        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
+        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
+
+    #change antispoof rule in vmchain
+    try:
+        delcmd = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-in | sed 's/!--set/! --set/' | sed 's/-A/-D/'"
+        delcmd2 = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-out | sed 's/!--set/! --set/'| sed 's/-A/-D/'"
+        inscmd = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-in | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' | sed 's/!--set/! --set/'"
+        inscmd2 = "iptables-save| grep '\-A " +  vmchain_default + "' | grep  physdev-in | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/' | sed 's/!--set/! --set/'"
+        inscmd3 = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-out | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' | sed 's/!--set/! --set/'"
+        inscmd4 = "iptables-save| grep '\-A " +  vmchain_default + "' | grep  physdev-out | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/'  | sed 's/!--set/! --set/'"
+        
+        ipts = []
+        for cmd in [delcmd, delcmd2, inscmd, inscmd2, inscmd3, inscmd4]:
+            cmds = util.pread2(['/bin/bash', '-c', cmd]).split('\n')
+            cmds.pop()
+            for c in cmds:
+                    ipt = c.split(' ')
+                    ipt.insert(0, 'iptables')
+                    ipt.pop()
+                    ipts.append(ipt)
+        
+        for ipt in ipts:
+            try:
+                util.pread2(filter(None,ipt))
+            except:
+                logging.debug("Failed to rewrite antispoofing rules for vm " + vm_name)
+    except:
+        logging.debug("No rules found for vm " + vm_name)
+
+    destroy_ebtables_rules(vmchain)
+    destroy_arptables_rules(vmchain)
+    [vm_ip, vm_mac] = get_vm_mac_ip_from_log(vmchain)
+    default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
+
+    #check wether the vm has secondary ips
+    if is_secondary_ips_set(vm_name) == True:
+        vmips = get_vm_sec_ips(vm_name)
+        #add arp rules for the secondaryp ip
+        for ip in vmips:
+            arp_rules_vmip(vmchain, vifs, [ip], vm_mac, "-A")
+
+
+    default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
+    rewrite_rule_log_for_vm(vm_name, curr_domid)
+    return True
+
+
+
+@echo
+def get_vm_sec_ips(vm_name):
+    logfilename = "/var/run/cloud/" + vm_name +".ip"
+
+    lines = (line.rstrip() for line in open(logfilename))
+    for line in lines:
+        try:
+            [_vmName,_vmIP,_vmID] = line.split(',')
+            break
+        except ValueError,v:
+            [_vmName,_vmIP,_vmID] = line.split(',')
+
+    _vmIPS = _vmIP.split(":")[:-1]
+    return _vmIPS
+
+@echo
+def is_secondary_ips_set(vm_name):
+    logfilename = "/var/run/cloud/" + vm_name +".ip"
+    if not os.path.exists(logfilename):
+        return False
+
+    return True
+
+@echo
+def rewrite_rule_log_for_vm(vm_name, new_domid):
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    if not os.path.exists(logfilename):
+        return 
+    lines = (line.rstrip() for line in open(logfilename))
+    
+    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
+    for line in lines:
+        try:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
+            break
+        except ValueError,v:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
+    
+    write_rule_log_for_vm(_vmName, _vmID, _vmIP, new_domid, _signature, '-1', _vmMac)
+
+def get_rule_log_for_vm(session, vmName):
+    vm_name = vmName;
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    if not os.path.exists(logfilename):
+        return ''
+    
+    lines = (line.rstrip() for line in open(logfilename))
+    
+    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
+    for line in lines:
+        try:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
+            break
+        except ValueError,v:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
+    
+    return ','.join([_vmName, _vmID, _vmIP, _domID, _signature, _seqno])
+
+@echo
+def get_vm_mac_ip_from_log(vm_name):
+    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '0.0.0.0', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    if not os.path.exists(logfilename):
+        return ['_', '_']
+    
+    lines = (line.rstrip() for line in open(logfilename))
+    for line in lines:
+        try:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
+            break
+        except ValueError,v:
+            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
+    
+    return [ _vmIP, _vmMac]
+
+@echo
+def get_rule_logs_for_vms(session, args):
+    host_uuid = args.pop('host_uuid')
+    try:
+        thishost = session.xenapi.host.get_by_uuid(host_uuid)
+        hostrec = session.xenapi.host.get_record(thishost)
+        vms = hostrec.get('resident_VMs')
+    except:
+        logging.debug("Failed to get host from uuid " + host_uuid)
+        return ' '
+    
+    result = []
+    try:
+        for name in [session.xenapi.VM.get_name_label(x) for x in vms]:
+            if 1 not in [ name.startswith(c) for c in ['r-', 's-', 'v-', 'i-', 'l-'] ]:
+                continue
+            network_rules_for_rebooted_vm(session, name)
+            if name.startswith('i-'):
+                log = get_rule_log_for_vm(session, name)
+                result.append(log)
+    except:
+        logging.debug("Failed to get rule logs, better luck next time!")
+        
+    return ";".join(result)
+
+@echo
+def cleanup_rules_for_dead_vms(session):
+  try:
+    vms = session.xenapi.VM.get_all()
+    cleaned = 0
+    for vm_name in [session.xenapi.VM.get_name_label(x) for x in vms]:
+        if 1 in [ vm_name.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-'] ]:
+            vm = session.xenapi.VM.get_by_name_label(vm_name)
+            if len(vm) != 1:
+                continue
+            vm_rec = session.xenapi.VM.get_record(vm[0])
+            state = vm_rec.get('power_state')
+            if state != 'Running' and state != 'Paused':
+                logging.debug("vm " + vm_name + " is not running, cleaning up")
+                destroy_network_rules_for_vm(session, {'vmName':vm_name})
+                cleaned = cleaned+1
+                
+    logging.debug("Cleaned up rules for " + str(cleaned) + " vms")
+  except:
+    logging.debug("Failed to cleanup rules for dead vms!")
+        
+
+@echo
+def cleanup_rules(session, args):
+  instance = args.get('instance')
+  if not instance:
+    instance = 'VM'
+  resident_vms = []
+  try:
+    hostname = util.pread2(['/bin/bash', '-c', 'hostname']).split('\n')
+    if len(hostname) < 1:
+       raise Exception('Could not find hostname of this host')
+    thishost = session.xenapi.host.get_by_name_label(hostname[0])
+    if len(thishost) < 1:
+       raise Exception("Could not find host record from hostname %s of this host"%hostname[0])
+    hostrec = session.xenapi.host.get_record(thishost[0])
+    vms = hostrec.get('resident_VMs')
+    resident_vms = [session.xenapi.VM.get_name_label(x) for x in vms]
+    logging.debug('cleanup_rules: found %s resident vms on this host %s' % (len(resident_vms)-1, hostname[0]))
+ 
+    chainscmd = "iptables-save | grep '^:' | awk '{print $1}' | cut -d':' -f2 | sed 's/-def/-%s/'| sed 's/-eg//' | sort|uniq" % instance
+    chains = util.pread2(['/bin/bash', '-c', chainscmd]).split('\n')
+    vmchains = [ch  for ch in chains if 1 in [ ch.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-']]]
+    logging.debug('cleanup_rules: found %s iptables chains for vms on this host %s' % (len(vmchains), hostname[0]))
+    cleaned = 0
+    cleanup = []
+    for chain in vmchains:
+        vmname = chain
+        if vmname not in resident_vms:
+            vmname = chain + "-untagged"
+            if vmname not in resident_vms:
+                logging.debug("vm " + chain + " is not running on this host, cleaning up")
+                cleanup.append(chain)
+
+    for vm_name in cleanup:
+        destroy_network_rules_for_vm(session, {'vmName':vm_name})
+                    
+    logging.debug("Cleaned up rules for " + str(len(cleanup)) + " chains")
+    return str(len(cleanup))                
+  except Exception, ex:
+    logging.debug("Failed to cleanup rules, reason= " + str(ex))
+    return '-1';
+
+@echo
+def check_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno):
+    vm_name = vmName;
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    if not os.path.exists(logfilename):
+        logging.debug("Failed to find logfile %s" %logfilename)
+        return [True, True, True]
+        
+    lines = (line.rstrip() for line in open(logfilename))
+    
+    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
+    try:
+        for line in lines:
+            try:
+                [_vmName,_vmID,_vmIP,_domID,_signature,_seqno, _vmMac] = line.split(',')
+            except ValueError,v:
+                [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
+            break
+    except:
+        logging.debug("Failed to parse log file for vm " + vmName)
+        remove_rule_log_for_vm(vmName)
+        return [True, True, True]
+    
+    reprogramDefault = False
+    if (domID != _domID) or (vmID != _vmID) or (vmIP != _vmIP):
+        logging.debug("Change in default info set of vm %s" % vmName)
+        return [True, True, True]
+    else:
+        logging.debug("No change in default info set of vm %s" % vmName)
+    
+    reprogramChain = False
+    rewriteLog = True
+    if (int(seqno) > int(_seqno)):
+        if (_signature != signature):
+            reprogramChain = True
+            logging.debug("Seqno increased from %s to %s: reprogamming "\
+                        "ingress rules for vm %s" % (_seqno, seqno, vmName))
+        else:
+            logging.debug("Seqno increased from %s to %s: but no change "\
+                        "in signature for vm: skip programming ingress "\
+                        "rules %s" % (_seqno, seqno, vmName))
+    elif (int(seqno) < int(_seqno)):
+        logging.debug("Seqno decreased from %s to %s: ignoring these "\
+                        "ingress rules for vm %s" % (_seqno, seqno, vmName))
+        rewriteLog = False
+    elif (signature != _signature):
+        logging.debug("Seqno %s stayed the same but signature changed from "\
+                    "%s to %s for vm %s" % (seqno, _signature, signature, vmName))
+        rewriteLog = True
+        reprogramChain = True
+    else:
+        logging.debug("Seqno and signature stayed the same: %s : ignoring these "\
+                        "ingress rules for vm %s" % (seqno, vmName))
+        rewriteLog = False
+        
+    return [reprogramDefault, reprogramChain, rewriteLog]
+    
+@echo
+def write_secip_log_for_vm (vmName, secIps, vmId):
+    vm_name = vmName
+    logfilename = "/var/run/cloud/"+vm_name+".ip"
+    logging.debug("Writing log to " + logfilename)
+    logf = open(logfilename, 'w')
+    output = ','.join([vmName, secIps, vmId])
+    result = True
+
+    try:
+        logf.write(output)
+        logf.write('\n')
+    except:
+        logging.debug("Failed to write to rule log file " + logfilename)
+        result = False
+
+    logf.close()
+
+    return result
+
+@echo
+def remove_secip_log_for_vm(vmName):
+    vm_name = vmName
+    logfilename = "/var/run/cloud/"+vm_name+".ip"
+
+    result = True
+    try:
+        os.remove(logfilename)
+    except:
+        logging.debug("Failed to delete rule log file " + logfilename)
+        result = False
+
+    return result
+
+@echo
+def write_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno, vmMac='ff:ff:ff:ff:ff:ff'):
+    vm_name = vmName
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+    logging.debug("Writing log to " + logfilename)
+    logf = open(logfilename, 'w')
+    output = ','.join([vmName, vmID, vmIP, domID, signature, seqno, vmMac])
+    result = True
+    try:
+        logf.write(output)
+        logf.write('\n')
+    except:
+        logging.debug("Failed to write to rule log file " + logfilename)
+        result = False
+        
+    logf.close()
+    
+    return result
+
+@echo
+def remove_rule_log_for_vm(vmName):
+    vm_name = vmName
+    logfilename = "/var/run/cloud/" + vm_name +".log"
+
+    result = True
+    try:
+        os.remove(logfilename)
+    except:
+        logging.debug("Failed to delete rule log file " + logfilename)
+        result = False
+    
+    return result
+
+@echo
+def inflate_rules (zipped):
+   return zlib.decompress(base64.b64decode(zipped))
+
+@echo
+def cache_ipset_keyword():
+    tmpname = 'ipsetqzvxtmp'
+    try:
+        util.pread2(['/bin/bash', '-c', 'ipset -N ' + tmpname + ' iptreemap'])
+    except:
+        util.pread2(['/bin/bash', '-c', 'ipset -F ' + tmpname])
+
+    try:
+        util.pread2(['/bin/bash', '-c', 'iptables -A INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
+        util.pread2(['/bin/bash', '-c', 'iptables -D INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
+        keyword = 'set'
+    except:
+        keyword = 'match-set'
+    
+    try:
+       util.pread2(['/bin/bash', '-c', 'ipset -X ' + tmpname])
+    except:
+       pass
+       
+    cachefile = "/var/cache/cloud/ipset.keyword"
+    logging.debug("Writing ipset keyword to " + cachefile)
+    cachef = open(cachefile, 'w')
+    try:
+        cachef.write(keyword)
+        cachef.write('\n')
+    except:
+        logging.debug("Failed to write to cache file " + cachef)
+        
+    cachef.close()
+    return keyword
+    
+@echo
+def get_ipset_keyword():
+    cachefile = "/var/cache/cloud/ipset.keyword"
+    keyword = 'match-set'
+    
+    if not os.path.exists(cachefile):
+        logging.debug("Failed to find ipset keyword cachefile %s" %cachefile)
+        keyword = cache_ipset_keyword()
+    else:
+        lines = (line.rstrip() for line in open(cachefile))
+        for line in lines:
+            keyword = line
+            break
+
+    return keyword
+
+@echo
+def network_rules(session, args):
+  try:
+    vm_name = args.get('vmName')
+    vm_ip = args.get('vmIP')
+    vm_id = args.get('vmID')
+    vm_mac = args.get('vmMAC')
+    signature = args.pop('signature')
+    seqno = args.pop('seqno')
+    sec_ips = args.get("secIps")
+    deflated = 'false'
+    if 'deflated' in args:
+        deflated = args.pop('deflated')
+    
+    try:
+        vm = session.xenapi.VM.get_by_name_label(vm_name)
+        if len(vm) != 1:
+             logging.debug("### Could not get record for vm ## " + vm_name)
+             return 'false'
+        vm_rec = session.xenapi.VM.get_record(vm[0])
+        domid = vm_rec.get('domid')
+    except:
+        logging.debug("### Failed to get domid for vm  ## " + vm_name)
+        return 'false'
+    if domid == '-1':
+        logging.debug("### Failed to get domid for vm (-1):  " + vm_name)
+        return 'false'
+   
+    vif = "vif" + domid + ".0"
+    tap = "tap" + domid + ".0"
+    vifs = [vif]
+    try:
+        util.pread2(['ifconfig', tap])
+        vifs.append(tap)
+    except:
+        pass
+   
+
+    reason = 'seqno_change_or_sig_change'
+    [reprogramDefault, reprogramChain, rewriteLog] = \
+             check_rule_log_for_vm (vm_name, vm_id, vm_ip, domid, signature, seqno)
+    
+    if not reprogramDefault and not reprogramChain:
+        logging.debug("No changes detected between current state and received state")
+        reason = 'seqno_same_sig_same'
+        if rewriteLog:
+            reason = 'seqno_increased_sig_same'
+            write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac)
+        logging.debug("Programming network rules for vm  %s seqno=%s signature=%s guestIp=%s,"\
+               " do nothing, reason=%s" % (vm_name, seqno, signature, vm_ip, reason))
+        return 'true'
+           
+    if not reprogramChain:
+        logging.debug("###Not programming any ingress rules since no changes detected?")
+        return 'true'
+
+    if reprogramDefault:
+        logging.debug("Change detected in vmId or vmIp or domId, resetting default rules")
+        default_network_rules(session, args)
+        reason = 'domid_change'
+    
+    rules = args.pop('rules')
+    if deflated.lower() == 'true':
+       rules = inflate_rules (rules)
+    keyword = '--' + get_ipset_keyword() 
+    lines = rules.split(' ')
+
+    logging.debug("Programming network rules for vm  %s seqno=%s numrules=%s signature=%s guestIp=%s,"\
+              " update iptables, reason=%s" % (vm_name, seqno, len(lines), signature, vm_ip, reason))
+    
+    cmds = []
+    egressrules = 0
+    for line in lines:
+        tokens = line.split(':')
+        if len(tokens) != 5:
+          continue
+        type = tokens[0]
+        protocol = tokens[1]
+        start = tokens[2]
+        end = tokens[3]
+        cidrs = tokens.pop();
+        ips = cidrs.split(",")
+        ips.pop()
+        allow_any = False
+
+        if type == 'E':
+            vmchain = egress_chain_name(vm_name)
+            action = "RETURN"
+            direction = "dst"
+            egressrules = egressrules + 1
+        else:
+            vmchain = chain_name(vm_name)
+            action = "ACCEPT"
+            direction = "src"
+        if  '0.0.0.0/0' in ips:
+            i = ips.index('0.0.0.0/0')
+            del ips[i]
+            allow_any = True
+        range = start + ":" + end
+        if ips:    
+            ipsetname = vmchain + "_" + protocol + "_" + start + "_" + end
+            if start == "-1":
+                ipsetname = vmchain + "_" + protocol + "_any"
+
+            if ipset(ipsetname, protocol, start, end, ips) == False:
+                logging.debug(" failed to create ipset for rule " + str(tokens))
+
+            if protocol == 'all':
+                iptables = ['iptables', '-I', vmchain, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
+            elif protocol != 'icmp':
+                iptables = ['iptables', '-I', vmchain, '-p',  protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
+            else:
+                range = start + "/" + end
+                if start == "-1":
+                    range = "any"
+                iptables = ['iptables', '-I', vmchain, '-p',  'icmp', '--icmp-type',  range,  '-m', 'set', keyword, ipsetname, direction, '-j', action]
+                
+            cmds.append(iptables)
+            logging.debug(iptables)
+        
+        if allow_any and protocol != 'all':
+            if protocol != 'icmp':
+                iptables = ['iptables', '-I', vmchain, '-p',  protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-j', action]
+            else:
+                range = start + "/" + end
+                if start == "-1":
+                    range = "any"
+                iptables = ['iptables', '-I', vmchain, '-p',  'icmp', '--icmp-type',  range, '-j', action]
+            cmds.append(iptables)
+            logging.debug(iptables)
+      
+    vmchain = chain_name(vm_name)
+    try:
+        util.pread2(['iptables', '-F', vmchain])
+    except:
+        logging.debug("Ignoring failure to delete chain " + vmchain)
+        util.pread2(['iptables', '-N', vmchain])
+
+    egress_vmchain = egress_chain_name(vm_name)
+    try:
+        util.pread2(['iptables', '-F', egress_vmchain])
+    except:
+        logging.debug("Ignoring failure to delete chain " + egress_vmchain)
+        util.pread2(['iptables', '-N', egress_vmchain])
+
+    
+    for cmd in cmds:
+        util.pread2(cmd)
+        
+    if egressrules == 0 :
+        util.pread2(['iptables', '-A', egress_vmchain, '-j', 'RETURN'])
+    else:
+        util.pread2(['iptables', '-A', egress_vmchain, '-j', 'DROP'])
+   
+    util.pread2(['iptables', '-A', vmchain, '-j', 'DROP'])
+
+    if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac) == False:
+        return 'false'
+    
+    return 'true'
+  except:
+    logging.debug("Failed to network rule !")
+
+@echo
+def bumpUpPriority(session, args):
+    sargs = args['args']
+    cmd = sargs.split(' ')
+    cmd.insert(0, CS_DIR + "bumpUpPriority.sh")
+    cmd.insert(0, "/bin/bash")
+    try:
+        txt = util.pread2(cmd)
+        txt = 'success'
+    except:
+        logging.debug("bump up priority fail! ")
+        txt = ''
+
+    return txt
+@echo
+def forceShutdownVM(session, args):
+    domId = args['domId']
+    try:
+        cmd = ["/opt/xensource/debug/xenops", "destroy_domain", "-domid", domId]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+
+@echo
+def create_privatetemplate_from_snapshot(session, args):
+    templatePath = args['templatePath']
+    snapshotPath = args['snapshotPath']
+    tmpltLocalDir = args['tmpltLocalDir']
+    try:
+        cmd = ["bash", CS_DIR + "create_privatetemplate_from_snapshot.sh",snapshotPath, templatePath, tmpltLocalDir]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+@echo
+def upgrade_snapshot(session, args):
+    templatePath = args['templatePath']
+    snapshotPath = args['snapshotPath']
+    try:
+        cmd = ["bash", CS_DIR + "upgrate_snapshot.sh",snapshotPath, templatePath]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+@echo
+def copy_vhd_to_secondarystorage(session, args):
+    mountpoint = args['mountpoint']
+    vdiuuid = args['vdiuuid']
+    sruuid = args['sruuid']
+    try:
+        cmd = ["bash", CS_DIR + "copy_vhd_to_secondarystorage.sh", mountpoint, vdiuuid, sruuid]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+@echo
+def copy_vhd_from_secondarystorage(session, args):
+    mountpoint = args['mountpoint']
+    sruuid = args['sruuid']
+    namelabel = args['namelabel']
+    try:
+        cmd = ["bash", CS_DIR + "copy_vhd_from_secondarystorage.sh", mountpoint, sruuid, namelabel]
+        txt = util.pread2(cmd)
+    except:
+        txt = '10#failed'
+    return txt
+
+@echo
+def setup_heartbeat_sr(session, args):
+    host = args['host']
+    sr = args['sr']
+    try:
+        cmd = ["bash", CS_DIR + "setup_heartbeat_sr.sh", host, sr]
+        txt = util.pread2(cmd)
+    except:
+        txt = ''
+    return txt
+
+@echo
+def setup_heartbeat_file(session, args):
+    host = args['host']
+    sr = args['sr']
+    add = args['add']
+    try:
+        cmd = ["bash", CS_DIR + "setup_heartbeat_file.sh", host, sr, add]
+        txt = util.pread2(cmd)
+    except:
+        txt = ''
+    return txt
+
+@echo
+def check_heartbeat(session, args):
+    host = args['host']
+    interval = args['interval']
+    try:
+       cmd = ["bash", CS_DIR + "check_heartbeat.sh", host, interval]
+       txt = util.pread2(cmd)
+    except:
+       txt=''
+    return txt
+    
+   
+@echo
+def heartbeat(session, args):
+    host = args['host']
+    interval = args['interval']
+    try: 
+       cmd = ["/bin/bash", CS_DIR + "launch_hb.sh", host, interval]
+       txt = util.pread2(cmd)
+    except:
+       txt='fail'
+    return txt
+
+if __name__ == "__main__":
+     XenAPIPlugin.dispatch({"forceShutdownVM":forceShutdownVM, 
+	                        "upgrade_snapshot":upgrade_snapshot,
+                            "create_privatetemplate_from_snapshot":create_privatetemplate_from_snapshot,
+                            "copy_vhd_to_secondarystorage":copy_vhd_to_secondarystorage, 
+							"copy_vhd_from_secondarystorage":copy_vhd_from_secondarystorage, 
+							"setup_heartbeat_sr":setup_heartbeat_sr, 
+							"setup_heartbeat_file":setup_heartbeat_file, 
+							"check_heartbeat":check_heartbeat, 
+							"heartbeat": heartbeat, 
+							"pingtest": pingtest, 
+							"setup_iscsi":setup_iscsi,  
+                            "getgateway": getgateway, 
+							"preparemigration": preparemigration, 
+                            "setIptables": setIptables, 
+							"pingdomr": pingdomr, 
+							"pingxenserver": pingxenserver,  
+                            "savePassword": savePassword, 
+                            "saveDhcpEntry": saveDhcpEntry, 
+							"setFirewallRule": setFirewallRule, 
+							"routerProxy": routerProxy, 
+                            "setLoadBalancerRule": setLoadBalancerRule, 
+							"createFile": createFile, 
+							"deleteFile": deleteFile, 
+                            "network_rules":network_rules, 
+                            "can_bridge_firewall":can_bridge_firewall, 
+							"default_network_rules":default_network_rules,
+                            "destroy_network_rules_for_vm":destroy_network_rules_for_vm, 
+                            "default_network_rules_systemvm":default_network_rules_systemvm, 
+                            "network_rules_vmSecondaryIp":network_rules_vmSecondaryIp,
+                            "createipAlias":createipAlias,
+                            "configdnsmasq":configdnsmasq,
+                            "deleteipAlias":deleteipAlias,
+                            "get_rule_logs_for_vms":get_rule_logs_for_vms, 
+			                "add_to_VCPUs_params_live":add_to_VCPUs_params_live,
+                            "setLinkLocalIP":setLinkLocalIP,
+                            "cleanup_rules":cleanup_rules,
+                            "bumpUpPriority":bumpUpPriority,
+                            "createFileInDomr":createFileInDomr,
+                            "kill_copy_process":kill_copy_process})

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-plugin-lib.py
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-lib.py b/scripts/vm/hypervisor/xenserver/cloud-plugin-lib.py
new file mode 100644
index 0000000..6b17d0b
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-lib.py
@@ -0,0 +1,221 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# Common function for Cloudstack's XenAPI plugins
+
+import ConfigParser
+import logging
+import os
+import subprocess
+
+from time import localtime, asctime
+
+DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s"
+DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
+DEFAULT_LOG_FILE = "/var/log/cloud/cloud-plugins.log"
+
+PLUGIN_CONFIG_PATH = "/etc/cloud/cloud-plugins.conf"
+OVSDB_PID_PATH = "/var/run/openvswitch/ovsdb-server.pid"
+OVSDB_DAEMON_PATH = "ovsdb-server"
+OVS_PID_PATH = "/var/run/openvswitch/ovs-vswitchd.pid"
+OVS_DAEMON_PATH = "ovs-vswitchd"
+VSCTL_PATH = "/usr/bin/ovs-vsctl"
+OFCTL_PATH = "/usr/bin/ovs-ofctl"
+XE_PATH = "/opt/xensource/bin/xe"
+
+
+class PluginError(Exception):
+    """Base Exception class for all plugin errors."""
+    def __init__(self, *args):
+        Exception.__init__(self, *args)
+
+
+def setup_logging(log_file=None):
+    debug = False
+    verbose = False
+    log_format = DEFAULT_LOG_FORMAT
+    log_date_format = DEFAULT_LOG_DATE_FORMAT
+    # try to read plugin configuration file
+    if os.path.exists(PLUGIN_CONFIG_PATH):
+        config = ConfigParser.ConfigParser()
+        config.read(PLUGIN_CONFIG_PATH)
+        try:
+            options = config.options('LOGGING')
+            if 'debug' in options:
+                debug = config.getboolean('LOGGING', 'debug')
+            if 'verbose' in options:
+                verbose = config.getboolean('LOGGING', 'verbose')
+            if 'format' in options:
+                log_format = config.get('LOGGING', 'format')
+            if 'date_format' in options:
+                log_date_format = config.get('LOGGING', 'date_format')
+            if 'file' in options:
+                log_file_2 = config.get('LOGGING', 'file')
+        except ValueError:
+            # configuration file contained invalid attributes
+            # ignore them
+            pass
+        except ConfigParser.NoSectionError:
+            # Missing 'Logging' section in configuration file
+            pass
+
+    root_logger = logging.root
+    if debug:
+        root_logger.setLevel(logging.DEBUG)
+    elif verbose:
+        root_logger.setLevel(logging.INFO)
+    else:
+        root_logger.setLevel(logging.WARNING)
+    formatter = logging.Formatter(log_format, log_date_format)
+
+    log_filename = log_file or log_file_2 or DEFAULT_LOG_FILE
+
+    logfile_handler = logging.FileHandler(log_filename)
+    logfile_handler.setFormatter(formatter)
+    root_logger.addHandler(logfile_handler)
+
+
+def do_cmd(cmd):
+    """Abstracts out the basics of issuing system commands. If the command
+    returns anything in stderr, a PluginError is raised with that information.
+    Otherwise, the output from stdout is returned.
+    """
+
+    pipe = subprocess.PIPE
+    logging.debug("Executing:%s", cmd)
+    proc = subprocess.Popen(cmd, shell=False, stdin=pipe, stdout=pipe,
+                            stderr=pipe, close_fds=True)
+    ret_code = proc.wait()
+    err = proc.stderr.read()
+    if ret_code:
+        logging.debug("The command exited with the error code: " +
+                      "%s (stderr output:%s)" % (ret_code, err))
+        raise PluginError(err)
+    output = proc.stdout.read()
+    if output.endswith('\n'):
+        output = output[:-1]
+    return output
+
+
+def _is_process_run(pidFile, name):
+    try:
+        fpid = open(pidFile, "r")
+        pid = fpid.readline()
+        fpid.close()
+    except IOError, e:
+        return -1
+
+    pid = pid[:-1]
+    ps = os.popen("ps -ae")
+    for l in ps:
+        if pid in l and name in l:
+            ps.close()
+            return 0
+
+    ps.close()
+    return -2
+
+
+def _is_tool_exist(name):
+    if os.path.exists(name):
+        return 0
+    return -1
+
+
+def check_switch():
+    global result
+
+    ret = _is_process_run(OVSDB_PID_PATH, OVSDB_DAEMON_PATH)
+    if ret < 0:
+        if ret == -1:
+            return "NO_DB_PID_FILE"
+        if ret == -2:
+            return "DB_NOT_RUN"
+
+    ret = _is_process_run(OVS_PID_PATH, OVS_DAEMON_PATH)
+    if ret < 0:
+        if ret == -1:
+            return "NO_SWITCH_PID_FILE"
+        if ret == -2:
+            return "SWITCH_NOT_RUN"
+
+    if _is_tool_exist(VSCTL_PATH) < 0:
+        return "NO_VSCTL"
+
+    if _is_tool_exist(OFCTL_PATH) < 0:
+        return "NO_OFCTL"
+
+    return "SUCCESS"
+
+
+def _build_flow_expr(**kwargs):
+    is_delete_expr = kwargs.get('delete', False)
+    flow = ""
+    if not is_delete_expr:
+        flow = "hard_timeout=%s,idle_timeout=%s,priority=%s"\
+                % (kwargs.get('hard_timeout', '0'),
+                   kwargs.get('idle_timeout', '0'),
+                   kwargs.get('priority', '1'))
+    in_port = 'in_port' in kwargs and ",in_port=%s" % kwargs['in_port'] or ''
+    dl_type = 'dl_type' in kwargs and ",dl_type=%s" % kwargs['dl_type'] or ''
+    dl_src = 'dl_src' in kwargs and ",dl_src=%s" % kwargs['dl_src'] or ''
+    dl_dst = 'dl_dst' in kwargs and ",dl_dst=%s" % kwargs['dl_dst'] or ''
+    nw_src = 'nw_src' in kwargs and ",nw_src=%s" % kwargs['nw_src'] or ''
+    nw_dst = 'nw_dst' in kwargs and ",nw_dst=%s" % kwargs['nw_dst'] or ''
+    proto = 'proto' in kwargs and ",%s" % kwargs['proto'] or ''
+    ip = ('nw_src' in kwargs or 'nw_dst' in kwargs) and ',ip' or ''
+    flow = (flow + in_port + dl_type + dl_src + dl_dst +
+            (ip or proto) + nw_src + nw_dst)
+    return flow
+
+
+def add_flow(bridge, **kwargs):
+    """
+    Builds a flow expression for **kwargs and adds the flow entry
+    to an Open vSwitch instance
+    """
+    flow = _build_flow_expr(**kwargs)
+    actions = 'actions' in kwargs and ",actions=%s" % kwargs['actions'] or ''
+    flow = flow + actions
+    addflow = [OFCTL_PATH, "add-flow", bridge, flow]
+    do_cmd(addflow)
+
+
+def del_flows(bridge, **kwargs):
+    """
+    Removes flows according to criteria passed as keyword.
+    """
+    flow = _build_flow_expr(delete=True, **kwargs)
+    # out_port condition does not exist for all flow commands
+    out_port = ("out_port" in kwargs and
+                ",out_port=%s" % kwargs['out_port'] or '')
+    flow = flow + out_port
+    delFlow = [OFCTL_PATH, 'del-flows', bridge, flow]
+    do_cmd(delFlow)
+
+
+def del_all_flows(bridge):
+    delFlow = [OFCTL_PATH, "del-flows", bridge]
+    do_cmd(delFlow)
+
+    normalFlow = "priority=0 idle_timeout=0 hard_timeout=0 actions=normal"
+    add_flow(bridge, normalFlow)
+
+
+def del_port(bridge, port):
+    delPort = [VSCTL_PATH, "del-port", bridge, port]
+    do_cmd(delPort)

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan b/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan
new file mode 100644
index 0000000..32eb794
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-ovs-pvlan
@@ -0,0 +1,148 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+
+import cloud-plugin-lib as lib
+import logging
+import os
+import sys
+import subprocess
+import time
+import XenAPIPlugin
+
+sys.path.append("/opt/xensource/sm/")
+import util
+
+from time import localtime as _localtime, asctime as _asctime
+
+CS_DIR = "/opt/cloud/bin/"
+
+xePath = "/opt/xensource/bin/xe"
+dhcpSetupPath = CS_DIR + "ovs-pvlan-dhcp-host.sh"
+vmSetupPath = CS_DIR + "ovs-pvlan-vm.sh"
+getDhcpIfacePath = CS_DIR + "ovs-get-dhcp-iface.sh"
+pvlanCleanupPath = CS_DIR + "ovs-pvlan-cleanup.sh"
+getBridgePath = CS_DIR + "ovs-get-bridge.sh"
+
+lib.setup_logging("/var/log/cloud/cloud-ovs-pvlan.log")
+
+def echo(fn):
+    def wrapped(*v, **k):
+        name = fn.__name__
+        logging.debug("#### CLOUD enter  %s ####" % name)
+        res = fn(*v, **k)
+        logging.debug("#### CLOUD exit  %s ####" % name)
+        return res
+    return wrapped
+
+@echo
+def setup_pvlan_dhcp(session, args):
+    op = args.pop("op")
+    nw_label = args.pop("nw-label")
+    primary = args.pop("primary-pvlan")
+    isolated = args.pop("isolated-pvlan")
+    dhcp_name = args.pop("dhcp-name")
+    dhcp_ip = args.pop("dhcp-ip")
+    dhcp_mac = args.pop("dhcp-mac")
+
+    res = lib.check_switch()
+    if res != "SUCCESS":
+        return "FAILURE:%s" % res
+
+    logging.debug("Network is:%s" % (nw_label))
+    bridge = lib.do_cmd([getBridgePath, nw_label])
+    logging.debug("Determine bridge/switch is :%s" % (bridge))
+
+    if op == "add":
+        logging.debug("Try to get dhcp vm %s port on the switch:%s" % (dhcp_name, bridge))
+        dhcp_iface = lib.do_cmd([getDhcpIfacePath, bridge, dhcp_name])
+        logging.debug("About to setup dhcp vm on the switch:%s" % bridge)
+        res = lib.do_cmd([dhcpSetupPath, "-A", "-b", bridge, "-p", primary,
+            "-i", isolated, "-n", dhcp_name, "-d", dhcp_ip, "-m", dhcp_mac,
+            "-I", dhcp_iface])
+	if res:
+	    result = "FAILURE:%s" % res
+	    return result;
+	logging.debug("Setup dhcp vm on switch program done")
+    elif op == "delete":
+        logging.debug("About to remove dhcp the switch:%s" % bridge)
+        res = lib.do_cmd([dhcpSetupPath, "-D", "-b", bridge, "-p", primary,
+            "-i", isolated, "-n", dhcp_name, "-d", dhcp_ip, "-m", dhcp_mac])
+	if res:
+	    result = "FAILURE:%s" % res
+	    return result;
+	logging.debug("Remove DHCP on switch program done")
+    
+    result = "true"
+    logging.debug("Setup_pvlan_dhcp completed with result:%s" % result)
+    return result
+
+@echo
+def setup_pvlan_vm(session, args):
+    op = args.pop("op")
+    nw_label = args.pop("nw-label")
+    primary = args.pop("primary-pvlan")
+    isolated = args.pop("isolated-pvlan")
+    vm_mac = args.pop("vm-mac")
+    trunk_port = 1
+
+    res = lib.check_switch()
+    if res != "SUCCESS":
+        return "FAILURE:%s" % res
+
+    bridge = lib.do_cmd([getBridgePath, nw_label])
+    logging.debug("Determine bridge/switch is :%s" % (bridge))
+
+    if op == "add":
+        logging.debug("About to setup vm on the switch:%s" % bridge)
+        res = lib.do_cmd([vmSetupPath, "-A", "-b", bridge, "-p", primary, "-i", isolated, "-v", vm_mac])
+	if res:
+	    result = "FAILURE:%s" % res
+	    return result;
+	logging.debug("Setup vm on switch program done")
+    elif op == "delete":
+        logging.debug("About to remove vm on the switch:%s" % bridge)
+        res = lib.do_cmd([vmSetupPath, "-D", "-b", bridge, "-p", primary, "-i", isolated, "-v", vm_mac])
+	if res:
+	    result = "FAILURE:%s" % res
+	    return result;
+	logging.debug("Remove vm on switch program done")
+
+    result = "true"
+    logging.debug("Setup_pvlan_vm_alone completed with result:%s" % result)
+    return result
+
+@echo
+def cleanup(session, args):
+    res = lib.check_switch()
+    if res != "SUCCESS":
+        return "FAILURE:%s" % res
+
+    res = lib.do_cmd([pvlanCleanUpPath])
+    if res:
+        result = "FAILURE:%s" % res
+        return result;
+
+    result = "true"
+    logging.debug("Setup_pvlan_vm_dhcp completed with result:%s" % result)
+    return result
+
+if __name__ == "__main__":
+    XenAPIPlugin.dispatch({"setup-pvlan-dhcp": setup_pvlan_dhcp,
+                           "setup-pvlan-vm": setup_pvlan_vm,
+                           "cleanup":cleanup})

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/cloud-plugin-ovstunnel
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/cloud-plugin-ovstunnel b/scripts/vm/hypervisor/xenserver/cloud-plugin-ovstunnel
new file mode 100644
index 0000000..9bdb630
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/cloud-plugin-ovstunnel
@@ -0,0 +1,261 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+
+# Creates a tunnel mesh across xenserver hosts
+# Enforces broadcast drop rules on ingress GRE tunnels
+
+import cloud_plugin-lib as lib
+import logging
+import os
+import sys
+import subprocess
+import time
+import XenAPIPlugin
+
+sys.path.append("/opt/xensource/sm/")
+import util
+
+from time import localtime as _localtime, asctime as _asctime
+
+xePath = "/opt/xensource/bin/xe"
+lib.setup_logging("/var/log/cloud/cloud-ovstunnel.log")
+
+
+def block_ipv6_v5(bridge):
+    lib.add_flow(bridge, priority=65000, dl_type='0x86dd', actions='drop')
+
+
+def block_ipv6_v6(bridge):
+    lib.add_flow(bridge, priority=65000, proto='ipv6', actions='drop')
+
+
+block_ipv6_handlers = {
+        '5': block_ipv6_v5,
+        '6': block_ipv6_v6}
+
+
+def echo(fn):
+    def wrapped(*v, **k):
+        name = fn.__name__
+        logging.debug("#### CLOUD enter  %s ####" % name)
+        res = fn(*v, **k)
+        logging.debug("#### CLOUD exit  %s ####" % name)
+        return res
+    return wrapped
+
+
+@echo
+def setup_ovs_bridge(session, args):
+    bridge = args.pop("bridge")
+    key = args.pop("key")
+    xs_nw_uuid = args.pop("xs_nw_uuid")
+    cs_host_id = args.pop("cs_host_id")
+
+    res = lib.check_switch()
+    if res != "SUCCESS":
+        return "FAILURE:%s" % res
+
+    logging.debug("About to manually create the bridge:%s" % bridge)
+    # create a bridge with the same name as the xapi network
+    # also associate gre key in other config attribute
+    res = lib.do_cmd([lib.VSCTL_PATH, "--", "--may-exist", "add-br", bridge,
+                                     "--", "set", "bridge", bridge,
+                                     "other_config:gre_key=%s" % key])
+    logging.debug("Bridge has been manually created:%s" % res)
+    # TODO: Make sure xs-network-uuid is set into external_ids
+    lib.do_cmd([lib.VSCTL_PATH, "set", "Bridge", bridge,
+                            "external_ids:xs-network-uuid=%s" % xs_nw_uuid])
+    # Non empty result means something went wrong
+    if res:
+        result = "FAILURE:%s" % res
+    else:
+        # Verify the bridge actually exists, with the gre_key properly set
+        res = lib.do_cmd([lib.VSCTL_PATH, "get", "bridge",
+                                          bridge, "other_config:gre_key"])
+        if key in res:
+            result = "SUCCESS:%s" % bridge
+        else:
+            result = "FAILURE:%s" % res
+        # Finally note in the xenapi network object that the network has
+        # been configured
+        xs_nw_uuid = lib.do_cmd([lib.XE_PATH, "network-list",
+                                "bridge=%s" % bridge, "--minimal"])
+        lib.do_cmd([lib.XE_PATH, "network-param-set", "uuid=%s" % xs_nw_uuid,
+                   "other-config:is-ovs-tun-network=True"])
+        conf_hosts = lib.do_cmd([lib.XE_PATH, "network-param-get",
+                                "uuid=%s" % xs_nw_uuid,
+                                "param-name=other-config",
+                                "param-key=ovs-host-setup", "--minimal"])
+        conf_hosts = cs_host_id + (conf_hosts and ',%s' % conf_hosts or '')
+        lib.do_cmd([lib.XE_PATH, "network-param-set", "uuid=%s" % xs_nw_uuid,
+                   "other-config:ovs-host-setup=%s" % conf_hosts])
+
+        # BLOCK IPv6 - Flow spec changes with ovs version
+        host_list_cmd = [lib.XE_PATH, 'host-list', '--minimal']
+        host_list_str = lib.do_cmd(host_list_cmd)
+        host_uuid = host_list_str.split(',')[0].strip()
+        version_cmd = [lib.XE_PATH, 'host-param-get', 'uuid=%s' % host_uuid,
+                                   'param-name=software-version',
+                                   'param-key=product_version']
+        version = lib.do_cmd(version_cmd).split('.')[0]
+        block_ipv6_handlers[version](bridge)
+    logging.debug("Setup_ovs_bridge completed with result:%s" % result)
+    return result
+
+
+@echo
+def destroy_ovs_bridge(session, args):
+    bridge = args.pop("bridge")
+    res = lib.check_switch()
+    if res != "SUCCESS":
+        return res
+    res = lib.do_cmd([lib.VSCTL_PATH, "del-br", bridge])
+    logging.debug("Bridge has been manually removed:%s" % res)
+    if res:
+        result = "FAILURE:%s" % res
+    else:
+        # Note that the bridge has been removed on xapi network object
+        xs_nw_uuid = lib.do_cmd([xePath, "network-list",
+                                "bridge=%s" % bridge, "--minimal"])
+        #FIXME: WOW, this an error
+        #lib.do_cmd([xePath,"network-param-set", "uuid=%s" % xs_nw_uuid,
+        #                  "other-config:ovs-setup=False"])
+        result = "SUCCESS:%s" % bridge
+
+    logging.debug("Destroy_ovs_bridge completed with result:%s" % result)
+    return result
+
+
+@echo
+def create_tunnel(session, args):
+    bridge = args.pop("bridge")
+    remote_ip = args.pop("remote_ip")
+    gre_key = args.pop("key")
+    src_host = args.pop("from")
+    dst_host = args.pop("to")
+
+    logging.debug("Entering create_tunnel")
+
+    res = lib.check_switch()
+    if res != "SUCCESS":
+        logging.debug("Openvswitch running: NO")
+        return "FAILURE:%s" % res
+
+    # We need to keep the name below 14 characters
+    # src and target are enough - consider a fixed length hash
+    name = "t%s-%s-%s" % (gre_key, src_host, dst_host)
+
+    # Verify the xapi bridge to be created
+    # NOTE: Timeout should not be necessary anymore
+    wait = [lib.VSCTL_PATH, "--timeout=30", "wait-until", "bridge",
+                    bridge, "--", "get", "bridge", bridge, "name"]
+    res = lib.do_cmd(wait)
+    if bridge not in res:
+        logging.debug("WARNING:Can't find bridge %s for creating " +
+                                  "tunnel!" % bridge)
+        return "FAILURE:NO_BRIDGE"
+    logging.debug("bridge %s for creating tunnel - VERIFIED" % bridge)
+    tunnel_setup = False
+    drop_flow_setup = False
+    try:
+        # Create a port and configure the tunnel interface for it
+        add_tunnel = [lib.VSCTL_PATH, "add-port", bridge,
+                                  name, "--", "set", "interface",
+                                  name, "type=gre", "options:key=%s" % gre_key,
+                                  "options:remote_ip=%s" % remote_ip]
+        lib.do_cmd(add_tunnel)
+        tunnel_setup = True
+        # verify port
+        verify_port = [lib.VSCTL_PATH, "get", "port", name, "interfaces"]
+        res = lib.do_cmd(verify_port)
+        # Expecting python-style list as output
+        iface_list = []
+        if len(res) > 2:
+            iface_list = res.strip()[1:-1].split(',')
+        if len(iface_list) != 1:
+            logging.debug("WARNING: Unexpected output while verifying " +
+                                      "port %s on bridge %s" % (name, bridge))
+            return "FAILURE:VERIFY_PORT_FAILED"
+
+        # verify interface
+        iface_uuid = iface_list[0]
+        verify_interface_key = [lib.VSCTL_PATH, "get", "interface",
+                                iface_uuid, "options:key"]
+        verify_interface_ip = [lib.VSCTL_PATH, "get", "interface",
+                               iface_uuid, "options:remote_ip"]
+
+        key_validation = lib.do_cmd(verify_interface_key)
+        ip_validation = lib.do_cmd(verify_interface_ip)
+
+        if not gre_key in key_validation or not remote_ip in ip_validation:
+            logging.debug("WARNING: Unexpected output while verifying " +
+                          "interface %s on bridge %s" % (name, bridge))
+            return "FAILURE:VERIFY_INTERFACE_FAILED"
+        logging.debug("Tunnel interface validated:%s" % verify_interface_ip)
+        cmd_tun_ofport = [lib.VSCTL_PATH, "get", "interface",
+                                          iface_uuid, "ofport"]
+        tun_ofport = lib.do_cmd(cmd_tun_ofport)
+        # Ensure no trailing LF
+        if tun_ofport.endswith('\n'):
+            tun_ofport = tun_ofport[:-1]
+        # add flow entryies for dropping broadcast coming in from gre tunnel
+        lib.add_flow(bridge, priority=1000, in_port=tun_ofport,
+                         dl_dst='ff:ff:ff:ff:ff:ff', actions='drop')
+        lib.add_flow(bridge, priority=1000, in_port=tun_ofport,
+                     nw_dst='224.0.0.0/24', actions='drop')
+        drop_flow_setup = True
+        logging.debug("Broadcast drop rules added")
+        return "SUCCESS:%s" % name
+    except:
+        logging.debug("An unexpected error occured. Rolling back")
+        if tunnel_setup:
+            logging.debug("Deleting GRE interface")
+            # Destroy GRE port and interface
+            lib.del_port(bridge, name)
+        if drop_flow_setup:
+            # Delete flows
+            logging.debug("Deleting flow entries from GRE interface")
+            lib.del_flows(bridge, in_port=tun_ofport)
+        # This will not cancel the original exception
+        raise
+
+
+@echo
+def destroy_tunnel(session, args):
+    bridge = args.pop("bridge")
+    iface_name = args.pop("in_port")
+    logging.debug("Destroying tunnel at port %s for bridge %s"
+                            % (iface_name, bridge))
+    ofport = get_field_of_interface(iface_name, "ofport")
+    lib.del_flows(bridge, in_port=ofport)
+    lib.del_port(bridge, iface_name)
+    return "SUCCESS"
+
+
+def get_field_of_interface(iface_name, field):
+    get_iface_cmd = [lib.VSCTL_PATH, "get", "interface", iface_name, field]
+    res = lib.do_cmd(get_iface_cmd)
+    return res
+
+
+if __name__ == "__main__":
+    XenAPIPlugin.dispatch({"create_tunnel": create_tunnel,
+                           "destroy_tunnel": destroy_tunnel,
+                           "setup_ovs_bridge": setup_ovs_bridge,
+                           "destroy_ovs_bridge": destroy_ovs_bridge})


[2/8] Merged vmops and vmopspremium. Rename all xapi plugins to start with cloud-plugin-. Rename vmops to cloud-plugin-generic.

Posted by ah...@apache.org.
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xcposs/cloud-plugin-snapshot
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xcposs/cloud-plugin-snapshot b/scripts/vm/hypervisor/xenserver/xcposs/cloud-plugin-snapshot
new file mode 100644
index 0000000..d13bc40
--- /dev/null
+++ b/scripts/vm/hypervisor/xenserver/xcposs/cloud-plugin-snapshot
@@ -0,0 +1,601 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# Version @VERSION@
+#
+# A plugin for executing script needed by CloudStack cloud 
+
+import os, sys, time
+import XenAPIPlugin
+sys.path.append("/usr/lib/xcp/sm/")
+import SR, VDI, SRCommand, util, lvutil
+from util import CommandException
+import vhdutil
+import shutil
+import lvhdutil
+import errno
+import subprocess
+import xs_errors
+import cleanup
+import stat
+import random
+
+VHD_UTIL = 'vhd-util'
+VHD_PREFIX = 'VHD-'
+CLOUD_DIR = '/run/cloud_mount'
+
+def echo(fn):
+    def wrapped(*v, **k):
+        name = fn.__name__
+        util.SMlog("#### CLOUD enter  %s ####" % name )
+        res = fn(*v, **k)
+        util.SMlog("#### CLOUD exit  %s ####" % name )
+        return res
+    return wrapped
+
+
+@echo
+def create_secondary_storage_folder(session, args):
+    local_mount_path = None
+
+    util.SMlog("create_secondary_storage_folder, args: " + str(args))
+
+    try:
+        try:
+            # Mount the remote resource folder locally
+            remote_mount_path = args["remoteMountPath"]
+            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
+            mount(remote_mount_path, local_mount_path)
+
+            # Create the new folder
+            new_folder = local_mount_path + "/" + args["newFolder"]
+            if not os.path.isdir(new_folder):
+                current_umask = os.umask(0)
+                os.makedirs(new_folder)
+                os.umask(current_umask)
+        except OSError, (errno, strerror):
+            errMsg = "create_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
+            util.SMlog(errMsg)
+            raise xs_errors.XenError(errMsg)
+        except:
+            errMsg = "create_secondary_storage_folder failed."
+            util.SMlog(errMsg)
+            raise xs_errors.XenError(errMsg)
+    finally:
+        if local_mount_path != None:
+            # Unmount the local folder
+            umount(local_mount_path)
+            # Remove the local folder
+            os.system("rmdir " + local_mount_path)
+        
+    return "1"
+
+@echo
+def delete_secondary_storage_folder(session, args):
+    local_mount_path = None
+
+    util.SMlog("delete_secondary_storage_folder, args: " + str(args))
+
+    try:
+        try:
+            # Mount the remote resource folder locally
+            remote_mount_path = args["remoteMountPath"]
+            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
+            mount(remote_mount_path, local_mount_path)
+
+            # Delete the specified folder
+            folder = local_mount_path + "/" + args["folder"]
+            if os.path.isdir(folder):
+                os.system("rm -f " + folder + "/*")
+                os.system("rmdir " + folder)
+        except OSError, (errno, strerror):
+            errMsg = "delete_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
+            util.SMlog(errMsg)
+            raise xs_errors.XenError(errMsg)
+        except:
+            errMsg = "delete_secondary_storage_folder failed."
+            util.SMlog(errMsg)
+            raise xs_errors.XenError(errMsg)
+    finally:
+        if local_mount_path != None:
+            # Unmount the local folder
+            umount(local_mount_path)
+            # Remove the local folder
+            os.system("rmdir " + local_mount_path)
+        
+    return "1"
+     
+@echo
+def post_create_private_template(session, args):
+    local_mount_path = None
+    try:
+        try:
+            # get local template folder 
+            templatePath = args["templatePath"]
+            local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
+            mount(templatePath, local_mount_path)
+            # Retrieve args
+            filename = args["templateFilename"]
+            name = args["templateName"]
+            description = args["templateDescription"]
+            checksum = args["checksum"]
+            file_size = args["size"]
+            virtual_size = args["virtualSize"]
+            template_id = args["templateId"]
+           
+            # Create the template.properties file
+            template_properties_install_path = local_mount_path + "/template.properties"
+            f = open(template_properties_install_path, "w")
+            f.write("filename=" + filename + "\n")
+            f.write("vhd=true\n")
+            f.write("id=" + template_id + "\n")
+            f.write("vhd.filename=" + filename + "\n")
+            f.write("public=false\n")
+            f.write("uniquename=" + name + "\n")
+            f.write("vhd.virtualsize=" + virtual_size + "\n")
+            f.write("virtualsize=" + virtual_size + "\n")
+            f.write("checksum=" + checksum + "\n")
+            f.write("hvm=true\n")
+            f.write("description=" + description + "\n")
+            f.write("vhd.size=" + str(file_size) + "\n")
+            f.write("size=" + str(file_size) + "\n")
+            f.close()
+            util.SMlog("Created template.properties file")
+           
+            # Set permissions
+            permissions = stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH
+            os.chmod(template_properties_install_path, permissions)
+            util.SMlog("Set permissions on template and template.properties")
+
+        except:
+            errMsg = "post_create_private_template failed."
+            util.SMlog(errMsg)
+            raise xs_errors.XenError(errMsg)
+
+    finally:
+        if local_mount_path != None:
+            # Unmount the local folder
+            umount(local_mount_path)
+            # Remove the local folder
+            os.system("rmdir " + local_mount_path)
+    return "1" 
+  
+def isfile(path, isISCSI):
+    errMsg = ''
+    exists = True
+    if isISCSI:
+        exists = checkVolumeAvailablility(path)
+    else:
+        exists = os.path.isfile(path)
+        
+    if not exists:
+        errMsg = "File " + path + " does not exist."
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return errMsg
+
+def copyfile(fromFile, toFile, isISCSI):
+    util.SMlog("Starting to copy " + fromFile + " to " + toFile)
+    errMsg = ''
+    try:
+        cmd = ['dd', 'if=' + fromFile, 'of=' + toFile, 'bs=4M']
+        txt = util.pread2(cmd)
+    except:
+        try:
+            os.system("rm -f " + toFile)
+        except:
+            txt = ''
+        txt = ''
+        errMsg = "Error while copying " + fromFile + " to " + toFile + " in secondary storage"
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+
+    util.SMlog("Successfully copied " + fromFile + " to " + toFile)
+    return errMsg
+
+def chdir(path):
+    try:
+        os.chdir(path)
+    except OSError, (errno, strerror):
+        errMsg = "Unable to chdir to " + path + " because of OSError with errno: " + str(errno) + " and strerr: " + strerror
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    util.SMlog("Chdired to " + path)
+    return
+
+def scanParent(path):
+    # Do a scan for the parent for ISCSI volumes
+    # Note that the parent need not be visible on the XenServer
+    parentUUID = ''
+    try:
+        lvName = os.path.basename(path)
+        dirname = os.path.dirname(path)
+        vgName = os.path.basename(dirname) 
+        vhdInfo = vhdutil.getVHDInfoLVM(lvName, lvhdutil.extractUuid, vgName)
+        parentUUID = vhdInfo.parentUuid
+    except:
+        errMsg = "Could not get vhd parent of " + path
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return parentUUID
+
+def getParent(path, isISCSI):
+    parentUUID = ''
+    try :
+        if isISCSI:
+            parentUUID = vhdutil.getParent(path, lvhdutil.extractUuid)
+        else:
+            parentUUID = vhdutil.getParent(path, cleanup.FileVDI.extractUuid)
+    except:
+        errMsg = "Could not get vhd parent of " + path
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return parentUUID
+
+def getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI):
+    snapshotVHD    = getVHD(snapshotUuid, isISCSI)
+    snapshotPath   = os.path.join(primarySRPath, snapshotVHD)
+
+    baseCopyUuid = ''
+    if isISCSI:
+        checkVolumeAvailablility(snapshotPath)
+        baseCopyUuid = scanParent(snapshotPath)
+    else:
+        baseCopyUuid = getParent(snapshotPath, isISCSI)
+    
+    util.SMlog("Base copy of snapshotUuid: " + snapshotUuid + " is " + baseCopyUuid)
+    return baseCopyUuid
+
+def setParent(parent, child):
+    try:
+        cmd = [VHD_UTIL, "modify", "-p", parent, "-n", child]
+        txt = util.pread2(cmd)
+    except:
+        errMsg = "Unexpected error while trying to set parent of " + child + " to " + parent 
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    util.SMlog("Successfully set parent of " + child + " to " + parent)
+    return
+
+def rename(originalVHD, newVHD):
+    try:
+        os.rename(originalVHD, newVHD)
+    except OSError, (errno, strerror):
+        errMsg = "OSError while renaming " + origiinalVHD + " to " + newVHD + "with errno: " + str(errno) + " and strerr: " + strerror
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return
+
+def makedirs(path):
+    if not os.path.isdir(path):
+        try:
+            os.makedirs(path)
+        except OSError, (errno, strerror):
+            umount(path)
+            if os.path.isdir(path):
+                return
+            errMsg = "OSError while creating " + path + " with errno: " + str(errno) + " and strerr: " + strerror
+            util.SMlog(errMsg)
+            raise xs_errors.XenError(errMsg)
+    return
+
+def mount(remoteDir, localDir):
+    makedirs(localDir)
+    options = "soft,tcp,timeo=133,retrans=1"
+    try: 
+        cmd = ['mount', '-o', options, remoteDir, localDir]
+        txt = util.pread2(cmd)
+    except:
+        txt = ''
+        errMsg = "Unexpected error while trying to mount " + remoteDir + " to " + localDir 
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    util.SMlog("Successfully mounted " + remoteDir + " to " + localDir)
+
+    return
+
+def umount(localDir):
+    try: 
+        cmd = ['umount', localDir]
+        util.pread2(cmd)
+    except CommandException:
+        errMsg = "CommandException raised while trying to umount " + localDir 
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+
+    util.SMlog("Successfully unmounted " + localDir)
+    return
+
+def mountSnapshotsDir(secondaryStorageMountPath, localMountPointPath, path):
+    # The aim is to mount secondaryStorageMountPath on 
+    # And create <accountId>/<instanceId> dir on it, if it doesn't exist already.
+    # Assuming that secondaryStorageMountPath  exists remotely
+
+    # Just mount secondaryStorageMountPath/<relativeDir>/SecondaryStorageHost/ everytime
+    # Never unmount.
+    # path is like "snapshots/account/volumeId", we mount secondary_storage:/snapshots
+    relativeDir = path.split("/")[0]
+    restDir = "/".join(path.split("/")[1:])
+    snapshotsDir = os.path.join(secondaryStorageMountPath, relativeDir)
+
+    makedirs(localMountPointPath)
+    # if something is not mounted already on localMountPointPath,
+    # mount secondaryStorageMountPath on localMountPath
+    if os.path.ismount(localMountPointPath):
+        # There is more than one secondary storage per zone.
+        # And we are mounting each sec storage under a zone-specific directory
+        # So two secondary storage snapshot dirs will never get mounted on the same point on the same XenServer.
+        util.SMlog("The remote snapshots directory has already been mounted on " + localMountPointPath)
+    else:
+        mount(snapshotsDir, localMountPointPath)
+
+    # Create accountId/instanceId dir on localMountPointPath, if it doesn't exist
+    backupsDir = os.path.join(localMountPointPath, restDir)
+    makedirs(backupsDir)
+    return backupsDir
+
+def unmountAll(path):
+    try:
+        for dir in os.listdir(path):
+            if dir.isdigit():
+                util.SMlog("Unmounting Sub-Directory: " + dir)
+                localMountPointPath = os.path.join(path, dir)
+                umount(localMountPointPath)
+    except:
+        util.SMlog("Ignoring the error while trying to unmount the snapshots dir")
+
+@echo
+def unmountSnapshotsDir(session, args):
+    dcId = args['dcId']
+    localMountPointPath = os.path.join(CLOUD_DIR, dcId)
+    localMountPointPath = os.path.join(localMountPointPath, "snapshots")
+    unmountAll(localMountPointPath)
+    try:
+        umount(localMountPointPath)
+    except:
+        util.SMlog("Ignoring the error while trying to unmount the snapshots dir.")
+
+    return "1"
+
+def getPrimarySRPath(session, primaryStorageSRUuid, isISCSI):
+    sr = session.xenapi.SR.get_by_uuid(primaryStorageSRUuid)
+    srrec = session.xenapi.SR.get_record(sr)
+    srtype = srrec["type"]
+    if srtype == "file":
+        pbd = session.xenapi.SR.get_PBDs(sr)[0]
+        pbdrec = session.xenapi.PBD.get_record(pbd)
+        primarySRPath = pbdrec["device_config"]["location"]
+        return primarySRPath
+    elif isISCSI:
+        primarySRDir = lvhdutil.VG_PREFIX + primaryStorageSRUuid
+        return os.path.join(lvhdutil.VG_LOCATION, primarySRDir)
+    else:
+        return os.path.join(SR.MOUNT_BASE, primaryStorageSRUuid)
+
+def getBackupVHD(UUID):
+    return UUID + '.' + SR.DEFAULT_TAP
+
+def getVHD(UUID, isISCSI):
+    if isISCSI:
+        return VHD_PREFIX + UUID
+    else:
+        return UUID + '.' + SR.DEFAULT_TAP
+
+def getIsTrueString(stringValue):
+    booleanValue = False
+    if (stringValue and stringValue == 'true'):
+        booleanValue = True
+    return booleanValue 
+
+def makeUnavailable(uuid, primarySRPath, isISCSI):
+    if not isISCSI:
+        return
+    VHD = getVHD(uuid, isISCSI)
+    path = os.path.join(primarySRPath, VHD)
+    manageAvailability(path, '-an')
+    return
+
+def manageAvailability(path, value):
+    if path.__contains__("/var/run/sr-mount"):
+        return
+    util.SMlog("Setting availability of " + path + " to " + value)
+    try:
+        cmd = ['/usr/sbin/lvchange', value, path]
+        util.pread2(cmd)
+    except: #CommandException, (rc, cmdListStr, stderr):
+        #errMsg = "CommandException thrown while executing: " + cmdListStr + " with return code: " + str(rc) + " and stderr: " + stderr
+        errMsg = "Unexpected exception thrown by lvchange"
+        util.SMlog(errMsg)
+        if value == "-ay":
+            # Raise an error only if we are trying to make it available.
+            # Just warn if we are trying to make it unavailable after the 
+            # snapshot operation is done.
+            raise xs_errors.XenError(errMsg)
+    return
+
+
+def checkVolumeAvailablility(path):
+    try:
+        if not isVolumeAvailable(path):
+            # The VHD file is not available on XenSever. The volume is probably
+            # inactive or detached.
+            # Do lvchange -ay to make it available on XenServer
+            manageAvailability(path, '-ay')
+    except:
+        errMsg = "Could not determine status of ISCSI path: " + path
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    
+    success = False
+    i = 0
+    while i < 6:
+        i = i + 1
+        # Check if the vhd is actually visible by checking for the link
+        # set isISCSI to true
+        success = isVolumeAvailable(path)
+        if success:
+            util.SMlog("Made vhd: " + path + " available and confirmed that it is visible")
+            break
+
+        # Sleep for 10 seconds before checking again.
+        time.sleep(10)
+
+    # If not visible within 1 min fail
+    if not success:
+        util.SMlog("Could not make vhd: " +  path + " available despite waiting for 1 minute. Does it exist?")
+
+    return success
+
+def isVolumeAvailable(path):
+    # Check if iscsi volume is available on this XenServer.
+    status = "0"
+    try:
+        p = subprocess.Popen(["/bin/bash", "-c", "if [ -L " + path + " ]; then echo 1; else echo 0;fi"], stdout=subprocess.PIPE)
+        status = p.communicate()[0].strip("\n")
+    except:
+        errMsg = "Could not determine status of ISCSI path: " + path
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+
+    return (status == "1")  
+
+def getVhdParent(session, args):
+    util.SMlog("getParent with " + str(args))
+    primaryStorageSRUuid      = args['primaryStorageSRUuid']
+    snapshotUuid              = args['snapshotUuid']
+    isISCSI                   = getIsTrueString(args['isISCSI']) 
+
+    primarySRPath = getPrimarySRPath(session, primaryStorageSRUuid, isISCSI)
+    util.SMlog("primarySRPath: " + primarySRPath)
+
+    baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
+
+    return  baseCopyUuid
+
+
+def backupSnapshot(session, args):
+    util.SMlog("Called backupSnapshot with " + str(args))
+    primaryStorageSRUuid      = args['primaryStorageSRUuid']
+    secondaryStorageMountPath = args['secondaryStorageMountPath']
+    snapshotUuid              = args['snapshotUuid']
+    prevBackupUuid            = args['prevBackupUuid']
+    backupUuid                = args['backupUuid']
+    isISCSI                   = getIsTrueString(args['isISCSI'])
+    path = args['path']
+    localMountPoint = args['localMountPoint']
+    primarySRPath = getPrimarySRPath(session, primaryStorageSRUuid, isISCSI)
+    util.SMlog("primarySRPath: " + primarySRPath)
+
+    baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
+    baseCopyVHD  = getVHD(baseCopyUuid, isISCSI)
+    baseCopyPath = os.path.join(primarySRPath, baseCopyVHD)
+    util.SMlog("Base copy path: " + baseCopyPath)
+
+
+    # Mount secondary storage mount path on XenServer along the path
+    # /var/run/sr-mount/<dcId>/snapshots/ and create <accountId>/<volumeId> dir
+    # on it.
+    backupsDir = mountSnapshotsDir(secondaryStorageMountPath, localMountPoint, path)
+    util.SMlog("Backups dir " + backupsDir)
+    prevBackupUuid = prevBackupUuid.split("/")[-1]
+    # Check existence of snapshot on primary storage
+    isfile(baseCopyPath, isISCSI)
+    if prevBackupUuid:
+        # Check existence of prevBackupFile
+        prevBackupVHD = getBackupVHD(prevBackupUuid)
+        prevBackupFile = os.path.join(backupsDir, prevBackupVHD)
+        isfile(prevBackupFile, False)
+
+    # copy baseCopyPath to backupsDir with new uuid
+    backupVHD = getBackupVHD(backupUuid)  
+    backupFile = os.path.join(backupsDir, backupVHD)
+    util.SMlog("Back up " + baseCopyUuid + " to Secondary Storage as " + backupUuid)
+    copyfile(baseCopyPath, backupFile, isISCSI)
+    vhdutil.setHidden(backupFile, False)
+
+    # Because the primary storage is always scanned, the parent of this base copy is always the first base copy.
+    # We don't want that, we want a chain of VHDs each of which is a delta from the previous.
+    # So set the parent of the current baseCopyVHD to prevBackupVHD 
+    if prevBackupUuid:
+        # If there was a previous snapshot
+        setParent(prevBackupFile, backupFile)
+
+    txt = "1#" + backupUuid
+    return txt
+
+@echo
+def deleteSnapshotBackup(session, args):
+    util.SMlog("Calling deleteSnapshotBackup with " + str(args))
+    secondaryStorageMountPath = args['secondaryStorageMountPath']
+    backupUUID                = args['backupUUID']
+    path = args['path']
+    localMountPoint = args['localMountPoint']
+
+    backupsDir = mountSnapshotsDir(secondaryStorageMountPath, localMountPoint, path)
+    # chdir to the backupsDir for convenience
+    chdir(backupsDir)
+
+    backupVHD = getBackupVHD(backupUUID)
+    util.SMlog("checking existence of " + backupVHD)
+
+    # The backupVHD is on secondary which is NFS and not ISCSI.
+    if not os.path.isfile(backupVHD):
+        util.SMlog("backupVHD " + backupVHD + "does not exist. Not trying to delete it")
+        return "1"
+    util.SMlog("backupVHD " + backupVHD + " exists.")
+        
+    # Just delete the backupVHD
+    try:
+        os.remove(backupVHD)
+    except OSError, (errno, strerror):
+        errMsg = "OSError while removing " + backupVHD + " with errno: " + str(errno) + " and strerr: " + strerror
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+
+    return "1"
+   
+@echo
+def revert_memory_snapshot(session, args):
+    util.SMlog("Calling revert_memory_snapshot with " + str(args))
+    vmName = args['vmName']
+    snapshotUUID = args['snapshotUUID']
+    oldVmUuid = args['oldVmUuid']
+    snapshotMemory = args['snapshotMemory']
+    hostUUID = args['hostUUID']
+    try:
+        cmd = '''xe vbd-list vm-uuid=%s | grep 'vdi-uuid' | grep -v 'not in database' | sed -e 's/vdi-uuid ( RO)://g' ''' % oldVmUuid
+        vdiUuids = os.popen(cmd).read().split()
+        cmd2 = '''xe vm-param-get param-name=power-state uuid=''' + oldVmUuid
+        if os.popen(cmd2).read().split()[0] != 'halted':
+            os.system("xe vm-shutdown force=true vm=" + vmName)
+        os.system("xe vm-destroy uuid=" + oldVmUuid)
+        os.system("xe snapshot-revert snapshot-uuid=" + snapshotUUID)
+        if snapshotMemory == 'true':
+            os.system("xe vm-resume vm=" + vmName + " on=" + hostUUID)
+        for vdiUuid in vdiUuids:
+            os.system("xe vdi-destroy uuid=" + vdiUuid)
+    except OSError, (errno, strerror):
+        errMsg = "OSError while reverting vm " + vmName + " to snapshot " + snapshotUUID + " with errno: " + str(errno) + " and strerr: " + strerror
+        util.SMlog(errMsg)
+        raise xs_errors.XenError(errMsg)
+    return "0"
+
+if __name__ == "__main__":
+    XenAPIPlugin.dispatch({"getVhdParent":getVhdParent,  "create_secondary_storage_folder":create_secondary_storage_folder, "delete_secondary_storage_folder":delete_secondary_storage_folder, "post_create_private_template":post_create_private_template, "backupSnapshot": backupSnapshot, "deleteSnapshotBackup": deleteSnapshotBackup, "unmountSnapshotsDir": unmountSnapshotsDir, "revert_memory_snapshot":revert_memory_snapshot})
+    
+

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xcposs/patch
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xcposs/patch b/scripts/vm/hypervisor/xenserver/xcposs/patch
index 6e1002e..6de96d4 100644
--- a/scripts/vm/hypervisor/xenserver/xcposs/patch
+++ b/scripts/vm/hypervisor/xenserver/xcposs/patch
@@ -26,43 +26,48 @@
 # If [source path] starts with '/', then it is absolute path.
 # If [source path] starts with '~', then it is path relative to management server home directory.
 # If [source path] does not start with '/' or '~', then it is relative path to the location of the patch file. 
+# 
+# The following specifies the paths to deposit files
+# /usr/lib/xcp/plugins - All XAPI plugins.  Every file placed here should start with "cloud-".
+# /etc/cloud - Configuration files for scripts.
+# /usr/lib/cloud/bin - All scripts used in the normal operation.
+# /usr/lib/cloud/tools/bin - All scripts used for testing/support that are meant to be run by hand.
+# /etc/logrotate.d - All cloud log rotation configuration files.  Every file placed here must start with "cloud-".
 NFSSR.py=/usr/lib/xcp/sm
-vmops=.,0755,/usr/lib/xcp/plugins
-ovsgre=..,0755,/usr/lib/xcp/plugins
-ovstunnel=..,0755,/usr/lib/xcp/plugins
-vmopsSnapshot=.,0755,/usr/lib/xcp/plugins
-hostvmstats.py=..,0755,/usr/lib/xcp/sm
+cloud-plugin-generic=.,0755,/usr/lib/xcp/plugins
+cloud-plugin-ovsgre=..,0755,/usr/lib/xcp/plugins
+cloud-plugin-ovstunnel=..,0755,/usr/lib/xcp/plugins
+cloud-plugin-snapshot=.,0755,/usr/lib/xcp/plugins
 systemvm.iso=../../../../../vms,0644,/usr/share/xcp/packages/iso/
 id_rsa.cloud=../../../systemvm,0600,/root/.ssh
-network_info.sh=..,0755,/usr/lib/xcp/bin
-setupxenserver.sh=..,0755,/usr/lib/xcp/bin
-make_migratable.sh=..,0755,/usr/lib/xcp/bin
-setup_iscsi.sh=..,0755,/usr/lib/xcp/bin
-pingtest.sh=../../..,0755,/usr/lib/xcp/bin
-dhcp_entry.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-ipassoc.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-save_password_to_domr.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-networkUsage.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-call_firewall.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-call_loadbalancer.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-l2tp_vpn.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-cloud-setup-bonding.sh=..,0755,/usr/lib/xcp/bin
-copy_vhd_to_secondarystorage.sh=.,0755,/usr/lib/xcp/bin
-copy_vhd_from_secondarystorage.sh=.,0755,/usr/lib/xcp/bin
-setup_heartbeat_sr.sh=..,0755,/usr/lib/xcp/bin
-setup_heartbeat_file.sh=..,0755,/usr/lib/xcp/bin
-check_heartbeat.sh=..,0755,/usr/lib/xcp/bin
-xenheartbeat.sh=..,0755,/usr/lib/xcp/bin
-launch_hb.sh=..,0755,/usr/lib/xcp/bin
-vhd-util=..,0755,/usr/lib/xcp/bin
-vmopspremium=.,0755,/usr/lib/xcp/plugins
-create_privatetemplate_from_snapshot.sh=.,0755,/usr/lib/xcp/bin
-upgrade_snapshot.sh=..,0755,/usr/lib/xcp/bin
-cloud-clean-vlan.sh=..,0755,/usr/lib/xcp/bin
-cloud-prepare-upgrade.sh=..,0755,/usr/lib/xcp/bin
-getRouterStatus.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-bumpUpPriority.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-getDomRVersion.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-router_proxy.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
-createipAlias.sh=..,0755,/usr/lib/xcp/bin
-deleteipAlias.sh=..,0755,/usr/lib/xcp/bin
+network_info.sh=..,0755,/usr/lib/cloud/bin
+setupxenserver.sh=..,0755,/usr/lib/cloud/bin
+make_migratable.sh=..,0755,/usr/lib/cloud/bin
+setup_iscsi.sh=..,0755,/usr/lib/cloud/bin
+pingtest.sh=../../..,0755,/usr/lib/cloud/bin
+dhcp_entry.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+ipassoc.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+save_password_to_domr.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+networkUsage.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+call_firewall.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+call_loadbalancer.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+l2tp_vpn.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+cloud-setup-bonding.sh=..,0755,/usr/lib/cloud/bin
+copy_vhd_to_secondarystorage.sh=.,0755,/usr/lib/cloud/bin
+copy_vhd_from_secondarystorage.sh=.,0755,/usr/lib/cloud/bin
+setup_heartbeat_sr.sh=..,0755,/usr/lib/cloud/bin
+setup_heartbeat_file.sh=..,0755,/usr/lib/cloud/bin
+check_heartbeat.sh=..,0755,/usr/lib/cloud/bin
+xenheartbeat.sh=..,0755,/usr/lib/cloud/bin
+launch_hb.sh=..,0755,/usr/lib/cloud/bin
+vhd-util=..,0755,/usr/lib/cloud/bin
+create_privatetemplate_from_snapshot.sh=.,0755,/usr/lib/cloud/bin
+upgrade_snapshot.sh=..,0755,/usr/lib/cloud/bin
+cloud-clean-vlan.sh=..,0755,/usr/lib/cloud/bin
+cloud-prepare-upgrade.sh=..,0755,/usr/lib/cloud/bin
+getRouterStatus.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+bumpUpPriority.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+getDomRVersion.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+router_proxy.sh=../../../../network/domr/,0755,/usr/lib/cloud/bin
+createipAlias.sh=..,0755,/usr/lib/cloud/bin
+deleteipAlias.sh=..,0755,/usr/lib/cloud/bin

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f2bb1ace/scripts/vm/hypervisor/xenserver/xcposs/vmops
----------------------------------------------------------------------
diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmops b/scripts/vm/hypervisor/xenserver/xcposs/vmops
deleted file mode 100644
index 2f6441a..0000000
--- a/scripts/vm/hypervisor/xenserver/xcposs/vmops
+++ /dev/null
@@ -1,1480 +0,0 @@
-#!/usr/bin/python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# Version @VERSION@
-#
-# A plugin for executing script needed by vmops cloud 
-
-import os, sys, time
-import XenAPIPlugin
-sys.path.extend(["/usr/lib/xcp/sm/", "/usr/local/sbin/", "/sbin/"])
-import base64
-import socket
-import stat
-import tempfile
-import util
-import subprocess
-import zlib
-from util import CommandException
-
-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
-
-@echo
-def setup_iscsi(session, args):
-   uuid=args['uuid']
-   try:
-       cmd = ["bash", "/usr/lib/xcp/bin/setup_iscsi.sh", uuid]
-       txt = util.pread2(cmd)
-   except:
-       txt = ''
-   return '> DONE <'
- 
-
-@echo
-def getgateway(session, args):
-    mgmt_ip = args['mgmtIP']
-    try:
-        cmd = ["bash", "/usr/lib/xcp/bin/network_info.sh", "-g", mgmt_ip]
-        txt = util.pread2(cmd)
-    except:
-        txt = ''
-
-    return txt
-    
-@echo
-def preparemigration(session, args):
-    uuid = args['uuid']
-    try:
-        cmd = ["/usr/lib/xcp/bin/make_migratable.sh", uuid]
-        util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog("Catch prepare migration exception" )
-        txt = ''
-
-    return txt
-
-@echo
-def setIptables(session, args):
-    try:
-        '''cmd = ["/bin/bash", "/usr/lib/xcp/bin/setupxenserver.sh"]
-        txt = util.pread2(cmd)'''
-        txt = 'success'
-    except:
-        util.SMlog("  setIptables execution failed "  )
-        txt = '' 
-
-    return txt
- 
-@echo
-def pingdomr(session, args):
-    host = args['host']
-    port = args['port']
-    socket.setdefaulttimeout(3)
-    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    try:
-        s.connect((host,int(port)))
-        txt = 'success'
-    except:
-        txt = ''
-    
-    s.close()
-
-    return txt
-
-@echo
-def kill_copy_process(session, args):
-    namelabel = args['namelabel']
-    try:
-        cmd = ["bash", "/usr/lib/xcp/bin/kill_copy_process.sh", namelabel]
-        txt = util.pread2(cmd)
-    except:
-        txt = 'false'
-    return txt
-
-@echo
-def pingxenserver(session, args):
-    txt = 'success'
-    return txt
-
-@echo
-def ipassoc(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/ipassoc.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog("  ip associate failed "  )
-        txt = '' 
-
-    return txt
-
-def pingtest(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/pingtest.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog("  pingtest failed "  )
-        txt = ''
-
-    return txt
-
-@echo
-def savePassword(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/save_password_to_domr.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog("  save password to domr failed "  )
-        txt = '' 
-
-    return txt
-
-@echo
-def saveDhcpEntry(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/dhcp_entry.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog(" save dhcp entry failed "  )
-        txt = '' 
-
-    return txt
-    
-@echo
-def lt2p_vpn(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/l2tp_vpn.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog("l2tp vpn failed "  )
-        txt = '' 
-
-    return txt    
-
-@echo
-def setLinkLocalIP(session, args):
-    brName = args['brName']
-    try:
-        cmd = ["ip", "route", "del", "169.254.0.0/16"]
-        txt = util.pread2(cmd)
-    except:
-        txt = '' 
-    try:
-        cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
-        txt = util.pread2(cmd)
-    except:
-
-        try:
-            cmd = ["brctl", "addbr", brName]
-            txt = util.pread2(cmd)
-        except:
-            pass
- 
-        try:
-            cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
-            txt = util.pread2(cmd)
-        except:
-            pass
-    try:
-        cmd = ["ip", "route", "add", "169.254.0.0/16", "dev", brName, "src", "169.254.0.1"]
-        txt = util.pread2(cmd)
-    except:
-        txt = '' 
-    txt = 'success'
-    return txt
-    
-@echo
-def setFirewallRule(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/call_firewall.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog(" set firewall rule failed "  )
-        txt = '' 
-
-    return txt
-
-@echo
-def setLoadBalancerRule(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/call_loadbalancer.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog(" set loadbalancer rule failed "  )
-        txt = '' 
-
-    return txt
-    
-@echo
-def createFile(session, args):
-    file_path = args['filepath']
-    file_contents = args['filecontents']
-
-    try:
-        f = open(file_path, "w")
-        f.write(file_contents)
-        f.close()
-        txt = 'success'
-    except:
-        util.SMlog(" failed to create HA proxy cfg file ")
-        txt = ''
-
-    return txt
-
-@echo
-def deleteFile(session, args):
-    file_path = args["filepath"]
-
-    try:
-        if os.path.isfile(file_path):
-            os.remove(file_path)
-        txt = 'success'
-    except:
-        util.SMlog(" failed to remove HA proxy cfg file ")
-        txt = ''
-
-    return txt
-
-
-@echo
-def networkUsage(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/networkUsage.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-    except:
-        util.SMlog("  network usage error "  )
-        txt = '' 
-
-    return txt
-    
-def get_private_nic(session, args):
-    vms = session.xenapi.VM.get_all()
-    host_uuid = args.get('host_uuid')
-    host = session.xenapi.host.get_by_uuid(host_uuid)
-    piflist = session.xenapi.host.get_PIFs(host)
-    mgmtnic = 'eth0'
-    for pif in piflist:
-        pifrec = session.xenapi.PIF.get_record(pif)
-        network = pifrec.get('network')
-        nwrec = session.xenapi.network.get_record(network)
-        if nwrec.get('name_label') == 'cloud-guest':
-            return pifrec.get('device')
-        if pifrec.get('management'):
-            mgmtnic = pifrec.get('device')
-    
-    return mgmtnic
-
-def chain_name(vm_name):
-    if vm_name.startswith('i-') or vm_name.startswith('r-'):
-        if vm_name.endswith('untagged'):
-            return '-'.join(vm_name.split('-')[:-1])
-    return vm_name
-
-def chain_name_def(vm_name):
-    if vm_name.startswith('i-'):
-        if vm_name.endswith('untagged'):
-            return '-'.join(vm_name.split('-')[:-2]) + "-def"
-        return '-'.join(vm_name.split('-')[:-1]) + "-def"
-    return vm_name
-  
-def egress_chain_name(vm_name):
-    return chain_name(vm_name) + "-eg"
-      
-@echo
-def can_bridge_firewall(session, args):
-    try:
-        util.pread2(['ebtables', '-V'])
-        util.pread2(['ipset', '-V'])
-    except:
-        return 'false'
-
-    host_uuid = args.get('host_uuid')
-    try:
-        util.pread2(['iptables', '-N', 'BRIDGE-FIREWALL'])
-        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged',  '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
-        util.pread2(['iptables', '-D', 'FORWARD',  '-j', 'RH-Firewall-1-INPUT'])
-    except:
-        util.SMlog('Chain BRIDGE-FIREWALL already exists')
-    privnic = get_private_nic(session, args)
-    result = 'true'
-    try:
-        util.pread2(['/bin/bash', '-c', 'iptables -n -L FORWARD | grep BRIDGE-FIREWALL'])
-    except:
-        try:
-            util.pread2(['iptables', '-I', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '-j', 'BRIDGE-FIREWALL'])
-            util.pread2(['iptables', '-A', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', privnic, '-j', 'ACCEPT'])
-            util.pread2(['iptables', '-A', 'FORWARD', '-j', 'DROP'])
-        except:
-            return 'false'
-    default_ebtables_rules()
-    allow_egress_traffic(session)
-    if not os.path.exists('/var/run/cloud'):
-        os.makedirs('/var/run/cloud')
-    if not os.path.exists('/var/cache/cloud'):
-        os.makedirs('/var/cache/cloud')
-    #get_ipset_keyword()
- 
-    cleanup_rules_for_dead_vms(session)
-    cleanup_rules(session, args)
-    
-    return result
-
-@echo
-def default_ebtables_rules():
-    try:
-        util.pread2(['ebtables', '-N',  'DEFAULT_EBTABLES'])
-        util.pread2(['ebtables', '-A', 'FORWARD', '-j'  'DEFAULT_EBTABLES'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'ACCEPT'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Request', '-j', 'ACCEPT'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Reply', '-j', 'ACCEPT'])
-        # deny mac broadcast and multicast
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Broadcast', '-j', 'DROP']) 
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Multicast', '-j', 'DROP']) 
-        # deny ip broadcast and multicast
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'DROP'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '224.0.0.0/4', '-j', 'DROP'])
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-j', 'RETURN'])
-        # deny ipv6
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv6', '-j', 'DROP'])
-        # deny vlan
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', '802_1Q', '-j', 'DROP'])
-        # deny all others (e.g., 802.1d, CDP)
-        util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES',  '-j', 'DROP'])
-    except:
-        util.SMlog('Chain DEFAULT_EBTABLES already exists')
-
-
-@echo
-def allow_egress_traffic(session):
-    devs = []
-    for pif in session.xenapi.PIF.get_all():
-        pif_rec = session.xenapi.PIF.get_record(pif)
-        vlan = pif_rec.get('VLAN')
-        dev = pif_rec.get('device')
-        if vlan == '-1':
-            devs.append(dev)
-        else:
-            devs.append(dev + "." + vlan)
-    for d in devs:
-        try:
-            util.pread2(['/bin/bash', '-c', "iptables -n -L FORWARD | grep '%s '" % d])
-        except:
-            try:
-                util.pread2(['iptables', '-I', 'FORWARD', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', d, '-j', 'ACCEPT'])
-            except:
-                util.SMlog("Failed to add FORWARD rule through to %s" % d)
-                return 'false'
-    return 'true'
-
-
-def ipset(ipsetname, proto, start, end, ips):
-    try:
-        util.pread2(['ipset', '-N', ipsetname, 'iptreemap'])
-    except:
-        util.SMlog("ipset chain already exists" + ipsetname)
-
-    result = True
-    ipsettmp = ''.join(''.join(ipsetname.split('-')).split('_')) + str(int(time.time()) % 1000)
-
-    try: 
-        util.pread2(['ipset', '-N', ipsettmp, 'iptreemap']) 
-    except:
-        util.SMlog("Failed to create temp ipset, reusing old name= " + ipsettmp)
-        try: 
-            util.pread2(['ipset', '-F', ipsettmp]) 
-        except:
-            util.SMlog("Failed to clear old temp ipset name=" + ipsettmp)
-            return False
-        
-    try: 
-        for ip in ips:
-            try:
-                util.pread2(['ipset', '-A', ipsettmp, ip])
-            except CommandException, cex:
-                if cex.reason.rfind('already in set') == -1:
-                   raise
-    except:
-        util.SMlog("Failed to program ipset " + ipsetname)
-        util.pread2(['ipset', '-F', ipsettmp]) 
-        util.pread2(['ipset', '-X', ipsettmp]) 
-        return False
-
-    try: 
-        util.pread2(['ipset', '-W', ipsettmp, ipsetname]) 
-    except:
-        util.SMlog("Failed to swap ipset " + ipsetname)
-        result = False
-
-    try: 
-        util.pread2(['ipset', '-F', ipsettmp]) 
-        util.pread2(['ipset', '-X', ipsettmp]) 
-    except:
-        # if the temporary name clashes next time we'll just reuse it
-        util.SMlog("Failed to delete temp ipset " + ipsettmp)
-
-    return result
-
-@echo 
-def destroy_network_rules_for_vm(session, args):
-    vm_name = args.pop('vmName')
-    vmchain = chain_name(vm_name)
-    vmchain_egress = egress_chain_name(vm_name)
-    vmchain_default = chain_name_def(vm_name)
-    
-    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
-    if vm_name.startswith('i-') or vm_name.startswith('r-') or vm_name.startswith('l-'):
-        try:
-            util.pread2(['iptables', '-F', vmchain_default])
-            util.pread2(['iptables', '-X', vmchain_default])
-        except:
-            util.SMlog("Ignoring failure to delete  chain " + vmchain_default)
-    
-    destroy_ebtables_rules(vmchain)
-    
-    try:
-        util.pread2(['iptables', '-F', vmchain])
-        util.pread2(['iptables', '-X', vmchain])
-    except:
-        util.SMlog("Ignoring failure to delete ingress chain " + vmchain)
-        
-   
-    try:
-        util.pread2(['iptables', '-F', vmchain_egress])
-        util.pread2(['iptables', '-X', vmchain_egress])
-    except:
-        util.SMlog("Ignoring failure to delete egress chain " + vmchain_egress)
-    
-    remove_rule_log_for_vm(vm_name)
-    
-    if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
-        return 'true'
-    
-    try:
-        setscmd = "ipset --save | grep " +  vmchain + " | grep '^-N' | awk '{print $2}'"
-        setsforvm = util.pread2(['/bin/bash', '-c', setscmd]).split('\n')
-        for set in setsforvm:
-            if set != '':
-                util.pread2(['ipset', '-F', set])       
-                util.pread2(['ipset', '-X', set])       
-    except:
-        util.SMlog("Failed to destroy ipsets for %" % vm_name)
-    
-    
-    return 'true'
-
-@echo
-def destroy_ebtables_rules(vm_chain):
-    
-    delcmd = "ebtables-save | grep " +  vm_chain + " | sed 's/-A/-D/'"
-    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
-    delcmds.pop()
-    for cmd in delcmds:
-        try:
-            dc = cmd.split(' ')
-            dc.insert(0, 'ebtables')
-            util.pread2(dc)
-        except:
-            util.SMlog("Ignoring failure to delete ebtables rules for vm " + vm_chain)
-    try:
-        util.pread2(['ebtables', '-F', vm_chain])
-        util.pread2(['ebtables', '-X', vm_chain])
-    except:
-            util.SMlog("Ignoring failure to delete ebtables chain for vm " + vm_chain)   
-
-@echo
-def destroy_arptables_rules(vm_chain):
-    delcmd = "arptables -vL FORWARD | grep " + vm_chain + " | sed 's/-i any//' | sed 's/-o any//' | awk '{print $1,$2,$3,$4}' "
-    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
-    delcmds.pop()
-    for cmd in delcmds:
-        try:
-            dc = cmd.split(' ')
-            dc.insert(0, 'arptables')
-            dc.insert(1, '-D')
-            dc.insert(2, 'FORWARD')
-            util.pread2(dc)
-        except:
-            util.SMlog("Ignoring failure to delete arptables rules for vm " + vm_chain)
-    
-    try:
-        util.pread2(['arptables', '-F', vm_chain])
-        util.pread2(['arptables', '-X', vm_chain])
-    except:
-        util.SMlog("Ignoring failure to delete arptables chain for vm " + vm_chain) 
-              
-@echo
-def default_ebtables_antispoof_rules(vm_chain, vifs, vm_ip, vm_mac):
-    if vm_mac == 'ff:ff:ff:ff:ff:ff':
-        util.SMlog("Ignoring since mac address is not valid")
-        return 'true'
-    
-    try:
-        util.pread2(['ebtables', '-N', vm_chain])
-    except:
-        try:
-            util.pread2(['ebtables', '-F', vm_chain])
-        except:
-            util.SMlog("Failed to create ebtables antispoof chain, skipping")
-            return 'true'
-
-    # note all rules for packets into the bridge (-i) precede all output rules (-o)
-    # always start after the first rule in the FORWARD chain that jumps to DEFAULT_EBTABLES chain
-    try:
-        for vif in vifs:
-            util.pread2(['ebtables', '-I', 'FORWARD', '2', '-i',  vif,  '-j', vm_chain])
-            util.pread2(['ebtables', '-A', 'FORWARD', '-o',  vif, '-j', vm_chain])
-    except:
-        util.SMlog("Failed to program default ebtables FORWARD rules for %s" % vm_chain)
-        return 'false'
-
-    try:
-        for vif in vifs:
-            # only allow source mac that belongs to the vm
-	    util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-s', '!', vm_mac,  '-j', 'DROP'])
-            # do not allow fake dhcp responses
-            util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '68', '-j', 'DROP'])
-            # do not allow snooping of dhcp requests
-            util.pread2(['ebtables', '-A', vm_chain, '-o', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'DROP'])
-    except:
-        util.SMlog("Failed to program default ebtables antispoof rules for %s" % vm_chain)
-        return 'false'
-
-    return 'true'
-
-@echo
-def default_arp_antispoof(vm_chain, vifs, vm_ip, vm_mac):
-    if vm_mac == 'ff:ff:ff:ff:ff:ff':
-        util.SMlog("Ignoring since mac address is not valid")
-        return 'true'
-
-    try:
-        util.pread2(['arptables',  '-N', vm_chain])
-    except:
-        try:
-            util.pread2(['arptables', '-F', vm_chain])
-        except:
-            util.SMlog("Failed to create arptables rule, skipping")
-            return 'true'
-
-    # note all rules for packets into the bridge (-i) precede all output rules (-o)
-    try:
-        for vif in vifs:
-           util.pread2(['arptables',  '-I', 'FORWARD', '-i',  vif, '-j', vm_chain])
-           util.pread2(['arptables',  '-A', 'FORWARD', '-o',  vif, '-j', vm_chain])
-    except:
-        util.SMlog("Failed to program default arptables rules in FORWARD chain vm=" + vm_chain)
-        return 'false'
-    
-    try:
-        for vif in vifs:
-            #accept arp replies into the bridge as long as the source mac and ips match the vm
-            util.pread2(['arptables',  '-A', vm_chain, '-i', vif, '--opcode', 'Reply', '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'ACCEPT'])
-            #accept any arp requests from this vm. In the future this can be restricted to deny attacks on hosts
-            #also important to restrict source ip and src mac in these requests as they can be used to update arp tables on destination
-            util.pread2(['arptables',  '-A', vm_chain, '-i', vif, '--opcode', 'Request',  '--source-mac',  vm_mac, '--source-ip',  vm_ip, '-j', 'RETURN'])
-            #accept any arp requests to this vm as long as the request is for this vm's ip
-            util.pread2(['arptables',  '-A', vm_chain, '-o', vif, '--opcode', 'Request', '--destination-ip', vm_ip, '-j', 'ACCEPT'])   
-            #accept any arp replies to this vm as long as the mac and ip matches
-            util.pread2(['arptables',  '-A', vm_chain, '-o', vif, '--opcode', 'Reply', '--destination-mac', vm_mac, '--destination-ip', vm_ip, '-j', 'ACCEPT'])   
-        util.pread2(['arptables',  '-A', vm_chain,  '-j', 'DROP'])
-
-    except:
-        util.SMlog("Failed to program default arptables  rules")
-        return 'false'
-
-    return 'true'
-
-@echo
-def default_network_rules_systemvm(session, args):
-    vm_name = args.pop('vmName')
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vm_name)
-        if len(vm) != 1:
-             return 'false'
-        vm_rec = session.xenapi.VM.get_record(vm[0])
-        vm_vifs = vm_rec.get('VIFs')
-        vifnums = [session.xenapi.VIF.get_record(vif).get('device') for vif in vm_vifs]
-        domid = vm_rec.get('domid')
-    except:
-        util.SMlog("### Failed to get domid or vif list for vm  ##" + vm_name)
-        return 'false'
-    
-    if domid == '-1':
-        util.SMlog("### Failed to get domid for vm (-1):  " + vm_name)
-        return 'false'
-
-    vifs = ["vif" + domid + "." + v for v in vifnums]
-    #vm_name =  '-'.join(vm_name.split('-')[:-1])
-    vmchain = chain_name(vm_name)
-   
- 
-    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
-  
-    try:
-        util.pread2(['iptables', '-N', vmchain])
-    except:
-        util.pread2(['iptables', '-F', vmchain])
-    
-    allow_egress_traffic(session)
-  
-    for vif in vifs:
-        try:
-            util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', vif, '-j', vmchain])
-            util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '4', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', vmchain])
-            util.pread2(['iptables', '-I', vmchain, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', 'RETURN'])
-        except:
-            util.SMlog("Failed to program default rules")
-            return 'false'
-	
-	
-    util.pread2(['iptables', '-A', vmchain, '-j', 'ACCEPT'])
-    
-    if write_rule_log_for_vm(vm_name, '-1', '_ignore_', domid, '_initial_', '-1') == False:
-        util.SMlog("Failed to log default network rules for systemvm, ignoring")
-    return 'true'
-
-
-@echo
-def default_network_rules(session, args):
-    vm_name = args.pop('vmName')
-    vm_ip = args.pop('vmIP')
-    vm_id = args.pop('vmID')
-    vm_mac = args.pop('vmMAC')
-    
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vm_name)
-        if len(vm) != 1:
-             util.SMlog("### Failed to get record for vm  " + vm_name)
-             return 'false'
-        vm_rec = session.xenapi.VM.get_record(vm[0])
-        domid = vm_rec.get('domid')
-    except:
-        util.SMlog("### Failed to get domid for vm " + vm_name)
-        return 'false'
-    if domid == '-1':     
-        util.SMlog("### Failed to get domid for vm (-1):  " + vm_name)
-        return 'false'
-    
-    vif = "vif" + domid + ".0"
-    tap = "tap" + domid + ".0"
-    vifs = [vif]
-    try:
-        util.pread2(['ifconfig', tap])
-        vifs.append(tap)
-    except:
-        pass
-
-    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
-
-     
-    vmchain =  chain_name(vm_name)
-    vmchain_egress =  egress_chain_name(vm_name)
-    vmchain_default = chain_name_def(vm_name)
-    
-    destroy_ebtables_rules(vmchain)
-    
-
-    try:
-        util.pread2(['iptables', '-N', vmchain])
-    except:
-        util.pread2(['iptables', '-F', vmchain])
-    
-    try:
-        util.pread2(['iptables', '-N', vmchain_egress])
-    except:
-        util.pread2(['iptables', '-F', vmchain_egress])
-        
-    try:
-        util.pread2(['iptables', '-N', vmchain_default])
-    except:
-        util.pread2(['iptables', '-F', vmchain_default])        
-
-    try:
-        for v in vifs:
-            util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
-            util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
-        util.pread2(['iptables', '-A', vmchain_default, '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
-        #allow dhcp
-        for v in vifs:
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-p', 'udp', '--dport', '67', '--sport', '68',  '-j', 'ACCEPT'])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-p', 'udp', '--dport', '68', '--sport', '67',  '-j', 'ACCEPT'])
-
-        #don't let vm spoof its ip address
-        for v in vifs:
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip,'-p', 'udp', '--dport', '53', '-j', 'RETURN'])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', '!', vm_ip, '-j', 'DROP'])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '--destination', '!', vm_ip, '-j', 'DROP'])
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip, '-j', vmchain_egress])
-        
-        for v in vifs:
-            util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v,  '-j', vmchain])
-    except:
-        util.SMlog("Failed to program default rules for vm " + vm_name)
-        return 'false'
-    
-    default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
-    default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
-    
-    if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, '_initial_', '-1', vm_mac) == False:
-        util.SMlog("Failed to log default network rules, ignoring")
-        
-    util.SMlog("Programmed default rules for vm " + vm_name)
-    return 'true'
-
-@echo
-def check_domid_changed(session, vmName):
-    curr_domid = '-1'
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vmName)
-        if len(vm) != 1:
-             util.SMlog("### Could not get record for vm ## " + vmName)
-        else:
-            vm_rec = session.xenapi.VM.get_record(vm[0])
-            curr_domid = vm_rec.get('domid')
-    except:
-        util.SMlog("### Failed to get domid for vm  ## " + vmName)
-        
-    
-    logfilename = "/var/run/cloud/" + vmName +".log"
-    if not os.path.exists(logfilename):
-        return ['-1', curr_domid]
-    
-    lines = (line.rstrip() for line in open(logfilename))
-    
-    [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno, _vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
-    for line in lines:
-        try:
-            [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno,_vmMac] = line.split(',')
-        except ValueError,v:
-            [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno] = line.split(',')
-        break
-    
-    return [curr_domid, old_domid]
-
-@echo
-def delete_rules_for_vm_in_bridge_firewall_chain(vmName):
-    vm_name = vmName
-    vmchain = chain_name_def(vm_name)
-    
-    delcmd = "iptables-save | grep '\-A BRIDGE-FIREWALL' | grep " +  vmchain + " | sed 's/-A/-D/'"
-    delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
-    delcmds.pop()
-    for cmd in delcmds:
-        try:
-            dc = cmd.split(' ')
-            dc.insert(0, 'iptables')
-            dc.pop()
-            util.pread2(filter(None, dc))
-        except:
-              util.SMlog("Ignoring failure to delete rules for vm " + vmName)
-
-  
-@echo
-def network_rules_for_rebooted_vm(session, vmName):
-    vm_name = vmName
-    [curr_domid, old_domid] = check_domid_changed(session, vm_name)
-    
-    if curr_domid == old_domid:
-        return True
-    
-    if old_domid == '-1':
-        return True
-    
-    if curr_domid == '-1':
-        return True
-    
-    util.SMlog("Found a rebooted VM -- reprogramming rules for  " + vm_name)
-    
-    delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
-    if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
-        default_network_rules_systemvm(session, {"vmName":vm_name})
-        return True
-    
-    vif = "vif" + curr_domid + ".0"
-    tap = "tap" + curr_domid + ".0"
-    vifs = [vif]
-    try:
-        util.pread2(['ifconfig', tap])
-        vifs.append(tap)
-    except:
-        pass
-    vmchain = chain_name(vm_name)
-    vmchain_default = chain_name_def(vm_name)
-
-    for v in vifs:
-        util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
-        util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
-
-    #change antispoof rule in vmchain
-    try:
-        delcmd = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-in | sed 's/-A/-D/'"
-        delcmd2 = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-out | sed 's/-A/-D/'"
-        inscmd = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-in | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' "
-        inscmd2 = "iptables-save| grep '\-A " +  vmchain_default + "' | grep  physdev-in | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/' "
-        inscmd3 = "iptables-save | grep '\-A " +  vmchain_default + "' | grep  physdev-out | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' "
-        inscmd4 = "iptables-save| grep '\-A " +  vmchain_default + "' | grep  physdev-out | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/' "
-        
-        ipts = []
-        for cmd in [delcmd, delcmd2, inscmd, inscmd2, inscmd3, inscmd4]:
-            cmds = util.pread2(['/bin/bash', '-c', cmd]).split('\n')
-            cmds.pop()
-            for c in cmds:
-                    ipt = c.split(' ')
-                    ipt.insert(0, 'iptables')
-                    ipt.pop()
-                    ipts.append(ipt)
-        
-        for ipt in ipts:
-            try:
-                util.pread2(filter(None,ipt))
-            except:
-                util.SMlog("Failed to rewrite antispoofing rules for vm " + vm_name)
-        
-        util.pread2(['/bin/bash', '-c', 'iptables -D ' + vmchain_default + " -j " + vmchain])
-        util.pread2(['/bin/bash', '-c', 'iptables -A ' + vmchain_default + " -j " + vmchain])
-    except:
-        util.SMlog("No rules found for vm " + vm_name)
-
-    destroy_ebtables_rules(vmchain)
-    destroy_arptables_rules(vmchain)
-    [vm_ip, vm_mac] = get_vm_mac_ip_from_log(vmchain)
-    default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
-    default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
-    rewrite_rule_log_for_vm(vm_name, curr_domid)
-    return True
-
-def rewrite_rule_log_for_vm(vm_name, new_domid):
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    if not os.path.exists(logfilename):
-        return 
-    lines = (line.rstrip() for line in open(logfilename))
-    
-    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
-    for line in lines:
-        try:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
-            break
-        except ValueError,v:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
-    
-    write_rule_log_for_vm(_vmName, _vmID, _vmIP, new_domid, _signature, '-1', _vmMac)
-
-def get_rule_log_for_vm(session, vmName):
-    vm_name = vmName;
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    if not os.path.exists(logfilename):
-        return ''
-    
-    lines = (line.rstrip() for line in open(logfilename))
-    
-    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
-    for line in lines:
-        try:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
-            break
-        except ValueError,v:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
-    
-    return ','.join([_vmName, _vmID, _vmIP, _domID, _signature, _seqno])
-
-@echo
-def get_vm_mac_ip_from_log(vm_name):
-    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '0.0.0.0', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    if not os.path.exists(logfilename):
-        return ['_', '_']
-    
-    lines = (line.rstrip() for line in open(logfilename))
-    for line in lines:
-        try:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
-            break
-        except ValueError,v:
-            [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
-    
-    return [ _vmIP, _vmMac]
-
-@echo
-def get_rule_logs_for_vms(session, args):
-    host_uuid = args.pop('host_uuid')
-    try:
-        thishost = session.xenapi.host.get_by_uuid(host_uuid)
-        hostrec = session.xenapi.host.get_record(thishost)
-        vms = hostrec.get('resident_VMs')
-    except:
-        util.SMlog("Failed to get host from uuid " + host_uuid)
-        return ' '
-    
-    result = []
-    try:
-        for name in [session.xenapi.VM.get_name_label(x) for x in vms]:
-            if 1 not in [ name.startswith(c) for c in ['r-', 's-', 'v-', 'i-', 'l-'] ]:
-                continue
-            network_rules_for_rebooted_vm(session, name)
-            if name.startswith('i-'):
-                log = get_rule_log_for_vm(session, name)
-                result.append(log)
-    except:
-        util.SMlog("Failed to get rule logs, better luck next time!")
-        
-    return ";".join(result)
-
-@echo
-def cleanup_rules_for_dead_vms(session):
-  try:
-    vms = session.xenapi.VM.get_all()
-    cleaned = 0
-    for vm_name in [session.xenapi.VM.get_name_label(x) for x in vms]:
-        if 1 in [ vm_name.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-'] ]:
-            vm = session.xenapi.VM.get_by_name_label(vm_name)
-            if len(vm) != 1:
-                continue
-            vm_rec = session.xenapi.VM.get_record(vm[0])
-            state = vm_rec.get('power_state')
-            if state != 'Running' and state != 'Paused':
-                util.SMlog("vm " + vm_name + " is not running, cleaning up")
-                destroy_network_rules_for_vm(session, {'vmName':vm_name})
-                cleaned = cleaned+1
-                
-    util.SMlog("Cleaned up rules for " + str(cleaned) + " vms")
-  except:
-    util.SMlog("Failed to cleanup rules for dead vms!")
-        
-
-@echo
-def cleanup_rules(session, args):
-  instance = args.get('instance')
-  if not instance:
-    instance = 'VM'
-  resident_vms = []
-  try:
-    hostname = util.pread2(['/bin/bash', '-c', 'hostname']).split('\n')
-    if len(hostname) < 1:
-       raise Exception('Could not find hostname of this host')
-    thishost = session.xenapi.host.get_by_name_label(hostname[0])
-    if len(thishost) < 1:
-       raise Exception("Could not find host record from hostname %s of this host"%hostname[0])
-    hostrec = session.xenapi.host.get_record(thishost[0])
-    vms = hostrec.get('resident_VMs')
-    resident_vms = [session.xenapi.VM.get_name_label(x) for x in vms]
-    util.SMlog('cleanup_rules: found %s resident vms on this host %s' % (len(resident_vms)-1, hostname[0]))
- 
-    chainscmd = "iptables-save | grep '^:' | awk '{print $1}' | cut -d':' -f2 | sed 's/-def/-%s/'| sed 's/-eg//' | sort|uniq" % instance
-    chains = util.pread2(['/bin/bash', '-c', chainscmd]).split('\n')
-    vmchains = [ch  for ch in chains if 1 in [ ch.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-']]]
-    util.SMlog('cleanup_rules: found %s iptables chains for vms on this host %s' % (len(vmchains), hostname[0]))
-    cleaned = 0
-    cleanup = []
-    for chain in vmchains:
-        vm = session.xenapi.VM.get_by_name_label(chain)
-        if len(vm) != 1:
-            vm = session.xenapi.VM.get_by_name_label(chain + "-untagged")
-            if len(vm) != 1:
-                util.SMlog("chain " + chain + " does not correspond to a vm, cleaning up")
-                cleanup.append(chain)
-                continue
-        if chain not in resident_vms:
-            util.SMlog("vm " + chain + " is not running, cleaning up")
-            cleanup.append(chain)
-                
-    for vm_name in cleanup:
-        destroy_network_rules_for_vm(session, {'vmName':vm_name})
-                    
-    util.SMlog("Cleaned up rules for " + str(len(cleanup)) + " chains")
-    return str(len(cleanup))                
-  except Exception, ex:
-    util.SMlog("Failed to cleanup rules, reason= " + str(ex))
-    return '-1';
-
-@echo
-def check_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno):
-    vm_name = vmName;
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    if not os.path.exists(logfilename):
-        util.SMlog("Failed to find logfile %s" %logfilename)
-        return [True, True, True]
-        
-    lines = (line.rstrip() for line in open(logfilename))
-    
-    [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
-    try:
-        for line in lines:
-            try:
-                [_vmName,_vmID,_vmIP,_domID,_signature,_seqno, _vmMac] = line.split(',')
-            except ValueError,v:
-                [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
-            break
-    except:
-        util.SMlog("Failed to parse log file for vm " + vmName)
-        remove_rule_log_for_vm(vmName)
-        return [True, True, True]
-    
-    reprogramDefault = False
-    if (domID != _domID) or (vmID != _vmID) or (vmIP != _vmIP):
-        util.SMlog("Change in default info set of vm %s" % vmName)
-        return [True, True, True]
-    else:
-        util.SMlog("No change in default info set of vm %s" % vmName)
-    
-    reprogramChain = False
-    rewriteLog = True
-    if (int(seqno) > int(_seqno)):
-        if (_signature != signature):
-            reprogramChain = True
-            util.SMlog("Seqno increased from %s to %s: reprogamming "\
-                        "ingress rules for vm %s" % (_seqno, seqno, vmName))
-        else:
-            util.SMlog("Seqno increased from %s to %s: but no change "\
-                        "in signature for vm: skip programming ingress "\
-                        "rules %s" % (_seqno, seqno, vmName))
-    elif (int(seqno) < int(_seqno)):
-        util.SMlog("Seqno decreased from %s to %s: ignoring these "\
-                        "ingress rules for vm %s" % (_seqno, seqno, vmName))
-        rewriteLog = False
-    elif (signature != _signature):
-        util.SMlog("Seqno %s stayed the same but signature changed from "\
-                    "%s to %s for vm %s" % (seqno, _signature, signature, vmName))
-        rewriteLog = True
-        reprogramChain = True
-    else:
-        util.SMlog("Seqno and signature stayed the same: %s : ignoring these "\
-                        "ingress rules for vm %s" % (seqno, vmName))
-        rewriteLog = False
-        
-    return [reprogramDefault, reprogramChain, rewriteLog]
-    
-
-@echo
-def write_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno, vmMac='ff:ff:ff:ff:ff:ff'):
-    vm_name = vmName
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-    util.SMlog("Writing log to " + logfilename)
-    logf = open(logfilename, 'w')
-    output = ','.join([vmName, vmID, vmIP, domID, signature, seqno, vmMac])
-    result = True
-    try:
-        logf.write(output)
-        logf.write('\n')
-    except:
-        util.SMlog("Failed to write to rule log file " + logfilename)
-        result = False
-        
-    logf.close()
-    
-    return result
-
-@echo
-def remove_rule_log_for_vm(vmName):
-    vm_name = vmName
-    logfilename = "/var/run/cloud/" + vm_name +".log"
-
-    result = True
-    try:
-        os.remove(logfilename)
-    except:
-        util.SMlog("Failed to delete rule log file " + logfilename)
-        result = False
-    
-    return result
-
-@echo
-def inflate_rules (zipped):
-   return zlib.decompress(base64.b64decode(zipped))
-
-@echo
-def cache_ipset_keyword():
-    tmpname = 'ipsetqzvxtmp'
-    try:
-        util.pread2(['/bin/bash', '-c', 'ipset -N ' + tmpname + ' iptreemap'])
-    except:
-        util.pread2(['/bin/bash', '-c', 'ipset -F ' + tmpname])
-
-    try:
-        util.pread2(['/bin/bash', '-c', 'iptables -A INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
-        util.pread2(['/bin/bash', '-c', 'iptables -D INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
-        keyword = 'set'
-    except:
-        keyword = 'match-set'
-    
-    try:
-       util.pread2(['/bin/bash', '-c', 'ipset -X ' + tmpname])
-    except:
-       pass
-       
-    cachefile = "/var/cache/cloud/ipset.keyword"
-    util.SMlog("Writing ipset keyword to " + cachefile)
-    cachef = open(cachefile, 'w')
-    try:
-        cachef.write(keyword)
-        cachef.write('\n')
-    except:
-        util.SMlog("Failed to write to cache file " + cachef)
-        
-    cachef.close()
-    return keyword
-    
-@echo
-def get_ipset_keyword():
-    cachefile = "/var/cache/cloud/ipset.keyword"
-    keyword = 'match-set'
-    
-    if not os.path.exists(cachefile):
-        util.SMlog("Failed to find ipset keyword cachefile %s" %cachefile)
-        keyword = cache_ipset_keyword()
-    else:
-        lines = (line.rstrip() for line in open(cachefile))
-        for line in lines:
-            keyword = line
-            break
-
-    return keyword
-
-@echo
-def network_rules(session, args):
-  try:
-    vm_name = args.get('vmName')
-    vm_ip = args.get('vmIP')
-    vm_id = args.get('vmID')
-    vm_mac = args.get('vmMAC')
-    signature = args.pop('signature')
-    seqno = args.pop('seqno')
-    deflated = 'false'
-    if 'deflated' in args:
-        deflated = args.pop('deflated')
-    
-    try:
-        vm = session.xenapi.VM.get_by_name_label(vm_name)
-        if len(vm) != 1:
-             util.SMlog("### Could not get record for vm ## " + vm_name)
-             return 'false'
-        vm_rec = session.xenapi.VM.get_record(vm[0])
-        domid = vm_rec.get('domid')
-    except:
-        util.SMlog("### Failed to get domid for vm  ## " + vm_name)
-        return 'false'
-    if domid == '-1':
-        util.SMlog("### Failed to get domid for vm (-1):  " + vm_name)
-        return 'false'
-   
-    vif = "vif" + domid + ".0"
-    tap = "tap" + domid + ".0"
-    vifs = [vif]
-    try:
-        util.pread2(['ifconfig', tap])
-        vifs.append(tap)
-    except:
-        pass
-   
-
-    reason = 'seqno_change_or_sig_change'
-    [reprogramDefault, reprogramChain, rewriteLog] = \
-             check_rule_log_for_vm (vm_name, vm_id, vm_ip, domid, signature, seqno)
-    
-    if not reprogramDefault and not reprogramChain:
-        util.SMlog("No changes detected between current state and received state")
-        reason = 'seqno_same_sig_same'
-        if rewriteLog:
-            reason = 'seqno_increased_sig_same'
-            write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac)
-        util.SMlog("Programming network rules for vm  %s seqno=%s signature=%s guestIp=%s,"\
-               " do nothing, reason=%s" % (vm_name, seqno, signature, vm_ip, reason))
-        return 'true'
-           
-    if not reprogramChain:
-        util.SMlog("###Not programming any ingress rules since no changes detected?")
-        return 'true'
-
-    if reprogramDefault:
-        util.SMlog("Change detected in vmId or vmIp or domId, resetting default rules")
-        default_network_rules(session, args)
-        reason = 'domid_change'
-    
-    rules = args.pop('rules')
-    if deflated.lower() == 'true':
-       rules = inflate_rules (rules)
-    keyword = '--' + get_ipset_keyword() 
-    lines = rules.split(' ')
-
-    util.SMlog("Programming network rules for vm  %s seqno=%s numrules=%s signature=%s guestIp=%s,"\
-              " update iptables, reason=%s" % (vm_name, seqno, len(lines), signature, vm_ip, reason))
-    
-    cmds = []
-    egressrules = 0
-    for line in lines:
-        tokens = line.split(':')
-        if len(tokens) != 5:
-          continue
-        type = tokens[0]
-        protocol = tokens[1]
-        start = tokens[2]
-        end = tokens[3]
-        cidrs = tokens.pop();
-        ips = cidrs.split(",")
-        ips.pop()
-        allow_any = False
-
-        if type == 'E':
-            vmchain = egress_chain_name(vm_name)
-            action = "RETURN"
-            direction = "dst"
-            egressrules = egressrules + 1
-        else:
-            vmchain = chain_name(vm_name)
-            action = "ACCEPT"
-            direction = "src"
-        if  '0.0.0.0/0' in ips:
-            i = ips.index('0.0.0.0/0')
-            del ips[i]
-            allow_any = True
-        range = start + ":" + end
-        if ips:    
-            ipsetname = vmchain + "_" + protocol + "_" + start + "_" + end
-            if start == "-1":
-                ipsetname = vmchain + "_" + protocol + "_any"
-
-            if ipset(ipsetname, protocol, start, end, ips) == False:
-                util.SMlog(" failed to create ipset for rule " + str(tokens))
-
-            if protocol == 'all':
-                iptables = ['iptables', '-I', vmchain, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
-            elif protocol != 'icmp':
-                iptables = ['iptables', '-I', vmchain, '-p',  protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
-            else:
-                range = start + "/" + end
-                if start == "-1":
-                    range = "any"
-                iptables = ['iptables', '-I', vmchain, '-p',  'icmp', '--icmp-type',  range,  '-m', 'set', keyword, ipsetname, direction, '-j', action]
-                
-            cmds.append(iptables)
-            util.SMlog(iptables)
-        
-        if allow_any and protocol != 'all':
-            if protocol != 'icmp':
-                iptables = ['iptables', '-I', vmchain, '-p',  protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-j', action]
-            else:
-                range = start + "/" + end
-                if start == "-1":
-                    range = "any"
-                iptables = ['iptables', '-I', vmchain, '-p',  'icmp', '--icmp-type',  range, '-j', action]
-            cmds.append(iptables)
-            util.SMlog(iptables)
-      
-    vmchain = chain_name(vm_name)        
-    util.pread2(['iptables', '-F', vmchain])
-    egress_vmchain = egress_chain_name(vm_name)        
-    util.pread2(['iptables', '-F', egress_vmchain])
-    
-    for cmd in cmds:
-        util.pread2(cmd)
-        
-    if egressrules == 0 :
-        util.pread2(['iptables', '-A', egress_vmchain, '-j', 'RETURN'])
-    else:
-        util.pread2(['iptables', '-A', egress_vmchain, '-j', 'DROP'])
-   
-    util.pread2(['iptables', '-A', vmchain, '-j', 'DROP'])
-
-    if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac) == False:
-        return 'false'
-    
-    return 'true'
-  except:
-    util.SMlog("Failed to network rule !")
-
-@echo
-def checkRouter(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/getRouterStatus.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-    except:
-        util.SMlog("  check router status fail! ")
-        txt = '' 
-
-    return txt
-
-@echo
-def bumpUpPriority(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/bumpUpPriority.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        txt = 'success'
-    except:
-        util.SMlog("bump up priority fail! ")
-        txt = ''
-
-    return txt
-    
-@echo
-def setDNATRule(session, args):
-    add = args["add"]
-    if add == "false":
-        util.pread2(["iptables", "-t", "nat", "-F"])
-    else:
-        ip = args["ip"]
-        port = args["port"]
-        util.pread2(["iptables", "-t", "nat", "-F"])
-        util.pread2(["iptables", "-t", "nat", "-A", "PREROUTING", "-i", "xenbr0", "-p", "tcp", "--dport", port, "-m", "state", "--state", "NEW", "-j", "DNAT", "--to-destination", ip +":443"])
-    return ""
-
-@echo
-def createISOVHD(session, args):
-    #hack for XCP on ubuntu 12.04, as can't attach iso to a vm
-    vdis = session.xenapi.VDI.get_by_name_label("systemvm-vdi");
-    util.SMlog(vdis)
-    if len(vdis) > 0:
-        vdi_record = session.xenapi.VDI.get_record(vdis[0])
-        vdi_uuid = vdi_record['uuid']
-        return vdi_uuid
-    localsrUUid = args['uuid'];
-    sr = session.xenapi.SR.get_by_uuid(localsrUUid)
-    data = {'name_label': "systemvm-vdi",
-            'SR': sr,
-            'virtual_size': '50000000',
-            'type': 'user',
-            'sharable':False,
-            'read_only':False,
-            'other_config':{},
-            }
-    vdi = session.xenapi.VDI.create(data);
-    vdi_record = session.xenapi.VDI.get_record(vdi)
-
-    vdi_uuid = vdi_record['uuid']
-
-    vms = session.xenapi.VM.get_all()
-    ctrldom = None
-    for vm in vms:
-        dom0 = session.xenapi.VM.get_is_control_domain(vm)
-        if dom0 is False:
-            continue
-        else:
-            ctrldom = vm
-
-    if ctrldom is None:
-        return "Failed"
-
-    vbds = session.xenapi.VM.get_VBDs(ctrldom)
-    if len(vbds) == 0:
-        vbd = session.xenapi.VBD.create({"VDI": vdi, "VM": ctrldom, "type":"Disk", "device": "xvda4",  "bootable": False, "mode": "RW", "userdevice": "4", "empty":False,
-                              "other_config":{}, "qos_algorithm_type":"", "qos_algorithm_params":{}})
-    else:
-        vbd = vbds[0]
-
-    vbdr = session.xenapi.VBD.get_record(vbd)
-    if session.xenapi.VBD.get_currently_attached(vbd) is False:
-        session.xenapi.VBD.plug(vbd)
-        vbdr = session.xenapi.VBD.get_record(vbd)
-    util.pread2(["dd", "if=/usr/share/xcp/packages/iso/systemvm.iso", "of=" + "/dev/" + vbdr["device"]])
-    session.xenapi.VBD.unplug(vbd)
-    session.xenapi.VBD.destroy(vbd)
-    return vdi_uuid
-
-@echo
-def routerProxy(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/router_proxy.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-        if txt is None or len(txt) == 0 :
-            txt = 'success'
-    except:
-        util.SMlog("routerProxy command " + sargs + " failed "  )
-        txt = ''
-
-    return txt
-
-@echo
-def getDomRVersion(session, args):
-    sargs = args['args']
-    cmd = sargs.split(' ')
-    cmd.insert(0, "/usr/lib/xcp/bin/getDomRVersion.sh")
-    cmd.insert(0, "/bin/bash")
-    try:
-        txt = util.pread2(cmd)
-    except:
-        util.SMlog("  get domR version fail! ")
-        txt = '' 
-
-    return txt
-
-if __name__ == "__main__":
-     XenAPIPlugin.dispatch({"pingtest": pingtest, "setup_iscsi":setup_iscsi,  
-                            "getgateway": getgateway, "preparemigration": preparemigration, 
-                            "setIptables": setIptables, "pingdomr": pingdomr, "pingxenserver": pingxenserver,  
-                            "ipassoc": ipassoc, "savePassword": savePassword, 
-                            "saveDhcpEntry": saveDhcpEntry, "setFirewallRule": setFirewallRule, 
-                            "setLoadBalancerRule": setLoadBalancerRule, "createFile": createFile, "deleteFile": deleteFile, 
-                            "networkUsage": networkUsage, "network_rules":network_rules, 
-                            "can_bridge_firewall":can_bridge_firewall, "default_network_rules":default_network_rules,
-                            "destroy_network_rules_for_vm":destroy_network_rules_for_vm, 
-                            "default_network_rules_systemvm":default_network_rules_systemvm, 
-                            "get_rule_logs_for_vms":get_rule_logs_for_vms, 
-                            "setLinkLocalIP":setLinkLocalIP, "lt2p_vpn":lt2p_vpn,
-                            "cleanup_rules":cleanup_rules, "checkRouter":checkRouter,
-                            "bumpUpPriority":bumpUpPriority, "getDomRVersion":getDomRVersion,
-                            "kill_copy_process":kill_copy_process,
-                            "createISOVHD":createISOVHD,
-                            "routerProxy":routerProxy,
-                            "setDNATRule":setDNATRule})