Created
December 21, 2015 17:36
-
-
Save bbertka/41a6730cff6462cb77f6 to your computer and use it in GitHub Desktop.
CF Lattice API CRUD Demo via Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implements Create, Debug, and Delete Lattice Instance usable from within a Python app | |
# | |
import os, jsonr | |
import requests | |
# Note these are only a few variables, closer inspection of this Gist will show that there is alot of possibility here based on your app container. | |
# needed to make requests | |
auth_header = {'Authorization':'Basic YmJlcnRrYTprYXJtYTE5NzY='} | |
MEMORY = 128 | |
INSTANCES = 1 | |
session.SCREENNAME = 'p3Geek' #twitter handle | |
myglobals.APP_KEY = os.getenv('APP_KEY') | |
myglobals.APP_SECRET = os.getenv('APP_SECRET') | |
myglobals.ACCESS_TOKEN = os.getenv('ACCESS_TOKEN') | |
myglobals.ACCESS_TOKEN_SECRET = os.getenv('ACCESS_TOKEN_SECRET') | |
myglobals.APP_URI =json.loads(os.getenv("VCAP_APPLICATION"))['uris'][0] | |
myglobals. DIEGO_RECEPTOR = os.getenv('DIEGO_RECEPTOR') #just the xip.io address | |
def delete_instance(): | |
headers = auth_header | |
r = requests.delete("http://receptor.%s/v1/desired_lrps/ltc-%s" % (myglobals.DIEGO_RECEPTOR, session.SCREENNAME.lower() ), headers=headers) | |
return dict(message=r.content) | |
def debug_instance(): | |
headers = auth_header | |
r = requests.get("http://receptor.%s/v1/desired_lrps/ltc-%s"% ( myglobals.DIEGO_RECEPTOR, session.SCREENNAME.lower() ), headers=headers) | |
return dict(message=r.content) | |
def create_instance(): | |
if not session.SCREENNAME: | |
redirect('/') | |
query= { | |
"metrics_guid": "ltc-%s" % session.SCREENNAME.lower(), | |
"domain": "lattice", | |
"disk_mb": 0, | |
"monitor": { | |
"run": { | |
"path": "/tmp/healthcheck", | |
"args": [ | |
"-timeout", | |
"1s", | |
"-port", | |
"8080" | |
], | |
"resource_limits": {}, | |
"user": "vcap", | |
"log_source": "HEALTH" | |
} | |
}, | |
"start_timeout": 0, | |
"setup": { | |
"download": { | |
"to": "/tmp", | |
"cache_key": "", | |
"from": "http://file_server.service.dc1.consul:8080/v1/static/healthcheck.tgz", | |
"user": "vcap" | |
} | |
}, | |
"log_source": "APP", | |
"routes": { | |
"cf-router": [ | |
{ | |
"hostnames": [ | |
"ltc-%s.%s" % (session.SCREENNAME.lower(), myglobals.DIEGO_RECEPTOR ), | |
"ltc-%s-8080.%s" % (session.SCREENNAME.lower(), myglobals.DIEGO_RECEPTOR ) | |
], | |
"port": 8080 | |
} | |
] | |
}, | |
"log_guid": "ltc-%s" % session.SCREENNAME.lower(), | |
"ports": [ | |
8080 | |
], | |
"memory_mb": MEMORY, | |
"instances": INSTANCES, | |
"cpu_weight": 100, | |
"env": [ | |
{ | |
"name": "HOME", | |
"value": "/" | |
}, | |
{ | |
"name": "APP_KEY", | |
"value": "%s" % myglobals.APP_KEY | |
}, | |
{ | |
"name": "APP_SECRET", | |
"value": "%s" % myglobals.APP_SECRET | |
}, | |
{ | |
"name": "OAUTH_TOKEN_SECRET", | |
"value": "%s" % session.USER_OAUTH_TOKEN_SECRET | |
}, | |
{ | |
"name": "MAX_CHART_SIZE", | |
"value": "500" | |
}, | |
{ | |
"name": "INCLUDE_TWITTER_HASH", | |
"value": "%s" % ','.join(session.TRACK) | |
}, | |
{ | |
"name": "PROCESS_GUID", | |
"value": "ltc-%s" % session.SCREENNAME.lower() | |
}, | |
{ | |
"name": "PATH", | |
"value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" | |
}, | |
{ | |
"name": "OAUTH_TOKEN", | |
"value": "%s" % session.USER_OAUTH_TOKEN | |
}, | |
{ | |
"name": "VCAP_APP_PORT", | |
"value": "8080" | |
}, | |
{ | |
"name": "PORT", | |
"value": "8080" | |
} | |
], | |
"action": { | |
"run": { | |
"path": "/bin/sh", | |
"args": [ | |
"-c", | |
"python -u bot.py" | |
], | |
"resource_limits": {}, | |
"user": "vcap", | |
"dir": "/ltc-twitter-sentiment-demo" | |
} | |
}, | |
"process_guid": "ltc-%s" % session.SCREENNAME.lower(), | |
"rootfs": "docker:///bbertka/ltc-twitter-sentiment-demo#latest" | |
} | |
headers = auth_header | |
r = requests.post("http://receptor.%s/v1/desired_lrps" % myglobals.DIEGO_RECEPTOR, data=json.dumps(query), headers=headers) | |
return r.content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment