1- #!/usr/bin/python
1+ #!/usr/bin/python3
22# -*- coding: utf-8 -*-
33# Licensed to the Apache Software Foundation (ASF) under one
44# or more contributor license agreements. See the NOTICE file
1919
2020import argparse
2121import sys
22- import urllib
22+ import urllib . request , urllib . parse , urllib . error
2323import uuid
2424import subprocess
2525import os
@@ -90,7 +90,7 @@ def populateOptions(self):
9090 self .databaseuserpassword = ""
9191 if self .args .templatesuffix :
9292 self .templatesuffix = self .args .templatesuffix
93- print 'Password for DB: %s' % self .databaseuserpassword
93+ print ( 'Password for DB: %s' % self .databaseuserpassword )
9494
9595 def errorAndExit (self , msg ):
9696 err = '''\n \n We apologize for below error:
@@ -117,11 +117,11 @@ def runCmd(self, cmds):
117117
118118 def runMysql (self , query ):
119119 try :
120- print 'Running Query: %s' % query
120+ print ( 'Running Query: %s' % query )
121121 mysqlCmds = ['mysql' , '--user=%s' % self .databaseusername , '--host=%s' % self .databasehostname , '--password=%s' % self .databaseuserpassword , '--skip-column-names' , '-U' , 'cloud' , '-e "%s"' % query ]
122122 templateId = self .runCmd (mysqlCmds )
123- print 'TemplateId is : %s' % templateId
124- except Exception , e :
123+ print ( 'TemplateId is : %s' % templateId )
124+ except Exception as e :
125125 err = '''Encountering an error when executing mysql script\n %s''' % str (e )
126126 self .errorAndExit (err )
127127 return templateId
@@ -137,9 +137,9 @@ def fetchTemplateDetails(self):
137137
138138 def downloadTemplate (self ):
139139 self .systemvmtemplatepath = self .templateName + "." + self .fileextension
140- print 'Downloading template from %s To %s' % (self .systemvmtemplateurl , self .systemvmtemplatepath )
140+ print ( 'Downloading template from %s To %s' % (self .systemvmtemplateurl , self .systemvmtemplatepath ) )
141141 try :
142- templateFileDownloadUrl = urllib .urlretrieve (self .systemvmtemplateurl , self .systemvmtemplatepath , reporthook = self .report )
142+ templateFileDownloadUrl = urllib .request . urlretrieve (self .systemvmtemplateurl , self .systemvmtemplatepath , reporthook = self .report )
143143 except Exception :
144144 self .errorAndExit ("Failed to download template file from %s" % self .systemvmtemplateurl )
145145
@@ -150,23 +150,23 @@ def report(tmp, blocknr, blocksize, size):
150150 def installTemplate (self ):
151151 destDir = self .mountpoint + os .sep + "template" + os .sep + "tmpl" + os .sep + "1" + os .sep + str (self .template )
152152 self .destDir = destDir
153- print 'The desination Directory is : %s' % destDir
153+ print ( 'The desination Directory is : %s' % destDir )
154154 try :
155155 if self .forcecleanup :
156156 if os .path .exists (destDir ):
157157 shutil .rmtree (destDir )
158158 if not os .path .exists (destDir ):
159159 os .makedirs (destDir )
160- except Exception , e :
160+ except Exception as e :
161161 self .errorAndExit ('Failed to create directories on the mounted path.. %s' % str (e ))
162- print 'Installing Template to : %s' % destDir
162+ print ( 'Installing Template to : %s' % destDir )
163163 tmpFile = self .templateName + "." + "tmp"
164164 self .uncompressFile (tmpFile )
165- print 'Moving the decompressed file to destination directory %s... which could take a long time, please wait' % destDir
165+ print ( 'Moving the decompressed file to destination directory %s... which could take a long time, please wait' % destDir )
166166 shutil .move (tmpFile , destDir + os .sep + self .templateName )
167167
168168 def uncompressFile (self , fileName ):
169- print 'Uncompressing the file %s... which could take a long time, please wait' % self .systemvmtemplatepath
169+ print ( 'Uncompressing the file %s... which could take a long time, please wait' % self .systemvmtemplatepath )
170170 if self .fileextension == 'gz' :
171171 compressedFile = gzip .GzipFile (self .systemvmtemplatepath , 'rb' )
172172 decompressedData = compressedFile .read ()
@@ -181,7 +181,7 @@ def uncompressFile(self, fileName):
181181 decompressedFile = file (fileName , 'wb' )
182182 decompressedFile .write (decompressedData )
183183 decompressedFile .close ()
184- print ''
184+ print ( '' )
185185 elif self .fileextension == 'zip' :
186186 zippedFile = zipfile .ZipFile (self .systemvmtemplatepath , 'r' )
187187 zippedFiles = zippedFile .namelist ()
@@ -191,7 +191,7 @@ def uncompressFile(self, fileName):
191191 decompressedFile .write (decompressedData )
192192 decompressedFile .close ()
193193 zippedFile .close ()
194- print ''
194+ print ( '' )
195195 else :
196196 self .errorAndExit ('Not supported file type %s to decompress' % self .fileextension )
197197 self .fileSize = os .path .getsize (fileName )
@@ -226,11 +226,11 @@ def run(self):
226226 self .installTemplate ()
227227 self .writeProperties ()
228228 finally :
229- print ''
230- print ''
231- print "CloudStack has successfully installed system template"
232- print ''
229+ print ( '' )
230+ print ( '' )
231+ print ( "CloudStack has successfully installed system template" )
232+ print ( '' )
233233
234234if __name__ == "__main__" :
235235 o = InstallSysTemplate ()
236- o .run ()
236+ o .run ()
0 commit comments