2017-09-26 16:43:37 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
'''
|
2017-10-03 16:13:42 -07:00
|
|
|
doublethink/cli.py - doublethink Command Line Tools
|
2017-09-26 16:43:37 -07:00
|
|
|
|
|
|
|
Copyright (C) 2017 Internet Archive
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os, sys
|
|
|
|
import argparse
|
|
|
|
import doublethink
|
2017-10-03 13:56:32 -07:00
|
|
|
import logging
|
2017-09-26 16:43:37 -07:00
|
|
|
|
|
|
|
def purge_stale_services(argv=None):
|
|
|
|
"""Command-line utility to periodically purge stale entries from the "services" table.
|
|
|
|
|
|
|
|
It is designed to be used in conjunction with cron.
|
|
|
|
"""
|
|
|
|
argv = argv or sys.argv
|
|
|
|
arg_parser = argparse.ArgumentParser(
|
2017-10-03 16:41:10 -07:00
|
|
|
prog=os.path.basename(argv[0]), description=(
|
|
|
|
'doublethink-purge-stale-services: utility to periodically '
|
|
|
|
'purge stale entries from the "services" table.'))
|
2017-09-26 16:43:37 -07:00
|
|
|
|
2017-10-03 16:41:10 -07:00
|
|
|
arg_parser.add_argument(
|
|
|
|
"-d", "--rethinkdb-db", required=True, dest="database",
|
|
|
|
help="A RethinkDB database containing a 'services' table")
|
2017-09-26 16:43:37 -07:00
|
|
|
|
|
|
|
arg_parser.add_argument("-s", "--rethinkdb-servers",
|
2017-10-03 13:56:32 -07:00
|
|
|
metavar="SERVERS", dest="servers", default='localhost',
|
2017-09-26 17:00:17 -07:00
|
|
|
help="rethinkdb servers, e.g. db0.foo.org,db0.foo.org:38015,db1.foo.org")
|
|
|
|
|
|
|
|
arg_parser.add_argument(
|
|
|
|
'-v', '--verbose', dest='log_level', action='store_const',
|
|
|
|
default=logging.INFO, const=logging.DEBUG, help=(
|
|
|
|
'verbose logging'))
|
2017-09-26 16:43:37 -07:00
|
|
|
args = arg_parser.parse_args(argv[1:])
|
2017-09-26 17:00:17 -07:00
|
|
|
logging.basicConfig(
|
|
|
|
stream=sys.stdout, level=args.log_level, format=(
|
|
|
|
'%(asctime)s %(process)d %(levelname)s %(threadName)s '
|
|
|
|
'%(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s'))
|
2017-09-26 16:43:37 -07:00
|
|
|
|
|
|
|
args.servers = [srv.strip() for srv in args.servers.split(",")]
|
|
|
|
|
2017-10-03 13:56:32 -07:00
|
|
|
rethinker = doublethink.Rethinker(servers=args.servers, db=args.database)
|
2017-09-26 16:43:37 -07:00
|
|
|
registry = doublethink.services.ServiceRegistry(rethinker)
|
2017-10-03 14:38:31 -07:00
|
|
|
registry.purge_stale_services()
|
2017-10-03 16:41:10 -07:00
|
|
|
return 0
|