func handleCommon(name, usage string, argv []string) (args []string) { opts := optparse.Parser( "Usage: amp " + name + " <path> [options]\n\n " + usage + "\n") profile := opts.String([]string{"--profile"}, "development", "the config profile to use [development]", "NAME") noConsoleLog := opts.Bool([]string{"--no-console-log"}, false, "disable output to stdout/stderr") args = opts.Parse(argv) if len(args) == 0 { opts.PrintUsage() runtime.Exit(0) } runtime.SetProfile(*profile) if !*noConsoleLog { logging.AddConsoleLogger() logging.AddConsoleFilter(nodule.FilterConsoleLog) } return }
func ampNode(argv []string, usage string) { opts := optparse.Parser( "Usage: amp node <config.yaml> [options]\n\n " + usage + "\n") nodeHost := opts.StringConfig("node-host", "", "the host to bind this node to") nodePort := opts.IntConfig("node-port", 8050, "the port to bind this node to [8050]") ctrlHost := opts.StringConfig("control-host", "", "the host to bind the nodule control socket to") ctrlPort := opts.IntConfig("control-port", 8051, "the port to bind the nodule control socket to [8051]") nodules := opts.StringConfig("nodules", "*", "comma-separated list of nodules to initialise [*]") nodulePaths := opts.StringConfig("nodule-paths", ".", "comma-separated list of nodule container directories [nodule]") masterNodes := opts.StringConfig("master-nodes", "localhost:8060", "comma-separated addresses of amp master nodes [localhost:8060]") masterKeyPath := opts.StringConfig("master-key", "cert/master.key", "the path to the file containing the amp master public key [cert/master.key]") debug, _, runPath := runtime.DefaultOpts("node", opts, argv, nil) masterClient, err := master.NewClient(*masterNodes, *masterKeyPath) if err != nil { runtime.StandardError(err) } logging.AddConsoleFilter(nodule.FilterConsoleLog) node, err := nodule.NewHost( runPath, *nodeHost, *nodePort, *ctrlHost, *ctrlPort, *nodules, strings.SplitN(*nodulePaths, ",", -1), masterClient) if err != nil { runtime.StandardError(err) } err = node.Run(debug) logging.Wait() if err != nil { runtime.Exit(1) } }
func DefaultOpts(name string, opts *optparse.OptionParser, argv []string, consoleFilter logging.Filter) (bool, string, string) { var ( configPath string instanceDirectory string err os.Error ) debug := opts.Bool([]string{"-d", "--debug"}, false, "enable debug mode") genConfig := opts.Bool([]string{"-g", "--gen-config"}, false, "show the default yaml config") runDirectory := opts.StringConfig("run-dir", "run", "the path to the run directory to store locks, pid files, etc. [run]") logDirectory := opts.StringConfig("log-dir", "log", "the path to the log directory [log]") logRotate := opts.StringConfig("log-rotate", "never", "specify one of 'hourly', 'daily' or 'never' [never]") noConsoleLog := opts.BoolConfig("no-console-log", false, "disable server requests being logged to the console [false]") extraConfig := opts.StringConfig("extra-config", "", "path to a YAML config file with additional options") // Parse the command line options. args := opts.Parse(argv) // Print the default YAML config file if the ``-g`` flag was specified. if *genConfig { opts.PrintDefaultConfigFile(name) Exit(0) } // Assume the parent directory of the config as the instance directory. if len(args) >= 1 { configPath, err = filepath.Abs(filepath.Clean(args[0])) if err != nil { StandardError(err) } err = opts.ParseConfig(configPath, os.Args) if err != nil { StandardError(err) } instanceDirectory, _ = filepath.Split(configPath) } else { opts.PrintUsage() Exit(0) } // Load the extra config file with additional options if one has been // specified. if *extraConfig != "" { extraConfigPath, err := filepath.Abs(filepath.Clean(*extraConfig)) if err != nil { StandardError(err) } extraConfigPath = JoinPath(instanceDirectory, extraConfigPath) err = opts.ParseConfig(extraConfigPath, os.Args) if err != nil { StandardError(err) } } // Create the log directory if it doesn't exist. logPath := JoinPath(instanceDirectory, *logDirectory) err = os.MkdirAll(logPath, 0755) if err != nil { StandardError(err) } // Create the run directory if it doesn't exist. runPath := JoinPath(instanceDirectory, *runDirectory) err = os.MkdirAll(runPath, 0755) if err != nil { StandardError(err) } // Setup the file and console logging. var rotate int switch *logRotate { case "daily": rotate = logging.RotateDaily case "hourly": rotate = logging.RotateHourly case "never": rotate = logging.RotateNever default: Error("ERROR: Unknown log rotation format %q\n", *logRotate) } if !*noConsoleLog { logging.AddConsoleLogger() logging.AddConsoleFilter(consoleFilter) } _, err = logging.AddFileLogger(name, logPath, rotate, logging.InfoLog) if err != nil { Error("ERROR: Couldn't initialise logfile: %s\n", err) } _, err = logging.AddFileLogger("error", logPath, rotate, logging.ErrorLog) if err != nil { Error("ERROR: Couldn't initialise logfile: %s\n", err) } // Initialise the runtime -- which will run the process on multiple // processors if possible. Init() // Initialise the process-related resources. InitProcess(name, runPath) return *debug, instanceDirectory, runPath }