syncthing/lib/versioner/external.go

127 lines
3.0 KiB
Go
Raw Normal View History

2015-03-19 10:31:21 +00:00
// Copyright (C) 2015 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2015-03-19 10:31:21 +00:00
package versioner
import (
"context"
2015-03-19 10:31:21 +00:00
"errors"
"fmt"
2015-03-19 10:31:21 +00:00
"os"
"os/exec"
"strings"
"time"
2015-04-14 10:31:25 +00:00
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/fs"
"github.com/kballard/go-shellquote"
2015-03-19 10:31:21 +00:00
)
func init() {
// Register the constructor for this type of versioner with the name "external"
factories["external"] = newExternal
2015-03-19 10:31:21 +00:00
}
type external struct {
2015-03-19 10:31:21 +00:00
command string
filesystem fs.Filesystem
2015-03-19 10:31:21 +00:00
}
func newExternal(cfg config.FolderConfiguration) Versioner {
command := cfg.Versioning.Params["command"]
2015-03-19 10:31:21 +00:00
if build.IsWindows {
command = strings.ReplaceAll(command, `\`, `\\`)
}
s := external{
2015-03-19 10:31:21 +00:00
command: command,
filesystem: cfg.Filesystem(nil),
2015-03-19 10:31:21 +00:00
}
Implement facility based logger, debugging via REST API This implements a new debug/trace infrastructure based on a slightly hacked up logger. Instead of the traditional "if debug { ... }" I've rewritten the logger to have no-op Debugln and Debugf, unless debugging has been enabled for a given "facility". The "facility" is just a string, typically a package name. This will be slightly slower than before; but not that much as it's mostly a function call that returns immediately. For the cases where it matters (the Debugln takes a hex.Dump() of something for example, and it's not in a very occasional "if err != nil" branch) there is an l.ShouldDebug(facility) that is fast enough to be used like the old "if debug". The point of all this is that we can now toggle debugging for the various packages on and off at runtime. There's a new method /rest/system/debug that can be POSTed a set of facilities to enable and disable debug for, or GET from to get a list of facilities with descriptions and their current debug status. Similarly a /rest/system/log?since=... can grab the latest log entries, up to 250 of them (hardcoded constant in main.go) plus the initial few. Not implemented in this commit (but planned) is a simple debug GUI available on /debug that shows the current log in an easily pasteable format and has checkboxes to enable the various debug facilities. The debug instructions to a user then becomes "visit this URL, check these boxes, reproduce your problem, copy and paste the log". The actual log viewer on the hypothetical /debug URL can poll regularly for new log entries and this bypass the 250 line limit. The existing STTRACE=foo variable is still obeyed and just sets the start state of the system.
2015-10-03 15:25:21 +00:00
l.Debugf("instantiated %#v", s)
2015-03-19 10:31:21 +00:00
return s
}
2015-04-28 20:32:10 +00:00
// Archive moves the named file away to a version archive. If this function
// returns nil, the named file does not exist any more (has been archived).
func (v external) Archive(filePath string) error {
info, err := v.filesystem.Lstat(filePath)
if fs.IsNotExist(err) {
Implement facility based logger, debugging via REST API This implements a new debug/trace infrastructure based on a slightly hacked up logger. Instead of the traditional "if debug { ... }" I've rewritten the logger to have no-op Debugln and Debugf, unless debugging has been enabled for a given "facility". The "facility" is just a string, typically a package name. This will be slightly slower than before; but not that much as it's mostly a function call that returns immediately. For the cases where it matters (the Debugln takes a hex.Dump() of something for example, and it's not in a very occasional "if err != nil" branch) there is an l.ShouldDebug(facility) that is fast enough to be used like the old "if debug". The point of all this is that we can now toggle debugging for the various packages on and off at runtime. There's a new method /rest/system/debug that can be POSTed a set of facilities to enable and disable debug for, or GET from to get a list of facilities with descriptions and their current debug status. Similarly a /rest/system/log?since=... can grab the latest log entries, up to 250 of them (hardcoded constant in main.go) plus the initial few. Not implemented in this commit (but planned) is a simple debug GUI available on /debug that shows the current log in an easily pasteable format and has checkboxes to enable the various debug facilities. The debug instructions to a user then becomes "visit this URL, check these boxes, reproduce your problem, copy and paste the log". The actual log viewer on the hypothetical /debug URL can poll regularly for new log entries and this bypass the 250 line limit. The existing STTRACE=foo variable is still obeyed and just sets the start state of the system.
2015-10-03 15:25:21 +00:00
l.Debugln("not archiving nonexistent file", filePath)
2015-03-19 10:31:21 +00:00
return nil
} else if err != nil {
return err
}
if info.IsSymlink() {
panic("bug: attempting to version a symlink")
}
2015-03-19 10:31:21 +00:00
Implement facility based logger, debugging via REST API This implements a new debug/trace infrastructure based on a slightly hacked up logger. Instead of the traditional "if debug { ... }" I've rewritten the logger to have no-op Debugln and Debugf, unless debugging has been enabled for a given "facility". The "facility" is just a string, typically a package name. This will be slightly slower than before; but not that much as it's mostly a function call that returns immediately. For the cases where it matters (the Debugln takes a hex.Dump() of something for example, and it's not in a very occasional "if err != nil" branch) there is an l.ShouldDebug(facility) that is fast enough to be used like the old "if debug". The point of all this is that we can now toggle debugging for the various packages on and off at runtime. There's a new method /rest/system/debug that can be POSTed a set of facilities to enable and disable debug for, or GET from to get a list of facilities with descriptions and their current debug status. Similarly a /rest/system/log?since=... can grab the latest log entries, up to 250 of them (hardcoded constant in main.go) plus the initial few. Not implemented in this commit (but planned) is a simple debug GUI available on /debug that shows the current log in an easily pasteable format and has checkboxes to enable the various debug facilities. The debug instructions to a user then becomes "visit this URL, check these boxes, reproduce your problem, copy and paste the log". The actual log viewer on the hypothetical /debug URL can poll regularly for new log entries and this bypass the 250 line limit. The existing STTRACE=foo variable is still obeyed and just sets the start state of the system.
2015-10-03 15:25:21 +00:00
l.Debugln("archiving", filePath)
2015-03-19 10:31:21 +00:00
if v.command == "" {
return errors.New("command is empty, please enter a valid command")
}
words, err := shellquote.Split(v.command)
2015-03-19 10:31:21 +00:00
if err != nil {
return fmt.Errorf("command is invalid: %w", err)
2015-03-19 10:31:21 +00:00
}
context := map[string]string{
"%FOLDER_FILESYSTEM%": v.filesystem.Type().String(),
"%FOLDER_PATH%": v.filesystem.URI(),
"%FILE_PATH%": filePath,
}
for i, word := range words {
for key, val := range context {
word = strings.ReplaceAll(word, key, val)
}
words[i] = word
2015-03-19 10:31:21 +00:00
}
cmd := exec.Command(words[0], words[1:]...)
2015-03-19 10:31:21 +00:00
env := os.Environ()
// filter STGUIAUTH and STGUIAPIKEY from environment variables
filteredEnv := []string{}
for _, x := range env {
if !strings.HasPrefix(x, "STGUIAUTH=") && !strings.HasPrefix(x, "STGUIAPIKEY=") {
filteredEnv = append(filteredEnv, x)
}
}
cmd.Env = filteredEnv
combinedOutput, err := cmd.CombinedOutput()
l.Debugln("external command output:", string(combinedOutput))
2015-03-19 10:31:21 +00:00
if err != nil {
if eerr, ok := err.(*exec.ExitError); ok && len(eerr.Stderr) > 0 {
return fmt.Errorf("%v: %v", err, string(eerr.Stderr))
}
2015-03-19 10:31:21 +00:00
return err
}
// return error if the file was not removed
if _, err = v.filesystem.Lstat(filePath); fs.IsNotExist(err) {
2015-03-19 10:31:21 +00:00
return nil
}
return errors.New("file was not removed by external script")
2015-03-19 10:31:21 +00:00
}
func (external) GetVersions() (map[string][]FileVersion, error) {
return nil, ErrRestorationNotSupported
}
func (external) Restore(_ string, _ time.Time) error {
return ErrRestorationNotSupported
}
func (external) Clean(_ context.Context) error {
return nil
}