Skip to content

Instantly share code, notes, and snippets.

@mwhahaha
Created April 8, 2021 21:45
Show Gist options
  • Save mwhahaha/5d4e36397b518bff0057d63d56bc46ac to your computer and use it in GitHub Desktop.
Save mwhahaha/5d4e36397b518bff0057d63d56bc46ac to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import etcd3gw
import platform
import shutil
import subprocess
import urllib.parse
class Facter2Etcd:
_exe = None
_instance = None
def __init__(self):
raise RuntimeError("Use instance()")
@classmethod
def instance(cls):
if cls._instance is None:
cls._instance = cls.__new__(cls)
return cls._instance
@property
def exe(self) -> str:
if not self._exe:
self._exe = shutil.which("facter")
if not self._exe:
raise RuntimeError("facter is not available")
return self._exe
def run(self, host, port, proto, base_key):
cmd = [self.exe, '-j']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
out, err = proc.communicate()
rc = proc.returncode
if rc != 0:
raise RuntimeError(f"Failure running facter: {err}")
k = "/".join([base_key, 'facter'])
c = etcd3gw.client(host=host, port=port, protocol=proto)
if not c.put(k, out):
raise RuntimeError(f"Uploading data to {k} failed.")
print(f"Success! Data uploaded to {k}")
if __name__ == "__main__":
obj = Facter2Etcd.instance()
parser = argparse.ArgumentParser(
description="Utility to push facter facts to etcd")
parser.add_argument('--etcd-uri', default='http://127.0.0.1:2379', help='URI to the etcd gateway')
parser.add_argument('--key-prefix', default=('/' + platform.uname()[1]), help='etcd key prefix')
args = parser.parse_args()
uri = urllib.parse.urlparse(args.etcd_uri)
obj.run(uri.hostname, uri.port, uri.scheme, args.key_prefix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment