Permalink
Browse files

- Some refactoring in carbon-cache, add tests for the conf module. (i…

…n progress)
  • Loading branch information...
1 parent db5d742 commit 6dbde48e02d7e079a4f90352f5726ebdb72d9b5f Sidnei da Silva committed Jun 15, 2011
View
@@ -1 +1,2 @@
MANIFEST
+_trial_temp
View
@@ -17,7 +17,6 @@
import os
import socket
import pwd
-import optparse
import atexit
from os.path import basename, dirname, exists, join, isdir
@@ -34,16 +33,10 @@
pass
from twisted.internet import reactor
-
# Figure out where we're installed
BIN_DIR = dirname( os.path.abspath(__file__) )
ROOT_DIR = dirname(BIN_DIR)
-STORAGE_DIR = join(ROOT_DIR, 'storage')
-LOG_DIR = join(STORAGE_DIR, 'log', 'carbon-cache')
LIB_DIR = join(ROOT_DIR, 'lib')
-CONF_DIR = join(ROOT_DIR, 'conf')
-__builtins__.CONF_DIR = CONF_DIR # evil I know, but effective.
-
sys.path.insert(0, LIB_DIR)
# Capture useful debug info for this commonly reported problem
@@ -56,33 +49,15 @@
print '__file__=%s' % __file__
sys.exit(1)
+# Read config (we want failures to occur before daemonizing)
+from carbon.conf import settings, get_default_parser, parse_options, read_config
-# Parse command line options
-parser = optparse.OptionParser(usage='%prog [options] <start|stop|status>')
-parser.add_option('--debug', action='store_true', help='Run in the foreground, log to stdout')
-parser.add_option('--profile', help='Record performance profile data to the given file')
-parser.add_option('--pidfile', default=join(STORAGE_DIR, '%s.pid' % program), help='Write pid to the given file')
-parser.add_option('--config', default=join(CONF_DIR, 'carbon.conf'), help='Use the given config file')
-parser.add_option('--logdir', default=LOG_DIR, help="Write logs in the given directory")
-parser.add_option('--instance', default=None, help="Manage a specific carbon instnace")
-
-(options, args) = parser.parse_args()
-
-if not args:
- parser.print_usage()
- raise SystemExit(1)
+(options, args) = parse_options(get_default_parser())
+read_config(program, options, ROOT_DIR=ROOT_DIR)
-# Assume standard locations when using --instance
-if options.instance:
- instance = str(options.instance)
- pidfile = join(STORAGE_DIR, '%s-%s.pid' % (program, instance))
- logdir = "%s-%s/" % (LOG_DIR.rstrip('/'), instance)
- config = options.config
-else:
- instance = None
- pidfile = options.pidfile
- logdir = options.logdir
- config = options.config
+instance = options.instance
+pidfile = settings.pidfile
+logdir = settings.LOG_DIR
__builtins__.instance = instance # This isn't as evil as you might think
__builtins__.program = program
@@ -127,31 +102,18 @@
print "%s (instance %s) is not running" % (program, instance)
raise SystemExit(0)
-elif action != 'start':
- parser.print_usage()
- raise SystemExit(1)
-
-
if exists(pidfile):
print "Pidfile %s already exists, is %s already running?" % (pidfile, program)
raise SystemExit(1)
-
-# Read config (we want failures to occur before daemonizing)
-from carbon.conf import settings
-settings.readFrom(config, 'cache')
-
-if instance:
- settings.readFrom(config, 'cache:%s' % instance)
-
# Import application components
from carbon.log import logToStdout, logToDir
from carbon.listeners import MetricLineReceiver, MetricPickleReceiver, CacheQueryHandler, startListener
from carbon.cache import MetricCache
from carbon.instrumentation import startRecording
from carbon.events import metricReceived
-storage_schemas = join(CONF_DIR, 'storage-schemas.conf')
+storage_schemas = join(settings.CONF_DIR, 'storage-schemas.conf')
if not exists(storage_schemas):
print "Error: missing required config %s" % storage_schemas
sys.exit(1)
View
@@ -12,6 +12,8 @@
See the License for the specific language governing permissions and
limitations under the License."""
+from os.path import join, dirname, normpath
+from optparse import OptionParser
from ConfigParser import ConfigParser
@@ -113,3 +115,100 @@ def readFrom(self, path, section):
settings = Settings()
settings.update(defaults)
+
+
+def get_default_parser(usage="%prog [options] <start|stop|status>"):
+ """Create a parser for command line options."""
+ parser = OptionParser(usage=usage)
+ parser.add_option(
+ "--debug", action="store_true",
+ help="Run in the foreground, log to stdout")
+ parser.add_option(
+ "--profile",
+ help="Record performance profile data to the given file")
+ parser.add_option(
+ "--pidfile", default=None,
+ help="Write pid to the given file")
+ parser.add_option(
+ "--config",
+ default=None,
+ help="Use the given config file")
+ parser.add_option(
+ "--logdir",
+ default=None,
+ help="Write logs in the given directory")
+ parser.add_option(
+ "--instance",
+ default=None,
+ help="Manage a specific carbon instance")
+
+ return parser
+
+
+def parse_options(parser, args):
+ """
+ Parse command line options and print usage message if no arguments were
+ provided for the command.
+ """
+ (options, args) = parser.parse_args(args)
+
+ if not args:
+ parser.print_usage()
+ raise SystemExit(1)
+
+ if args[0] not in ("start", "stop", "status"):
+ parser.print_usage()
+ raise SystemExit(1)
+
+ return options, args
+
+
+def read_config(program, options, **kwargs):
+ """
+ Read settings for 'program' from configuration file specified by
+ 'options.config', with missing values provided by 'defaults'.
+ """
+ settings = Settings()
+ settings.update(defaults)
+
+ # Initialize default values if not set yet.
+ for name, value in kwargs.items():
+ settings.setdefault(name, value)
+
+ # At minimum, 'ROOT_DIR' needs to be set.
+ if settings.get("ROOT_DIR", None) is None:
+ raise ValueError("ROOT_DIR needs to be provided.")
+
+ if options.config is None:
+ raise ValueError("--config needs to be provided.")
+
+ # Default config directory to same directory as the specified carbon
+ # configuration file.
+ settings.setdefault("CONF_DIR", dirname(normpath(options.config)))
+
+ # Other settings default to relative paths from the root directory, for
+ # backwards-compatibility.
+ settings.setdefault("STORAGE_DIR", join(settings.ROOT_DIR, "storage"))
+ settings.setdefault("LOG_DIR", join(settings.STORAGE_DIR, "log", program))
+
+ # Read configuration options from program-specific section.
+ section = program[len("carbon-"):]
+ settings.readFrom(options.config, section)
+
+ # If a specific instance of the program is specified, augment the settings
+ # with the instance-specific settings and provide sane defaults for
+ # optional settings.
+ if options.instance:
+ settings.readFrom(options.config, "%s:%s" % (section, options.instance))
+ settings["pidfile"] = (options.pidfile or
+ join(settings.STORAGE_DIR,
+ "%s-%s.pid" % (program, options.instance)))
+ settings["LOG_DIR"] = (options.logdir or
+ "%s-%s/" % (settings.LOG_DIR.rstrip('/'),
+ options.instance))
+ else:
+ settings["pidfile"] = (options.pidfile or
+ join(settings.STORAGE_DIR, '%s.pid' % program))
+ settings["LOG_DIR"] = (options.logdir or settings.LOG_DIR)
+
+ return settings
@@ -14,16 +14,16 @@
import os, re
from os.path import join, exists
-from carbon.conf import OrderedConfigParser, Settings, settings
+from carbon.conf import OrderedConfigParser, settings
try:
import cPickle as pickle
except ImportError:
import pickle
-STORAGE_SCHEMAS_CONFIG = join(CONF_DIR, 'storage-schemas.conf')
-STORAGE_LISTS_DIR = join(CONF_DIR, 'lists')
+STORAGE_SCHEMAS_CONFIG = join(settings.CONF_DIR, 'storage-schemas.conf')
+STORAGE_LISTS_DIR = join(settings.CONF_DIR, 'lists')
def getFilesystemPath(metric):
No changes.
Oops, something went wrong.

0 comments on commit 6dbde48

Please sign in to comment.