From 474d79f81fd6b068c741cfa21e1bcf8a95393fb2 Mon Sep 17 00:00:00 2001 From: Veselin Bochev Date: Wed, 1 Jun 2022 11:21:51 +0300 Subject: [PATCH] added pre-terraform script to the list of binaries --- bin/pre-terraform | 185 ++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100755 bin/pre-terraform diff --git a/bin/pre-terraform b/bin/pre-terraform new file mode 100755 index 0000000..786ebc6 --- /dev/null +++ b/bin/pre-terraform @@ -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 ' % 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 ' +__copyright__ = '(C) 2022 by Veselin Bochev, Pixelpark GmbH, Berlin' + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/setup.py b/setup.py index b05f627..fcc5ef6 100644 --- a/setup.py +++ b/setup.py @@ -134,7 +134,7 @@ read_requirements() # ----------------------------------- __scripts__ = [ - 'bin/create-terraform' + 'bin/create-terraform', 'bin/pre-terraform' ] # ----------------------------------- -- 2.39.5