From 1737d3f7b22ed41c4ea8424a554c04cc967ab61a Mon Sep 17 00:00:00 2001 From: mzz2017 <2017@duck.com> Date: Mon, 27 Feb 2023 15:10:15 +0800 Subject: [PATCH] feat: allow no node and add run file --- cmd/run.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index fab2f8b..59a3cef 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -14,13 +14,25 @@ import ( "os/signal" "path/filepath" "runtime" + "strconv" "strings" "syscall" ) +const ( + PidFilePath = "/var/run/dae.pid" +) + +func init() { + runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file") + runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "disable timestamp") + runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-runfile", "", false, "not generate /var/run/dae.pid") +} + var ( cfgFile string disableTimestamp bool + disableRunFile bool runCmd = &cobra.Command{ Use: "run", @@ -50,11 +62,6 @@ var ( } ) -func init() { - runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file") - runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "disable timestamp") -} - func Run(log *logrus.Logger, conf *config.Config) (err error) { // New ControlPlane. @@ -72,6 +79,9 @@ func Run(log *logrus.Logger, conf *config.Config) (err error) { go func() { <-readyChan sdnotify.Ready() + if !disableRunFile { + _ = os.WriteFile(PidFilePath, []byte(strconv.Itoa(os.Getpid())), 0644) + } }() if listener, err = c.ListenAndServe(readyChan, conf.Global.TproxyPort); err != nil { log.Errorln("ListenAndServe:", err) @@ -124,6 +134,7 @@ loop: if e := c.Close(); e != nil { return fmt.Errorf("close control plane: %w", e) } + _ = os.Remove(PidFilePath) return nil } @@ -136,17 +147,23 @@ func newControlPlane(log *logrus.Logger, bpf interface{}, conf *config.Config) ( } } // Resolve subscriptions to nodes. + resolvingfailed := false for _, sub := range conf.Subscription { tag, nodes, err := internal.ResolveSubscription(log, filepath.Dir(cfgFile), string(sub)) if err != nil { log.Warnf(`failed to resolve subscription "%v": %v`, sub, err) + resolvingfailed = true } if len(nodes) > 0 { tagToNodeList[tag] = append(tagToNodeList[tag], nodes...) } } if len(tagToNodeList) == 0 { - return nil, fmt.Errorf("no node found, which could because all subscription resolving failed") + if resolvingfailed { + log.Warnln("No node found because all subscription resolving failed.") + } else { + log.Warnln("No node found.") + } } if len(conf.Global.LanInterface) == 0 && len(conf.Global.WanInterface) == 0 {