var err error configDir, err = ioutil.TempDir("", "") Expect(err).NotTo(HaveOccurred()) dataDir, err = ioutil.TempDir("", "") Expect(err).NotTo(HaveOccurred()) cfg = config.Config{} cfg.Consul.Agent.DnsConfig.MaxStale = "5s" cfg.Consul.Agent.DnsConfig.RecursorTimeout = "5s" cfg.Node = config.ConfigNode{Name: "node", Index: 0} cfg.Path.ConsulConfigDir = configDir cfg.Path.DataDir = dataDir writer = chaperon.NewConfigWriter(configDir, logger) }) It("writes a config file to the consul_config dir", func() { err := writer.Write(cfg) Expect(err).NotTo(HaveOccurred()) buf, err := ioutil.ReadFile(filepath.Join(configDir, "config.json")) Expect(err).NotTo(HaveOccurred()) conf := map[string]interface{}{ "server": false, "domain": "", "datacenter": "", "data_dir": dataDir, "log_level": "",
func main() { flagSet := flag.NewFlagSet("flags", flag.ContinueOnError) flagSet.Var(&recursors, "recursor", "specifies the address of an upstream DNS `server`, may be specified multiple times") flagSet.StringVar(&configFile, "config-file", "", "specifies the config `file`") flagSet.BoolVar(&foreground, "foreground", false, "if true confab will wait for consul to exit") if len(os.Args) < 2 { printUsageAndExit("invalid number of arguments", flagSet) } if err := flagSet.Parse(os.Args[2:]); err != nil { os.Exit(1) } configFileContents, err := ioutil.ReadFile(configFile) if err != nil { stderr.Printf("error reading configuration file: %s", err) os.Exit(1) } cfg, err := config.ConfigFromJSON(configFileContents) if err != nil { stderr.Printf("error reading configuration file: %s", err) os.Exit(1) } path, err := exec.LookPath(cfg.Path.AgentPath) if err != nil { printUsageAndExit(fmt.Sprintf("\"agent_path\" %q cannot be found", cfg.Path.AgentPath), flagSet) } if len(cfg.Path.PIDFile) == 0 { printUsageAndExit("\"pid_file\" cannot be empty", flagSet) } logger := lager.NewLogger("confab") logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.INFO)) agentRunner := &agent.Runner{ Path: path, PIDFile: cfg.Path.PIDFile, ConfigDir: cfg.Path.ConsulConfigDir, Recursors: recursors, Stdout: os.Stdout, Stderr: os.Stderr, Logger: logger, } consulAPIClient, err := api.NewClient(api.DefaultConfig()) if err != nil { panic(err) // not tested, NewClient never errors } agentClient := &agent.Client{ ExpectedMembers: cfg.Consul.Agent.Servers.LAN, ConsulAPIAgent: consulAPIClient.Agent(), ConsulRPCClient: nil, Logger: logger, } retrier := utils.NewRetrier(clock.NewClock(), 1*time.Second) controller := chaperon.Controller{ AgentRunner: agentRunner, AgentClient: agentClient, Retrier: retrier, EncryptKeys: cfg.Consul.EncryptKeys, Logger: logger, ServiceDefiner: config.ServiceDefiner{logger}, ConfigDir: cfg.Path.ConsulConfigDir, Config: cfg, } keyringRemover := chaperon.NewKeyringRemover(cfg.Path.KeyringFile, logger) configWriter := chaperon.NewConfigWriter(cfg.Path.ConsulConfigDir, logger) var r runner = chaperon.NewClient(controller, consulagent.NewRPCClient, keyringRemover, configWriter) if controller.Config.Consul.Agent.Mode == "server" { bootstrapChecker := chaperon.NewBootstrapChecker(logger, agentClient, status.Client{ConsulAPIStatus: consulAPIClient.Status()}, time.Sleep) r = chaperon.NewServer(controller, configWriter, consulagent.NewRPCClient, bootstrapChecker) } switch os.Args[1] { case "start": _, err = os.Stat(controller.Config.Path.ConsulConfigDir) if err != nil { printUsageAndExit(fmt.Sprintf("\"consul_config_dir\" %q could not be found", controller.Config.Path.ConsulConfigDir), flagSet) } if utils.IsRunningProcess(agentRunner.PIDFile) { stderr.Println("consul_agent is already running, please stop it first") os.Exit(1) } if len(agentClient.ExpectedMembers) == 0 { printUsageAndExit("at least one \"expected-member\" must be provided", flagSet) } timeout := utils.NewTimeout(time.After(time.Duration(controller.Config.Confab.TimeoutInSeconds) * time.Second)) if err := r.Start(cfg, timeout); err != nil { stderr.Printf("error during start: %s", err) r.Stop() os.Exit(1) } if foreground { if err := agentRunner.Wait(); err != nil { stderr.Printf("error during wait: %s", err) r.Stop() os.Exit(1) } } case "stop": if err := r.Stop(); err != nil { stderr.Printf("error during stop: %s", err) os.Exit(1) } default: printUsageAndExit(fmt.Sprintf("invalid COMMAND %q", os.Args[1]), flagSet) } }