func main() { userset := flag.NewFlagSet("fleet", flag.ExitOnError) printVersion := userset.Bool("version", false, "Print the version and exit") cfgPath := userset.String("config", "", fmt.Sprintf("Path to config file. Fleet will look for a config at %s by default.", DefaultConfigFile)) err := userset.Parse(os.Args[1:]) if err == flag.ErrHelp { userset.Usage() os.Exit(1) } args := userset.Args() if len(args) == 1 && args[0] == "version" { *printVersion = true } else if len(args) != 0 { userset.Usage() os.Exit(1) } if *printVersion { fmt.Println("fleetd version", version.Version) os.Exit(0) } log.Infof("Starting fleetd version %v", version.Version) cfgset := flag.NewFlagSet("fleet", flag.ExitOnError) cfgset.Int("verbosity", 0, "Logging level") cfgset.Var(&stringSlice{}, "etcd_servers", "List of etcd endpoints") cfgset.String("etcd_keyfile", "", "SSL key file used to secure etcd communication") cfgset.String("etcd_certfile", "", "SSL certification file used to secure etcd communication") cfgset.String("etcd_cafile", "", "SSL Certificate Authority file used to secure etcd communication") cfgset.String("etcd_key_prefix", registry.DefaultKeyPrefix, "Keyspace for fleet data in etcd") cfgset.Float64("etcd_request_timeout", 1.0, "Amount of time in seconds to allow a single etcd request before considering it failed.") cfgset.Float64("engine_reconcile_interval", 2.0, "Interval at which the engine should reconcile the cluster schedule in etcd.") cfgset.String("public_ip", "", "IP address that fleet machine should publish") cfgset.String("metadata", "", "List of key-value metadata to assign to the fleet machine") cfgset.String("agent_ttl", agent.DefaultTTL, "TTL in seconds of fleet machine state in etcd") cfgset.Bool("verify_units", false, "DEPRECATED - This option is ignored") cfgset.String("authorized_keys_file", "", "DEPRECATED - This option is ignored") globalconf.Register("", cfgset) cfg, err := getConfig(cfgset, *cfgPath) if err != nil { log.Fatalf(err.Error()) } log.Debugf("Creating Server") srv, err := server.New(*cfg) if err != nil { log.Fatalf("Failed creating Server: %v", err.Error()) } srv.Run() reconfigure := func() { log.Infof("Reloading configuration from %s", *cfgPath) cfg, err := getConfig(cfgset, *cfgPath) if err != nil { log.Fatalf(err.Error()) } log.Infof("Restarting server components") srv.Stop() srv, err = server.New(*cfg) if err != nil { log.Fatalf(err.Error()) } srv.Run() } shutdown := func() { log.Infof("Gracefully shutting down") srv.Stop() srv.Purge() os.Exit(0) } writeState := func() { log.Infof("Dumping server state") encoded, err := json.Marshal(srv) if err != nil { log.Errorf("Failed to dump server state: %v", err) return } if _, err := os.Stdout.Write(encoded); err != nil { log.Errorf("Failed to dump server state: %v", err) return } os.Stdout.Write([]byte("\n")) log.Debugf("Finished dumping server state") } signals := map[os.Signal]func(){ syscall.SIGHUP: reconfigure, syscall.SIGTERM: shutdown, syscall.SIGINT: shutdown, syscall.SIGUSR1: writeState, } listenForSignals(signals) }
func Main() { userset := flag.NewFlagSet("fleet", flag.ExitOnError) printVersion := userset.Bool("version", false, "Print the version and exit") cfgPath := userset.String("config", "", fmt.Sprintf("Path to config file. Fleet will look for a config at %s by default.", DefaultConfigFile)) userset.Usage = func() { fmt.Fprintf(os.Stderr, "%s\nUsage of %s:\n", FleetdDescription, os.Args[0]) userset.PrintDefaults() } err := userset.Parse(os.Args[1:]) if err != nil { userset.Usage() os.Exit(1) } args := userset.Args() if len(args) > 0 { // support `fleetd version` the same as `fleetd --version` if args[0] == "version" { *printVersion = true } else { fmt.Fprintf(os.Stderr, "%s takes no arguments. Did you mean to invoke fleetctl instead?\n", os.Args[0]) userset.Usage() os.Exit(1) } } if *printVersion { fmt.Println("fleetd version", version.Version) os.Exit(0) } log.Infof("Starting fleetd version %v", version.Version) cfgset := flag.NewFlagSet("fleet", flag.ExitOnError) cfgset.Int("verbosity", 0, "Logging level") cfgset.Var(&pkg.StringSlice{"http://127.0.0.1:2379", "http://127.0.0.1:4001"}, "etcd_servers", "List of etcd endpoints") cfgset.String("etcd_username", "", "username for secure etcd communication") cfgset.String("etcd_password", "", "password for secure etcd communication") cfgset.String("etcd_keyfile", "", "SSL key file used to secure etcd communication") cfgset.String("etcd_certfile", "", "SSL certification file used to secure etcd communication") cfgset.String("etcd_cafile", "", "SSL Certificate Authority file used to secure etcd communication") cfgset.String("etcd_key_prefix", registry.DefaultKeyPrefix, "Keyspace for fleet data in etcd") cfgset.Float64("etcd_request_timeout", 1.0, "Amount of time in seconds to allow a single etcd request before considering it failed.") cfgset.Float64("engine_reconcile_interval", 2.0, "Interval at which the engine should reconcile the cluster schedule in etcd.") cfgset.String("public_ip", "", "IP address that fleet machine should publish") cfgset.String("metadata", "", "List of key-value metadata to assign to the fleet machine") cfgset.String("agent_ttl", agent.DefaultTTL, "TTL in seconds of fleet machine state in etcd") cfgset.String("units_directory", "/run/fleet/units/", "Path to the fleet units directory") cfgset.Bool("systemd_user", false, "When true use systemd --user)") cfgset.Int("token_limit", 100, "Maximum number of entries per page returned from API requests") cfgset.Bool("enable_grpc", false, "When possible, uses grpc to communicate between engine and agent") cfgset.Bool("disable_engine", false, "Disable the engine entirely, use with care") cfgset.Bool("disable_watches", false, "Disable the use of etcd watches. Increases scheduling latency") cfgset.Bool("verify_units", false, "DEPRECATED - This option is ignored") cfgset.String("authorized_keys_file", "", "DEPRECATED - This option is ignored") globalconf.Register("", cfgset) cfg, err := getConfig(cfgset, *cfgPath) if err != nil { log.Fatal(err) } log.Debugf("Creating Server") srv, err := server.New(*cfg, nil) if err != nil { log.Fatalf("Failed creating Server: %v", err) } srv.Run() srvMutex := sync.Mutex{} reconfigure := func() { log.Infof("Reloading configuration from %s", *cfgPath) srvMutex.Lock() defer srvMutex.Unlock() cfg, err := getConfig(cfgset, *cfgPath) if err != nil { log.Fatal(err) } log.Infof("Restarting server components") srv.SetReconfigServer(true) // Get Server.listeners[] to keep it for a new server, // before killing the old server. oldListeners := srv.GetApiServerListeners() srv.Kill() // The new server takes the original listeners. srv, err = server.New(*cfg, oldListeners) if err != nil { log.Fatal(err) } srv.Run() srv.SetReconfigServer(false) } shutdown := func() { log.Infof("Gracefully shutting down") srvMutex.Lock() defer srvMutex.Unlock() srv.Kill() srv.Purge() os.Exit(0) } writeState := func() { log.Infof("Dumping server state") srvMutex.Lock() defer srvMutex.Unlock() encoded, err := json.Marshal(srv) if err != nil { log.Errorf("Failed to dump server state: %v", err) return } if _, err := os.Stdout.Write(encoded); err != nil { log.Errorf("Failed to dump server state: %v", err) return } os.Stdout.Write([]byte("\n")) log.Debugf("Finished dumping server state") } signals := map[os.Signal]func(){ syscall.SIGHUP: reconfigure, syscall.SIGTERM: shutdown, syscall.SIGINT: shutdown, syscall.SIGUSR1: writeState, } listenForSignals(signals) }
func main() { // We use a FlagSets since glog adds a bunch of flags we do not want to publish userset := flag.NewFlagSet("fleet", flag.ExitOnError) printVersion := userset.Bool("version", false, "Print the version and exit") cfgPath := userset.String("config", "", fmt.Sprintf("Path to config file. Fleet will look for a config at %s by default.", DefaultConfigFile)) // Initialize logging so we have it set up while parsing config information config.UpdateLoggingFlagsFromConfig(flag.CommandLine, &config.Config{}) err := userset.Parse(os.Args[1:]) if err == flag.ErrHelp { userset.Usage() syscall.Exit(1) } if *printVersion { fmt.Println("fleet version", version.Version) os.Exit(0) } cfgset := flag.NewFlagSet("fleet", flag.ExitOnError) cfgset.Int("verbosity", 0, "Logging level") cfgset.Var(&stringSlice{}, "etcd_servers", "List of etcd endpoints") cfgset.String("boot_id", "", "Override default BootID of fleet machine") cfgset.String("public_ip", "", "IP address that fleet machine should publish") cfgset.String("metadata", "", "List of key-value metadata to assign to the fleet machine") cfgset.String("unit_prefix", "", "Prefix that should be used for all systemd units") cfgset.String("agent_ttl", agent.DefaultTTL, "TTL in seconds of fleet machine state in etcd") cfgset.Bool("verify_units", false, "Verify unit file signatures using local SSH identities") cfgset.String("authorized_keys_file", sign.DefaultAuthorizedKeysFile, "File containing public SSH keys to be used for signature verification") globalconf.Register("", cfgset) cfg, err := getConfig(cfgset, *cfgPath) if err != nil { glog.Error(err.Error()) syscall.Exit(1) } srv := server.New(*cfg) srv.Run() reconfigure := func() { glog.Infof("Reloading configuration from %s", *cfgPath) cfg, err := getConfig(cfgset, *cfgPath) if err != nil { glog.Errorf(err.Error()) syscall.Exit(1) } srv.Stop() srv = server.New(*cfg) srv.Run() } shutdown := func() { glog.Infof("Gracefully shutting down") srv.Stop() srv.Purge() syscall.Exit(0) } writeState := func() { glog.Infof("Dumping server state") encoded, err := json.Marshal(srv) if err != nil { glog.Errorf("Failed to dump server state: %v", err) return } if _, err := os.Stdout.Write(encoded); err != nil { glog.Errorf("Failed to dump server state: %v", err) return } os.Stdout.Write([]byte("\n")) glog.V(1).Infof("Finished dumping server state") } signals := map[os.Signal]func(){ syscall.SIGHUP: reconfigure, syscall.SIGTERM: shutdown, syscall.SIGINT: shutdown, syscall.SIGUSR1: writeState, } listenForSignals(signals) }
func main() { // We use a FlagSets since glog adds a bunch of flags we do not want to publish userset := flag.NewFlagSet("fleet", flag.ExitOnError) printVersion := userset.Bool("version", false, "Print the version and exit") cfgPath := userset.String("config", "/etc/fleet/fleet.conf", "Path to config file") err := userset.Parse(os.Args[1:]) if err == flag.ErrHelp { userset.Usage() syscall.Exit(1) } if *printVersion { fmt.Println("fleet version", version.Version) os.Exit(0) } cfgset := flag.NewFlagSet("fleet", flag.ExitOnError) cfgset.Int("verbosity", 0, "Logging level") cfgset.Var(&stringSlice{}, "etcd_servers", "List of etcd endpoints") cfgset.String("boot_id", "", "Override default BootID of fleet machine") cfgset.String("public_ip", "", "IP address that fleet machine should publish") cfgset.String("metadata", "", "List of key-value metadata to assign to the fleet machine") cfgset.String("unit_prefix", "", "Prefix that should be used for all systemd units") cfgset.String("agent_ttl", agent.DefaultTTL, "TTL in seconds of fleet machine state in etcd") globalconf.Register("", cfgset) cfg, err := getConfig(cfgset, *cfgPath) if err != nil { glog.Error(err.Error()) syscall.Exit(1) } config.UpdateLoggingFlagsFromConfig(cfg) etcd.SetLogger(etcdLogger{}) srv := server.New(*cfg) srv.Run() reconfigure := func() { glog.Infof("Reloading configuration from %s", *cfgPath) cfg, err := getConfig(cfgset, *cfgPath) if err != nil { glog.Errorf(err.Error()) syscall.Exit(1) } srv.Stop() config.UpdateLoggingFlagsFromConfig(cfg) srv = server.New(*cfg) srv.Run() } shutdown := func() { glog.Infof("Gracefully shutting down") srv.Stop() srv.Purge() syscall.Exit(0) } signals := map[os.Signal]func(){ syscall.SIGHUP: reconfigure, syscall.SIGTERM: shutdown, syscall.SIGINT: shutdown, } listenForSignals(signals) }
func main() { // We use a custom FlagSet since golang/glog adds a bunch of flags we // do not want to publish flagset := flag.NewFlagSet("fleet", flag.ExitOnError) printVersion := flagset.Bool("version", false, "Prints the version.") cfgPath := flagset.String("config", "", "Path to config file.") err := flagset.Parse(os.Args[1:]) // We do this manually since we're using a custom FlagSet if err == flag.ErrHelp { flag.Usage() syscall.Exit(1) } if *printVersion { fmt.Println("fleet version", version.Version) os.Exit(0) } // Print out to stderr by default (stderr instead of stdout due to glog's choices) flag.Lookup("logtostderr").Value.Set("true") cfg, err := loadConfigFromPath(*cfgPath) if err != nil { glog.Errorf(err.Error()) syscall.Exit(1) } etcd.SetLogger(etcdLogger{}) srv := server.New(*cfg) srv.Run() reconfigure := func() { glog.Infof("Reloading config file from %s", *cfgPath) cfg, err := loadConfigFromPath(*cfgPath) if err != nil { glog.Errorf(err.Error()) syscall.Exit(1) } else { srv.Stop() srv = server.New(*cfg) srv.Run() } } shutdown := func() { glog.Infof("Gracefully shutting down") srv.Stop() srv.Purge() syscall.Exit(0) } signals := map[os.Signal]func(){ syscall.SIGHUP: reconfigure, syscall.SIGTERM: shutdown, syscall.SIGINT: shutdown, } listenForSignals(signals) }