salt icon indicating copy to clipboard operation
salt copied to clipboard

grains.mem_total is returned as str on FreeBSD

Open m87carlson opened this issue 13 years ago • 5 comments

For some reason, mem_total is returned as a str:

    elif osdata['kernel'] in ('FreeBSD', 'OpenBSD', 'NetBSD', 'Darwin'):
        sysctl = salt.utils.which('sysctl')
        if sysctl:
            if osdata['kernel'] == 'Darwin':
                mem = __salt__['cmd.run']('{0} -n hw.memsize'.format(sysctl))
            else:
                mem = __salt__['cmd.run']('{0} -n hw.physmem'.format(sysctl))
            if (osdata['kernel'] == 'NetBSD' and mem.startswith('-')):
                mem = __salt__['cmd.run']('{0} -n hw.physmem64'.format(sysctl))
            grains['mem_total'] = str(int(mem) / 1024 / 1024)

so I'm unable to perform the following math inside of a template:

maintenance_work_mem = {{ grains.mem_total / 16 }}MB                             

Because salt returns with the following error:

----------
    State: - file
    Name:      /data/db/postgresql/postgresql.conf
    Function:  managed
        Result:    False
        Comment:   Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/salt/utils/templates.py", line 63, in render_tmpl
    output = render_str(tmplstr, context, tmplpath)
  File "/usr/local/lib/python2.7/site-packages/salt/utils/templates.py", line 116, in render_jinja_tmpl
    output = jinja_env.from_string(tmplstr).render(**context)
  File "/usr/local/lib/python2.7/site-packages/Jinja2-2.7.1-py2.7.egg/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python2.7/site-packages/Jinja2-2.7.1-py2.7.egg/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 37, in top-level template code
TypeError: unsupported operand type(s) for /: 'str' and 'int'

m87carlson avatar Sep 06 '13 20:09 m87carlson

Do you by any chance know if this is different from what other distros are doing? Is this FreeBSD specific?

In any case, thanks for the report. Should be very easy to fix.

basepi avatar Sep 06 '13 22:09 basepi

In fact, we always love pull requests. ;)

basepi avatar Sep 06 '13 22:09 basepi

I'd be happy to do a pull request :)

FreeBSD isn't doing anything different, the sysctl returns a number:

sysctl hw.physmem

hw.physmem: 4274274304

On Fri, Sep 6, 2013 at 3:41 PM, Colton Myers [email protected]:

In fact, we always love pull requests. ;)

— Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/7104#issuecomment-23973983 .

m87carlson avatar Sep 06 '13 23:09 m87carlson

alright, see pull #7108

On Fri, Sep 6, 2013 at 4:17 PM, Mike Carlson [email protected] wrote:

I'd be happy to do a pull request :)

FreeBSD isn't doing anything different, the sysctl returns a number:

sysctl hw.physmem

hw.physmem: 4274274304

On Fri, Sep 6, 2013 at 3:41 PM, Colton Myers [email protected]:

In fact, we always love pull requests. ;)

— Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/7104#issuecomment-23973983 .

m87carlson avatar Sep 06 '13 23:09 m87carlson

@m87carlson Merged, good catch, and thanks for the pull req!

terminalmage avatar Sep 08 '13 02:09 terminalmage