Created
January 22, 2018 06:39
-
-
Save arubdesu/2b21b9722035d00359e4ad491cf87b0d to your computer and use it in GitHub Desktop.
my fork
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/munki/simian/bin/python | |
# | |
# Copyright 2010 Google Inc. All Rights Reserved. | |
"""Script to output Simian dependent facter-like output of various variables.""" | |
import json | |
import os | |
import re | |
import subprocess | |
from simian import settings | |
def Exec(cmd): | |
"""Executes a process and returns exit code, stdout, stderr. | |
Args: | |
cmd: str or sequence, command and optional arguments to execute. | |
Returns: | |
Tuple. (Integer return code, string standard out, string standard error). | |
""" | |
if type(cmd) is str: | |
shell = True | |
else: | |
shell = False | |
try: | |
p = subprocess.Popen( | |
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell) | |
stdout, stderr = p.communicate() | |
return p.returncode, stdout, stderr | |
except (IOError, OSError), e: | |
return (99, '', str(e)) | |
def GetSerialNumber(): | |
"""Returns the machine serial number.""" | |
return_code, stdout, unused_stderr = Exec( | |
['system_profiler', 'SPHardwareDataType']) | |
serial_number = 'UNKNOWN_SERIAL_NUMBER' | |
if return_code != 0: | |
return serial_number | |
serial_number_re = re.compile('Serial number[^:]+:\s+(.*)', re.IGNORECASE) | |
for l in stdout.splitlines(): | |
m = serial_number_re.search(l) | |
if m: | |
serial_number = m.group(1) | |
break | |
return serial_number | |
def GetFacterFacts(): | |
"""Returns a dictionary of facter facts.""" | |
facter_paths = ('/usr/bin/facter', '/opt/puppetlabs/bin/puppet') | |
facter_path = None | |
for path in facter_paths: | |
if os.path.exists(path): | |
facter_path = path | |
break | |
if facter_path == '/usr/bin/facter': | |
return_code, stdout, unused_stderr = Exec([facter_path, '-p']) | |
else: | |
return_code, stdout, unused_stderr = Exec([facter_path, 'facts', '--render-as', 'json']) | |
# If execution of facter was successful build the client identifier | |
if return_code != 0: | |
return {} | |
facts = {} | |
# Convert facter output to a dictionary | |
if facter_path == '/usr/bin/facter': | |
lines = stdout.splitlines() | |
for line in lines: | |
(key, unused_sep, value) = line.split(' ', 2) | |
value = value.strip() | |
facts[key] = value | |
else: | |
pre_facts = json.loads(stdout) | |
facts = {} | |
pre_dict = pre_facts['values'] | |
for each in pre_dict.keys(): | |
if type(pre_dict[each]) != unicode and type(pre_dict[each]) != str: | |
continue | |
else: | |
facts[each] = pre_dict[each] | |
return facts | |
SETTINGS_FACTER = { | |
'certname': settings.CERTNAME or GetSerialNumber().lower(), | |
'primary_user': settings.PRIMARY_USER, | |
'sp_local_host_name': settings.HOSTNAME, | |
'configtrack': settings.CONFIGTRACK, | |
'simiantrack': settings.SIMIANTRACK, | |
'site': settings.SITE, | |
'applesus': settings.APPLESUS, | |
} | |
def main(): | |
facts = GetFacterFacts() | |
# place Simian configs where facter is lacking (may be 100%). | |
for name in SETTINGS_FACTER: | |
if name not in facts: | |
facts[name] = SETTINGS_FACTER[name] | |
print """certname => %(certname)s | |
primary_user => %(primary_user)s | |
sp_local_host_name => %(sp_local_host_name)s | |
configtrack => %(configtrack)s | |
simiantrack => %(simiantrack)s | |
site => %(site)s | |
applesus => %(applesus)s""" % facts | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment