github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/model/instance/lifecycle/reset.go (about) 1 package lifecycle 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/cozy/cozy-stack/model/bitwarden/settings" 8 "github.com/cozy/cozy-stack/model/instance" 9 "github.com/cozy/cozy-stack/pkg/consts" 10 "github.com/cozy/cozy-stack/pkg/couchdb" 11 "golang.org/x/sync/errgroup" 12 ) 13 14 // Reset will clean all the data from the instances, and most apps. It should 15 // be used only just before an import. 16 func Reset(inst *instance.Instance) error { 17 instanceSettings, err := inst.SettingsDocument() 18 if err != nil { 19 return err 20 } 21 bitwardenSettings, err := settings.Get(inst) 22 if err != nil && !couchdb.IsNotFoundError(err) { 23 return err 24 } 25 if err = deleteAccounts(inst); err != nil { 26 return err 27 } 28 removeTriggers(inst) 29 if err = inst.VFS().Delete(); err != nil { 30 return err 31 } 32 if err = couchdb.DeleteAllDBs(inst); err != nil { 33 return err 34 } 35 36 // XXX CouchDB is eventually consistent, which means that we have a small 37 // risk that recreating a database just after the deletion can fail 38 // silently. So, we wait 2 seconds to limit the risk. 39 time.Sleep(2 * time.Second) 40 41 g, _ := errgroup.WithContext(context.Background()) 42 g.Go(func() error { return couchdb.CreateDB(inst, consts.Files) }) 43 g.Go(func() error { return couchdb.CreateDB(inst, consts.Apps) }) 44 g.Go(func() error { return couchdb.CreateDB(inst, consts.Konnectors) }) 45 g.Go(func() error { return couchdb.CreateDB(inst, consts.OAuthClients) }) 46 g.Go(func() error { return couchdb.CreateDB(inst, consts.Jobs) }) 47 g.Go(func() error { return couchdb.CreateDB(inst, consts.Triggers) }) 48 g.Go(func() error { return couchdb.CreateDB(inst, consts.Permissions) }) 49 g.Go(func() error { return couchdb.CreateDB(inst, consts.Sharings) }) 50 g.Go(func() error { return couchdb.CreateDB(inst, consts.BitwardenCiphers) }) 51 g.Go(func() error { return couchdb.CreateDB(inst, consts.SessionsLogins) }) 52 g.Go(func() error { return couchdb.CreateDB(inst, consts.Notifications) }) 53 g.Go(func() error { return couchdb.CreateDB(inst, consts.Contacts) }) 54 g.Go(func() error { 55 if err := couchdb.CreateDB(inst, consts.Settings); err != nil { 56 return err 57 } 58 if bitwardenSettings != nil { 59 bitwardenSettings.SetRev("") 60 if err := bitwardenSettings.Save(inst); err != nil { 61 return err 62 } 63 } 64 instanceSettings.SetRev("") 65 return couchdb.CreateNamedDocWithDB(inst, instanceSettings) 66 // The myself contact is created by the import, not here, so that this 67 // document has the same ID than on the source instance. 68 }) 69 if err = g.Wait(); err != nil { 70 return err 71 } 72 73 if err = DefineViewsAndIndex(inst); err != nil { 74 return err 75 } 76 if err = inst.VFS().InitFs(); err != nil { 77 return err 78 } 79 80 for _, app := range []string{"home", "store", "settings"} { 81 if err = installApp(inst, app); err != nil { 82 inst.Logger().Errorf("Failed to install %s: %s", app, err) 83 } 84 } 85 return nil 86 }