[Pkg-debile-commits] [debile-master] 03/26: Added some user function support

Sylvestre Ledru sylvestre at alioth.debian.org
Tue Aug 20 16:22:34 UTC 2013


This is an automated email from the git hooks/post-receive script.

sylvestre pushed a commit to branch master
in repository debile-master.

commit 1405efc9b26763bc99fc9eff484c0f14fbbdad24
Author: Léo Cavaillé <leo at cavaille.net>
Date:   Fri Aug 9 14:34:50 2013 +0200

    Added some user function support
---
 lucy/orm.py    |    5 +++
 lucy/server.py |  136 +++++++++++++++++++++++++++++++++++++++++++++-----------
 skel/lucy.ini  |    3 ++
 3 files changed, 117 insertions(+), 27 deletions(-)

diff --git a/lucy/orm.py b/lucy/orm.py
index ac967a8..ee3fd10 100644
--- a/lucy/orm.py
+++ b/lucy/orm.py
@@ -21,9 +21,14 @@ class User(Base):
     __tablename__ = 'users'
     id = Column(Integer, primary_key=True)
     login = Column(String(100))
+    password = Column(String(100))
     name = Column(String(100))
     email = Column(String(100))
     gpg_fingerprint = Column(String(100))
+    admin = Column(Boolean)
+
+    def serialize(self):
+        return row2dict(self)
 
 
 class Machine(Base):
diff --git a/lucy/server.py b/lucy/server.py
index 94a009d..56581c6 100644
--- a/lucy/server.py
+++ b/lucy/server.py
@@ -17,6 +17,7 @@ import threading
 import os.path
 import os
 import logging
+import gnupg
 
 NAMESPACE = threading.local()
 
@@ -27,6 +28,9 @@ engine = create_engine(config.get('db', 'engine'), echo=True)
 session_factory = sessionmaker(bind=engine)
 Session = scoped_session(session_factory)
 
+gnupg_default_home = os.path.join(os.environ['HOME'], '.gnupg')
+gpg = gnupg.GPG(gnupghome=gnupg_default_home, verbose=False)
+
 def row2dict(row):
     d = {}
     for column in row.__table__.columns:
@@ -71,8 +75,103 @@ def user_method(fn):
             raise Exception("You can't do that")
     return _
 
+def admin_method(fn):
+    def _(*args, **kwargs):
+        try:
+            if not get_user().admin:
+                raise KeyError("You are not an admin, denied")
+            return fn(*args, **kwargs)
+        except KeyError:
+            raise Exception("You can't do that")
+    return _
 
 class LucyInterface(object):
+    @user_method
+    def get_server_info(self):
+        if get_user().admin:
+            admin = "(admin)"
+        else:
+            admin = ""
+        success = "Connected successfully to lucy server, authenticated as %s %s" % (get_user().name, admin)
+        return success
+
+    @admin_method
+    def create_user(self, login, password, name, email, ascii_armored_key):
+        session = Session()
+        # Validate first by creating an entity that login/name/email are valid
+        try:
+            u = User(login=login,
+                     password=password,
+                     name=name,
+                     email=email)
+        except Exception:
+            return None, "Error with login, password, name or email given"
+
+        # Try importing the GPG key to see if it is valid
+        import_result = gpg.import_keys(ascii_armored_key)
+        if import_result.count != 1:
+            # u goes out of scope and does not persist in the DB
+            return None, "Error when importing the key"
+
+        u.gpg_fingerprint = import_result.fingerprints[0]
+
+        try:
+            session.add(u)
+            session.commit()
+        except Exception:
+            session.rollback()
+            return None, "Oops something screwed up"
+
+        user_url = "%s/hacker/%s" % (config.get('paths', 'debuildme'), login)
+
+        return u.serialize(), "User %s with key %s added, see %s" % (name, u.gpg_fingerprint, user_url)
+
+    @user_method
+    def list_user(self):
+        session = Session()
+        users = [ u.serialize() for u in session.query(User).all()]
+        for u in users:
+            u.pop('password', None)
+        return users
+
+    @admin_method
+    def create_machine(self, name, password, ascii_armored_key):
+        session = Session()
+        # Validate first by creating an entity that login/name/email are valid
+        try:
+            m = Machine(name=name,
+                        password=password)
+        except Exception:
+            return None, "Error with name or password given"
+
+        # Try importing the GPG key to see if it is valid
+        import_result = gpg.import_keys(ascii_armored_key)
+        if import_result.count != 1:
+            # u goes out of scope and does not persist in the DB
+            return None, "Error when importing the key"
+
+        m.gpg_fingerprint = import_result.fingerprints[0]
+
+        try:
+            session.add(m)
+            session.commit()
+        except Exception:
+            session.rollback()
+            return None, "Oops something screwed up"
+
+        machine_url = "%s/machine/%s" % (config.get('paths', 'debuildme'), name)
+
+        return m.serialize(), "Machine %s with key %s added, see %s" % (name, m.gpg_fingerprint, machine_url)
+
+    @user_method
+    def list_machine(self):
+        session = Session()
+        machines = [ m.serialize() for m in session.query(Machine).all()]
+        for m in machines:
+            m.pop('password', None)
+        return machines
+
+
     def get_dsc(self, source_id):
         """
         Get the .dsc path if the package is a valid source package. Otherwise
@@ -87,30 +186,13 @@ class LucyInterface(object):
             dsc=s.dsc,
         )
 
-    def get_scanbuildhtml_write_location(self, job_uuid):
-        path = os.path.join(config.get('paths', 'job'), str(job_uuid))
-        # FIXME : awful
-        if not os.path.exists(path):
-            os.makedirs(path)
-        path = os.path.join(path, 'scan-build')
-        # FIXME: unsafe code, we should verify if the job id exists
-        return path
-
-    def get_log_write_location(self, job_uuid):
-        path = os.path.join(config.get('paths', 'job'), str(job_uuid))
-        # FIXME : awful
-        if not os.path.exists(path):
-            os.makedirs(path)
-        path = os.path.join(path, 'log.txt')
-        # FIXME: unsafe code, we should verify if the job id exists
-        return path
-
-    def get_firehose_write_location(self, job_uuid):
+    def get_write_location(self, job_uuid):
+        """
+        Return the path where we put output files for this job UUID.
+        """
         path = os.path.join(config.get('paths', 'job'), str(job_uuid))
-        # FIXME : awful
         if not os.path.exists(path):
             os.makedirs(path)
-        path = os.path.join(path, 'firehose.xml')
         return path
 
     def get_deb_info(self, binary_id):
@@ -156,7 +238,7 @@ class LucyInterface(object):
         Get an unassigned lint job from suite suites, arches arches
         """
         session = Session()
-    
+
         j = session.query(Job).\
                 filter(Job.assigned_at == None).\
                 filter(Job.machine == None).\
@@ -206,7 +288,7 @@ class LucyInterface(object):
         if failed:
             send_failed_email(job, package, report)
 
-        return 
+        return
     #return rid
 
     @machine_method
@@ -250,15 +332,15 @@ class LucyAuthMixIn(SimpleXMLRPCRequestHandler):
             return True
         return self.authenticate_user(entity, password)
 
-    def authenticate_user(self, user_name, password):
+    def authenticate_user(self, user_login, password):
         session = Session()
         try:
-            user = session.query(User).filter(User.name == user_name).one()
+            user = session.query(User).filter(User.login == user_login).one()
             if user.password == password:
                 NAMESPACE.user = user
                 return True
         except NoResultFound, e:
-            logging.error("User claiming name %s does not exist", user_name)        
+            logging.error("User claiming name %s does not exist", user_login)
         return False
 
     def authenticate_machine(self, machine_name, password):
@@ -272,7 +354,7 @@ class LucyAuthMixIn(SimpleXMLRPCRequestHandler):
                 session.commit()
                 return True
         except NoResultFound, e:
-            logging.error("Machine claiming name %s does not exist", machine_name)        
+            logging.error("Machine claiming name %s does not exist", machine_name)
         return False
 
     def parse_request(self, *args):
diff --git a/skel/lucy.ini b/skel/lucy.ini
index ee47a6a..2193728 100644
--- a/skel/lucy.ini
+++ b/skel/lucy.ini
@@ -30,6 +30,9 @@ arches=amd64,i386
 ; Different paths needed to access or to store lucy files
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 [paths]
+; debuild.me URL to send links to the clients
+; if needed
+debuildme_url=http://debuild.me
 ; NOT USED YET
 #keyring=
 ; This has to be the dput target of ricky

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-debile/debile-master.git



More information about the Pkg-debile-commits mailing list