]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
minor optimizations, error fixes, support for Solaris servers, OS info collection...
authorVeselin Bochev <veselin.bochev@WKMZT0869FF7.global.publicisgroupe.net>
Wed, 1 Jun 2022 14:17:17 +0000 (17:17 +0300)
committerVeselin Bochev <veselin.bochev@WKMZT0869FF7.global.publicisgroupe.net>
Wed, 1 Jun 2022 14:17:17 +0000 (17:17 +0300)
bin/pre-terraform

index 1f3aae382e4c4d8e99f3bcf3232721139c55c909..691cc48d5e486eae6708e7a7231fc0114aeba0be 100755 (executable)
@@ -1,5 +1,7 @@
 #!/usr/bin/env python
-
+# Version checking copied from `create-terraform` script
+# written by Frank B.
+#
 from __future__ import print_function
 
 import sys
@@ -16,6 +18,8 @@ if sys.version_info[1] < 4:
         *sys.version_info), file=sys.stderr)
     sys.exit(1)
 
+# Actual `pre-terraform` code begins here
+#
 import math
 from os import _exit
 from urllib.request import urlopen,Request
@@ -24,9 +28,12 @@ from urllib.parse import quote_plus
 import json
 import yaml
 
-
+# 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
+# can be enabled on demand.
+#
 # http_handler = urllib.request.HTTPHandler(debuglevel=1)
-
 # try:
 #   import ssl
 #   https_handler = urllib.request.HTTPSHandler(debuglevel=1)
@@ -36,6 +43,7 @@ import yaml
 # urllib.request.install_opener(opener)
 
 url = "https://puppetdb01.pixelpark.com/pdb/query/v4/facts"
+
 try:
     data = '["=", "certname", "%s"]' % (sys.argv[1]) # argv[1] is the hostname we ask information for from the PuppetDB
 except:
@@ -101,7 +109,7 @@ sample_pre_tf = {
         "customer": "__",
         "purpose": "__",
         "datastore_cluster": "ds-cluster-hdd-vmcc-l105-01       <<< Check this",
-        "datastore_type": "sata",
+        "datastore_type": "sata                                 <<< Check this",
         "root_disk": { "size": 0 },
         "data_disks": [],
         "puppet": {
@@ -145,16 +153,29 @@ try:
     sample_pre_tf["defaults"]["dns_options"] = f"timeout: {timeout} attempts: {attempts}"
 
     # Disks root
-    sample_pre_tf["defaults"]["root_disk"]["size"] = int(data["disks"]["value"]["sda"]["size_bytes"]/int(1<<30))
+    # Note: This is required because on Solaris setup is different than RedHat based distros
+    # and disks are numbered sd0, sd1, etc., also for some servers sd0 reports 0 size
+    try:
+        sample_pre_tf["defaults"]["root_disk"]["size"] = int(data["disks"]["value"]["sda"]["size_bytes"]/int(1<<30))
+    except:
+        sample_pre_tf["defaults"]["root_disk"]["size"] = "<<<<<<<<<<<<<<<<< Check this"
 
     # Disks additional
+    # Note: Same as above
     for disk in data["disks"]["value"]:
         if disk.startswith("sd") and disk != "sda":
-            sample_pre_tf["defaults"]["data_disks"].append(
-                {
-                    "size": int(data["disks"]["value"][disk]["size"].split(".")[0])
-                }
-            )
+            try:
+                sample_pre_tf["defaults"]["data_disks"].append(
+                    {
+                        "size": int(data["disks"]["value"][disk]["size"].split(".")[0])
+                    }
+                )
+            except:
+                sample_pre_tf["defaults"]["data_disks"].append(
+                    {
+                        "size": "<<<<<<<<<<<<<<<<< Check this"
+                    }
+                )
 
 
     # Interface IPs
@@ -188,8 +209,9 @@ try:
     # Environment
     sample_pre_tf["defaults"]["puppet"]["tier"] = data["tier"]["value"]
     sample_pre_tf["defaults"]["puppet"]["environment"] = data["tier"]["value"]
-except:
+except Exception as e:
     print("ERROR: Failed parsing required fields from output. Please check if hostname is in PuppetDB")
+    # print(f"ERROR: {e}")
     _exit(1)
 
 # Print the YAML configuration file
@@ -204,4 +226,4 @@ __copyright__ = '(C) 2022 by Veselin Bochev, Pixelpark GmbH, Berlin'
 # These are the facts - All
 # print(facts)
 
-# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 
\ No newline at end of file
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4