]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
fix memory sizes, proper conversion from human readable to rounded int in GB format
authorVeselin Bochev <veselin.bochev@WKMZT0869FF7.global.publicisgroupe.net>
Tue, 7 Jun 2022 09:46:45 +0000 (12:46 +0300)
committerVeselin Bochev <veselin.bochev@WKMZT0869FF7.global.publicisgroupe.net>
Tue, 7 Jun 2022 09:46:45 +0000 (12:46 +0300)
bin/pre-terraform

index 6efbecab6382b15dda127982126d9a2d7916c4c2..8093648b1fb70c39b83ef6948e0f61f6a6d605f4 100755 (executable)
@@ -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"]