Skip to content

Commit

Permalink
VR: fix issue if userdata is binary data
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhouapache committed Mar 4, 2024
1 parent 240db19 commit 8d354b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
27 changes: 14 additions & 13 deletions systemvm/debian/opt/cloud/bin/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,31 +630,32 @@ def __deletefile(self, ip, folder, file):
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:
Expand Down
31 changes: 18 additions & 13 deletions systemvm/debian/opt/cloud/bin/vmdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8d354b3

Please sign in to comment.