Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Fabric decorator
    primarykey
    data
    text
    <p>I've some fabric tasks in my fabfile and I need to initialize, the env variable before their execution. I'm trying to use a decorator, it works but fabric always says "no host found Please specify (single)" however if I print the content of my variable "env" all seems good. Also I call my tasks from another python script.</p> <pre><code>from fabric.api import * from instances import find_instances def init_env(func): def wrapper(*args, **kwargs): keysfolder = 'keys/' env.user = 'admin' env.key_filename = '%skey_%s_prod.pem'%(keysfolder, args[0]) env.hosts = find_instances(args[1]) return func(args[0], args[1]) return wrapper @init_env def restart_apache2(region, groupe): print(env.hosts) run('/etc/init.d/apache2 restart') return True </code></pre> <p>My script which call the fabfile:</p> <pre><code>from fabfile import init_env, restart_apache2 restart_apache2('eu-west-1', 'apache2') </code></pre> <p>Output of print in restart apache2:</p> <p>[u'10.10.0.1', u'10.10.0.2']</p> <p>Any idea why my task restart_apache2 doesn't use the env variable?</p> <p>Thanks</p> <p>EDIT:</p> <p>Which is interesting it's if in my script which calls the fabfile, I use settings from fabric.api and set a host ip, it works. This show that my decorator has well initialized the env variable because the key and user are send to fabric. It's only the env.hosts that's not read by fabric...</p> <p>EDIT2:</p> <p>I can reach my goal with using settings from fabric.api, like that:</p> <pre><code>@init_env def restart_apache2(region, groupe): for i in env.hosts: with settings(host_string = '%s@%s' % (env.user, i)): run('/etc/init.d/apache2 restart') return True </code></pre> <p>Bonus question, has there a solution to use directly the env.hosts without settings?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload