forked from lavender2020/marathon-autoscale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marathon_scale_test.py
109 lines (88 loc) · 4.19 KB
/
marathon_scale_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
__author__ = 'tkraus'
import sys
import requests
import json
import pprint
import math
marathon_host = input("Enter the resolvable hostname or IP of your Marathon Instance : ")
marathon_app = raw_input("Enter the Marathon Application Name to Configure Autoscale for from the Marathon UI : ")
autoscale_multiplier = float(raw_input("Enter Autoscale multiplier for triggered Autoscale (ie 1.5) : "))
class marathon(object):
def __init__(self,marathon_host):
self.name=marathon_host
self.uri=("http://"+marathon_host+":8080")
def get_all_apps(self):
response=requests.get(self.uri + '/v2/apps').json()
if response['apps'] ==[]:
print ("No Apps found on Marathon")
sys.exit(1)
else:
apps=[]
for i in response['apps']:
appid=i['id'].strip('/')
apps.append(appid)
print ("Found the following App LIST on Marathon =",apps)
self.apps=apps
return apps
def get_app_details(self,marathon_app):
response=requests.get(self.uri + '/v2/apps/'+ marathon_app).json()
if (response['app']['tasks'] ==[]):
print ('No task data on Marathon for App !', marathon_app)
else:
app_instances=response['app']['instances']
self.appinstances=app_instances
print(marathon_app,"has",self.appinstances,"deployed instances")
app_task_dict={}
for i in response['app']['tasks']:
taskid=i['id']
print ('DEBUG - taskId=',taskid)
hostid=i['host']
print ('DEBUG - hostId=', hostid)
app_task_dict[str(taskid)]=str(hostid)
return app_task_dict
def scale_app(self,marathon_app,autoscale_multiplier):
target_instances_float=self.appinstances * autoscale_multiplier
target_instances=math.ceil(target_instances_float)
data ={'instances': target_instances}
json_data=json.dumps(data)
headers = {'Content-type': 'application/json'}
response=requests.put(self.uri + '/v2/apps/'+ marathon_app,json_data,headers=headers)
print ('Scale_app return status code =', response.status_code)
print(response.content)
print(response.headers)
def get_task_agentstatistics(task,host):
# Get the performance Metrics for all the tasks for the Marathon App specified
# by connecting to the Mesos Agent and then making a REST call against Mesos statistics
# Return to Statistics for the specific task for the marathon_app
response=requests.get('http://'+host + ':5051/monitor/statistics.json').json()
print ('DEBUG -- Getting Mesos Metrics for Mesos Agent =',host)
for i in response:
executor_id=i['executor_id']
print("DEBUG -- Printing each Executor ID ",executor_id)
if (executor_id == task):
task_stats =i['statistics']
# print ('****Specific stats for task',executor_id,'=',task_stats)
return task_stats
if __name__ == "__main__":
print ("This application tested with Python3 only")
# Initialize the Marathon object
aws_marathon=marathon(marathon_host)
# Print the initialized object properties
print(aws_marathon.name, " Object and Properties")
# Call get_all_apps method for new object created from aws_marathon class and return all apps
marathon_apps = aws_marathon.get_all_apps()
print ("Marathon 'aws_marathon.get_all_apps' method call", marathon_apps)
# Quick sanity check to test for apps existence in MArathon.
if (marathon_app in marathon_apps):
print ("Found your Marathon App=",marathon_app)
else:
print ("Could not find your App =",marathon_app)
sys.exit(1)
# Return the .apps property of the object created from aws_marathon class
print ("Marathon App 'apps' property call", aws_marathon.apps)
# Return a dictionary comprised of the target app taskId and hostId.
app_task_dict = aws_marathon.get_app_details(marathon_app)
print ("Autoscale triggered based Mem 'or' CPU exceeding threshold")
aws_marathon.scale_app(marathon_app,autoscale_multiplier)
print("Successfully completed program...")
sys.exit(0)