X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=lib%2Fsyncthing%2Fst.go;h=bc6529917bae15deb8f1152d2ec998de92598dd4;hb=a2effe5ecec057748e30050bd2d3bad6501a2aef;hp=3380cda785ce7515f99c901c8f0b38e2802f3410;hpb=ea3b17feb2eb2d54bbc27dc75eee60bd1fe67d27;p=src%2Fxds%2Fxds-server.git diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go index 3380cda..bc65299 100644 --- a/lib/syncthing/st.go +++ b/lib/syncthing/st.go @@ -27,11 +27,13 @@ import ( // SyncThing . type SyncThing struct { - BaseURL string - APIKey string - Home string - STCmd *exec.Cmd - STICmd *exec.Cmd + BaseURL string + APIKey string + Home string + STCmd *exec.Cmd + STICmd *exec.Cmd + MyID string + Connected bool // Private fields binDir string @@ -41,6 +43,7 @@ type SyncThing struct { conf *xdsconfig.Config client *common.HTTPClient log *logrus.Logger + Events *Events } // ExitChan Channel used for process exit @@ -88,7 +91,6 @@ type FolderStatus struct { // NewSyncThing creates a new instance of Syncthing func NewSyncThing(conf *xdsconfig.Config, log *logrus.Logger) *SyncThing { var url, apiKey, home, binDir string - var err error stCfg := conf.FileConf.SThgConf if stCfg != nil { @@ -106,13 +108,7 @@ func NewSyncThing(conf *xdsconfig.Config, log *logrus.Logger) *SyncThing { } if home == "" { - home = "/mnt/share" - } - - if binDir == "" { - if binDir, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil { - binDir = "/usr/local/bin" - } + panic("home parameter must be set") } s := SyncThing{ @@ -125,22 +121,42 @@ func NewSyncThing(conf *xdsconfig.Config, log *logrus.Logger) *SyncThing { conf: conf, } + // Create Events monitoring + s.Events = s.NewEventListener() + return &s } // Start Starts syncthing process func (s *SyncThing) startProc(exeName string, args []string, env []string, eChan *chan ExitChan) (*exec.Cmd, error) { + var err error + var exePath string // Kill existing process (useful for debug ;-) ) if os.Getenv("DEBUG_MODE") != "" { 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 == "." { + exe, _ := os.Executable() + if exeAbsPath, err := filepath.Abs(exe); err == nil { + if exePath, err := filepath.EvalSymlinks(exeAbsPath); err == nil { + bdir = filepath.Dir(exePath) + } + } + } + + exePath, err = exec.LookPath(path.Join(bdir, exeName)) if err != nil { - return nil, fmt.Errorf("Cannot find %s executable in %s", exeName, s.binDir) + // Let's try in /opt/AGL/bin + exePath, err = exec.LookPath(path.Join("opt", "AGL", "bin", exeName)) + if err != nil { + return nil, fmt.Errorf("Cannot find %s executable in %s", exeName, bdir) + } } - cmd := exec.Command(path, args...) + cmd := exec.Command(exePath, args...) cmd.Env = os.Environ() for _, ev := range env { cmd.Env = append(cmd.Env, ev) @@ -211,13 +227,13 @@ func (s *SyncThing) Start() (*exec.Cmd, error) { env := []string{ "STNODEFAULTFOLDER=1", "STNOUPGRADE=1", - "STNORESTART=1", + "STNORESTART=1", // FIXME SEB remove ? } s.STCmd, err = s.startProc("syncthing", args, env, &s.exitSTChan) // Use autogenerated apikey if not set by config.json - if s.APIKey == "" { + if err == nil && 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 { @@ -296,6 +312,7 @@ func (s *SyncThing) StopInotify() { // Connect Establish HTTP connection with Syncthing func (s *SyncThing) Connect() error { var err error + s.Connected = false s.client, err = common.HTTPNewClient(s.BaseURL, common.HTTPClientConfig{ URLPrefix: "/rest", @@ -312,9 +329,22 @@ func (s *SyncThing) Connect() error { return fmt.Errorf("ERROR: cannot connect to Syncthing (null client)") } - s.client.SetLogger(s.log) + // Redirect HTTP log into a file + s.client.SetLogLevel(s.conf.Log.Level.String()) + s.client.LoggerPrefix = "SYNCTHING: " + s.client.LoggerOut = s.conf.LogVerboseOut - return nil + s.MyID, err = s.IDGet() + if err != nil { + return fmt.Errorf("ERROR: cannot retrieve ID") + } + + s.Connected = true + + // Start events monitoring + err = s.Events.Start() + + return err } // IDGet returns the Syncthing ID of Syncthing instance running locally @@ -360,44 +390,3 @@ func (s *SyncThing) IsConfigInSync() (bool, error) { } return d.ConfigInSync, nil } - -// FolderStatus Returns all information about the current -func (s *SyncThing) FolderStatus(folderID string) (*FolderStatus, error) { - var data []byte - var res FolderStatus - if folderID == "" { - return nil, fmt.Errorf("folderID not set") - } - if err := s.client.HTTPGet("db/status?folder="+folderID, &data); err != nil { - return nil, err - } - if err := json.Unmarshal(data, &res); err != nil { - return nil, err - } - return &res, nil -} - -// IsFolderInSync Returns true when folder is in sync -func (s *SyncThing) IsFolderInSync(folderID string) (bool, error) { - // FIXME better to detected FolderCompletion event (/rest/events) - // See https://docs.syncthing.net/dev/events.html - sts, err := s.FolderStatus(folderID) - if err != nil { - return false, err - } - return sts.NeedBytes == 0, nil -} - -// FolderScan Request immediate folder scan. -// Scan all folders if folderID param is empty -func (s *SyncThing) FolderScan(folderID string, subpath string) error { - url := "db/scan" - if folderID != "" { - url += "?folder=" + folderID - - if subpath != "" { - url += "&sub=" + subpath - } - } - return s.client.HTTPPost(url, "") -}