66# to you under the Apache License, Version 2.0 (the
77# "License"); you may not use this file except in compliance
88# with the License. You may obtain a copy of the License at
9- #
9+ #
1010# http://www.apache.org/licenses/LICENSE-2.0
11- #
11+ #
1212# Unless required by applicable law or agreed to in writing,
1313# software distributed under the License is distributed on an
1414# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1818
1919# Version @VERSION@
2020#
21- # A plugin for executing script needed by cloud stack
21+ # A plugin for executing script needed by Apache CloudStack
2222
2323import os , sys , time
2424import XenAPIPlugin
2525sys .path .extend (["/opt/xensource/sm/" ])
2626import util
2727import cloudstack_pluginlib as lib
2828import logging
29+ import datetime
2930
3031lib .setup_logging ("/var/log/cloud/swiftxenserver.log" )
3132
3233def echo (fn ):
3334 def wrapped (* v , ** k ):
3435 name = fn .__name__
35- logging .debug ("#### VMOPS enter %s ####" % name )
36+ logging .debug ("#### CLOUD enter %s ####" , name )
3637 res = fn (* v , ** k )
37- logging .debug ("#### VMOPS exit %s ####" % name )
38+ logging .debug ("#### CLOUD exit %s ####" , name )
3839 return res
3940 return wrapped
4041
@@ -52,12 +53,16 @@ def upload(args):
5253 lfilename = args ['lfilename' ]
5354 isISCSI = args ['isISCSI' ]
5455 segment = 0
55- logging .debug ("#### VMOPS upload %s to swift ####" , lfilename )
56+ storagepolicy = None
57+ if "storagepolicy" in args :
58+ storagepolicy = args ["storagepolicy" ]
59+ logging .debug ("#### CLOUD upload begin %s/%s to swift ####" , container , lfilename )
60+ timestamp_begin = datetime .datetime .now ()
5661 savedpath = os .getcwd ()
5762 os .chdir (ldir )
5863 try :
59- if isISCSI == 'ture ' :
60- cmd1 = [ lvchange , "-ay" , lfilename ]
64+ if isISCSI == 'true ' :
65+ cmd1 = [ lvchange , "-ay" , lfilename ]
6166 util .pread2 (cmd1 )
6267 cmd1 = [ lvdisplay , "-c" , lfilename ]
6368 lines = util .pread2 (cmd ).split (':' );
@@ -66,13 +71,27 @@ def upload(args):
6671 segment = 1
6772 else :
6873 size = os .path .getsize (lfilename )
69- if size > MAX_SEG_SIZE :
74+ if size > MAX_SEG_SIZE :
7075 segment = 1
71- if segment :
76+ if segment :
7277 cmd = [SWIFT , "-A" , url , "-U" , account + ":" + username , "-K" , key , "upload" , "-S" , str (MAX_SEG_SIZE ), container , lfilename ]
7378 else :
7479 cmd = [SWIFT , "-A" , url ,"-U" , account + ":" + username , "-K" , key , "upload" , container , lfilename ]
80+ if storagepolicy is not None :
81+ cmd .append ("--storage-policy" )
82+ cmd .append (storagepolicy )
7583 util .pread2 (cmd )
84+ cmd2 = [SWIFT , "-A" , url ,"-U" , account + ":" + username , "-K" , key , "stat" , container , lfilename ]
85+ upload_stat = util .pread2 (cmd2 )
86+ upload_stat = [line for line in upload_stat .split ('\n ' ) if "Content Length" in line ]
87+ upload_stat = upload_stat [0 ].split (': ' )[1 ]
88+ upload_diff = size - long (upload_stat )
89+ if upload_diff != 0 :
90+ logging .error ("#### CLOUD upload file size diff: %s" , upload_diff )
91+ timestamp_end = datetime .datetime .now ()
92+ timestamp_delta = timestamp_end - timestamp_begin
93+ rate = (size / 1024 / 1024 ) / timestamp_delta .seconds
94+ logging .debug ("#### CLOUD upload complete %s/%s to swift: %s @ %s MB/s ####" , container , lfilename , str (timestamp_delta )[:7 ], rate )
7695 return 'true'
7796 finally :
7897 os .chdir (savedpath )
@@ -86,8 +105,6 @@ def swift(session, args):
86105 return upload (args )
87106 elif op == 'download' :
88107 return download (args )
89- elif op == 'delete' :
90- cmd = ["st" , "-A https://" + hostname + ":8080/auth/v1.0 -U " + account + ":" + username + " -K " + token + " delete " + rfilename ]
91108 else :
92109 logging .debug ("doesn't support swift operation %s " % op )
93110 return 'false'
@@ -96,6 +113,6 @@ def swift(session, args):
96113 return 'true'
97114 except :
98115 return 'false'
99-
116+
100117if __name__ == "__main__" :
101118 XenAPIPlugin .dispatch ({"swift" : swift })
0 commit comments