Douglas Garstang
2015-01-06 18:43:46 UTC
All,
I'm trying to use fabric with both roles and environments (ie prod, stage,
test etc). I started by following the example at
http://yerb.net/blog/2014/03/03/multiple-environments-for-deployment-using-fabric/,
which gave me the idea of using a task to set the environment.
However, I also need to populate the roledefs for that environment. My
issue is that the environment is required to populate the roledefs.
However, if I try and call the function to do that before the task that
sets the environment, I don't know what the environment is yet. Conversely,
if I try and populate the roledefs later, in the task that sets the
environment, fabric complains "The following specified roles do not exist:".
This example below is where I attempt to set the roledefs after/during the
task, and fabric complains with "The following specified roles do not
exist:".
import os
from boto import ec2
from boto.exception import *
from fabric.api import *
def do_roledefs(environment):
hosts = {}
conn = ec2.connect_to_region('us-east-1',
aws_access_key_id = os.environ['AWS_ACCESS_KEY'],
aws_secret_access_key = os.environ['AWS_SECRET_KEY'])
ec2_filter = { 'instance-state-name' : 'running', "tag:environment" :
environment }
reservations = conn.get_all_instances(filters=ec2_filter)
instances = [i for r in reservations for i in r.instances]
for i in instances:
if 'role' in i.tags:
if i.tags['role'] in hosts:
hosts[i.tags['role']].append(i.private_ip_address)
else:
hosts[i.tags['role']] = [i.private_ip_address]
env.roledefs = hosts
@task
def dev():
stage_set('dev')
@task
def qa():
stage_set('qa')
def stage_set(stage_name='test'):
do_roledefs(stage_name)
Doug
I'm trying to use fabric with both roles and environments (ie prod, stage,
test etc). I started by following the example at
http://yerb.net/blog/2014/03/03/multiple-environments-for-deployment-using-fabric/,
which gave me the idea of using a task to set the environment.
However, I also need to populate the roledefs for that environment. My
issue is that the environment is required to populate the roledefs.
However, if I try and call the function to do that before the task that
sets the environment, I don't know what the environment is yet. Conversely,
if I try and populate the roledefs later, in the task that sets the
environment, fabric complains "The following specified roles do not exist:".
This example below is where I attempt to set the roledefs after/during the
task, and fabric complains with "The following specified roles do not
exist:".
import os
from boto import ec2
from boto.exception import *
from fabric.api import *
def do_roledefs(environment):
hosts = {}
conn = ec2.connect_to_region('us-east-1',
aws_access_key_id = os.environ['AWS_ACCESS_KEY'],
aws_secret_access_key = os.environ['AWS_SECRET_KEY'])
ec2_filter = { 'instance-state-name' : 'running', "tag:environment" :
environment }
reservations = conn.get_all_instances(filters=ec2_filter)
instances = [i for r in reservations for i in r.instances]
for i in instances:
if 'role' in i.tags:
if i.tags['role'] in hosts:
hosts[i.tags['role']].append(i.private_ip_address)
else:
hosts[i.tags['role']] = [i.private_ip_address]
env.roledefs = hosts
@task
def dev():
stage_set('dev')
@task
def qa():
stage_set('qa')
def stage_set(stage_name='test'):
do_roledefs(stage_name)
Doug