mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
[4.20] VR: fix issue if userdata is binary data (#8739)
This commit is contained in:
parent
817251f1f8
commit
1662af131e
@ -630,31 +630,32 @@ class CsVmMetadata(CsDataBag):
|
||||
if os.path.exists(datafile):
|
||||
os.remove(datafile)
|
||||
|
||||
def __writefile(self, dest, data, mode):
|
||||
fh = open(dest, mode)
|
||||
self.__exflock(fh)
|
||||
fh.write(data)
|
||||
self.__unflock(fh)
|
||||
fh.close()
|
||||
os.chmod(dest, 0o644)
|
||||
|
||||
def __createfile(self, ip, folder, file, data):
|
||||
dest = "/var/www/html/" + folder + "/" + ip + "/" + file
|
||||
metamanifestdir = "/var/www/html/" + folder + "/" + ip
|
||||
metamanifest = metamanifestdir + "/meta-data"
|
||||
|
||||
# base64 decode userdata
|
||||
if folder == "userdata" or folder == "user-data":
|
||||
if data is not None:
|
||||
if data is not None:
|
||||
# base64 decode userdata
|
||||
if folder == "userdata" or folder == "user-data":
|
||||
# need to pad data if it is not valid base 64
|
||||
if len(data) % 4 != 0:
|
||||
data += (4 - (len(data) % 4)) * "="
|
||||
data = base64.b64decode(data)
|
||||
|
||||
fh = open(dest, "w")
|
||||
self.__exflock(fh)
|
||||
if data is not None:
|
||||
if isinstance(data, str):
|
||||
fh.write(data)
|
||||
self.__writefile(dest, data, "w")
|
||||
elif isinstance(data, bytes):
|
||||
fh.write(data.decode())
|
||||
self.__writefile(dest, data, "wb")
|
||||
else:
|
||||
fh.write("")
|
||||
self.__unflock(fh)
|
||||
fh.close()
|
||||
os.chmod(dest, 0o644)
|
||||
self.__writefile(dest, "", "w")
|
||||
|
||||
if folder == "metadata" or folder == "meta-data":
|
||||
try:
|
||||
|
||||
@ -34,7 +34,7 @@ import threading
|
||||
import urllib.parse
|
||||
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from socketserver import ThreadingMixIn #, ForkingMixIn
|
||||
from socketserver import ThreadingMixIn
|
||||
|
||||
|
||||
passMap = {}
|
||||
|
||||
@ -87,14 +87,16 @@ install_package() {
|
||||
if [ "$os" == "" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
local package=${package_properties["package_name"]}
|
||||
local file=${package_properties["file_name"]}
|
||||
|
||||
local DEBIAN_RELEASE=$(lsb_release -rs)
|
||||
if [ "$os" != "$DEBIAN_RELEASE" ]; then
|
||||
log_it "Skipped the installation of package $package on Debian $DEBIAN_RELEASE as it can only be installed on Debian $os."
|
||||
return
|
||||
fi
|
||||
|
||||
local package=${package_properties["package_name"]}
|
||||
local file=${package_properties["file_name"]}
|
||||
if [ -z "$package" ] || [ -z "$file" ]; then
|
||||
log_it "Skipped the installation due to empty package of file name (package name: $package, file name: $file)."
|
||||
return
|
||||
|
||||
@ -81,28 +81,33 @@ def deletefile(ip, folder, file):
|
||||
os.remove(datafile)
|
||||
|
||||
|
||||
def writefile(dest, data, mode):
|
||||
fh = open(dest, mode)
|
||||
exflock(fh)
|
||||
fh.write(data)
|
||||
unflock(fh)
|
||||
fh.close()
|
||||
os.chmod(dest, 0o644)
|
||||
|
||||
|
||||
def createfile(ip, folder, file, data):
|
||||
dest = "/var/www/html/" + folder + "/" + ip + "/" + file
|
||||
metamanifestdir = "/var/www/html/" + folder + "/" + ip
|
||||
metamanifest = metamanifestdir + "/meta-data"
|
||||
|
||||
# base64 decode userdata
|
||||
if folder == "userdata" or folder == "user-data":
|
||||
if data is not None:
|
||||
data = base64.b64decode(data)
|
||||
|
||||
fh = open(dest, "w")
|
||||
exflock(fh)
|
||||
if data is not None:
|
||||
# base64 decode userdata
|
||||
if folder == "userdata" or folder == "user-data":
|
||||
# need to pad data if it is not valid base 64
|
||||
if len(data) % 4 != 0:
|
||||
data += (4 - (len(data) % 4)) * "="
|
||||
data = base64.b64decode(data)
|
||||
if isinstance(data, str):
|
||||
fh.write(data)
|
||||
writefile(dest, data, "w")
|
||||
elif isinstance(data, bytes):
|
||||
fh.write(data.decode())
|
||||
writefile(dest, data, "wb")
|
||||
else:
|
||||
fh.write("")
|
||||
unflock(fh)
|
||||
fh.close()
|
||||
os.chmod(dest, 0o644)
|
||||
writefile(dest, "", "w")
|
||||
|
||||
if folder == "metadata" or folder == "meta-data":
|
||||
try:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user