aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVikraman Choudhury <vikraman.choudhury@gmail.com>2011-05-12 03:33:44 +0530
committerVikraman Choudhury <vikraman.choudhury@gmail.com>2011-05-12 03:33:44 +0530
commit33a30c369c78d8b992f326e96bbc9d03784ace76 (patch)
treea48ade1c141237d7d1aecdee02668fcc3db383b0
parentsql tables created (diff)
downloadgentoostats-33a30c369c78d8b992f326e96bbc9d03784ace76.tar.gz
gentoostats-33a30c369c78d8b992f326e96bbc9d03784ace76.tar.bz2
gentoostats-33a30c369c78d8b992f326e96bbc9d03784ace76.zip
server working, can POST data successfully :)
-rwxr-xr-xserver/main.py22
-rw-r--r--server/post.py57
-rw-r--r--server/sql/init.sql18
-rw-r--r--server/templates/index.html7
-rw-r--r--server/templates/stats.html9
5 files changed, 98 insertions, 15 deletions
diff --git a/server/main.py b/server/main.py
index 51ae445..92e24ce 100755
--- a/server/main.py
+++ b/server/main.py
@@ -2,26 +2,36 @@
import web
import config
-from config import render
import json
+from config import render
+from post import handler
urls = (
r'/', 'index',
r'/(.+)', 'stats'
)
+db = web.database(
+ dbn='mysql',
+ user='vh4x0r',
+ pw='vh4x0r',
+ db='gentoostats'
+ )
+
class index:
def GET(self):
- return render.index()
+ hosts = db.select('hosts')
+ return render.index(hosts)
class stats:
def GET(self, uuid):
- return '<html><body>GET success</body></html>'
+ hosts = db.select('hosts', vars=locals(), where="uuid=$uuid")
+ env = db.select('env', vars=locals(), where="uuid=$uuid")
+ return render.stats(uuid, hosts, env)
def POST(self, uuid):
- pdata = json.JSONDecoder().decode(web.data())
- print pdata
- return 'Post for ' + uuid + ' successful'
+ post_data = json.JSONDecoder().decode(web.data())
+ return handler(uuid, post_data, db)
def notfound():
return web.notfound(render.error_404())
diff --git a/server/post.py b/server/post.py
new file mode 100644
index 0000000..4cd0712
--- /dev/null
+++ b/server/post.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+import re
+
+def pkgsplit(pkgname):
+ #TODO: Improve this
+ cpv={}
+ pkgsplit = pkgname.split('/',1)
+ cpv['cat'] = pkgsplit[0]
+
+ pv_re =re.compile(r'(?x)^(?P<pn>[\w\+][\w\+-]*?(?P<pn_inval>-(cvs\.)?(\d+)((\.\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\d*)*)(-r(\d+))?)?)-(?P<ver>(cvs\.)?(\d+)((\.\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\d*)*))(-r(?P<rev>\d+))?$')
+ m = pv_re.match(pkgsplit[1])
+ cpv['pn'] = m.group('pn')
+ rev = m.group('rev')
+ if rev is None:
+ cpv['ver'] = m.group('ver')
+ else:
+ cpv['ver'] = m.group('ver') + '-r' + rev
+
+ return cpv
+
+def handler(uuid, data, db):
+ if data['PROTOCOL'] != 1:
+ return 'Unsupported protocol!'
+
+ if data['AUTH']['UUID'] != uuid:
+ return 'Invalid uuid!'
+
+ host = db.select('hosts', vars={'uuid':uuid}, where='uuid=$uuid')
+ if len(host):
+ if data['AUTH']['PASSWD'] == host[0].passwd:
+ exists = True
+ else:
+ return 'Wrong password!'
+ else:
+ db.insert('hosts', uuid=uuid, passwd=data['AUTH']['PASSWD'])
+ exists = False
+
+ if exists:
+ db.delete('env', vars={'uuid':uuid}, where='uuid=$uuid')
+
+ for var in ['CFLAGS', 'CXXFLAGS', 'LDFLAGS', 'CHOST', 'FEATURES']:
+ db.insert('env', uuid=uuid, var=var, value=data[var])
+
+ if exists:
+ db.delete('useflags', vars={'uuid':uuid}, where='uuid=$uuid')
+
+ pkg = data['PACKAGES']
+ for cpv in pkg.keys():
+ t = pkgsplit(cpv)
+ db.insert('packages', cat=t['cat'], pkg=t['pn'], ver=t['ver'])
+ pkey = db.select('packages', vars={'cat':t['cat'], 'pkg':t['pn'], 'ver':t['ver']},
+ where='cat=$cat and pkg=$pkg and ver=$ver', what='pkey')[0].pkey
+ for use in pkg[cpv]:
+ db.insert('useflags', uuid=uuid, useflag=use, pkey=str(pkey))
+
+ return 'POST for ' + uuid + ' successful'
diff --git a/server/sql/init.sql b/server/sql/init.sql
index 4fa490a..68d309f 100644
--- a/server/sql/init.sql
+++ b/server/sql/init.sql
@@ -11,22 +11,24 @@ create table hosts (
drop table if exists env;
create table env (
uuid varchar (40) references hosts.uuid,
- var varchar (15) not null,
- value varchar (100),
+ var varchar (20) not null,
+ value varchar (512),
primary key (uuid, var)
);
drop table if exists packages;
create table packages (
- cat varchar (20) not null,
- pkg varchar (20) not null,
- ver varchar (20) not null,
- pkey serial primary key
+ cat varchar (40) not null,
+ pkg varchar (40) not null,
+ ver varchar (40) not null,
+ pkey serial,
+ primary key (cat, pkg, ver, pkey)
);
drop table if exists useflags;
create table useflags (
uuid varchar (40) references host.uuid,
- useflag varchar (20) not null,
- pkey serial references packages.pkey
+ useflag varchar (40) not null,
+ pkey bigint unsigned references packages.pkey,
+ primary key (uuid, useflag, pkey)
);
diff --git a/server/templates/index.html b/server/templates/index.html
index 11fafb6..76c16e5 100644
--- a/server/templates/index.html
+++ b/server/templates/index.html
@@ -1,4 +1,9 @@
+$def with (hosts)
$var title: Gentoostats
-Welcome to the gentoostats webapp
+Welcome to the gentoostats webapp <br/>
+List of hosts: <br/>
+
+$for host in hosts:
+ <li><a href=$host.uuid>$host.uuid</li>
diff --git a/server/templates/stats.html b/server/templates/stats.html
new file mode 100644
index 0000000..7081b02
--- /dev/null
+++ b/server/templates/stats.html
@@ -0,0 +1,9 @@
+$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!