[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[minion-cvs] More directory work.



Update of /home/minion/cvsroot/src/minion/lib/mixminion/directory
In directory moria.mit.edu:/tmp/cvs-serv18030/src/minion/lib/mixminion/directory

Modified Files:
	DirMain.py Directory.py ServerInbox.py ServerList.py 
Log Message:
More directory work.

   Make serverdesc publication optional.
   Add a republish command.
   Rewrite the directory CLI for directory automation.



Index: DirMain.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/directory/DirMain.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- DirMain.py	23 May 2003 07:54:11 -0000	1.9
+++ DirMain.py	26 May 2003 21:08:13 -0000	1.10
@@ -17,7 +17,7 @@
 from mixminion.Common import createPrivateDir, formatTime, LOG
 from mixminion.Crypto import init_crypto, pk_fingerprint, pk_generate, \
      pk_PEM_load, pk_PEM_save
-from mixminion.directory.ServerList import ServerList
+from mixminion.directory.Directory import Directory, DirectoryConfig
 
 USAGE = """\
 Usage: %s -d <directory> command
@@ -30,98 +30,136 @@
       fingerprint              [Return the fingerprint of this directory's pk]
 """.strip()
 
-def getIdentity(baseDir):
-    """Load the identity key stored under the base directory, creating it
-       if necessary."""
-    createPrivateDir(baseDir)
-    fname = os.path.join(baseDir, "identity")
-    if not os.path.exists(fname):
-        print "No public key found; generating new key..."
-        key = pk_generate(2048)
-        pk_PEM_save(key, fname)
-        return key
-    else:
-        return pk_PEM_load(fname)
+def getDirectory():
+    fn = os.environ.get('MINION_DIR_CONF')
+    if not fn:
+        fn = os.expanduser("~/.mixminion_dir.cf")
+        if not os.path.exists(fn):
+            fn = None
+    if not fn:
+        fn = "/etc/mixion_dir.cf"
+        if not os.path.exists(fn):
+            fn = None
+    if not fn:
+        raise UIError("No configuration file found")
+
+    try:
+        config = DirectoryConfig(filename=fn)
+    except ConfigError, e:
+        raise UIError("Error in %s: %s", fn, e)
+
+    return Directory(config)
 
 def usageAndExit(cmd):
     """Print a usage message and exit"""
     print USAGE%cmd
-    sys.exit(1)
+    sys.exit
 
-def cmd_import(cmd, base, rest):
-    if len(rest) != 1: usageAndExit(cmd)
-    lst = ServerList(base)
-    lst.importServerInfo(rest[0], knownOnly=1)
-    print "Imported."
+def cmd_init():
+    d = getDirectory()
+    d.setupDirectories()
+    d.getServerList()
+    d.getServerInbox()
 
-def cmd_import_new(cmd, base, rest):
-    if len(rest) != 1: usageAndExit(cmd)
-    lst = ServerList(base)
-    lst.importServerInfo(rest[0], knownOnly=0)
-    print "Imported."
+def cmd_update(args):
+    if args:
+        raise UIError("mixminion dir update takes no arguments")
+    
+    d = getDirectory()
+    serverList = d.getServerList()
+    inbox = d.getInbox()
+    inbox.acceptUpdates(serverList)
+
+def cmd_list(args):
+    if args:
+        raise UIError("mixminion dir list takes no arguments")
+
+    d = getDirectory()
+    inbox = d.getInbox()
+    inbox.listPendingServers(sys.stdout)
+
+def cmd_import(args):
+    d = getDirectory()
+    inbox = d.getInbox()
+    serverLsit = d.getServerList()
+
+    if not args:
+        print "(No server names given)"
+
+    bad, good = 0,0
+    for name in args:
+        print "Importing server %r..."%name
+        try:
+            inbox.acceptNewServer(serverList, name)
+            good += 1
+            print "Imported."
+        except UIError, e:
+            bad += 1
+            print "Error importing %r: %s"%(name, e)
+
+    print "\n%s servers imported, %s rejected." %(good,bad)
+
+def cmd_generate(args):
+    if args:
+        raise UIError("mixminion dir generate takes no arguments")
+
+    d = getDirectory()
+    serverList = d.getServerList()
+    key = d.getIdentity()
+    serverList.clean()
+
+    config = d.getConfig()
+
+    badServers = config['Directory'].get('BadServer', [])
+    location = config['Publishing']['Location']
+    print >>sys.stderr, "(Bad servers==%r)"%badServers
 
-def cmd_generate(cmd, base, rest):
-    if len(rest) != 0: usageAndExit(cmd)
-    lst = ServerList(base)
-    lst.clean()
-    key = getIdentity(base)
-    # XXXX Until we have support for automatic directory generation, we
-    # XXXX set the validity time to be pretty long: 2 months.
     now = time.time()
-    twoMonthsLater = now + 60*60*24*30*2
-    lst.generateDirectory(startAt=now, endAt=twoMonthsLater, extraTime=0,
-                          identityKey=key)
-    print >>sys.stderr, "Directory generated."
+    tomorrow = now+60*60*24
+    twoWeeks = 60*60*24*14
+    
+    serverList.generateDirectory(startAt=now, endAt=tomorrow,
+                                 extraTime=twoWeeks,
+                                 identityKey=key,
+                                 badServers=badServers)
+    print >>sys.stderr, "Directory generated; publishing."
 
-def cmd_export(cmd, base, rest):
-    if len(rest) != 1: usageAndExit(cmd)
-    lst = ServerList(base)
-    fname = lst.getDirectoryFilename()
-    if not os.path.exists(fname):
-        print >>sys.stderr, "No directory has been generated"
-    st = os.stat(fname)
-    print >>sys.stderr, "Exporting directory from %s"%(
-        formatTime(st[stat.ST_MTIME]))
-    if rest[0] == '-':
-        f = open(fname)
-        d = f.read()
-        f.close()
-        sys.stdout.write(d)
-    elif rest[0].endswith(".gz"):
+    fname = serverList.getDirectoryFilename()
+
+    if location.endswith(".gz"):
         fIn = open(fname)
-        fOut = gzip.GzipFile(rest[0], 'wb')
+        fOut = gzip.GzipFile(location, 'wb')
         fOut.write(fIn.read())
         fIn.close()
         fOut.close()
     else:
-        shutil.copy(fname, rest[0])
-        print >>sys.stderr, "Exported."
+        shutil.copy(fname, location)
 
-def cmd_remove(cmd, base, rest):
-    if len(rest) != 1: usageAndExit(cmd)
-    lst = ServerList(base)
-    lst.expungeServersByNickname(rest[0])
+    print >>sys.stderr, "Published."
 
-def cmd_fingerprint(cmd, base, rest):
-    if len(rest) != 0: usageAndExit(cmd)
-    key = getIdentity(base)
+def cmd_fingerprint(args):
+    if args:
+        raise UIError("mixminion dir fingerprint takes no arguments")
+    d = getDirectory()
+    key = d.getIdentity()
     print pk_fingerprint(key)
 
-SUBCOMMANDS = { 'import' : cmd_import,
-                'import-new' : cmd_import_new,
+SUBCOMMANDS = { 'initialize' : cmd_init,
+                'update' : cmd_update,
+                'list' : cmd_list,
+                'import-new' : cmd_import,
                 'generate' : cmd_generate,
-                'export' : cmd_export,
-                'remove' : cmd_remove,
-                'fingerprint' : cmd_fingerprint }
+                'fingerprint' : cmd_fingerprint
+                }
 
 def main(cmd, args):
-    if len(args) < 3 or args[0] != "-d" or args[0] in ('-h', '--help'):
-        usageAndExit(cmd)
-    baseDir = args[1]
-    command = args[2]
+    if len(args)<1 or ('-h', '--help') in args:
+        usageAndExit()
+    command = args[0]
+    args = args[1:]
     if not SUBCOMMANDS.has_key(command):
         print >>sys.stderr, "Unknown command", command
-        usageAndExit(cmd)
+        usageAndExit()
     init_crypto()
     LOG.setMinSeverity("INFO")
-    SUBCOMMANDS[command](cmd, baseDir, args[3:])
+    SUBCOMMANDS[command](args)

Index: Directory.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/directory/Directory.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Directory.py	25 May 2003 23:11:43 -0000	1.2
+++ Directory.py	26 May 2003 21:08:13 -0000	1.3
@@ -81,6 +81,9 @@
                           self.config.cgi_gid,
                           0640)
 
+    def getConfig(self):
+        return self.config
+
     def getServerList(self):
         if not self.serverList:
             from mixminion.directory.ServerList import ServerList
@@ -92,6 +95,18 @@
             from mixminion.directory.ServerInbox import ServerInbox
             self.inbox = ServerInbox(self.inboxBase, self.getIDCache())
         return self.inbox
+
+    def getIdentity(self):
+        _ = self.getServerList()
+        fname = os.path.join(self.directoryBase, "identity")
+        if not os.path.exists(fname):
+            print "No public key found; generating new key..."
+            key = pk_generate(2048)
+            pk_PEM_save(key, fname)
+            return key
+        else:
+            return pk_PEM_load(fname)
+
             
 class DirectoryConfig(C._ConfigFile):
     _restrictFormat = 1
@@ -99,10 +114,17 @@
     _syntax = {
         'Host' : C.ClientConfig._syntax['Host'],
         "Directory-Store" : {
+           "__SECTION__" : ( ), 
            "Homedir" : ('REQUIRE', None, None),
            "DirUser" : ('REQUIRE', None, None),
            "CGIUser" : ('REQUIRE', None, None),
            "CGIGroup" : ('REQUIRE', None, None),
+        },
+        'Directory' : {
+           "BadServer" : ("ALLOW*", None, None)
+        },
+        'Publishing' : {
+           "Location" : ('REQUIRE', None, None)
         } }
     def __init__(self, filename=None, string=None):
         C._ConfigFile.__init__(self, filename, string)

Index: ServerInbox.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/directory/ServerInbox.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- ServerInbox.py	25 May 2003 23:11:43 -0000	1.2
+++ ServerInbox.py	26 May 2003 21:08:13 -0000	1.3
@@ -82,7 +82,7 @@
         self._doAccept(serverList, self.updateQueue, incoming, [],
                        knownOnly=1)
 
-    def acceptNewServers(self, serverList, nickname):
+    def acceptNewServer(self, serverList, nickname):
         if ':' in nickname:
             nickname, fingerprint = nickname.split(":")
         else:

Index: ServerList.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/directory/ServerList.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- ServerList.py	25 May 2003 21:57:05 -0000	1.19
+++ ServerList.py	26 May 2003 21:08:13 -0000	1.20
@@ -225,7 +225,7 @@
                           startAt, endAt, extraTime,
                           identityKey,
                           publicationTime=None,
-                          goodServers=None):
+                          badServers=()):
         """Generate and sign a new directory, to be effective from <startAt>
            through <endAt>.  It includes all servers that are valid at
            any time between <startAt> and <endAt>+>extraTime>.  The directory
@@ -257,10 +257,7 @@
                 contents.append(f.read())
                 f.close()
 
-            if goodServers is None:
-                goodServers = [n for n,_,_ in included]
-            else:
-                goodServers = [n for n,_,_ in included if n in goodServers]
+            goodServers = [n for n,_,_ in included if n not in badServers]
             g = {}
             for n in goodServers: g[n]=1
             goodServers = g.keys()