Advertisement
Guest User

Untitled

a guest
Mar 1st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. import spur
  5. import multiprocessing
  6.  
  7. IT=0
  8. HOST_LIST = {
  9. "192.168.10.4": True,
  10. "192.168.10.6": True,
  11. "192.168.10.7": True,
  12. "192.168.10.8": True
  13. }
  14. BOOL_TO_COMMAND = {
  15. True: 'start',
  16. False: 'stop'
  17. }
  18. COMMAND_TPL = 'sudo systemctl %s monasca-log-transformer'
  19.  
  20.  
  21. def pick_random_host():
  22. global IT
  23.  
  24. keys = HOST_LIST.keys()
  25. length = len(keys)
  26.  
  27. if IT == length:
  28. IT = 0
  29.  
  30. key = keys[IT]
  31. IT += 1
  32.  
  33. return key
  34.  
  35.  
  36. def get_command(val):
  37. return COMMAND_TPL % BOOL_TO_COMMAND.get(val)
  38.  
  39.  
  40. def run():
  41. print 'Entering the loop, changing status by 60 seconds'
  42. print 'Initial state of hosts %s' % HOST_LIST
  43. while True:
  44. print 'Waiting for 30 seconds'
  45. time.sleep(30)
  46. print '---------------------------------------------------------------'
  47. try:
  48. host_ip = pick_random_host()
  49. status = HOST_LIST[host_ip]
  50. command = get_command(not status)
  51. HOST_LIST[host_ip] = not status
  52.  
  53. shell = spur.SshShell(hostname=host_ip,
  54. port=22,
  55. private_key_file=(
  56. '/home/kornicameister/.ssh/id_rsa'),
  57. missing_host_key=spur.ssh.MissingHostKey.accept,
  58. username='vagrant',
  59. password='vagrant')
  60.  
  61. res = shell.run(command.split(' '))
  62. print '[', host_ip, ']', '[', HOST_LIST[
  63. host_ip], ']', command, \
  64. res.return_code, \
  65. res.output
  66. except Exception:
  67. pass
  68. print '---------------------------------------------------------------'
  69.  
  70.  
  71. if __name__ == '__main__':
  72. process = multiprocessing.Process(target=run)
  73. process.start()
  74. process.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement