aboutsummaryrefslogtreecommitdiffstats
path: root/lib/syncthing
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-06-23 16:55:54 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-06-23 18:03:58 +0200
commitf53cc64ea17876c7ad8f088fc069448b2414b808 (patch)
tree6fefa38dda16c21c4fc63621f47e0b5398aff0f6 /lib/syncthing
parent228292f1a8b2758cca63c934fdb0762377e5b31a (diff)
Add workaround for Syncthing apikey setting.
Diffstat (limited to 'lib/syncthing')
-rw-r--r--lib/syncthing/st.go34
1 files changed, 33 insertions, 1 deletions
diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go
index 5976e2f..8fd50d3 100644
--- a/lib/syncthing/st.go
+++ b/lib/syncthing/st.go
@@ -169,13 +169,45 @@ func (s *SyncThing) Start() (*exec.Cmd, error) {
env := []string{
"STNODEFAULTFOLDER=1",
+ "STNOUPGRADE=1",
+ "STNORESTART=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>")