Reusing states in custom state modules #3513

Closed
madduck opened this Issue Jan 30, 2013 · 7 comments

Comments

Projects
None yet
3 participants
@madduck
Contributor

madduck commented Jan 30, 2013

With reference to #3460, I tried to create my own state sudo.permission in _states, which would basically just control existence of a template instance in /etc/sudoers.d. With #2385 solved, this would be an adequate solution to my desire to provide module encapsulation and reusability at the SLS level.

In fact, it might be the best approach, as it would be possible to keep the SLS at YAML, possibly with templating, while all Python would be in proper Python modules.

Unfortunately, the following does not work:

from salt.states.file import managed as file_managed
def mystate(name, foo):
    return file_managed(name, …, context={foo=foo})

as the file.managed() function needs access to the lazy loader __salt__, which a regular import does not make available — unfortunately.

It has been suggested to me to use execution modules directly, but that would mean I'd have to reimplement large portions of file.managed.

It has also been suggested to do something like

__salt__('state.single')('file.managed', …)

and while that might very well work, it seems like a lot of overhead and overkill.

Please provide a means by which custom state modules (and state modules in general) can reuse functionality from other state modules easily, without having to go full-circle around the command publisher.

@thatch45

This comment has been minimized.

Show comment
Hide comment
@thatch45

thatch45 Feb 1, 2013

Member

Right now you can cross call states the way you want, you just need to do the setup of the states dict is all:

import salt.loader
states = salt.loader.states(__opts__, __salt__)
states['file.managed'](...)

Does this solve your needs?

Member

thatch45 commented Feb 1, 2013

Right now you can cross call states the way you want, you just need to do the setup of the states dict is all:

import salt.loader
states = salt.loader.states(__opts__, __salt__)
states['file.managed'](...)

Does this solve your needs?

@madduck

This comment has been minimized.

Show comment
Hide comment
@madduck

madduck Feb 1, 2013

Contributor

This looks like a lot of work (loading all states), which has already been done. Couldn't there be a better way?

Contributor

madduck commented Feb 1, 2013

This looks like a lot of work (loading all states), which has already been done. Couldn't there be a better way?

@madduck

This comment has been minimized.

Show comment
Hide comment
@madduck

This comment has been minimized.

Show comment
Hide comment
@madduck

madduck Feb 1, 2013

Contributor

Actually, I am sorry, it does not work. The reason is that __opts__ is not available. I had forgotten to call saltutil.sync_states… Doh!

Contributor

madduck commented Feb 1, 2013

Actually, I am sorry, it does not work. The reason is that __opts__ is not available. I had forgotten to call saltutil.sync_states… Doh!

@thatch45 thatch45 closed this May 28, 2013

@madduck

This comment has been minimized.

Show comment
Hide comment
@madduck

madduck Jun 10, 2013

Contributor

Why did you close this issue?

Contributor

madduck commented Jun 10, 2013

Why did you close this issue?

@johnnoone

This comment has been minimized.

Show comment
Hide comment
@johnnoone

johnnoone Nov 28, 2013

Contributor

@madduck this snippet work, but it's not obvious

opts = __opts__.copy()
opts['grains'] = __grains__
__states__ = salt.loader.states(opts, __salt__)
Contributor

johnnoone commented Nov 28, 2013

@madduck this snippet work, but it's not obvious

opts = __opts__.copy()
opts['grains'] = __grains__
__states__ = salt.loader.states(opts, __salt__)
@madduck

This comment has been minimized.

Show comment
Hide comment
@madduck

madduck Nov 28, 2013

Contributor

That's gross (but thanks!). It's really high time that the Salt loader gets replaced by proper Python module loading. Principle of least surprise!

Contributor

madduck commented Nov 28, 2013

That's gross (but thanks!). It's really high time that the Salt loader gets replaced by proper Python module loading. Principle of least surprise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment