Skip to content

Instantly share code, notes, and snippets.

@jerith
Last active May 25, 2016 11:54
Show Gist options
  • Save jerith/f3da3a7186876502e915190f245bdb54 to your computer and use it in GitHub Desktop.
Save jerith/f3da3a7186876502e915190f245bdb54 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from HTMLParser import HTMLParser
from urlparse import urlparse
import requests
MC2_URL = 'http://mc2.infr.controller.seed-stack.local'
class MC2Wrangler(object):
ORG_PATH = '/admin/organizations/organization/'
ADD_ORG_PATH = ORG_PATH + 'add/'
SELECT_ORG_PATH = '/organizations/%s/select/'
ADD_APP_PATH = '/docker/add/'
def __init__(self, mc2_url):
self.base_url = mc2_url
self.session = requests.Session()
def url(self, path):
if urlparse(path).scheme:
return path
return ''.join([self.base_url, path])
def get(self, path, **kw):
return self.session.get(self.url(path), **kw)
def post(self, path, **kw):
return self.session.post(self.url(path), **kw)
def post_form(self, path, data, **kw):
r = self.get(path)
data = data.copy()
data['csrfmiddlewaretoken'] = r.cookies['csrftoken']
return self.post(path, data=data, **kw)
def login(self, user, pw):
login_url = self.get('/').url
return self.post_form(login_url, {'username': user, 'password': pw})
def list_orgs(self):
r = self.get(self.ORG_PATH)
dalp = DjangoAdminListParser()
dalp.feed(r.text)
return dalp.items
def add_org(self, name):
orgs = self.list_orgs()
if name in orgs:
print "Org %s already exists, not creating." % (name,)
return
return self.post_form(self.ADD_ORG_PATH, data={
"name": name,
"slug": name,
"organizationuserrelation_set-TOTAL_FORMS": "1",
"organizationuserrelation_set-INITIAL_FORMS": "0",
"organizationuserrelation_set-MIN_NUM_FORMS": "0",
"organizationuserrelation_set-MAX_NUM_FORMS": "1000",
"organizationuserrelation_set-0-user": "1",
"organizationuserrelation_set-0-is_admin": "on",
"organizationuserrelation_set-0-organization": "",
"organizationuserrelation_set-0-id": "",
"organizationuserrelation_set-__prefix__-user": "",
"organizationuserrelation_set-__prefix__-organization": "",
"organizationuserrelation_set-__prefix__-id": "",
"_save": "Save",
})
def select_org(self, name):
return self.get(self.SELECT_ORG_PATH % (name,))
def add_app(self, name, docker_image, **kw):
app_fields = {
"name": name,
"description": "",
"domain_urls": "",
"docker_image": docker_image,
"port": "8080",
"volume_needed": "False",
"volume_path": "",
"env-TOTAL_FORMS": "1",
"env-INITIAL_FORMS": "0",
"env-MIN_NUM_FORMS": "0",
"env-MAX_NUM_FORMS": "1000",
"env-0-id": "",
"env-0-key": "",
"env-0-value": "",
"label-TOTAL_FORMS": "1",
"label-INITIAL_FORMS": "0",
"label-MIN_NUM_FORMS": "0",
"label-MAX_NUM_FORMS": "1000",
"label-0-id": "",
"label-0-name": "",
"label-0-value": "",
"marathon_cmd": "",
"marathon_cpus": "",
"marathon_mem": "32",
"marathon_instances": "",
"marathon_health_check_path": "",
"webhook_token": "",
}
app_fields.update(kw)
return self.post_form(self.ADD_APP_PATH, data=app_fields)
class DjangoAdminListParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.items = []
self.found_item = False
def handle_starttag(self, tag, attrs):
if tag == 'th' and ('class', 'field-name') in attrs:
self.found_item = True
def handle_data(self, data):
if self.found_item:
self.items.append(data)
self.found_item = False
w = MC2Wrangler(MC2_URL)
r = w.login('admin', 'pass')
print 'login:', r
r = w.add_org('x')
print 'add org:', r
r = w.select_org('x')
print 'select org:', r
r = w.add_app(
'python server',
docker_image='python:3-alpine',
marathon_cmd='python3 -m http.server 8080')
print 'add app:', r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment