Use autogenerated Synchting apikey.
[src/xds/xds-server.git] / lib / syncthing / st.go
index 15cab0d..841901d 100644 (file)
@@ -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"
@@ -27,13 +31,15 @@ type SyncThing struct {
        APIKey  string
        Home    string
        STCmd   *exec.Cmd
+       STICmd  *exec.Cmd
 
        // Private fields
-       binDir     string
-       logsDir    string
-       exitSTChan chan ExitChan
-       client     *common.HTTPClient
-       log        *logrus.Logger
+       binDir      string
+       logsDir     string
+       exitSTChan  chan ExitChan
+       exitSTIChan chan ExitChan
+       client      *common.HTTPClient
+       log         *logrus.Logger
 }
 
 // ExitChan Channel used for process exit
@@ -170,9 +176,48 @@ 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("<apikey>(.*)</apikey>")
+                               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  url=%s", s.BaseURL)
+
+       args := []string{
+               "-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(exeName, args, env, &s.exitSTIChan)
+
+       return s.STICmd, err
+}
+
 func (s *SyncThing) stopProc(pname string, proc *os.Process, exit chan ExitChan) {
        if err := proc.Signal(os.Interrupt); err != nil {
                s.log.Infof("Proc interrupt %s error: %s", pname, err.Error())
@@ -199,6 +244,15 @@ func (s *SyncThing) Stop() {
        s.STCmd = nil
 }
 
+// StopInotify Stops syncthing process
+func (s *SyncThing) StopInotify() {
+       if s.STICmd == nil {
+               return
+       }
+       s.stopProc("syncthing-inotify", s.STICmd.Process, s.exitSTIChan)
+       s.STICmd = nil
+}
+
 // Connect Establish HTTP connection with Syncthing
 func (s *SyncThing) Connect() error {
        var err error
@@ -217,6 +271,9 @@ func (s *SyncThing) Connect() error {
        if s.client == nil {
                return fmt.Errorf("ERROR: cannot connect to Syncthing (null client)")
        }
+
+       s.client.SetLogger(s.log)
+
        return nil
 }