Skip to content

Instantly share code, notes, and snippets.

@homingli
Created July 31, 2013 19:09
Show Gist options
  • Save homingli/6125144 to your computer and use it in GitHub Desktop.
Save homingli/6125144 to your computer and use it in GitHub Desktop.
Python scripts used in ActiveState webinar "Advanced Scaling Techniques with PaaS"
from stackato.interfaces import StackatoInterface
from bs4 import BeautifulSoup
import requests
import yaml
CREDENTIALS = yaml.load(open('credentials.yml'))
APP_NAME = 'your-app-name'
NEW_RELIC_USER_ID = 'your-user-id'
NEW_RELIC_APPLICATION_ID = 'your-app-id'
# scaling threshold
CPU_THRESHOLD = 80.5
# flags
DEBUG = False
VERBOSE = True
# max instance
MAX_INSTANCES = 3
def get_newrelic_threshold_value(user, appid, name='CPU'):
headers = {'x-api-key': CREDENTIALS['token']}
url = 'https://api.newrelic.com/api/v1/accounts/' + user + '/applications/' + appid + '/threshold_values.xml'
if DEBUG:
print url
print headers
r = requests.get(url, headers=headers)
# read from the response body as xml data
soup = BeautifulSoup(r.text, 'xml')
# return the value of the specified threshold_value's name from the response
return soup.find_all(attrs={'name': name})[0]['metric_value']
if __name__ == "__main__":
# convert to float, since the CPU threshold value is returned as a string
metric_value = float(get_newrelic_threshold_value(NEW_RELIC_USER_ID,NEW_RELIC_APPLICATION_ID))
if VERBOSE:
print "metric_value is " + str(metric_value)
print "CPU_THRESHOLD is " + str(CPU_THRESHOLD)
if metric_value < CPU_THRESHOLD:
if VERBOSE:
print "metric_value < CPU_THRESHOLD"
print('application running normally. no new instances spawned.')
else:
if VERBOSE:
print "metric_value => CPU_THRESHOLD"
sti = StackatoInterface(CREDENTIALS['target'], CREDENTIALS['username'], CREDENTIALS['password'])
if sti.login():
app = sti.get_app(APP_NAME)
print APP_NAME + " currently running " + str(app['runningInstances']) + " instances."
if app['instances'] < MAX_INSTANCES:
# increase the number of instances
app['instances'] += 1
# making a PUT request to the application
if sti.put_app(APP_NAME, app):
print('added one more instance to this application.')
else:
print('reached max instances.')
target: https://api.stackato-xxxx.local/
username: your-stackato-username
password: your-stackato-password
token: your-newrelic-api-key
requests
PyYAML
beautifulsoup4
python-stackato
import requests
import yaml
CREDENTIALS = yaml.load(open('credentials.yml'))
APP_NAME = 'your-app-name'
NEW_RELIC_USER_ID = 'your-user-id'
NEW_RELIC_APPLICATION_ID = 'your-app-id'
headers = {'x-api-key': CREDENTIALS['token']}
url = 'https://api.newrelic.com/api/v1/accounts/' + NEW_RELIC_USER_ID + '/applications/' + NEW_RELIC_APPLICATION_ID + '/threshold_values.xml'
print requests.get(url, headers=headers).text
from stackato.interfaces import StackatoInterface
import yaml
import json
CREDENTIALS = yaml.load(open('credentials.yml'))
sti = StackatoInterface(CREDENTIALS['target'], CREDENTIALS['username'], CREDENTIALS['password'])
if sti.login():
print json.dumps(sti.get_apps())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment