Guest User

Untitled

a guest
Jan 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. from fabric.api import *
  2. import agi_config
  3. import fab_lib
  4.  
  5. # these lists are populated with 'username@hostname' strings by prep_hosts()
  6. ast_hostnames_as_root = []
  7. ast_hostnames_as_tyger = []
  8. agi_hostnames_as_root = []
  9. agi_hostnames_as_tyger = []
  10.  
  11. # these lists are populated with host data hashes.
  12. agi_hosts = []
  13. ast_hosts = []
  14.  
  15. # populates above arrays. Note becuase it has a safety valve,
  16. # you can call this method as many times as you want.
  17. prepped = False
  18.  
  19. def prep_hosts():
  20. global prepped, agi_hosts, ast_hosts, ast_hostnames_as_root, ast_hostnames_as_tyger, agi_hostnames_as_root, agi_hostnames_as_tyger
  21.  
  22. if prepped:
  23. return
  24. agi_config.config_context('dev_server')
  25.  
  26. agi_hosts = fab_lib.get_service_data('agihost')
  27. for h in agi_hosts:
  28. agi_hostnames_as_root.append('root@' + h['hostname'])
  29. agi_hostnames_as_tyger.append('tyger@' + h['hostname'])
  30.  
  31. ast_hosts = fab_lib.get_service_data('asthost')
  32. for h in ast_hosts:
  33. ast_hostnames_as_root.append('root@' + h['hostname'])
  34. ast_hostnames_as_tyger.append('tyger@' + h['hostname'])
  35. prepped = True
  36.  
  37. # runs the same shutdown routine on all agi hosts-should be executed on each asterisk server
  38. # expects an agi_host array from the web service
  39.  
  40. def switch_agi_server_down(agi_host):
  41. global agi_hosts
  42. prep_hosts()
  43.  
  44. for ud_agi_host in agi_hosts:
  45. env.hosts = ['tyger@' + ud_agi_host['hostname']]
  46. if agi_host['id'] == ud_agi_host['id']:
  47. command = fab_lib.switch_agi_server(ud_agi_host, 'down')
  48. else:
  49. command = fab_lib.switch_agi_server(ud_agi_host, 'up')
  50. print 'switch agi server: ', command
  51. run(command)
  52.  
  53. def point_fab_to_asterisk_servers(user):
  54. global ast_hostnames_as_tyger, ast_hostnames_as_root
  55. print 'point_fab_to_asterisk_servers: start'
  56. prep_hosts()
  57.  
  58. if user == 'tyger':
  59. env.hosts = ast_hostnames_as_tyger
  60. print 'setting asterisk hosts to ', ast_hostnames_as_tyger
  61. elif user == 'root':
  62. env.hosts = ast_hostnames_as_root
  63. print 'setting asterisk hosts to ', ast_hostnames_as_root
  64. else:
  65. raise Exception('unknown user for asterisk point')
  66. print 'point_fab_to_asterisk_servers: hosts = ', env.hosts
  67.  
  68.  
  69. def agi_status():
  70. global ast_hosts, ast_hostnames_as_root,env
  71. prep_hosts()
  72.  
  73. point_fab_to_asterisk_servers('root')
  74. env.hosts = ast_hostnames_as_root
  75.  
  76. command = fab_lib.get_agi_status()
  77. print'running ', command, 'on hosts ', env.hosts
  78. print run(command, True)
  79.  
  80. # turns down a specific asterisk host from the perspective of each agi server
  81.  
  82. def shut_down_agi_server(agi_host=False):
  83. prep_hosts()
  84. # for test purposes, we are shutting down the first agi server by default
  85. if False == agi_host:
  86. agi_host = agi_hosts[0]
  87. point_fab_to_asterisk_servers('root');
  88. switch_agi_server_down(agi_host)
  89.  
  90. # @TODO: validate against expected results
Add Comment
Please, Sign In to add comment