Permalink
Browse files

Refactor + add some package files

  • Loading branch information...
1 parent f834c2c commit 6c23ef4a517de28273c450de91e221a30aa0bd0c @rs committed Jan 12, 2012
Showing with 419 additions and 299 deletions.
  1. +1 −0 .gitignore
  2. +19 −0 LICENCE
  3. 0 README.md
  4. +85 −0 bin/zkfarmer
  5. +2 −0 requirements.txt
  6. +31 −0 setup.py
  7. +0 −299 zkfarm
  8. +28 −0 zkfarmer/__init__.py
  9. +126 −0 zkfarmer/conf.py
  10. +127 −0 zkfarmer/watcher.py
View
@@ -0,0 +1 @@
+*.pyc
View
19 LICENCE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Olivier Poitrey <rs@dailymotion.com>.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
No changes.
View
@@ -0,0 +1,85 @@
+#!/usr/bin/env python
+#
+# This file is part of the zkfarmer package.
+# (c) Olivier Poitrey <rs@dailymotion.com>
+#
+# For the full copyright and license information, please view the LICENSE
+# file that was distributed with this source code.
+
+import sys
+import os
+sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
+
+from zkfarmer.conf import Conf
+from zkfarmer import ZkFarmer
+import zookeeper
+import logging
+
+
+def main():
+ from zktools.connection import ZkConnection
+ from optparse import OptionParser
+ from signal import signal, SIGTERM, SIGINT
+
+ parser = OptionParser(usage='usage: %prog [options] <join|export> /path/to/root/zknode /path/to/conf',
+ description='Register the current host as a node of a service defined by a zookeeper node path on ' +
+ 'one side and export the farm node list into a configuration file on the other side. ' +
+ 'Additionnaly, each node can carry its own configuration.')
+ parser.add_option('-H', '--host', dest='host', default=os.environ.get('ZKHOST', 'localhost:2181'),
+ help='list of zookeeper hosts:port sperated by commas')
+ parser.add_option('-f', '--format', dest='format', type='choice', choices=['json', 'php', 'dir'],
+ help='set the configuration format (supported formats: json, php, dir)')
+ parser.add_option('-q', '--quiet', dest='quiet', action='store_true')
+ parser.add_option('-v', '--verbose', dest='verbose', action='store_true')
+
+ (options, args) = parser.parse_args()
+
+ if len(args) is not 3:
+ parser.error('Missing argument')
+
+ COMMAND = args[0]
+ ROOT_NODE_PATH = args[1]
+ CONF_PATH = args[2]
+
+ if ROOT_NODE_PATH[0] is not "/":
+ parser.error('First argument must be the full path to the zookeeper node to create (eg: /services/db)')
+
+ if options.verbose:
+ level = logging.DEBUG
+ zookeeper.set_debug_level(zookeeper.LOG_LEVEL_DEBUG)
+ elif options.quiet:
+ level = logging.WARN
+ zookeeper.set_debug_level(zookeeper.LOG_LEVEL_WARN)
+ else:
+ level = logging.INFO
+ zookeeper.set_debug_level(zookeeper.LOG_LEVEL_INFO)
+
+ logging.basicConfig(level=level, format='%(asctime)s %(levelname)s %(message)s')
+
+ try:
+ conf = Conf(CONF_PATH, options.format)
+ except ValueError, e:
+ parser.error(e)
+ exit(1)
+
+ zkconn = ZkConnection(host=options.host)
+
+ def sighandler(sig, frame):
+ zkconn.close()
+ exit()
+
+ signal(SIGTERM, sighandler)
+ signal(SIGINT, sighandler)
+
+ farmer = ZkFarmer(zkconn)
+
+ if COMMAND == 'export':
+ farmer.export(conf, ROOT_NODE_PATH)
+ elif COMMAND == 'join':
+ farmer.join(conf, ROOT_NODE_PATH)
+ else:
+ parser.error('Invalid command: %s' % COMMAND)
+
+
+if __name__ == "__main__":
+ main()
View
@@ -0,0 +1,2 @@
+watchdog==0.5.4
+zktools==0.1
View
@@ -0,0 +1,31 @@
+from setuptools import setup, find_packages
+import re
+
+
+def parse_requirements(file_name):
+ requirements = []
+ for line in open(file_name, 'r').read().split('\n'):
+ if re.match(r'(\s*#)|(\s*$)', line):
+ continue
+ if re.match(r'\s*-e\s+', line):
+ # TODO support version numbers
+ requirements.append(re.sub(r'\s*-e\s+.*#egg=(.*)$', r'\1', line))
+ elif re.match(r'\s*-f\s+', line):
+ pass
+ else:
+ requirements.append(line)
+ return requirements
+
+setup(
+ name='zkfarmer',
+ version='0.1.0',
+ author='Olivier Poitrey',
+ author_email='rs@dailymotion.com',
+ packages=find_packages(),
+ scripts=['bin/zkfarmer'],
+ url='http://github.com/rs/zkfarmer',
+ license='LICENSE',
+ description='Easy distributed server farm management using Apache ZooKeeper.',
+ long_description=open('README.md').read(),
+ install_requires=parse_requirements('requirements.txt')
+)
Oops, something went wrong.

0 comments on commit 6c23ef4

Please sign in to comment.