salt module with a decorator

89 views
Skip to first unread message

Emile `iMil' Heitor

unread,
May 20, 2013, 5:29:14 PM5/20/13
to salt-...@googlegroups.com

Hi,

I am finalizing a module which needs, for pretty much every function,
to create a session, and logout that session when the job is done.
To achieve this, I used a simple decorator:

def _session_handle(function):
def _internal(*args, **kwargs):
_session = _get_session()
ret = function(_session.xenapi, *args, **kwargs)
_session.logout()
return ret
return _internal

Which is referred to like this:

@_session_handle
def vm_info(xapi, vm_=None):

While the module behaves correctly when it is called from `salt-call', it
fails with the following error when it's called from plain `salt':

$ sudo salt 'coruscant' virt.vm_info
coruscant:
Missing arguments executing "virt.vm_info": ArgSpec(args=[], varargs='args', keywords='kwargs', defaults=None)

First I thought it was related to the fact that the decorator added a session
parameter to the functions, so I made the session variable global, so the
decorator now looks like this:

def _session_handle(function):
def _internal(*args, **kwargs):
global xapi
_session = _get_session()
xapi = _session.xenapi
ret = function(*args, **kwargs)
_session.logout()
return ret
return _internal

Nevertheless, I am still stuck with the same "Missing argument" error.
Is there any known issue when using a decorator within modules? I can't
actually find one that uses it except for @salt.utils.memoize.

Thanks for any pointer.

Cheers,

------------------------------------------------------------------
Emile `iMil' Heitor .ᅵ. <imil@{home.imil.net,NetBSD.org,gcu.info}>
_
| http://imil.net | ASCII ribbon campaign ( )
| http://www.NetBSD.org | - against HTML email X
| http://gcu.info | & vCards / \

Colton Myers

unread,
May 20, 2013, 6:40:44 PM5/20/13
to salt-...@googlegroups.com
Do you have a link to your in-progress module?  I'd be interested to see where/how the decorator is defined.

(Really I have no idea why it would work on salt-call and not on salt, so I'm just looking for more information.)

--
Colton Myers


Emile `iMil' Heitor .°. <imil@{home.imil.net,NetBSD.org,gcu.info}>

                                                                _
              | http://imil.net        | ASCII ribbon campaign ( )
              | http://www.NetBSD.org  |  - against HTML email  X
              | http://gcu.info        |              & vCards / \


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Emile `iMil' Heitor

unread,
May 21, 2013, 1:41:23 AM5/21/13
to salt-...@googlegroups.com

> Do you have a link to your in-progress module? �I'd be interested to see
> where/how the decorator is defined.

Of course, you'll find it there http://imil.net/stuff/xapi.py

Thanks

Emile `iMil' Heitor

unread,
May 21, 2013, 4:23:10 AM5/21/13
to salt-...@googlegroups.com
On Mon, 20 May 2013, Emile `iMil' Heitor wrote:

> Nevertheless, I am still stuck with the same "Missing argument" error.
> Is there any known issue when using a decorator within modules? I can't
> actually find one that uses it except for @salt.utils.memoize.

FWIW, using context manager instead of a decorator seem to work.

------------------------------------------------------------------
Emile `iMil' Heitor .�. <imil@{home.imil.net,NetBSD.org,gcu.info}>

Colton Myers

unread,
May 21, 2013, 12:45:42 PM5/21/13
to salt-...@googlegroups.com
Very strange.  Let me ask those who know the import and loader systems in salt better than I, and get back to you.  I myself can't imagine what's different between running on salt-call vs salt.

--
Colton Myers


Thomas S Hatch

unread,
May 22, 2013, 5:07:38 AM5/22/13
to salt-...@googlegroups.com
This error is thrown when there is a TypeError thrown by the calling of the function. The functions are all loaded and called in the same way via salt and salt-call though...

Thomas S. Hatch  |  Founder, CTO


5272 South College Drive, Suite 301 | Murray, UT 84123


To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.

Emile `iMil' Heitor

unread,
May 22, 2013, 7:04:40 AM5/22/13
to salt-...@googlegroups.com

Hi Thomas,

> This error is thrown when there is a TypeError thrown by the calling of the
> function. The functions are all loaded and called in the same way via salt
> and salt-call though...

Well, as you may have seen, I've moved from decorator to context manager
which makes the overall session handling code cleaner anyway.
Side note, the @contextmanager decorator is exposed as an available
function with sys.doc:

$ sudo salt 'coruscant' sys.doc virt|grep contextmanager
virt.contextmanager:
@contextmanager decorator.
@contextmanager

Is there a way to avoid that?

Thomas S Hatch

unread,
May 22, 2013, 7:31:55 AM5/22/13
to salt-...@googlegroups.com
Ahh, yes, it should not be imported as a function, lemme look

Thomas S. Hatch  |  Founder, CTO


5272 South College Drive, Suite 301 | Murray, UT 84123


Thomas S Hatch

unread,
May 22, 2013, 7:35:32 AM5/22/13
to salt-...@googlegroups.com

Thomas S. Hatch  |  Founder, CTO


5272 South College Drive, Suite 301 | Murray, UT 84123


Emile `iMil' Heitor

unread,
May 22, 2013, 7:58:30 AM5/22/13
to salt-...@googlegroups.com

> https://github.com/saltstack/salt/commit/1e50ec05376a88f9d8d0e7c84c9ea4542f
> ed1846

Thanks :)

Just curious, what's the rule and/or where can I find it?

Antoine Pitrou

unread,
May 23, 2013, 4:21:36 AM5/23/13
to salt-...@googlegroups.com
Le Mon, 20 May 2013 23:29:14 +0200 (CEST),
Emile `iMil' Heitor <im...@NetBSD.org> a
écrit :
>
> Hi,
>
> I am finalizing a module which needs, for pretty much every function,
> to create a session, and logout that session when the job is done.
> To achieve this, I used a simple decorator:
>
> def _session_handle(function):
> def _internal(*args, **kwargs):
> _session = _get_session()
> ret = function(_session.xenapi, *args, **kwargs)
> _session.logout()
> return ret
> return _internal

For the record, you could use a try..finally here, so as to logout even
if your function() raises an exception.

Regards

Antoine.


Thomas S Hatch

unread,
May 23, 2013, 4:54:54 AM5/23/13
to salt-...@googlegroups.com
The rule? Or the quiet option? The rule is to just never import salt.modules.anything

Thomas S. Hatch  |  Founder, CTO


5272 South College Drive, Suite 301 | Murray, UT 84123


Reply all
Reply to author
Forward
0 new messages