X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fsyncthing%2Fst.go;h=957dd6533167edbf936f93f2c81cf6d285efa094;hb=db2c02a9bfd709c662872fccc86d011136e9d8d1;hp=9452fbd6b9aacbde7c8e172d654a5110814f9e91;hpb=40a7183f3b4aa32379aa8b4949f5f9c5e32f79f6;p=src%2Fxds%2Fxds-server.git diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go index 9452fbd..957dd65 100644 --- a/lib/syncthing/st.go +++ b/lib/syncthing/st.go @@ -15,6 +15,10 @@ import ( "io" + "io/ioutil" + + "regexp" + "github.com/Sirupsen/logrus" "github.com/iotbzh/xds-server/lib/common" "github.com/iotbzh/xds-server/lib/xdsconfig" @@ -34,6 +38,7 @@ type SyncThing struct { logsDir string exitSTChan chan ExitChan exitSTIChan chan ExitChan + conf *xdsconfig.Config client *common.HTTPClient log *logrus.Logger } @@ -81,6 +86,7 @@ func NewSyncThing(conf *xdsconfig.Config, log *logrus.Logger) *SyncThing { binDir: binDir, logsDir: conf.FileConf.LogsDir, log: log, + conf: conf, } return &s @@ -172,27 +178,44 @@ func (s *SyncThing) Start() (*exec.Cmd, error) { 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 { + defer fd.Close() + if b, err := ioutil.ReadAll(fd); err == nil { + re := regexp.MustCompile("(.*)") + key := re.FindStringSubmatch(string(b)) + if len(key) >= 1 { + s.APIKey = key[1] + } + } + } + } + return s.STCmd, err } // StartInotify Starts syncthing-inotify process func (s *SyncThing) StartInotify() (*exec.Cmd, error) { var err error + exeName := "syncthing-inotify" - s.log.Infof(" STI home=%s", s.Home) s.log.Infof(" STI url=%s", s.BaseURL) args := []string{ - "--home=" + s.Home, "-target=" + s.BaseURL, } + if s.APIKey != "" { + args = append(args, "-api="+s.APIKey) + s.log.Infof("%s uses apikey=%s", exeName, s.APIKey) + } if s.log.Level == logrus.DebugLevel { args = append(args, "-verbosity=4") } env := []string{} - s.STICmd, err = s.startProc("syncthing-inotify", args, env, &s.exitSTIChan) + s.STICmd, err = s.startProc(exeName, args, env, &s.exitSTIChan) return s.STICmd, err }