Skip to content

Instantly share code, notes, and snippets.

@bingomanatee
Created September 14, 2011 21:52
Show Gist options
  • Save bingomanatee/1217903 to your computer and use it in GitHub Desktop.
Save bingomanatee/1217903 to your computer and use it in GitHub Desktop.
A fabric script file
from fabric.api import *
import agi_config
import fab_lib
# these lists are populated with 'username@hostname' strings by prep_hosts()
ast_hostnames_as_root = []
ast_hostnames_as_tyger = []
agi_hostnames_as_root = []
agi_hostnames_as_tyger = []
# these lists are populated with host data hashes.
agi_hosts = []
ast_hosts = []
# populates above arrays. Note becuase it has a safety valve,
# you can call this method as many times as you want.
prepped = False
def prep_hosts():
global prepped, agi_hosts, ast_hosts, ast_hostnames_as_root, ast_hostnames_as_tyger, agi_hostnames_as_root, agi_hostnames_as_tyger
if prepped:
return
agi_config.config_context('dev_server')
agi_hosts = fab_lib.get_service_data('agihost')
for h in agi_hosts:
agi_hostnames_as_root.append('root@' + h['hostname'])
agi_hostnames_as_tyger.append('tyger@' + h['hostname'])
ast_hosts = fab_lib.get_service_data('asthost')
for h in ast_hosts:
ast_hostnames_as_root.append('root@' + h['hostname'])
ast_hostnames_as_tyger.append('tyger@' + h['hostname'])
prepped = True
# runs the same shutdown routine on all agi hosts-should be executed on each asterisk server
# expects an agi_host array from the web service
def switch_agi_server_down(agi_host):
global agi_hosts
prep_hosts()
for ud_agi_host in agi_hosts:
env.hosts = ['tyger@' + ud_agi_host['hostname']]
if agi_host['id'] == ud_agi_host['id']:
command = fab_lib.switch_agi_server(ud_agi_host, 'down')
else:
command = fab_lib.switch_agi_server(ud_agi_host, 'up')
print 'switch agi server: ', command
run(command)
def point_fab_to_asterisk_servers(user):
global ast_hostnames_as_tyger, ast_hostnames_as_root
print 'point_fab_to_asterisk_servers: start'
prep_hosts()
if user == 'tyger':
env.hosts = ast_hostnames_as_tyger
print 'setting asterisk hosts to ', ast_hostnames_as_tyger
elif user == 'root':
env.hosts = ast_hostnames_as_root
print 'setting asterisk hosts to ', ast_hostnames_as_root
else:
raise Exception('unknown user for asterisk point')
print 'point_fab_to_asterisk_servers: hosts = ', env.hosts
def agi_status():
global ast_hosts, ast_hostnames_as_root,env
prep_hosts()
point_fab_to_asterisk_servers('root')
env.hosts = ast_hostnames_as_root
command = fab_lib.get_agi_status()
print'running ', command, 'on hosts ', env.hosts
print run(command, True)
# turns down a specific asterisk host from the perspective of each agi server
def shut_down_agi_server(agi_host=False):
prep_hosts()
# for test purposes, we are shutting down the first agi server by default
if False == agi_host:
agi_host = agi_hosts[0]
point_fab_to_asterisk_servers('root');
switch_agi_server_down(agi_host)
# @TODO: validate against expected results
@bingomanatee
Copy link
Author

Okay here is the before/after case that stymies me :

  • If I call point_fab_to_asterisk_servers('root') from WITHIN agi_status, but do NOT put it on the command line, the console asks me to manually set the hosts.
  • If I DO call point_fab_to_asterisk_servers:root in the fab console command, the console accepts the new configuration.
    Why can't I change configurations before I call run() in a single def function?

********** DOESN'T WORK ******************
[root@gecko deploy]# fab prep_hosts agi_status shut_down_agi_server
getting url https://web-dev.telmate.com/api/appserv_servers?cluster=dev&super_cluster=dev&server_type=agihost
returned data: [{"type":"agihost","server_type":"agihost","name_canonical":"","mod_by":61165,"cluster_id":5,"cluster":"dev","type_num":1,"super_cluster":"dev","server_index":1000,"mod_date":"2011/09/02 00:33:49 +0000","app_server_type_id":5,"host_or_ip":0,"canonical":"agihost1","hostname":"agi-dev","id":1,"deleted":false,"control2_id":null,"super_cluster_id":5,"cluster":"dev","ip":"172.17.10.14","hostname":"agi-dev","super_cluster":"dev","control1_id":null,"active":1},{"type":"agihost","server_type":"agihost","name_canonical":"","mod_by":61165,"cluster_id":5,"cluster":"dev","type_num":2,"super_cluster":"dev","server_index":1001,"mod_date":"2011/09/02 00:33:55 +0000","app_server_type_id":5,"host_or_ip":0,"canonical":"agihost2","hostname":"agi2-dev","id":145,"deleted":false,"control2_id":null,"super_cluster_id":5,"cluster":"dev","ip":"172.17.10.31","hostname":"agi2-dev","super_cluster":"dev","control1_id":null,"active":1}]
getting url https://web-dev.telmate.com/api/appserv_servers?cluster=dev&super_cluster=dev&server_type=asthost
returned data: [{"type":"asthost","server_type":"asthost","name_canonical":"","mod_by":61165,"cluster_id":5,"cluster":"dev","type_num":1,"super_cluster":"dev","server_index":1000,"mod_date":"2011/09/02 00:34:02 +0000","app_server_type_id":15,"host_or_ip":0,"canonical":"asthost1","hostname":"asthost-dev","id":2,"deleted":false,"control2_id":null,"super_cluster_id":5,"cluster":"dev","ip":"172.17.10.13","hostname":"asthost-dev","super_cluster":"dev","control1_id":null,"active":1},{"type":"asthost","server_type":"asthost","name_canonical":"","mod_by":67115,"cluster_id":5,"cluster":"dev","type_num":2,"super_cluster":"dev","server_index":1001,"mod_date":"2011/09/14 00:48:22 +0000","app_server_type_id":15,"host_or_ip":0,"canonical":"asthost2","hostname":"asthost2-dev","id":3,"deleted":false,"control2_id":null,"super_cluster_id":5,"cluster":"dev","ip":"172.17.10.25","hostname":"asthost2-dev","super_cluster":"dev","control1_id":null,"active":1}]
point_fab_to_asterisk_servers: start
setting asterisk hosts to [u'root@asthost-dev', u'root@asthost2-dev']
point_fab_to_asterisk_servers: hosts = [u'root@asthost-dev', u'root@asthost2-dev']
get_agi_status()
running asterisk -rx "tyger agi status" on hosts [u'root@asthost-dev', u'root@asthost2-dev']
No hosts found. Please specify (single) host string for connection:

***** DOES WORK ******
[root@gecko deploy]# fab prep_hosts point_fab_to_asterisk_servers:root agi_status shut_down_agi_server
getting url https://web-dev.telmate.com/api/appserv_servers?cluster=dev&super_cluster=dev&server_type=agihost
returned data: [{"type":"agihost","server_type":"agihost","name_canonical":"","mod_by":61165,"cluster_id":5,"cluster":"dev","type_num":1,"super_cluster":"dev","server_index":1000,"mod_date":"2011/09/02 00:33:49 +0000","app_server_type_id":5,"host_or_ip":0,"canonical":"agihost1","hostname":"agi-dev","id":1,"deleted":false,"control2_id":null,"super_cluster_id":5,"cluster":"dev","ip":"172.17.10.14","hostname":"agi-dev","super_cluster":"dev","control1_id":null,"active":1},{"type":"agihost","server_type":"agihost","name_canonical":"","mod_by":61165,"cluster_id":5,"cluster":"dev","type_num":2,"super_cluster":"dev","server_index":1001,"mod_date":"2011/09/02 00:33:55 +0000","app_server_type_id":5,"host_or_ip":0,"canonical":"agihost2","hostname":"agi2-dev","id":145,"deleted":false,"control2_id":null,"super_cluster_id":5,"cluster":"dev","ip":"172.17.10.31","hostname":"agi2-dev","super_cluster":"dev","control1_id":null,"active":1}]
getting url https://web-dev.telmate.com/api/appserv_servers?cluster=dev&super_cluster=dev&server_type=asthost
returned data: [{"type":"asthost","server_type":"asthost","name_canonical":"","mod_by":61165,"cluster_id":5,"cluster":"dev","type_num":1,"super_cluster":"dev","server_index":1000,"mod_date":"2011/09/02 00:34:02 +0000","app_server_type_id":15,"host_or_ip":0,"canonical":"asthost1","hostname":"asthost-dev","id":2,"deleted":false,"control2_id":null,"super_cluster_id":5,"cluster":"dev","ip":"172.17.10.13","hostname":"asthost-dev","super_cluster":"dev","control1_id":null,"active":1},{"type":"asthost","server_type":"asthost","name_canonical":"","mod_by":67115,"cluster_id":5,"cluster":"dev","type_num":2,"super_cluster":"dev","server_index":1001,"mod_date":"2011/09/14 00:48:22 +0000","app_server_type_id":15,"host_or_ip":0,"canonical":"asthost2","hostname":"asthost2-dev","id":3,"deleted":false,"control2_id":null,"super_cluster_id":5,"cluster":"dev","ip":"172.17.10.25","hostname":"asthost2-dev","super_cluster":"dev","control1_id":null,"active":1}]
point_fab_to_asterisk_servers: start
setting asterisk hosts to [u'root@asthost-dev', u'root@asthost2-dev']
point_fab_to_asterisk_servers: hosts = [u'root@asthost-dev', u'root@asthost2-dev']
[root@asthost-dev] Executing task 'agi_status'
point_fab_to_asterisk_servers: start
setting asterisk hosts to [u'root@asthost-dev', u'root@asthost2-dev']
point_fab_to_asterisk_servers: hosts = [u'root@asthost-dev', u'root@asthost2-dev']
get_agi_status()
running asterisk -rx "tyger agi status" on hosts [u'root@asthost-dev', u'root@asthost2-dev']
[root@asthost-dev] run: asterisk -rx "tyger agi status"
[root@asthost-dev] out: agihost1 DOWN
[root@asthost-dev] out: agihost2 UP
[root@asthost-dev] out:
agihost1 DOWN
agihost2 UP
[root@asthost2-dev] Executing task 'agi_status'
point_fab_to_asterisk_servers: start
setting asterisk hosts to [u'root@asthost-dev', u'root@asthost2-dev']
point_fab_to_asterisk_servers: hosts = [u'root@asthost-dev', u'root@asthost2-dev']
get_agi_status()
running asterisk -rx "tyger agi status" on hosts [u'root@asthost-dev', u'root@asthost2-dev']
[root@asthost2-dev] run: asterisk -rx "tyger agi status"
[root@asthost2-dev] out: agihost1 DOWN
[root@asthost2-dev] out: agihost2 UP
[root@asthost2-dev] out:
agihost1 DOWN
agihost2 UP
[root@asthost-dev] Executing task 'shut_down_agi_server'
point_fab_to_asterisk_servers: start
setting asterisk hosts to [u'root@asthost-dev', u'root@asthost2-dev']
point_fab_to_asterisk_servers: hosts = [u'root@asthost-dev', u'root@asthost2-dev']
switch_server(agihost1, down)
switch agi server: asterisk -rx "tyger agi status agihost1 down"
[root@asthost-dev] run: asterisk -rx "tyger agi status agihost1 down"
[root@asthost-dev] out: agihost1 DOWN
[root@asthost-dev] out: agihost2 UP
[root@asthost-dev] out:
switch_server(agihost2, up)
switch agi server: asterisk -rx "tyger agi status agihost2 up"
[root@asthost-dev] run: asterisk -rx "tyger agi status agihost2 up"
[root@asthost-dev] out: agihost1 DOWN
[root@asthost-dev] out: agihost2 UP
[root@asthost-dev] out:
[root@asthost2-dev] Executing task 'shut_down_agi_server'
point_fab_to_asterisk_servers: start
setting asterisk hosts to [u'root@asthost-dev', u'root@asthost2-dev']
point_fab_to_asterisk_servers: hosts = [u'root@asthost-dev', u'root@asthost2-dev']
switch_server(agihost1, down)
switch agi server: asterisk -rx "tyger agi status agihost1 down"
[root@asthost2-dev] run: asterisk -rx "tyger agi status agihost1 down"
[root@asthost2-dev] out: agihost1 DOWN
[root@asthost2-dev] out: agihost2 UP
[root@asthost2-dev] out:
switch_server(agihost2, up)
switch agi server: asterisk -rx "tyger agi status agihost2 up"
[root@asthost2-dev] run: asterisk -rx "tyger agi status agihost2 up"
[root@asthost2-dev] out: agihost1 DOWN
[root@asthost2-dev] out: agihost2 UP
[root@asthost2-dev] out:

Done.
Disconnecting from asthost2-dev... done.
Disconnecting from asthost-dev... done.
[root@gecko deploy]#

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