demux demux - 20 days ago 10
Python Question

python fabric put with dynamic remote_path

I have two servers that I need to deploy to, but they have a slightly different setup. The application is deployed to a different path on each server (

/var/www/sites/my_site
and
/var/www/my_site
).

My file looks somewhat like this:

env.roledefs = {
'production': ['host1.foo.bar', 'host2.foo.bar']
}

@task
@roles(['production'])
def deploy():
files = getBundlePaths()

for file in files:
# How would I go about uploading to a different path per server?
put(file, ...)

Answer

Maybe you can setup another variable with the paths and key off that from the current host you are on. Something like this

from fabric.api import *

env.roledefs = {
    'production': ['host1.foo.bar', 'host2.foo.bar']
}

env.paths = {
    'production': {
        'host1.foo.bar': '/var/www/sites/my_site',
        'host2.foo.bar': '/var/www/my_site'
    }
}

@task
@roles(['production'])
def deploy():
    files = getBundlePaths()

    path = env.paths[env.effective_roles[0]][env.host]
    print(path)

    for file in files:
        put(file, path)
Comments