carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostbox
2
3import (
4 "bytes"
5 "context"
6 "database/sql"
7 "encoding/json"
8 "io"
9 "net/url"
10 "os"
11 "path/filepath"
12)
13
14// GenerationLaunches includes hosts absent from the reservation directory.
15// Only the checked local journal is eligible; an agent list is not a census.
16func (j *LaunchJournal) GenerationLaunches(ctx context.Context) ([]LaunchInventory, error) {
17 if j == nil || j.db == nil || j.remote != nil {
18 return nil, ErrUnsupported
19 }
20 if err := journalIdentity(j.path, j.identity); err != nil {
21 return nil, err
22 }
23 rows, err := journalExecutorFor(ctx, j.db).QueryContext(ctx, "SELECT identity, body FROM launches ORDER BY identity")
24 if err != nil {
25 return nil, err
26 }
27 defer rows.Close()
28 var out []LaunchInventory
29 for rows.Next() {
30 var key string
31 var body []byte
32 if err := rows.Scan(&key, &body); err != nil {
33 return nil, err
34 }
35 if len(body) > 1<<20 || len(out) >= 8192 {
36 return nil, ErrTypedInventory
37 }
38 var in LaunchInventory
39 dec := json.NewDecoder(bytes.NewReader(body))
40 dec.DisallowUnknownFields()
41 if dec.Decode(&in) != nil || dec.Decode(new(any)) != io.EOF || !validLaunchOwner(in.Target) || launchKey(in.Target) != key || !in.Completed.Valid() || in.ControllerPID <= 0 || in.ProcessStart == "" {
42 return nil, ErrTypedInventory
43 }
44 out = append(out, in)
45 }
46 if err := rows.Err(); err != nil {
47 return nil, err
48 }
49 if err := journalIdentity(j.path, j.identity); err != nil {
50 return nil, err
51 }
52 return out, nil
53}
54
55// OpenGenerationLaunchReader never creates a database or initializes its schema.
56// Production callers must check root ownership of the database and sidecars.
57func OpenGenerationLaunchReader(path string) (*LaunchJournal, error) {
58 if !filepath.IsAbs(path) {
59 return nil, ErrUnsupported
60 }
61 if err := checkJournalPath(path); err != nil {
62 return nil, err
63 }
64 identity, err := os.Lstat(path)
65 if err != nil {
66 return nil, err
67 }
68 db, err := sql.Open("sqlite", "file:"+url.PathEscape(path)+"?mode=ro&_pragma=busy_timeout(5000)")
69 if err != nil {
70 return nil, err
71 }
72 db.SetMaxOpenConns(1)
73 return &LaunchJournal{db: db, path: path, identity: identity}, nil
74}
75