Skip to content

Commit a322bb4

Browse files
committed
download process done, test testupload func
1 parent d0686df commit a322bb4

1 file changed

Lines changed: 84 additions & 15 deletions

File tree

vmconvert/vmconvert.py

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
qdown = Queue()
2323

2424

25-
2625
def InitLog(filename, console=True):
2726
logger = logging.getLogger(filename)
2827
logger.setLevel(logging.DEBUG)
@@ -319,8 +318,8 @@ def checkRecord(vmname, method):
319318
def writesuffix(logfile, fname, suffix):
320319
if not os.path.exists(".vm"):
321320
os.mkdir(".vm")
322-
logfile.info("Download completed")
323-
open(".vm/" + fname + suffix, "a").close()
321+
322+
open(".vm/" + fname + suffix, "a").close()
324323

325324

326325
class base(object):
@@ -463,20 +462,27 @@ def uploadImg(self, imgPath, imgFile, vmname):
463462

464463
url = "".join([apiURL, suffix])
465464

466-
with open(imgFile) as img:
467-
req = requests.put(url, headers=headers, data=img)
465+
try:
466+
msg = "Open the img: %s" % imgFile
467+
self.logfile.info(msg)
468+
with open(imgFile) as img:
469+
msg = "Put the img: %s" % imgPath
470+
self.logfile.info(msg)
471+
req = requests.put(url, headers=headers, data=img)
472+
except Exception as e:
473+
msg = "Open the img file failed"
474+
self.logfile.critical(msg)
475+
self.logfile.critical(e)
476+
return None
468477

469478
if req.status_code == 204:
470479
ret = "ok"
471-
if not os.path.exists(".vm"):
472-
os.mkdir(".vm")
473-
open(".vm/" + vmname + ".upload", "a").close()
474-
480+
shex.writesuffix(self.logfile, vmname, ".upload")
475481
tmpret = shex.remove(imgFile)
476482
if tmpret:
477483
self.logfile.critical(tmpret)
478484
else:
479-
self.logfile.critical("upload abnormally")
485+
self.logfile.critical("UPload abnormally")
480486
self.logfile.critical(req.content)
481487
ret = req.content
482488

@@ -641,14 +647,30 @@ def upload():
641647
shex.writesuffix(logfile, vmname, ".wrong")
642648
continue
643649

644-
op = opCli()
650+
op = opCli(logfile)
645651
imgfile = vmname + "-qcow2"
646652
msg = "Create the image from the vm file: %s" % vmname
647653
logfile.info(msg)
648-
img = op.createImg(vmname)
654+
655+
if not shex.checkRecord(vmname, ".img"):
656+
img = op.createImg(vmname)
657+
else:
658+
if shex.checkRecord(vmname, ".upload"):
659+
logfile.info("image have been uploaded, skip this")
660+
else:
661+
imgdata = open(".vm/" + vmname + ".img")
662+
img = json.load(imgdata)
663+
664+
print img
649665
if img:
650666
msg = "The image create: %s" % vmname
651667
logfile.info(msg)
668+
shex.writesuffix(logfile, vmname, ".img")
669+
670+
with open(".vm/" + vmname + ".img", "w") as rf:
671+
json.dump(img, rf)
672+
673+
logfile.info("The image created")
652674
else:
653675
logfile.critical("Image create failed!!!")
654676
shex.writesuffix(logfile, vmname, ".wrong")
@@ -658,10 +680,20 @@ def upload():
658680
imgID = img["id"]
659681

660682
logfile.info("UPload image")
661-
uploadRet = op.uploadImg(imgPath, imgfile, vmname)
683+
uploaded = shex.checkRecord(vmname, ".upload")
684+
msg = "Check if the image uploaded: %s" % uploaded
685+
logfile.info(msg)
686+
687+
if not shex.checkRecord(vmname, ".upload"):
688+
uploadRet = op.uploadImg(imgPath, imgfile, vmname)
689+
else:
690+
uploadRet = "uploaded"
691+
662692
if uploadRet == "ok":
663693
logfile.info("UPload completed")
664694
shex.writesuffix(logfile, vmname, ".upload")
695+
elif uploadRet == "uploaded":
696+
logfile.info("Have been uploaded")
665697
else:
666698
logfile.critical("UPload failed")
667699
logfile.critical(uploadRet)
@@ -670,7 +702,11 @@ def upload():
670702

671703
try:
672704
ext_vlan = "MC_VLAN_10.2." + vmip.split(".")[2]
705+
msg = "The floating ip network: %s" % ext_vlan
706+
logfile.info(msg)
673707
fix_vlan = op.cf.get(ext_vlan, "fix")
708+
msg = "The fixed ip network: %s" % fix_vlan
709+
logfile.info(msg)
674710
except Exception as e:
675711
logfile.critical("The network is invalid")
676712
logfile.error(e)
@@ -679,20 +715,50 @@ def upload():
679715
continue
680716

681717
networklis = op.listNet()["networks"]
682-
network = [net["id"] for net in networklis if net["name"] == fix_vlan][0]
718+
#print networklis
719+
720+
network = [net["id"] for net in networklis if net["name"] == fix_vlan]
721+
722+
if network:
723+
network = network[0]
724+
msg = "Get the fixed network: %s" % network
725+
logfile.info()
726+
else:
727+
logfile.critical("Can't get the fixed network")
728+
shex.writesuffix(logfile, vmname, ".wrong")
729+
continue
683730

684731
flavorlis = op.listFlavor()["flavors"]
685732
flavor = [[f["id"], f["ram"], f["vcpus"], f["disk"]] for f in flavorlis if f["ram"] >= vmmem and f["vcpus"] >= vmcpu and f["disk"] >= vmdisk]
733+
686734
if len(flavor) > 1:
687735
flavor.sort(key=lambda x: x[3])
688736
flavor = flavor[0][0]
737+
msg = "There more than one flaors for select,so random select disk most mininal: %s" % flavor
738+
logfile.info(msg)
739+
689740
elif len(flavor) == 0:
741+
logfile.info("There no suitable flavor for specify")
742+
msg = "Try create the flavor: mem:: %s, cpu:: %s, disk:: %s" % (vmmem, vmcpu, vmdisk)
743+
logfile.info(msg)
690744
flavor = op.creatFlavor(vmmem, vmcpu, vmdisk)
691745
else:
746+
msg = "only one flavor for select: %s" % flavor
747+
logfile.info(msg)
692748
flavor = flavor[0][0]
693749

750+
if flavor:
751+
logfile.info("Flavor selected succuess")
752+
else:
753+
logfile.critical("Flavor selected failed")
754+
logfile.critical("Exit.....")
755+
continue
756+
694757
instance = "-".join([vmowner, vmname])
758+
msg = "Try to create the server: instanceName:: %s imgID:: %s network:: %s" %(instance, imgID, network)
695759
server = op.boot(instance, imgID, flavor, network)
760+
msg = "The server boot action status: %s" % server.status_code
761+
logfile.info(msg)
696762

697763
if server:
698764
if server.status_code == 202:
@@ -707,6 +773,7 @@ def upload():
707773
continue
708774

709775
serverID = server["server"]["id"]
776+
msg = "Try to add floating ip to the instance: %s" % vmip
710777
floatingip = op.addFlotingIP(serverID, vmip)
711778

712779
if floatingip:
@@ -720,6 +787,7 @@ def upload():
720787
logfile.critical("Add floationgip failed")
721788
shex.writesuffix(logfile, vmname, ".wrong")
722789

790+
logfile.info("UPload process done......")
723791

724792
def batch(action, size, *args):
725793
"""batch run the action for higher performance"""
@@ -903,5 +971,6 @@ def main():
903971

904972
if __name__ == "__main__":
905973
#main()
906-
testdownload()
974+
#testdownload()
975+
testupload()
907976
#testvmware()

0 commit comments

Comments
 (0)