From 2df0245fbfff3a6a3355c3c6184898067518bcd6 Mon Sep 17 00:00:00 2001 From: Veselin Bochev Date: Tue, 7 Jun 2022 12:46:45 +0300 Subject: [PATCH] fix memory sizes, proper conversion from human readable to rounded int in GB format --- bin/pre-terraform | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bin/pre-terraform b/bin/pre-terraform index 6efbeca..8093648 100755 --- a/bin/pre-terraform +++ b/bin/pre-terraform @@ -28,6 +28,17 @@ from urllib.parse import quote_plus import json import yaml +# We need to convert human readable sizes to INT in GB +# we always round to 1GB (if puppet shows less than that) +# we should always check with client documentation and make +# sure proper sizes are set manually if incorrect +# this is to make sure we do not have that much false positives +UNITS = { "KiB": 1048576, "MiB": 1024, "GiB": 1} +def readHumanReadable(filesize): + for key in UNITS.keys(): + if key.lower() in filesize.lower(): + return str(math.ceil(float(filesize.split(" ")[0]) / float(UNITS[key]))) + # This is for debugging purposes, it helps track the HTTP requests' # input and ouput and detect errors along the way. Better leave it here # for debuggin purposes. The script will later be fixed so that debug mode @@ -133,7 +144,8 @@ sample_pre_tf = { } try: - print(f"OS according to facts: {data['os']['value']['name']} {data['os']['value']['release']['full']}\n") + print(f"OS according to facts: {data['os']['value']['name']} {data['os']['value']['release']['full']}") + print(f"Memory according to facts: {data['memorysize']['value']}\n") # Options resolv.conf try: @@ -197,7 +209,7 @@ try: sample_pre_tf["defaults"]["num_cpus"] = data["processorcount"]["value"] # Memory - sample_pre_tf["defaults"]["memory"] = str(math.ceil(float(data["memorysize"]["value"].split(" ")[0]))) + "GB" + sample_pre_tf["defaults"]["memory"] = readHumanReadable(data["memorysize"]["value"]) + "GB" # Customer sample_pre_tf["defaults"]["puppet"]["customer"] = data["customer"]["value"] -- 2.39.5