// saveCurrentState saves the current running turtle apps. func saveCurrentState() error { // Create a new state value. var s state // Get all apps. curApps := apps.Apps() for _, app := range curApps { // Skip if not running. if !app.IsRunning() { continue } // Add the app name to the running apps slice. s.RunningApps = append(s.RunningApps, app.Name()) } // Encode the state value to TOML. buf := new(bytes.Buffer) err := toml.NewEncoder(buf).Encode(&s) if err != nil { return fmt.Errorf("failed to encode turtle state value to toml: %v", err) } // Write the result to the app settings file. err = ioutil.WriteFile(config.Config.StateFilePath(), buf.Bytes(), 0600) if err != nil { return fmt.Errorf("failed to save turtle state value: %v", err) } return nil }
// handleList sends a list of all Apps. func handleList(request *api.Request) (interface{}, error) { // Get all apps. curApps := apps.Apps() // Create the response value. res := api.ResponseList{ Apps: make([]api.ResponseListApp, len(curApps)), } // Add all the apps to the response value. for i, app := range curApps { // Get the turtlefile. // Continue also on error. var turtlefileName string t, err := app.Turtlefile() if err != nil { turtlefileName = "ERROR In FILE" } else { turtlefileName = t.Name } res.Apps[i] = api.ResponseListApp{ Name: app.Name(), Turtlefile: turtlefileName, State: app.State(), } } return res, nil }
func removeOldBackups() error { // Lock the mutex. removeOldBackupsMutex.Lock() defer removeOldBackupsMutex.Unlock() var allErr string addErr := func(err error) { allErr += err.Error() + "\n" } // Create the expiration unix timestamp. expire := time.Now().Unix() - config.Config.KeepBackupsDuration // Get all apps. curApps := apps.Apps() for _, app := range curApps { // Get all backups of the app. backups, err := app.Backups() if err != nil { addErr(err) continue } // Remove all old backups. for _, b := range backups { u, err := strconv.ParseInt(b, 10, 64) if err != nil { addErr(err) continue } // Skip if this is not an old backup. if (expire - u) <= 0 { continue } log.Infof("Removing old backup '%s' of app '%s'.", b, app.Name()) // Remove the backup. err = app.RemoveBackup(b) if err != nil { addErr(err) continue } } } // Trim the all error messages string. allErr = strings.TrimSpace(allErr) // Return the error(s) if present. if len(allErr) > 0 { return fmt.Errorf(allErr) } return nil }
// restoreState loads the turtle state file if present and // restores the previous running apps. func restoreState() error { var s state // Set the turtle state file path. statePath := config.Config.StateFilePath() // Skip if it does not exists. e, err := utils.Exists(statePath) if err != nil { return err } else if !e { return nil } // Load and decode the file. _, err = toml.DecodeFile(statePath, &s) if err != nil { return fmt.Errorf("failed to load turtle state file '%s': %v", statePath, err) } // Get all apps. curApps := apps.Apps() // Collect all errors during startup. var allErr string // Start the apps which where running during the last turtle daemon shutdown. for _, rApp := range s.RunningApps { for _, app := range curApps { // Skip if this is not our searched app or if it is already running. if rApp != app.Name() || app.IsRunning() { continue } // Start the app. err = app.Start() if err != nil { allErr += err.Error() + "\n" } } } // Trim the new linew at the end. allErr = strings.TrimSpace(allErr) // Return the error(s) if present. if len(allErr) > 0 { return fmt.Errorf(allErr) } return nil }