# Buggy code
def _bsd_cpudata(osdata):
    """
    Return CPU information for BSD-like systems
    """
    # Provides:
    #   cpuarch
    #   num_cpus
    #   cpu_model
    #   cpu_flags
    sysctl = salt.utils.which("sysctl")
    arch = salt.utils.which("arch")
    cmds = {}

    if sysctl:
        cmds.update(
            {
                "num_cpus": "{0} -n hw.ncpu".format(sysctl),
                "cpuarch": "{0} -n hw.machine".format(sysctl),
                "cpu_model": "{0} -n hw.model".format(sysctl),
            }
        )

    if arch and osdata["kernel"] == "OpenBSD":
        cmds["cpuarch"] = "{0} -s".format(arch)

    if osdata["kernel"] == "Darwin":
        cmds["cpu_model"] = "{0} -n machdep.cpu.brand_string".format(sysctl)
        cmds["cpu_flags"] = "{0} -n machdep.cpu.features".format(sysctl)

    grains = dict([(k, __salt__["cmd.run"](v)) for k, v in cmds.items()])

    if grains["cpu_flags"] and not isinstance(grains["cpu_flags"], list):
        grains["cpu_flags"] = grains["cpu_flags"].split(" ")

    if osdata["kernel"] == "NetBSD":
        grains["cpu_flags"] = []
        for line in __salt__["cmd.run"]("cpuctl identify 0").splitlines():
            m = re.match(r"cpu[0-9]:\ features[0-9]?\ .+<(.+)>", line)
            if m:
                flag = m.group(1).split(",")
                grains["cpu_flags"].extend(flag)

    if osdata["kernel"] == "FreeBSD" and os.path.isfile("/var/run/dmesg.boot"):
        grains["cpu_flags"] = []
        # TODO: at least it needs to be tested for BSD other then FreeBSD
        with salt.utils.fopen("/var/run/dmesg.boot", "r") as _fp:
            cpu_here = False
            for line in _fp:
                if line.startswith("CPU: "):
                    cpu_here = True  # starts CPU descr
                    continue
                if cpu_here:
                    if not line.startswith(" "):
                        break  # game over
                    if "Features" in line:
                        start = line.find("<")
                        end = line.find(">")
                        if start > 0 and end > 0:
                            flag = line[start + 1 : end].split(",")
                            grains["cpu_flags"].extend(flag)
    try:
        grains["num_cpus"] = int(grains["num_cpus"])
    except ValueError:
        grains["num_cpus"] = 1

    return grains


# Traceback (KeyError)
[root@starbuck /usr/ports/sysutils/py-salt]# service salt_master start
Starting salt_master.
[root@starbuck /usr/ports/sysutils/py-salt]#
Process Process-1:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap self.run()
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 170, in _clear_old_jobs
search = salt.search.Search(self.opts)
File "/usr/local/lib/python2.7/site-packages/salt/search/__init__.py", line 90, in __init__
matcher=False)
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 250, in __init__
self.gen_modules()
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 258, in gen_modules
whitelist=self.whitelist)
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 101, in minion_mods
whitelist=whitelist
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 675, in gen_functions
mod.__init__(self.opts)
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 56, in __init__
if __virtual__():
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 45, in __virtual__
if __grains__['os_family'] != 'Debian':
KeyError: 'os_family'
Process MWorker-4:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 655, in run
self.crypticle)
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 1419, in __init__
rend=False)
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 250, in __init__
self.gen_modules()
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 258, in gen_modules
whitelist=self.whitelist)
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 101, in minion_mods
whitelist=whitelist
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 675, in gen_functions
mod.__init__(self.opts)
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 56, in __init__
if __virtual__():
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 45, in __virtual__
if __grains__['os_family'] != 'Debian':
KeyError: 'os_family'
Process MWorker-5:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 655, in run
self.crypticle)
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 1419, in __init__
rend=False)
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 250, in __init__
self.gen_modules()
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 258, in gen_modules
whitelist=self.whitelist)
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 101, in minion_mods
whitelist=whitelist
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 675, in gen_functions
mod.__init__(self.opts)
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 56, in __init__
if __virtual__():
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 45, in __virtual__
if __grains__['os_family'] != 'Debian':
KeyError: 'os_family'
Process MWorker-8:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 655, in run
self.crypticle)
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 1419, in __init__
rend=False)
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 250, in __init__
self.gen_modules()
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 258, in gen_modules
whitelist=self.whitelist)
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 101, in minion_mods
whitelist=whitelist
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 675, in gen_functions
mod.__init__(self.opts)
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 56, in __init__
if __virtual__():
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 45, in __virtual__
if __grains__['os_family'] != 'Debian':
KeyError: 'os_family'
Process MWorker-7:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 655, in run
self.crypticle)
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 1419, in __init__
rend=False)
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 250, in __init__
self.gen_modules()
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 258, in gen_modules
whitelist=self.whitelist)
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 101, in minion_mods
whitelist=whitelist
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 675, in gen_functions
mod.__init__(self.opts)
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 56, in __init__
if __virtual__():
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 45, in __virtual__
if __grains__['os_family'] != 'Debian':
KeyError: 'os_family'
Process MWorker-6:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 655, in run
self.crypticle)
File "/usr/local/lib/python2.7/site-packages/salt/master.py", line 1419, in __init__
rend=False)
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 250, in __init__
self.gen_modules()
File "/usr/local/lib/python2.7/site-packages/salt/minion.py", line 258, in gen_modules
whitelist=self.whitelist)
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 101, in minion_mods
whitelist=whitelist
File "/usr/local/lib/python2.7/site-packages/salt/loader.py", line 675, in gen_functions
mod.__init__(self.opts)
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 56, in __init__
if __virtual__():
File "/usr/local/lib/python2.7/site-packages/salt/modules/apt.py", line 45, in __virtual__
if __grains__['os_family'] != 'Debian':
KeyError: 'os_family'

# Fixed code
def _bsd_cpudata(osdata):
    """
    Return CPU information for BSD-like systems
    """
    # Provides:
    #   cpuarch
    #   num_cpus
    #   cpu_model
    #   cpu_flags
    sysctl = salt.utils.which("sysctl")
    arch = salt.utils.which("arch")
    cmds = {}

    if sysctl:
        cmds.update(
            {
                "num_cpus": "{0} -n hw.ncpu".format(sysctl),
                "cpuarch": "{0} -n hw.machine".format(sysctl),
                "cpu_model": "{0} -n hw.model".format(sysctl),
            }
        )

    if arch and osdata["kernel"] == "OpenBSD":
        cmds["cpuarch"] = "{0} -s".format(arch)

    if osdata["kernel"] == "Darwin":
        cmds["cpu_model"] = "{0} -n machdep.cpu.brand_string".format(sysctl)
        cmds["cpu_flags"] = "{0} -n machdep.cpu.features".format(sysctl)

    grains = dict([(k, __salt__["cmd.run"](v)) for k, v in cmds.items()])

    if "cpu_flags" in grains and not isinstance(grains["cpu_flags"], list):
        grains["cpu_flags"] = grains["cpu_flags"].split(" ")

    if osdata["kernel"] == "NetBSD":
        grains["cpu_flags"] = []
        for line in __salt__["cmd.run"]("cpuctl identify 0").splitlines():
            m = re.match(r"cpu[0-9]:\ features[0-9]?\ .+<(.+)>", line)
            if m:
                flag = m.group(1).split(",")
                grains["cpu_flags"].extend(flag)

    if osdata["kernel"] == "FreeBSD" and os.path.isfile("/var/run/dmesg.boot"):
        grains["cpu_flags"] = []
        # TODO: at least it needs to be tested for BSD other then FreeBSD
        with salt.utils.fopen("/var/run/dmesg.boot", "r") as _fp:
            cpu_here = False
            for line in _fp:
                if line.startswith("CPU: "):
                    cpu_here = True  # starts CPU descr
                    continue
                if cpu_here:
                    if not line.startswith(" "):
                        break  # game over
                    if "Features" in line:
                        start = line.find("<")
                        end = line.find(">")
                        if start > 0 and end > 0:
                            flag = line[start + 1 : end].split(",")
                            grains["cpu_flags"].extend(flag)
    try:
        grains["num_cpus"] = int(grains["num_cpus"])
    except ValueError:
        grains["num_cpus"] = 1

    return grains