]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
added pre-terraform script to the list of binaries
authorVeselin Bochev <veselin.bochev@WKMZT0869FF7.global.publicisgroupe.net>
Wed, 1 Jun 2022 08:21:51 +0000 (11:21 +0300)
committerVeselin Bochev <veselin.bochev@WKMZT0869FF7.global.publicisgroupe.net>
Wed, 1 Jun 2022 08:21:51 +0000 (11:21 +0300)
bin/pre-terraform [new file with mode: 0755]
setup.py

diff --git a/bin/pre-terraform b/bin/pre-terraform
new file mode 100755 (executable)
index 0000000..786ebc6
--- /dev/null
@@ -0,0 +1,185 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+
+import sys
+
+if sys.version_info[0] != 3:
+    print("This script is intended to use with Python3.", file=sys.stderr)
+    print("You are using Python: {0}.{1}.{2}-{3}-{4}.\n".format(
+        *sys.version_info), file=sys.stderr)
+    sys.exit(1)
+
+if sys.version_info[1] < 4:
+    print("A minimal Python version of 3.4 is necessary to execute this script.", file=sys.stderr)
+    print("You are using Python: {0}.{1}.{2}-{3}-{4}.\n".format(
+        *sys.version_info), file=sys.stderr)
+    sys.exit(1)
+
+import math
+from os import _exit
+from urllib.request import urlopen,Request
+from urllib.parse import quote_plus
+
+import json
+import yaml
+
+
+# http_handler = urllib.request.HTTPHandler(debuglevel=1)
+
+# try:
+#   import ssl
+#   https_handler = urllib.request.HTTPSHandler(debuglevel=1)
+#   opener = urllib.request.build_opener(http_handler, https_handler)
+# except ImportError:
+#   opener = urllib.request.build_opener(http_handler)
+# 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:
+    # print("A minimum of one argument is needed to proceed.", file=sys.stderr)
+    print('usage: %s <hostname>' % sys.argv[0])
+    print()
+    print('Important: This is used to generate YAML file for the terraform `create-terraform`')
+    print('           script and be able to generate artifcats. Only used for hosts which are')
+    print('           already in the PuppetDB.')
+    print()
+    print('           Fields consisting of `__` should be manually checked and corrected.')
+    print('           Always double check all data before proceeding further.')
+    _exit(1)
+
+req = Request("%s?query=%s" % (url, quote_plus(data, encoding="ascii")), method='GET')
+req.add_header('Content-Type', 'application/x-www-form-urlencoded')
+req.add_header('Accept', '*/*')
+
+try:
+    res = urlopen(req)
+    facts = json.loads(res.read())
+except:
+    print("ERROR: There was an error handling request, please check both Data and URL.")
+    _exit(1)
+
+
+# All fields necessary (at present time) to fill
+# yaml files
+fields = [
+    "fqdn",
+    "processorcount",
+    "memorysize",
+    "disks",
+    "resolv_conf",
+    "customer",
+    "pp_owner",
+    "os",
+    "partitions",
+    "tier",
+    "pp_ip_cidr",
+    "ipaddress",
+    "pp_purpose"
+    ]
+
+data = {}
+
+for fact in facts:
+    if fact["name"] in fields:
+        data[fact["name"]] = fact
+
+# Sample yaml file infrastructure
+sample_pre_tf = {
+    "defaults": {
+        "vsphere": "live",
+        "cluster": "vmcc-l105-01",
+        "vm_folder": "Pixelpark/__",
+        "num_cpus": 0,
+        "memory": "__",
+        "boot_delay": 5,
+        "template": "__",
+        "customer": "__",
+        "purpose": "__",
+        "datastore_cluster": "ds-cluster-hdd-vmcc-l105-01",
+        "datastore_type": "sata",
+        "root_disk": { "size": 0 },
+        "data_disks": [],
+        "puppet": {
+            "customer": "__",
+            "project": "__",
+            "role": "default",
+            "tier": "__",
+            "environment": "__"
+            },
+        "namservers": [],
+        "searchdomains": [],
+        "dns_options": "__"
+    },
+    "vms": [
+        {
+            "name": "__",
+            "interface": []
+        }
+    ]
+}
+
+try:
+    # Nameservers resolv.conf
+    for ns in data["resolv_conf"]["value"]["nameserver"]:
+        sample_pre_tf["defaults"]["namservers"].append(ns)
+
+    # Searchdomains resolv.conf
+    for sd in data["resolv_conf"]["value"]["search"]:
+        sample_pre_tf["defaults"]["searchdomains"].append(sd)
+
+    # Disks root
+    sample_pre_tf["defaults"]["root_disk"]["size"] = int(data["disks"]["value"]["sda"]["size_bytes"]/int(1<<30))
+    # additional disks
+    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])
+                }
+            )
+
+
+    # Interface IPs
+    for iface in data["pp_ip_cidr"]["value"]:
+        if iface != "lo":
+            sample_pre_tf["vms"][0]["interface"].append(
+                {
+                    "address_v4": data["pp_ip_cidr"]["value"][iface][0].split("/")[0]
+                }
+            )
+
+    # VM Name
+    sample_pre_tf["vms"][0]["name"] = data["fqdn"]["value"]
+
+    # Processor Count
+    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"
+
+    # Customer
+    sample_pre_tf["defaults"]["puppet"]["customer"] = data["customer"]["value"]
+    sample_pre_tf["defaults"]["customer"] = data["pp_owner"]["value"]
+
+    # Project
+    sample_pre_tf["defaults"]["puppet"]["project"] = data["pp_owner"]["value"]
+
+    # Project
+    sample_pre_tf["defaults"]["purpose"] = data["pp_purpose"]["value"]
+
+    # Environment
+    sample_pre_tf["defaults"]["puppet"]["tier"] = data["tier"]["value"]
+    sample_pre_tf["defaults"]["puppet"]["environment"] = data["tier"]["value"]
+except:
+    print("ERROR: Failed parsing required fields from output. Please check if hostname is in PuppetDB")
+    _exit(1)
+
+print(yaml.dump(sample_pre_tf))
+
+__author__ = 'Veselin Bochev <veselin.bochev@pixelpark.com>'
+__copyright__ = '(C) 2022 by Veselin Bochev, Pixelpark GmbH, Berlin'
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 
index b05f62786b20c2b7b400cc76a9b0dde73fc39bc0..fcc5ef6b0accb793995be36f0dbd4f87fae0d1e8 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -134,7 +134,7 @@ read_requirements()
 
 # -----------------------------------
 __scripts__ = [
-    'bin/create-terraform'
+    'bin/create-terraform', 'bin/pre-terraform'
 ]
 
 # -----------------------------------