Example #1
0
							}
						},
						"mode": "server",
						"datacenter": "dc1",
						"log_level": "debug",
						"protocol_version": 1
					},
					"require_ssl": true,
					"encrypt_keys": ["key-1", "key-2"]
				},
				"confab": {
					"timeout_in_seconds": 30
				}
			}`)

			config, err := confab.ConfigFromJSON(json)
			Expect(err).NotTo(HaveOccurred())
			Expect(config).To(Equal(confab.Config{
				Path: confab.ConfigPath{
					AgentPath:       "/path/to/agent",
					ConsulConfigDir: "/consul/config/dir",
					PIDFile:         "/path/to/pidfile",
				},
				Node: confab.ConfigNode{
					Name:       "nodename",
					Index:      1234,
					ExternalIP: "10.0.0.1",
				},
				Consul: confab.ConfigConsul{
					Agent: confab.ConfigConsulAgent{
						Services: map[string]confab.ServiceDefinition{
Example #2
0
func main() {
	var controller confab.Controller

	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`")

	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)
	}

	config, err := confab.ConfigFromJSON(configFileContents)
	if err != nil {
		stderr.Printf("error reading configuration file: %s", err)
		os.Exit(1)
	}

	path, err := exec.LookPath(config.Path.AgentPath)
	if err != nil {
		printUsageAndExit(fmt.Sprintf("\"agent_path\" %q cannot be found", config.Path.AgentPath), flagSet)
	}

	if len(config.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:   config.Path.PIDFile,
		ConfigDir: config.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: config.Consul.Agent.Servers.LAN,
		ConsulAPIAgent:  consulAPIClient.Agent(),
		ConsulRPCClient: nil,
		Logger:          logger,
	}

	controller = confab.Controller{
		AgentRunner:    agentRunner,
		AgentClient:    agentClient,
		SyncRetryDelay: 1 * time.Second,
		SyncRetryClock: clock.NewClock(),
		EncryptKeys:    config.Consul.EncryptKeys,
		Logger:         logger,
		ServiceDefiner: confab.ServiceDefiner{logger},
		ConfigDir:      config.Path.ConsulConfigDir,
		Config:         config,
	}

	switch os.Args[1] {
	case "start":
		start(flagSet, path, controller, agentClient)
	case "stop":
		stop(path, controller, agentClient)
	default:
		printUsageAndExit(fmt.Sprintf("invalid COMMAND %q", os.Args[1]), flagSet)
	}
}