[tor-commits] [ooni-probe/master] ImStart implementing OONIBackenD

art at torproject.org art at torproject.org
Sat Aug 18 03:54:38 UTC 2012


commit 1d8240c746ec79381aafcb3d921568c90b70889b
Author: Arturo Filastò <art at torproject.org>
Date:   Fri Aug 17 20:57:32 2012 -0700

    ImStart implementing OONIBackenD
    (saving this commit from my broken laptop)
---
 oonib/INSTALL               |    4 ++
 oonib/report/api.py         |   98 +++++++++++++++++++++++++++++++++++++++++++
 oonib/report/db/__init__.py |   33 ++++++++++++++
 oonib/report/db/models.py   |   41 ++++++++++++++++++
 4 files changed, 176 insertions(+), 0 deletions(-)

diff --git a/oonib/INSTALL b/oonib/INSTALL
new file mode 100644
index 0000000..1622707
--- /dev/null
+++ b/oonib/INSTALL
@@ -0,0 +1,4 @@
+BEWARE: This requires python 2.7.3
+storm (Storm ORM)
+transaction (zope transaction)
+
diff --git a/oonib/report/api.py b/oonib/report/api.py
new file mode 100644
index 0000000..04f70e0
--- /dev/null
+++ b/oonib/report/api.py
@@ -0,0 +1,98 @@
+"""
+/report/do
+
+/report/pcap
+
+This is the async pcap reporting system. It requires the client to have created a report already, but can work independently from test progress.
+
+"""
+import random
+import strings
+from twisted.internet import reactor
+from cyclone import web
+backend_version = '0.0.1'
+
+def generateReportID():
+    size = 100
+    id = ''.join(random.choice(strings.ascii_letters) for x in range(size))
+    return id
+
+def newReport(software_name, software_version, test_name, test_version,
+               progress, content):
+    print "Software Name: %s" % software_name
+    print "Software Version: %s" % software_version
+    print "Test Name: %s" % test_name
+    print "Test Version: %s" % test_version
+    print "Progress: %s" % progress
+    print "Content: %s" % content
+    reportId = generateReportID()
+    return {'backend_version': backend_version, 'report_id': reportID}
+
+def updateReport(report_id, content):
+    print "Report ID: %s" % report_id
+    print "Content: %s" % content
+    return {'backend_version': '0.1', 'report_id': 'FOOBAR'}
+
+class DoReportHandler(web.RequestHandler):
+    """
+    Responsible for creating and updating reports.
+    """
+    def post(self):
+        """
+        Creates a new report with the input
+
+        * Request
+
+          {'software_name': 'XXX',
+           'software_version': 'XXX',
+           'test_name': 'XXX',
+           'test_version': 'XXX',
+           'progress': 'XXX',
+           'content': 'XXX'
+           }
+
+        * Response
+
+          {'backend_version': 'XXX', 'report_id': 'XXX'}
+
+        """
+        # This is the list of supported arguments
+        arguments = ['software_name', 'software_version',
+                     'test_name','test_version',
+                     'progress', 'content']
+        report = {}
+        error = None
+        for arg in arguments:
+            if len(self.get_arguments(arg)) == 0:
+                print "No %s specified" % arg
+                error = arg
+                break
+            report[arg] = self.get_argument(arg)
+        if not error:
+            self.write(newReport(**report))
+
+    def put(self):
+        """
+        Update an already existing report.
+
+          {'report_id': 'XXX',
+           'content': 'XXX'
+          }
+        """
+        pass
+
+class PCAPReportHandler(web.RequestHandler):
+    def get(self):
+        pass
+
+    def post(self):
+        pass
+
+handlers = [(r"/report/do", DoReportHandler),
+            (r"/report/pcap", PCAPReportHandler)]
+
+reporting = web.Application(handlers)
+reactor.listenTCP(8888, reporting, interface="127.0.0.1")
+print "starting bullshit"
+reactor.run()
+
diff --git a/oonib/report/db/__init__.py b/oonib/report/db/__init__.py
new file mode 100644
index 0000000..1248c19
--- /dev/null
+++ b/oonib/report/db/__init__.py
@@ -0,0 +1,33 @@
+__all__ = ['models']
+
+from twisted.python import log
+from twisted.python.threadpool import ThreadPool
+from twisted.internet.defer import inlineCallbacks
+from storm.twisted.transact import Transactor, transact
+from storm.locals import *
+
+dbtype = "sqlite"
+
+if dbtype == "sqlite":
+    from storm.uri import URI
+    from storm.databases.sqlite import SQLite
+    database = SQLite(URI('sqlite:///test.db'))
+
+threadpool = ThreadPool(0, 10)
+threadpool.start()
+transactor = Transactor(threadpool)
+
+ at inlineCallbacks
+def create_tables():
+    def create(query):
+        store = Store(database)
+        store.execute(query)
+        store.commit()
+
+    for x in models.__all__:
+        query = getattr(models.__getattribute__(x), 'create_query')
+        try:
+            yield transactor.run(create, query)
+        except:
+            log.msg("Failing in creating table for %s. Maybe it already exists?" % x)
+
diff --git a/oonib/report/db/models.py b/oonib/report/db/models.py
new file mode 100644
index 0000000..b4e56d0
--- /dev/null
+++ b/oonib/report/db/models.py
@@ -0,0 +1,41 @@
+from storm.twisted.transact import transact
+from storm.locals import *
+
+class Report(object):
+    """
+    This represents an OONI Report as stored in the database.
+
+    report_id: this is generated by the backend and is used by the client to
+               reference a previous report and append to it. It should be
+               treated as a shared secret between the probe and backend.
+
+    software_name: this indicates the name of the software performing the test
+                   (this will default to ooniprobe)
+
+    software_version: this is the version number of the software running the
+                      test.
+
+    test_name: the name of the test on which the report is being created.
+
+    test_version: indicates the version of the test
+
+    progress: what is the current progress of the report. This allows clients
+              to report event partial reports up to a certain percentage of
+              progress. Once the report is complete progress will be 100.
+
+    content: what is the content of the report. If the current progress is less
+             than 100 we should append to the YAML data structure that is
+             currently stored in such field.
+    """
+    id = Int(primary=True)
+
+    report_id = Unicode()
+
+    software_name = Unicode()
+    software_version = Unicode()
+    test_name = Unicode()
+    test_version = Unicode()
+    progress = Unicode()
+
+    content = Unicode()
+



More information about the tor-commits mailing list