X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fsyncthing%2Fst.go;h=1f7875749eddc42952413fe10a722fd8b44bc96a;hb=41109938056e2c0ca3af355de8947380e9a3b557;hp=5976e2f769c25fb273ffec032888f47ab3ddd03e;hpb=a50baa7c309f7eb55fe87c71f4c688ace325b6ac;p=src%2Fxds%2Fxds-agent.git diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go index 5976e2f..1f78757 100644 --- a/lib/syncthing/st.go +++ b/lib/syncthing/st.go @@ -17,8 +17,8 @@ import ( "os/exec" "github.com/Sirupsen/logrus" - "github.com/iotbzh/xds-agent/lib/common" "github.com/iotbzh/xds-agent/lib/xdsconfig" + common "github.com/iotbzh/xds-common/golib" "github.com/syncthing/syncthing/lib/config" ) @@ -95,9 +95,17 @@ func (s *SyncThing) startProc(exeName string, args []string, env []string, eChan exec.Command("bash", "-c", "pkill -9 "+exeName).Output() } - path, err := exec.LookPath(path.Join(s.binDir, exeName)) + // When not set (or set to '.') set bin to path of xds-agent executable + bdir := s.binDir + if bdir == "" || bdir == "." { + if dir, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil { + bdir = dir + } + } + + path, err := exec.LookPath(path.Join(bdir, exeName)) if err != nil { - return nil, fmt.Errorf("Cannot find %s executable in %s", exeName, s.binDir) + return nil, fmt.Errorf("Cannot find %s executable in %s", exeName, bdir) } cmd := exec.Command(path, args...) cmd.Env = os.Environ() @@ -169,13 +177,44 @@ func (s *SyncThing) Start() (*exec.Cmd, error) { env := []string{ "STNODEFAULTFOLDER=1", + "STNOUPGRADE=1", + } + + // XXX - temporary hack because -gui-apikey seems to correctly handle by + // syncthing the early first time + stConfigFile := filepath.Join(s.Home, "config.xml") + if s.APIKey != "" && !common.Exists(stConfigFile) { + s.log.Infof("Stop and restart Syncthing (hack for apikey setting)") + s.STCmd, err = s.startProc("syncthing", args, env, &s.exitSTChan) + tmo := 20 + for ; tmo > 0; tmo-- { + s.log.Debugf("Waiting Syncthing config.xml creation (%v)\n", tmo) + time.Sleep(500 * time.Millisecond) + if common.Exists(stConfigFile) { + break + } + } + if tmo <= 0 { + return nil, fmt.Errorf("Cannot start Syncthing for config file creation") + } + s.Stop() + read, err := ioutil.ReadFile(stConfigFile) + if err != nil { + return nil, fmt.Errorf("Cannot read Syncthing config file for apikey setting") + } + re := regexp.MustCompile(`.*`) + newContents := re.ReplaceAllString(string(read), ""+s.APIKey+"") + err = ioutil.WriteFile(stConfigFile, []byte(newContents), 0) + if err != nil { + return nil, fmt.Errorf("Cannot write Syncthing config file to set apikey") + } } s.STCmd, err = s.startProc("syncthing", args, env, &s.exitSTChan) // Use autogenerated apikey if not set by config.json if s.APIKey == "" { - if fd, err := os.Open(filepath.Join(s.Home, "config.xml")); err == nil { + if fd, err := os.Open(stConfigFile); err == nil { defer fd.Close() if b, err := ioutil.ReadAll(fd); err == nil { re := regexp.MustCompile("(.*)") @@ -269,7 +308,9 @@ func (s *SyncThing) Connect() error { return fmt.Errorf("ERROR: cannot connect to Syncthing (null client)") } - s.client.SetLogger(s.log) + s.client.SetLogLevel(s.log.Level.String()) + s.client.LoggerPrefix = "SYNCTHING: " + s.client.LoggerOut = s.log.Out return nil }