Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Merge release channels into the account database and rename it instance.db

3047be84e9bce79372051c09854ce9b05f12a9bf · Keenan Nemetz · 17d ago · parent 262e516e

Merge release channels into the account database and rename it instance.db

The instance keeps four SQLite files instead of five. Release channels were in a
file of their own only because they were added beside the derived index; they are
authoritative source state, like the roster, so they belong in the file an
operator restores. Nothing has to be migrated: channels.db never shipped in a
release, so only an instance tracking main can have one.

The account database is now data/instance.db. The package comments already called
it that; only the filename disagreed.

Moving an existing instance is an operator step, by decision: a rename under a
live server can lose the -wal file a hook subprocess still has open. Vole refuses
to start while data/users.db is still in place, and says what to move, rather
than creating an empty database beside it that would look like a successful
upgrade.

The list of databases now lives in Config.Databases, which TightenSecrets, the
hook plumbing, and the admin commands read, so a missed -wal file is no longer a
nine-file change. `vole admin account backup <path>` becomes `vole admin db
backup <dir>` and covers every authoritative database; `db list` and `db check`
answer what to back up and whether it is intact.

forge/instance.go +70−25

@@ -1,10 +1,10 @@
1 1 // Package forge opens and owns the state of a Vole instance.
2 2 //
3 // An instance is one directory of durable state: the user roster and access
4 // rules (data/users.db), the derived index, the CI queue, the object store, and
5 // the bare repositories. This package is the one place that knows how those fit
6 // together, so the server, the CLI's administrative commands and an embedding
7 // application all open the same set in the same order.
3 // An instance is one directory of durable state: the instance database (accounts,
4 // access rules, and release channels), the derived index, the CI queue, the
5 // object store, and the bare repositories. This package is the one place that
6 // knows how those fit together, so the server, the CLI's administrative commands
7 // and an embedding application all open the same set in the same order.
8 8 package forge
9 9
10 10 import (
@@ -49,12 +49,14 @@ type Instance struct {
49 49 // instance keeps its built-in presentation.
50 50 InstanceRepo *instancerepo.Store
51 51 // Secrets holds each repository's sealed values. It is a separate database
52 // from the roster because it is the one whose contents are encrypted, and it is
53 // opened last because nothing else depends on it.
52 // from the instance database because it is the one whose contents are
53 // encrypted, and it is opened last because nothing else depends on it.
54 54 Secrets *secret.Store
55 55 // Channels is the authoritative release-channel store. It is server-side
56 56 // source state, not a derived index: a channel is consumed from the public
57 // URL, not cloned.
57 // URL, not cloned. It shares the instance database with the roster, because a
58 // channel move has no Git worktree to touch and an index rebuild must not be
59 // able to discard one.
58 60 Channels *channel.Store
59 61
60 62 // ImportedACLs is how many legacy refs/vole/acl rule sets were imported by
@@ -82,13 +84,16 @@ func Open(cfg *config.Config, binPath string) (*Instance, error) {
82 84 if err := audit.Check(cfg.AuditLog()); err != nil {
83 85 return nil, err
84 86 }
85 ident, err := identity.Open(cfg.UsersDB())
87 if err := checkDatabases(cfg); err != nil {
88 return nil, err
89 }
90 ident, err := identity.Open(cfg.InstanceDB())
86 91 if err != nil {
87 92 return nil, fmt.Errorf("%w\nrun `vole admin init` first", err)
88 93 }
89 94 ident.AuditPath = cfg.AuditLog()
90 95
91 sessions, err := session.Open(cfg.UsersDB())
96 sessions, err := session.Open(cfg.InstanceDB())
92 97 if err != nil {
93 98 ident.Close()
94 99 return nil, fmt.Errorf("open sessions: %w", err)
@@ -108,7 +113,7 @@ func Open(cfg *config.Config, binPath string) (*Instance, error) {
108 113 ident.Close()
109 114 return nil, fmt.Errorf("open run queue: %w", err)
110 115 }
111 acls, err := access.Open(cfg.UsersDB())
116 acls, err := access.Open(cfg.InstanceDB())
112 117 if err != nil {
113 118 rn.Close()
114 119 ix.Close()
@@ -125,7 +130,7 @@ func Open(cfg *config.Config, binPath string) (*Instance, error) {
125 130 // keep satisfying a gate.
126 131 acls.Active = ident.Active
127 132
128 lk, err := lfs.OpenLocks(cfg.UsersDB())
133 lk, err := lfs.OpenLocks(cfg.InstanceDB())
129 134 if err != nil {
130 135 acls.Close()
131 136 rn.Close()
@@ -148,7 +153,7 @@ func Open(cfg *config.Config, binPath string) (*Instance, error) {
148 153 ident.Close()
149 154 return nil, err
150 155 }
151 views, err := pageview.Open(cfg.UsersDB())
156 views, err := pageview.Open(cfg.InstanceDB())
152 157 if err != nil {
153 158 lk.Close()
154 159 acls.Close()
@@ -179,7 +184,7 @@ func Open(cfg *config.Config, binPath string) (*Instance, error) {
179 184 // repository from leaving values behind for a name that is created again.
180 185 rs.Secrets = secrets
181 186
182 channels, err := channel.Open(cfg.ChannelsDB())
187 channels, err := channel.Open(cfg.InstanceDB())
183 188 if err != nil {
184 189 secrets.Close()
185 190 views.Close()
@@ -228,16 +233,19 @@ func TightenSecrets(cfg *config.Config) ([]string, error) {
228 233 cfg.SpoolDir(),
229 234 cfg.LFSDir(),
230 235 cfg.LogsDir(),
231 cfg.UsersDB(), cfg.UsersDB() + "-wal", cfg.UsersDB() + "-shm",
232 cfg.IndexDB(), cfg.IndexDB() + "-wal", cfg.IndexDB() + "-shm",
233 cfg.RunsDB(), cfg.RunsDB() + "-wal", cfg.RunsDB() + "-shm",
234 cfg.SecretsDB(), cfg.SecretsDB() + "-wal", cfg.SecretsDB() + "-shm",
235 cfg.ChannelsDB(), cfg.ChannelsDB() + "-wal", cfg.ChannelsDB() + "-shm",
236 }
237 // SQLite keeps a -wal and a -shm sibling beside every open database. The
238 // write-ahead file carries committed pages until the next checkpoint, so it
239 // is as sensitive as the database and is never left out of this list.
240 for _, db := range cfg.Databases() {
241 paths = append(paths, db.Path, db.Path+"-wal", db.Path+"-shm")
242 }
243 paths = append(paths,
236 244 cfg.SecretsKeyPath(),
237 245 cfg.AuditLog(),
238 246 cfg.SSHHostKey(),
239 247 cfg.ACMECachePath(),
240 }
248 )
241 249 results, err := secure.Harden(paths...)
242 250 var changed []string
243 251 for _, r := range results {
@@ -249,6 +257,40 @@ func TightenSecrets(cfg *config.Config) ([]string, error) {
249 257 return changed, nil
250 258 }
251 259
260 // checkDatabases refuses a data directory still laid out for an older Vole.
261 //
262 // Moving the file is the operator's step, by decision: an automatic rename would
263 // run under a live server whose hook subprocesses have the database open, where
264 // losing the -wal is silent. Creating an empty instance.db beside the old
265 // users.db is worse than either — it looks like a successful upgrade and starts
266 // a second roster with no accounts in it.
267 func checkDatabases(cfg *config.Config) error {
268 if _, err := os.Stat(cfg.InstanceDB()); err == nil {
269 if _, err := os.Stat(cfg.LegacyChannelsDB()); err == nil {
270 return fmt.Errorf("%s is no longer used: release channels live in %s now.\n"+
271 "No release shipped that file, so an instance that has one tracked main.\n"+
272 "Copy anything it holds, then delete it with its -wal and -shm files:\n"+
273 " sqlite3 %s \"ATTACH '%s' AS old;\n"+
274 " INSERT OR IGNORE INTO release_channels SELECT * FROM old.release_channels;\"\n"+
275 "See docs/operations.md#upgrade-to-the-instance-database",
276 cfg.LegacyChannelsDB(), cfg.InstanceDB(), cfg.InstanceDB(), cfg.LegacyChannelsDB())
277 }
278 return nil
279 }
280 if _, err := os.Stat(cfg.LegacyInstanceDB()); err == nil {
281 return fmt.Errorf("found %s, which this version reads as %s.\n"+
282 "Stop Vole and move it, taking the write-ahead file with it:\n"+
283 " mv %s %s\n"+
284 " mv %s-wal %s-wal\n"+
285 "A database moved without its -wal file silently loses the last writes.\n"+
286 "See docs/operations.md#upgrade-to-the-instance-database",
287 cfg.LegacyInstanceDB(), cfg.InstanceDB(),
288 cfg.LegacyInstanceDB(), cfg.InstanceDB(),
289 cfg.LegacyInstanceDB(), cfg.InstanceDB())
290 }
291 return nil
292 }
293
252 294 // OpenPath loads the configuration at cfgPath and opens the instance it names.
253 295 func OpenPath(cfgPath string) (*Instance, error) {
254 296 cfg, err := config.Load(config.ResolvePath(cfgPath))
@@ -290,7 +332,7 @@ func (i *Instance) Close() error {
290 332 }
291 333
292 334 // Init creates the three things an instance lives in — the config file, the
293 // repository directory and the roster database — and leaves existing state
335 // repository directory and the instance database — and leaves existing state
294 336 // alone. It returns the absolute instance directory.
295 337 func Init(dir string) (string, error) {
296 338 if dir == "" {
@@ -335,18 +377,21 @@ func InitAt(cfgPath string) (string, error) {
335 377 if err != nil {
336 378 return "", err
337 379 }
380 if err := checkDatabases(cfg); err != nil {
381 return "", err
382 }
338 383 if err := secure.Mkdir(cfg.DataDir); err != nil {
339 384 return "", err
340 385 }
341 386 if err := secure.Mkdir(cfg.ReposDir()); err != nil {
342 387 return "", err
343 388 }
344 if err := identity.Init(cfg.UsersDB()); err != nil {
389 if err := identity.Init(cfg.InstanceDB()); err != nil {
345 390 return "", err
346 391 }
347 // A roster created by an older install keeps whatever mode it was created
392 // An instance created by an older install keeps whatever mode it was created
348 393 // with; init is also an upgrade path, so tighten it here too.
349 if _, err := secure.HardenFileIfExists(cfg.UsersDB()); err != nil {
394 if _, err := secure.HardenFileIfExists(cfg.InstanceDB()); err != nil {
350 395 return "", err
351 396 }
352 397 if err := seedInstanceRepo(cfg); err != nil {
@@ -363,7 +408,7 @@ func seedInstanceRepo(cfg *config.Config) error {
363 408 if name == "" {
364 409 return nil
365 410 }
366 acls, err := access.Open(cfg.UsersDB())
411 acls, err := access.Open(cfg.InstanceDB())
367 412 if err != nil {
368 413 return fmt.Errorf("open access rules: %w", err)
369 414 }