Command line flag and REST command to reset and resync (fixes #85)

Still needs implemention in GUI
This commit is contained in:
Jakob Borg 2014-04-03 22:10:51 +02:00
parent 15699a39cf
commit d30a286f38
2 changed files with 33 additions and 1 deletions

View File

@ -38,6 +38,7 @@ func startGUI(addr string, m *Model) {
router.Post("/rest/config", restPostConfig) router.Post("/rest/config", restPostConfig)
router.Post("/rest/restart", restPostRestart) router.Post("/rest/restart", restPostRestart)
router.Post("/rest/reset", restPostReset)
router.Post("/rest/error", restPostError) router.Post("/rest/error", restPostError)
go func() { go func() {
@ -112,7 +113,12 @@ func restGetConfigInSync(w http.ResponseWriter) {
} }
func restPostRestart(req *http.Request) { func restPostRestart(req *http.Request) {
restart() go restart()
}
func restPostReset(req *http.Request) {
resetRepositories()
go restart()
} }
type guiFile scanner.File type guiFile scanner.File

View File

@ -59,8 +59,10 @@ const (
) )
func main() { func main() {
var reset bool
var showVersion bool var showVersion bool
flag.StringVar(&confDir, "home", getDefaultConfDir(), "Set configuration directory") flag.StringVar(&confDir, "home", getDefaultConfDir(), "Set configuration directory")
flag.BoolVar(&reset, "reset", false, "Prepare to resync from cluster")
flag.BoolVar(&showVersion, "version", false, "Show version") flag.BoolVar(&showVersion, "version", false, "Show version")
flag.Usage = usageFor(flag.CommandLine, usage, extraUsage) flag.Usage = usageFor(flag.CommandLine, usage, extraUsage)
flag.Parse() flag.Parse()
@ -163,6 +165,11 @@ func main() {
infof("Edit %s to taste or use the GUI\n", cfgFile) infof("Edit %s to taste or use the GUI\n", cfgFile)
} }
if reset {
resetRepositories()
os.Exit(0)
}
if profiler := os.Getenv("STPROFILER"); len(profiler) > 0 { if profiler := os.Getenv("STPROFILER"); len(profiler) > 0 {
go func() { go func() {
dlog.Println("Starting profiler on", profiler) dlog.Println("Starting profiler on", profiler)
@ -260,6 +267,25 @@ func main() {
select {} select {}
} }
func resetRepositories() {
suffix := fmt.Sprintf(".syncthing-reset-%d", time.Now().UnixNano())
for _, repo := range cfg.Repositories {
if _, err := os.Stat(repo.Directory); err == nil {
infof("Reset: Moving %s -> %s", repo.Directory, repo.Directory+suffix)
os.Rename(repo.Directory, repo.Directory+suffix)
}
}
pat := filepath.Join(confDir, "*.idx.gz")
idxs, err := filepath.Glob(pat)
if err == nil {
for _, idx := range idxs {
infof("Reset: Removing %s", idx)
os.Remove(idx)
}
}
}
func restart() { func restart() {
infoln("Restarting") infoln("Restarting")
if os.Getenv("SMF_FMRI") != "" || os.Getenv("STNORESTART") != "" { if os.Getenv("SMF_FMRI") != "" || os.Getenv("STNORESTART") != "" {