diff options
author | 2011-06-14 23:13:08 +0530 | |
---|---|---|
committer | 2011-06-14 23:13:08 +0530 | |
commit | f54a02be8f73be30e0607c8ca5c887a12f426c53 (patch) | |
tree | 7b89a291fa21500fa850650c0d5dc0018757b321 /server | |
parent | fix client url (diff) | |
download | gentoostats-f54a02be8f73be30e0607c8ca5c887a12f426c53.tar.gz gentoostats-f54a02be8f73be30e0607c8ca5c887a12f426c53.tar.bz2 gentoostats-f54a02be8f73be30e0607c8ca5c887a12f426c53.zip |
updated server code, added host page
Diffstat (limited to 'server')
-rw-r--r-- | server/config.py | 14 | ||||
-rw-r--r-- | server/helpers.py | 49 | ||||
-rw-r--r-- | server/host.py | 138 | ||||
-rw-r--r-- | server/index.py | 8 | ||||
-rwxr-xr-x | server/main.py | 44 | ||||
-rw-r--r-- | server/post.py | 81 | ||||
-rw-r--r-- | server/templates/host.html | 56 | ||||
-rw-r--r-- | server/templates/index.html | 9 | ||||
-rw-r--r-- | server/templates/stats.html | 9 |
9 files changed, 253 insertions, 155 deletions
diff --git a/server/config.py b/server/config.py index 5ef7087..f4e74cd 100644 --- a/server/config.py +++ b/server/config.py @@ -1,4 +1,18 @@ + import web +db = web.database( + dbn='mysql', + user='gentoo', + pw='gentoo', + db='gentoostats' + ) + render = web.template.render('templates/', base='layout') +def notfound(): + return web.notfound(render.error_404()) + +def internalerror(): + return web.internalerror(render.error_500()) + diff --git a/server/helpers.py b/server/helpers.py index ad70e31..ebd53a3 100644 --- a/server/helpers.py +++ b/server/helpers.py @@ -2,6 +2,12 @@ import uuid import re +# check valid uuid + +def is_uuid(uuid): + regex = re.compile(r'^(\w{8})-(\w{4})-(\w{4})-(\w{4})-(\w{12})$') + return regex.search(uuid) + # convert uuid string to raw bytes def uuidbin(string): @@ -31,59 +37,58 @@ def pkgsplit(pkgname): # lookup key and insert if not found def get_kwkey(db, keyword): - db_keyword = db.select('keywords', vars={'keyword':keyword}, where='keyword=$keyword') + db_keyword = db.select('KEYWORDS', vars={'keyword':keyword}, where='KEYWORD=$keyword') if len(db_keyword): - kwkey = db_keyword[0].kwkey + kwkey = db_keyword[0]['KWKEY'] else: - kwkey = db.insert('keywords', keyword=keyword) + kwkey = db.insert('KEYWORDS', KEYWORD=keyword) return kwkey def get_lkey(db, lang): - db_lang = db.select('lang', vars={'lang':lang}, where='lang=$lang') + db_lang = db.select('LANG', vars={'lang':lang}, where='LANG=$lang') if len(db_lang): - lkey = db_lang[0].lkey + lkey = db_lang[0]['LKEY'] else: - lkey = db.insert('lang', lang=lang) + lkey = db.insert('LANG', LANG=lang) return lkey def get_fkey(db, feature): - db_feature = db.select('features', vars={'feature':feature}, where='feature=$feature') + db_feature = db.select('FEATURES', vars={'feature':feature}, where='FEATURE=$feature') if len(db_feature): - fkey = db_feature[0].fkey + fkey = db_feature[0]['FKEY'] else: - fkey = db.insert('features', feature=feature) + fkey = db.insert('FEATURES', FEATURE=feature) return fkey def get_mkey(db, mirror): - db_mirror = db.select('gentoo_mirrors', vars={'mirror':mirror}, where='mirror=$mirror') + db_mirror = db.select('GENTOO_MIRRORS', vars={'mirror':mirror}, where='MIRROR=$mirror') if len(db_mirror): - mkey = db_mirror[0].mkey + mkey = db_mirror[0]['MKEY'] else: - mkey = db.insert('gentoo_mirrors', mirror=mirror) + mkey = db.insert('GENTOO_MIRRORS', MIRROR=mirror) return mkey def get_ukey(db, useflag): - db_useflag = db.select('useflags', vars={'useflag':useflag}, where='useflag=$useflag') + db_useflag = db.select('USEFLAGS', vars={'useflag':useflag}, where='USEFLAG=$useflag') if len(db_useflag): - ukey = db_useflag[0].ukey + ukey = db_useflag[0]['UKEY'] else: - ukey = db.insert('useflags', useflag=useflag) + ukey = db.insert('USEFLAGS', USEFLAG=useflag) return ukey def get_pkey(db, package): cpv = pkgsplit(package) - db_package = db.select('packages', vars=cpv, where='cat=$cat and pkg=$pkg and ver=$ver') + db_package = db.select('PACKAGES', vars=cpv, where='CAT=$cat and PKG=$pkg and VER=$ver') if len(db_package): - pkey = db_package[0].pkey + pkey = db_package[0]['PKEY'] else: - pkey = db.insert('packages', cat=cpv['cat'], pkg=cpv['pkg'], ver=cpv['ver']) + pkey = db.insert('PACKAGES', CAT=cpv['cat'], PKG=cpv['pkg'], VER=cpv['ver']) return pkey def get_rkey(db, repo): - db_repo = db.select('repositories', vars={'repo':repo}, where='repo=$repo') + db_repo = db.select('REPOSITORIES', vars={'repo':repo}, where='REPO=$repo') if len(db_repo): - rkey = db_repo[0].rkey + rkey = db_repo[0]['RKEY'] else: - rkey = db.insert('repositories', repo=repo) + rkey = db.insert('REPOSITORIES', REPO=repo) return rkey - diff --git a/server/host.py b/server/host.py new file mode 100644 index 0000000..50df581 --- /dev/null +++ b/server/host.py @@ -0,0 +1,138 @@ + +import web +import json +import helpers +import config +from config import render, db + +class Host(object): + + def GET(self, str_uuid): + if not helpers.is_uuid(str_uuid): + return config.notfound() + + uuid = helpers.uuidbin(str_uuid) + hosts = db.select('HOSTS', vars={'uuid':uuid}, where='UUID=$uuid', what='UUID') + if len(hosts) == 0: + return config.notfound() + + get_data = dict() + get_data['UUID'] = str_uuid + + env = db.select('ENV', vars={'uuid':uuid}, where="UUID=$uuid") + e = env[0] + for var in ['PLATFORM','LASTSYNC','PROFILE','ARCH','CHOST','CFLAGS','CXXFLAGS','FFLAGS','LDFLAGS','MAKEOPTS','SYNC']: + get_data[var] = e[var] + + get_data['FEATURES'] = list() + features = db.query('SELECT FEATURE FROM HOST_FEATURES NATURAL JOIN FEATURES WHERE UUID=$uuid', vars={'uuid':uuid}) + for f in features: + get_data['FEATURES'].append(f['FEATURE']) + + get_data['ACCEPT_KEYWORDS'] = list() + keywords = db.query('SELECT KEYWORD FROM GLOBAL_KEYWORDS NATURAL JOIN KEYWORDS WHERE UUID=$uuid', vars={'uuid':uuid}) + for k in keywords: + get_data['ACCEPT_KEYWORDS'].append(k['KEYWORD']) + + get_data['USE'] = list() + useflags = db.query('SELECT USEFLAG FROM GLOBAL_USEFLAGS NATURAL JOIN USEFLAGS WHERE UUID=$uuid', vars={'uuid':uuid}) + for u in useflags: + get_data['USE'].append(u['USEFLAG']) + + get_data['LANG'] = list() + lang = db.query('SELECT LANG FROM HOST_LANG NATURAL JOIN LANG WHERE UUID=$uuid', vars={'uuid':uuid}) + for l in lang: + get_data['LANG'].append(l['LANG']) + + get_data['GENTOO_MIRRORS'] = list() + mirrors = db.query('SELECT MIRROR FROM HOST_MIRRORS NATURAL JOIN GENTOO_MIRRORS WHERE UUID=$uuid', vars={'uuid':uuid}) + for m in mirrors: + get_data['GENTOO_MIRRORS'].append(m['MIRROR']) + + get_data['PACKAGES'] = dict() + packages = db.query('SELECT CAT, PKG, VER FROM INSTALLED_PACKAGES NATURAL JOIN PACKAGES WHERE UUID=$uuid ORDER BY CAT, PKG, VER', vars={'uuid':uuid}) + for p in packages: + cpv = p['CAT'] + '/' + p['PKG'] + '-' + p['VER'] + get_data['PACKAGES'][cpv] = dict() + + return render.host(get_data) + + def POST(self, str_uuid): + post_data = json.JSONDecoder().decode(web.data()) + + #TODO: Handle exceptions + if post_data['PROTOCOL'] != 1: + return 'Unsupported protocol!' + + if post_data['AUTH']['UUID'] != str_uuid: + return 'Invalid uuid!' + + uuid = helpers.uuidbin(str_uuid) + + # Insert in hosts + db_host = db.select('HOSTS', vars={'uuid':uuid}, where='UUID=$uuid') + if len(db_host): + if post_data['AUTH']['PASSWD'] != db_host[0]['PASSWD']: + return 'Wrong password!' + # This should delete all host entries from all tables + db.delete('HOSTS', vars={'uuid':uuid}, where='UUID=$uuid') + db.insert('HOSTS', UUID=uuid, PASSWD=post_data['AUTH']['PASSWD']) + + # Insert in env + db.insert('ENV', UUID=uuid, ARCH=post_data['ARCH'], CHOST=post_data['CHOST'], CFLAGS=post_data['CFLAGS'], + CXXFLAGS=post_data['CXXFLAGS'], FFLAGS=post_data['FFLAGS'], LDFLAGS=post_data['LDFLAGS'], + MAKEOPTS=post_data['MAKEOPTS'], SYNC=post_data['SYNC'], PLATFORM=post_data['PLATFORM'], + PROFILE=post_data['PROFILE'], LASTSYNC=post_data['LASTSYNC']) + + # Insert in GLOBAL_KEYWORDS + for keyword in post_data['ACCEPT_KEYWORDS']: + kwkey = helpers.get_kwkey(db, keyword) + db.insert('GLOBAL_KEYWORDS', UUID=uuid, KWKEY=kwkey) + + # Insert in HOST_LANG + for lang in post_data['LANG']: + lkey = helpers.get_lkey(db, lang) + db.insert('HOST_LANG', UUID=uuid, LKEY=lkey) + + # Insert in HOST_FEATURES + for feature in post_data['FEATURES']: + fkey = helpers.get_fkey(db, feature) + db.insert('HOST_FEATURES', UUID=uuid, FKEY=fkey) + + # Insert in HOST_MIRRORS + for mirror in post_data['GENTOO_MIRRORS']: + mkey = helpers.get_mkey(db, mirror) + db.insert('HOST_MIRRORS', UUID=uuid, MKEY=mkey) + + # Insert in GLOBAL_USEFLAGS + for useflag in post_data['USE']: + ukey = helpers.get_ukey(db, useflag) + db.insert('GLOBAL_USEFLAGS', UUID=uuid, UKEY=ukey) + + # Handle PACKAGES + for package in post_data['PACKAGES'].keys(): + pkey = helpers.get_pkey(db, package) + post_data_pkg = post_data['PACKAGES'][package] + kwkey = helpers.get_kwkey(db, post_data_pkg['KEYWORD']) + rkey = helpers.get_rkey(db, post_data_pkg['REPO']) + + # Insert in INSTALLED_PACKAGES + ipkey = db.insert('INSTALLED_PACKAGES', UUID=uuid, PKEY=pkey, BUILD_TIME=post_data_pkg['BUILD_TIME'], + COUNTER=post_data_pkg['COUNTER'], KWKEY=kwkey, RKEY=rkey, SIZE=post_data_pkg['SIZE']) + + # Insert in PLUS_USEFLAGS + for useflag in post_data_pkg['USE']['PLUS']: + ukey = helpers.get_ukey(db, useflag) + db.insert('PLUS_USEFLAGS', IPKEY=ipkey, UKEY=ukey) + + # Insert in MINUS_USEFLAGS + for useflag in post_data_pkg['USE']['MINUS']: + ukey = helpers.get_ukey(db, useflag) + db.insert('MINUS_USEFLAGS', IPKEY=ipkey, UKEY=ukey) + + # Insert in UNSET_USEFLAGS + for useflag in post_data_pkg['USE']['UNSET']: + ukey = helpers.get_ukey(db, useflag) + db.insert('UNSET_USEFLAGS', IPKEY=ipkey, UKEY=ukey) + + return 'POST for ' + str_uuid + ' successful' diff --git a/server/index.py b/server/index.py new file mode 100644 index 0000000..038ae51 --- /dev/null +++ b/server/index.py @@ -0,0 +1,8 @@ + +from config import render, db + +class Index(object): + def GET(self): + hosts = db.select('hosts', what='count(uuid) as count') + count = hosts[0].count + return render.index(count) diff --git a/server/main.py b/server/main.py index 08b8509..9eb74fa 100755 --- a/server/main.py +++ b/server/main.py @@ -2,49 +2,19 @@ import web import config -import json from config import render -from post import handler +from index import Index +from host import Host urls = ( - r'/', 'index', - r'/(.+)', 'stats' -) - -db = web.database( - dbn='mysql', - user='gentoo', - pw='gentoo', - db='gentoostats' - ) - -class index: - def GET(self): - hosts = db.select('hosts') - return render.index(hosts) - -class stats: - def GET(self, uuid): - if uuid == 'favicon.ico': - return notfound() - hosts = db.select('hosts', vars={'uuid':uuid}, where="uuid=$uuid") - env = db.select('env', vars={'uuid':uuid}, where="uuid=$uuid") - return render.stats(uuid, hosts, env) - - def POST(self, uuid): - post_data = json.JSONDecoder().decode(web.data()) - return handler(uuid, post_data, db) - -def notfound(): - return web.notfound(render.error_404()) - -def internalerror(): - return web.internalerror(render.error_500()) + r'/', 'Index', + r'/host/(.+)', 'Host' + ) app = web.application(urls, globals()) -app.notfound = notfound -app.internalerror = internalerror +app.notfound = config.notfound +app.internalerror = config.internalerror if __name__ == "__main__": app.run() diff --git a/server/post.py b/server/post.py deleted file mode 100644 index b9f4deb..0000000 --- a/server/post.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python - -from helpers import * - -def handler(uuid_str, data, db): - #TODO: Handle exceptions - if data['PROTOCOL'] != 1: - return 'Unsupported protocol!' - - if data['AUTH']['UUID'] != uuid_str: - return 'Invalid uuid!' - - uuid = uuidbin(uuid_str) - - # Insert in hosts - db_host = db.select('hosts', vars={'uuid':uuid}, where='uuid=$uuid') - if len(db_host): - if data['AUTH']['PASSWD'] != db_host[0].passwd: - return 'Wrong password!' - # This should delete all host entries from all tables - db.delete('hosts', vars={'uuid':uuid}, where='uuid=$uuid') - db.insert('hosts', uuid=uuid, passwd=data['AUTH']['PASSWD']) - - # Insert in env - db.insert('env', uuid=uuid, arch=data['ARCH'], chost=data['CHOST'], cflags=data['CFLAGS'], - cxxflags=data['CXXFLAGS'], fflags=data['FFLAGS'], ldflags=data['LDFLAGS'], - makeopts=data['MAKEOPTS'], sync=data['SYNC'], platform=data['PLATFORM'], - profile=data['PROFILE'], lastsync=data['LASTSYNC']) - - # Insert in global_keywords - for keyword in data['ACCEPT_KEYWORDS']: - kwkey = get_kwkey(db, keyword) - db.insert('global_keywords', uuid=uuid, kwkey=kwkey) - - # Insert in host_lang - for lang in data['LANG']: - lkey = get_lkey(db, lang) - db.insert('host_lang', uuid=uuid, lkey=lkey) - - # Insert in host_features - for feature in data['FEATURES']: - fkey = get_fkey(db, feature) - db.insert('host_features', uuid=uuid, fkey=fkey) - - # Insert in host_mirrors - for mirror in data['GENTOO_MIRRORS']: - mkey = get_mkey(db, mirror) - db.insert('host_mirrors', uuid=uuid, mkey=mkey) - - # Insert in global_useflags - for useflag in data['USE']: - ukey = get_ukey(db, useflag) - db.insert('global_useflags', uuid=uuid, ukey=ukey) - - # Handle packages - for package in data['PACKAGES'].keys(): - pkey = get_pkey(db, package) - data_pkg = data['PACKAGES'][package] - kwkey = get_kwkey(db, data_pkg['KEYWORD']) - rkey = get_rkey(db, data_pkg['REPO']) - - # Insert in installed_packages - ipkey = db.insert('installed_packages', uuid=uuid, pkey=pkey, build_time=data_pkg['BUILD_TIME'], - counter=data_pkg['COUNTER'], kwkey=kwkey, rkey=rkey, size=data_pkg['SIZE']) - - # Insert in plus_useflags - for useflag in data_pkg['USE']['PLUS']: - ukey = get_ukey(db, useflag) - db.insert('plus_useflags', ipkey=ipkey, ukey=ukey) - - # Insert in minus_useflags - for useflag in data_pkg['USE']['MINUS']: - ukey = get_ukey(db, useflag) - db.insert('minus_useflags', ipkey=ipkey, ukey=ukey) - - # Insert in unset_useflags - for useflag in data_pkg['USE']['UNSET']: - ukey = get_ukey(db, useflag) - db.insert('unset_useflags', ipkey=ipkey, ukey=ukey) - - return 'POST for ' + uuid_str + ' successful' diff --git a/server/templates/host.html b/server/templates/host.html new file mode 100644 index 0000000..04b1bff --- /dev/null +++ b/server/templates/host.html @@ -0,0 +1,56 @@ +$def with (data) +$var title: Host + +Stats for host $data['UUID'] : <br/> <br/> +<ul> +<li>Platform : $data['PLATFORM']</li> +<li>Profile : $data['PROFILE']</li> +<li>Timestamp of tree : $data['LASTSYNC']</li> +<li>ARCH : $data['ARCH']</li> +<li>CHOST : $data['CHOST']</li> +<li>CFLAGS : $data['CFLAGS']</li> +<li>CXXFLAGS : $data['CXXFLAGS']</li> +<li>FFLAGS : $data['FFLAGS']</li> +<li>LDLAGS : $data['LDFLAGS']</li> +<li>MAKEOPTS : $data['MAKEOPTS']</li> +<li>SYNC : $data['SYNC']</li> +<li>FEATURES : +<ul> +$for f in data['FEATURES']: + <li>$f</li> +</ul> +</li> +<li>ACCEPT_KEYWORDS : +<ul> +$for k in data['ACCEPT_KEYWORDS']: + <li>$k</li> +</ul> +</li> +<li>USE : +<ul> +$for u in data['USE']: + <li>$u</li> +</ul> +</li> +<li>LANG : +<ul> +$for l in data['LANG']: + <li>$l</li> +</ul> +</li> +<li>GENTOO_MIRRORS : +<ul> +$for m in data['GENTOO_MIRRORS']: + <li>$m</li> +</ul> +</li> +<li>Installed packages : +<ul> +$code: + packages = data['PACKAGES'].keys() + packages.sort() +$for p in packages: + <li>$p</li> +</ul> +</li> +</ul> diff --git a/server/templates/index.html b/server/templates/index.html index 76c16e5..86960c0 100644 --- a/server/templates/index.html +++ b/server/templates/index.html @@ -1,9 +1,6 @@ -$def with (hosts) +$def with (count) $var title: Gentoostats Welcome to the gentoostats webapp <br/> - -List of hosts: <br/> - -$for host in hosts: - <li><a href=$host.uuid>$host.uuid</li> +<br/> +Number of hosts: $count<br/> diff --git a/server/templates/stats.html b/server/templates/stats.html deleted file mode 100644 index 7081b02..0000000 --- a/server/templates/stats.html +++ /dev/null @@ -1,9 +0,0 @@ -$def with (uuid, hosts, env) -$var title: Stats - -$if len(hosts): - Stats for host $uuid : <br/> - $for e in env: - <li>$e.var = "$e.value"</li> -$else: - Host does not exist in records! |