Fixed syncthing binDir when set to '.'
[src/xds/xds-agent.git] / lib / syncthing / st.go
index 5976e2f..1f78757 100644 (file)
@@ -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(`<apikey>.*</apikey>`)
+               newContents := re.ReplaceAllString(string(read), "<apikey>"+s.APIKey+"</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("<apikey>(.*)</apikey>")
@@ -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
 }