| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "sync" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 11 | "amadan.net/rastrillo/rastrillo/db" |
| 12 | "amadan.net/rastrillo/rastrillo/migrate" |
| 13 | "golang.org/x/sys/unix" |
| 14 | ) |
| 15 | |
| 16 | type executionLifetime struct { |
| 17 | once sync.Once |
| 18 | lock *os.File |
| 19 | err error |
| 20 | } |
| 21 | |
| 22 | func OpenExecution(ctx context.Context, d *db.DB, path string, cfg Config, store *Store) (*Execution, error) { |
| 23 | if !cfg.ExecutionEnabled { |
| 24 | return nil, nil |
| 25 | } |
| 26 | if d == nil || store == nil || store.db != d || store.execution != nil || !filepath.IsAbs(cfg.ExecutionMarkerPath) || filepath.Clean(cfg.ExecutionMarkerPath) != cfg.ExecutionMarkerPath { |
| 27 | return nil, ErrInvalid |
| 28 | } |
| 29 | database, err := filepath.Abs(path) |
| 30 | if err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | // A different directory is the minimum enforceable separation. Deployment |
| 34 | // must preserve this storage independently of database backup and restore. |
| 35 | if filepath.Dir(database) == filepath.Dir(cfg.ExecutionMarkerPath) { |
| 36 | return nil, ErrInvalid |
| 37 | } |
| 38 | dir := filepath.Dir(cfg.ExecutionMarkerPath) |
| 39 | if err = os.MkdirAll(dir, 0700); err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | actual, err := filepath.EvalSymlinks(dir) |
| 43 | if err != nil || actual != dir { |
| 44 | return nil, ErrInvalid |
| 45 | } |
| 46 | info, err := os.Stat(dir) |
| 47 | if err != nil || info.Mode().Perm()&0077 != 0 { |
| 48 | return nil, ErrInvalid |
| 49 | } |
| 50 | fd, err := unix.Open(cfg.ExecutionMarkerPath+".startup.lock", unix.O_CREAT|unix.O_RDWR|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0600) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | lock := os.NewFile(uintptr(fd), cfg.ExecutionMarkerPath+".startup.lock") |
| 55 | if err = unix.Flock(fd, unix.LOCK_EX|unix.LOCK_NB); err != nil { |
| 56 | lock.Close() |
| 57 | return nil, ErrConflict |
| 58 | } |
| 59 | ok := false |
| 60 | defer func() { |
| 61 | if !ok { |
| 62 | lock.Close() |
| 63 | } |
| 64 | }() |
| 65 | if _, err = migrate.Apply(ctx, d, BootSchema); err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | if err = store.Bootstrap(ctx, cfg.Owners); err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | x, err := store.AttachSupervisedExecution(ctx, path, team.Options{Marker: cfg.ExecutionMarkerPath, Clock: team.RealClock{}, Authority: NewAuthority(store, team.RealClock{})}) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | x.lifetime = &executionLifetime{lock: lock} |
| 76 | if _, err = x.Core.Resume(ctx); err != nil { |
| 77 | _ = x.Close() |
| 78 | return nil, err |
| 79 | } |
| 80 | ok = true |
| 81 | return x, nil |
| 82 | } |
| 83 | func (e *Execution) Close() error { |
| 84 | if e == nil { |
| 85 | return nil |
| 86 | } |
| 87 | if e.lifetime == nil { |
| 88 | return e.Core.Close() |
| 89 | } |
| 90 | e.lifetime.once.Do(func() { e.lifetime.err = errors.Join(e.Core.Close(), e.lifetime.lock.Close()); e.store.execution = nil }) |
| 91 | return e.lifetime.err |
| 92 | } |
| 93 | |
| 94 | func (e *Execution) Reconcile(ctx context.Context) error { |
| 95 | if e == nil || e.Core == nil { |
| 96 | return ErrUnavailable |
| 97 | } |
| 98 | status, err := e.Core.Status(ctx) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if status.Quarantine { |
| 103 | return nil |
| 104 | } |
| 105 | var stale bool |
| 106 | if err = e.db.G.WithContext(ctx).Raw(`SELECT EXISTS(SELECT 1 FROM jobs WHERE grant_stale=1 AND state NOT IN ('succeeded','failed','cancelled'))`).Row().Scan(&stale); err != nil { |
| 107 | return err |
| 108 | } |
| 109 | if status.AdmissionPaused || stale { |
| 110 | if _, err = e.Core.Resume(ctx); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | } |
| 114 | return e.Sync(ctx) |
| 115 | } |
| 116 | |