[Checkins] SVN: zc.zk/trunk/s Added ``zookeeper_import`` shell script for importing ZooKeeper trees.

Jim Fulton jim at zope.com
Wed Jan 25 21:58:13 UTC 2012


Log message for revision 124173:
  Added ``zookeeper_import`` shell script for importing ZooKeeper trees.
  

Changed:
  U   zc.zk/trunk/setup.py
  U   zc.zk/trunk/src/zc/zk/README.txt
  U   zc.zk/trunk/src/zc/zk/scripts.py

-=-
Modified: zc.zk/trunk/setup.py
===================================================================
--- zc.zk/trunk/setup.py	2012-01-25 20:20:27 UTC (rev 124172)
+++ zc.zk/trunk/setup.py	2012-01-25 21:58:12 UTC (rev 124173)
@@ -23,6 +23,7 @@
 entry_points = """
 [console_scripts]
 zookeeper_export = zc.zk.scripts:export
+zookeeper_import = zc.zk.scripts:import_
 """
 
 from setuptools import setup

Modified: zc.zk/trunk/src/zc/zk/README.txt
===================================================================
--- zc.zk/trunk/src/zc/zk/README.txt	2012-01-25 20:20:27 UTC (rev 124172)
+++ zc.zk/trunk/src/zc/zk/README.txt	2012-01-25 21:58:12 UTC (rev 124173)
@@ -760,6 +760,92 @@
 The export script provides the same features as the ``export_tree``
 method. Use the ``--help`` option to see how to use it.
 
+zookeeper_import script
+=======================
+
+The `zc.zk` package provides a utility script for importing a
+ZooKeeper tree.  So, for example, given the tree::
+
+  /provision
+    /node1
+    /node2
+
+.. -> file_source
+
+    >>> with open('mytree.txt', 'w') as f: f.write(file_source)
+
+In the file ``mytree.txt``, we can import the file like this::
+
+    $ zookeeper_import zookeeper.example.com:2181 mytree.txt /fooservice
+
+.. -> sh
+
+    >>> command = sh.strip()
+    >>> expected = ''
+    >>> _, command, args = command.split(None, 2)
+    >>> import_ = pkg_resources.load_entry_point(
+    ...     'zc.zk', 'console_scripts', command)
+    >>> import_(args.split())
+
+    >>> zk.print_tree()
+    /cms : z4m cms
+      threads = 4
+      /databases
+        main -> /databases/cms
+      /providers
+        /1.2.3.4:5
+          pid = 4102
+    /databases
+      /cms
+        a = 1
+    /fooservice
+      secret = u'1234'
+      threads = 3
+      /providers
+        /192.168.0.42:8080
+          pid = 4102
+        /192.168.0.42:8081
+          pid = 4102
+        /192.168.0.42:8082
+          pid = 4102
+      /provision
+        /node1
+        /node2
+    /lb : ipvs
+      /pools
+        /cms
+          address = u'1.2.3.4:80'
+          providers -> /cms/providers
+
+  Read from stdin:
+
+    >>> stdin = sys.stdin
+    >>> sys.stdin = StringIO.StringIO('/x\n/y')
+    >>> import_('-d zookeeper.example.com:2181 - /fooservice'.split())
+    add /fooservice/x
+    add /fooservice/y
+
+    >>> sys.stdin = StringIO.StringIO('/x\n/y')
+    >>> import_('-d zookeeper.example.com:2181'.split())
+    add /x
+    add /y
+
+  Trim:
+
+    >>> sys.stdin = StringIO.StringIO('/provision\n/y')
+    >>> import_('-dt zookeeper.example.com:2181 - /fooservice'.split())
+    would delete /fooservice/provision/node1.
+    would delete /fooservice/provision/node2.
+    add /fooservice/y
+
+    >>> sys.stdin = stdin
+
+The import script provides the same features as the ``import_tree``
+method, with the exception that it provides less flexibility for
+specifing access control lists. Use the ``--help`` option to see how
+to use it.
+
+
 Graph analysis
 ==============
 
@@ -1018,7 +1104,7 @@
 Change History
 ==============
 
-0.6.0 (2012-01-??)
+0.6.0 (2012-01-25)
 ------------------
 
 - Improved ``register_server`` in the case when an empty host is
@@ -1026,6 +1112,8 @@
   <http://alastairs-place.net/projects/netifaces/>`_ is installed,
   ``register_server`` registers all of the IPv4 addresses [#ifaces]_.
 
+- Added ``zookeeper_import`` shell script for importing ZooKeeper trees.
+
 - ``delete_recursive`` now has a ``force`` argument to force deletion of
   ephemeral nodes.
 

Modified: zc.zk/trunk/src/zc/zk/scripts.py
===================================================================
--- zc.zk/trunk/src/zc/zk/scripts.py	2012-01-25 20:20:27 UTC (rev 124172)
+++ zc.zk/trunk/src/zc/zk/scripts.py	2012-01-25 21:58:12 UTC (rev 124173)
@@ -15,14 +15,15 @@
 import optparse
 import sys
 import zc.zk
+import zookeeper
 
 def export(args=None):
+    """Usage: %prog [options] connection [path]
+    """
     if args is None:
         args = sys.argv[1:]
 
-    parser = optparse.OptionParser("""
-    Usage: %prog [options] connection [path]
-    """)
+    parser = optparse.OptionParser(export.__doc__)
     parser.add_option('-e', '--ephemeral', action='store_true')
     parser.add_option('-o', '--output')
 
@@ -43,3 +44,57 @@
             f.write(data)
     else:
         print data,
+
+
+def import_(args=None):
+    """Usage: %prog [options] connection [import-file [path]]
+
+    Import a tree definition from a file.
+
+    If no import-file is provided or if the import file is -, then
+    data are read from standard input.
+    """
+
+    if args is None:
+        args = sys.argv[1:]
+
+    parser = optparse.OptionParser(import_.__doc__)
+    parser.add_option('-d', '--dry-run', action='store_true')
+    parser.add_option('-t', '--trim', action='store_true')
+    parser.add_option(
+        '-p', '--permission', type='int',
+        default=zookeeper.PERM_ALL,
+        help='ZooKeeper permission bits as integer,'
+        ' defaulting to zookeeper.PERM_ALL',
+        )
+
+    options, args = parser.parse_args(args)
+    if not (1 <= len(args) <= 3):
+        parser.parse_args(['-h'])
+
+    connection = args.pop(0)
+    if args:
+        import_file = args.pop(0)
+    else:
+        import_file = '-'
+
+    if args:
+        [path] = args
+    else:
+        path = '/'
+
+    logging.basicConfig(level=logging.WARNING)
+
+    zk = zc.zk.ZooKeeper(connection)
+    if import_file == '-':
+        import_file = sys.stdin
+    else:
+        import_file = open(import_file)
+
+    zk.import_tree(
+        import_file.read(), path,
+        trim=options.trim,
+        dry_run=options.dry_run,
+        acl=[zc.zk.world_permission(options.permission)],
+        )
+



More information about the checkins mailing list