Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import subprocess
  4. import argparse
  5. import math
  6.  
  7. VERBOSE = 0
  8. DEBUG = 0
  9.  
  10. IP = "10.212.136.60"
  11. USER = "prouser"
  12. PASSWORD = "greatpw"
  13.  
  14. RATE_PER_SERVER = 3
  15. MAX_SERVERS = 10
  16. MIN_SERVERS = 1
  17. parser = argparse.ArgumentParser(prog="dynascale.py")
  18. parser.add_argument('-v','--verbose',dest="verbose",help="Turn verbosity on",default=False,action="store_true")
  19. parser.add_argument('-d','--debug',dest="debug",help="Turn debuging on",default=False,action="store_true")
  20. parser.add_argument('-i','--ip',dest='ip', help='Ip address of haproxy server', default=IP)
  21. parser.add_argument('-u','--user',dest='user', help='Username for haproxy authentication', default=USER)
  22. parser.add_argument('-p','--password',dest='password', help='Password for haproxy authentication', default=PASSWORD)
  23.  
  24. arguments = parser.parse_args()
  25.  
  26. VERBOSE = arguments.verbose
  27. DEBUG = arguments.debug
  28. IP = arguments.ip
  29. USER = arguments.user
  30. PASSWORD = arguments.password
  31.  
  32. def verbose(text):
  33.     if VERBOSE:
  34.         print text
  35.  
  36. def debug(text):
  37.     if DEBUG:
  38.         print text
  39.  
  40. # 1 Get trafficdata(current rate)
  41. def get_rate(user,password,ip):
  42.     # curl -s http://username:password@ipaddress:1936;/csv
  43.     output = subprocess.check_output(["curl","-s","http://"+ user + ":" + password + "@" + ip + ":1936/\;csv"])
  44.     for line in output.split('\n'):
  45.         if "bookface" in line:
  46.             stats_array = line.split(',')
  47.             total_sessions = stats_array[4]
  48.             return float(total_sessions)
  49.        
  50. # Alternate function for testing purposes
  51. def get_rate_alt(user,password,ip):
  52.     return float(23)
  53.  
  54. # 2 get number of workers in swarm
  55. def get_workers():
  56.     output = subprocess.check_output(["docker service ls | grep bookface_web | awk '{print $4}' | sed -e 's/.*\///g'"], shell=True,executable='/bin/bash')
  57.     output.rstrip()
  58.     return float(output)
  59.  
  60. # Alternate function for testing purposes
  61. def get_workers_alt():
  62.     return float(10)
  63.  
  64. # Scale up or down depending on load and amount of current workers
  65. def scale_up(current,goal):
  66.     for i in range((current + 1), (goal + 1)):
  67.         verbose("Starting server " + str(i))
  68.     subprocess.call(["docker service update --replicas=" + str(goal) + " bf_bookface_web"],shell=True)
  69.  
  70. def scale_down(current,goal):
  71.     for i in range (current, goal, -1):
  72.         verbose("Shutting down server " + str(i))
  73.     subprocess.call(["docker service update --replicas=" + str(goal) + " bf_bookface_web"],shell=True)
  74.  
  75.  
  76. current_rate = get_rate(USER,PASSWORD,IP)
  77. verbose("Current rate: " + str(current_rate))
  78.  
  79. current_workers = get_workers()
  80. verbose("Current workers: " + str(current_workers))
  81.  
  82.  
  83. # 3 calculate the current needed capacity
  84. needed_capacity = math.ceil(current_rate / RATE_PER_SERVER)
  85. verbose("We need " + str(needed_capacity) + " to handle this rate")
  86.  
  87. if needed_capacity < MIN_SERVERS:
  88.     verbose("Adjusting needed capacity to minimun: " + str(MIN_SERVERS))
  89.     needed_capacity = MIN_SERVERS
  90. elif needed_capacity > MAX_SERVERS:
  91.     verbose("Adjusting needed capacity to maximum " + str(MAX_SERVERS))
  92.  
  93. # 4 compare current needed with a actual capacity and take action: reduce or increase or none of the above
  94. if needed_capacity > current_workers:
  95.     verbose("We need to increase the number of servers from " + str(current_workers) + " to " + str(needed_capacity))
  96.     scale_up(int(current_rate),int(needed_capacity))
  97. elif needed_capacity < current_workers:
  98.     verbose("We need to decrease the number of servers from " + str(current_workers) + " to " + str(needed_capacity))
  99.     scale_down(int(current_workers),int(needed_capacity))
  100. else:
  101.     verbose("No further tampering needed all is good my man")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement