| 1 | package hostguard |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // GenerationInstallations enumerates independently of generation reservations. |
| 10 | // Pending or quarantined installations cannot certify a destructive snapshot. |
| 11 | func (a *RootAuthority) GenerationInstallations(ctx context.Context) ([]FenceView, error) { |
| 12 | if a == nil || a.store == nil { |
| 13 | return nil, ErrInvalidInput |
| 14 | } |
| 15 | names, err := a.store.Names() |
| 16 | if err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | sort.Strings(names) |
| 20 | var out []FenceView |
| 21 | for _, name := range names { |
| 22 | if err := ctx.Err(); err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | if strings.HasSuffix(name, ".lock") { |
| 26 | continue |
| 27 | } |
| 28 | if !strings.HasSuffix(name, ".json") { |
| 29 | return nil, ErrMalformedRecord |
| 30 | } |
| 31 | host := strings.TrimSuffix(name, ".json") |
| 32 | view, err := a.generationInstallation(ctx, host) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | if view.Installation.InventoryRevision == 0 { |
| 37 | return nil, ErrNoRecord |
| 38 | } |
| 39 | out = append(out, view) |
| 40 | } |
| 41 | return out, nil |
| 42 | } |
| 43 | |
| 44 | func (a *RootAuthority) generationInstallation(ctx context.Context, host string) (FenceView, error) { |
| 45 | unlock, err := a.store.Lock(ctx, host+".lock") |
| 46 | if err != nil { |
| 47 | return FenceView{}, err |
| 48 | } |
| 49 | defer unlock() |
| 50 | r, err := a.read(host) |
| 51 | if err != nil { |
| 52 | return FenceView{}, err |
| 53 | } |
| 54 | view, err := a.viewLocked(host, Captured{Incarnation: r.Incarnation, Stamp: r.installation.Stamp}) |
| 55 | if err != nil { |
| 56 | return FenceView{}, err |
| 57 | } |
| 58 | // A visible final rename may have failed its directory sync. Recommit under |
| 59 | // the same host lock before these requirements can certify a generation read. |
| 60 | if err := a.write(r); err != nil { |
| 61 | return FenceView{}, err |
| 62 | } |
| 63 | return view, nil |
| 64 | } |
| 65 | |