Exemple #1
0
func init() {
	flag.Usage = printUsage

	// set the defaults
	defaultSSL = true
	defaultPort = 38332
	defaultAddress = "localhost"

	defaultConfigPath := computeDefaultConfigPath()

	flag.StringVarP(&flagConfigPath, "config", "c", defaultConfigPath, "config file path")
	flag.StringVarP(&flagUsername, "rpcuser", "u", "", "rpc user name")
	flag.StringVarP(&flagPassword, "rpcpassword", "p", "", "rpc password")
	flag.BoolVar(&flagSSL, "ssl", defaultSSL, "use secure sockets SSL")
	flag.IntVarP(&flagPort, "port", "n", defaultPort, "port number")
	flag.StringVarP(&flagAddress, "host", "a", defaultAddress, "server address")

	flag.BoolVarP(&verbose, "verbose", "v", false, "verbose output for debugging")
	// k , ignore server certificate errors (not recommended, only for testing self-signed certificates")

	flag.BoolVar(&flagHelp, "help", false, "Display this information")
	flag.BoolVar(&flagVersion, "version", false, "Display version information")
	flag.BoolVarP(&flagInsecure, "insecure", "k", false, "Allow connections to servers with 'insecure' SSL connections e.g. self-signed certificates. By default, this option is false, unless the server is localhost or an explicit IP address, in which case the option is true.")

	flag.Parse()

	flagSetMap = make(map[string]bool)
	flag.Visit(visitFlagSetMap)
}
Exemple #2
0
func main() {
	// environment can only override default address
	eaddr := os.Getenv("NCONFIGD_ETCD_ADDRESS")
	if eaddr == "" {
		eaddr = eaddress
	}

	logLevel := flag.StringP("log-level", "l", "warn", "log level")
	flag.StringVarP(&ansibleDir, "ansible", "a", ansibleDir, "directory containing the ansible run command")
	flag.StringP("etcd", "e", eaddress, "address of etcd server")
	configPath := flag.StringP("config", "c", "", "path to config file with prefixs")
	once := flag.BoolP("once", "o", false, "run only once and then exit")
	flag.Parse()
	flag.Visit(func(f *flag.Flag) {
		if f.Name == "etcd" {
			eaddr = f.Value.String()
		}
	})

	// Set up logging
	if err := logx.DefaultSetup(*logLevel); err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "logx.DefaultSetup",
			"level": *logLevel,
		}).Fatal("failed to set up logging")
	}

	// Load config containing prefixs to watch
	config, err := loadConfig(*configPath)
	if err != nil {
		log.WithFields(log.Fields{
			"error":      err,
			"configPath": *configPath,
		}).Fatal("failed to load config")
	}

	log.WithField("config", config).Info("config loaded")

	// set up etcd connection
	log.WithField("address", eaddr).Info("connection to etcd")
	etcdClient := etcd.NewClient([]string{eaddr})
	// make sure we can actually connect to etcd
	if !etcdClient.SyncCluster() {
		log.WithFields(log.Fields{
			"error":   err,
			"address": eaddr,
		}).Fatal("failed to connect to etcd cluster")
	}

	// always run initially
	runAnsible(config, eaddr, "")
	if *once {
		return
	}

	// set up watcher
	w := watchKeys(config, etcdClient)

	// to coordinate clean exiting between the consumer and the signal handler
	ready := make(chan struct{}, 1)
	ready <- struct{}{}

	// handle events
	go consumeResponses(config, eaddr, w, ready)

	// handle signals for clean shutdown
	sigs := make(chan os.Signal)
	signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)

	s := <-sigs
	log.WithField("signal", s).Info("signal received. waiting for current task to process")
	// wait until any current processing is finished
	<-ready
	_ = w.Close()
	log.Info("exiting")
}