| 1 | package hostedctl |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 9 | ) |
| 10 | |
| 11 | // boundedRunner is the runner the controller calls through. Each provider |
| 12 | // call gets a deadline derived from the caller's context (ruling R27): |
| 13 | // ProviderLaunchTimeout for Launch (R21) and ProviderCallTimeout for the |
| 14 | // rest, retries included. A caller's earlier deadline, or its cancellation, |
| 15 | // still ends the call first. Results are recorded by the caller on a |
| 16 | // context of its own. |
| 17 | type boundedRunner struct{ r isolatedjob.Runner } |
| 18 | |
| 19 | // Every Ref a runner hands back is checked with isolatedjob.Ref.Valid here, |
| 20 | // the one way into the controller, so no Ref it stores or logs can carry |
| 21 | // other text (ruling R36). A Launch with a bad Ref is a provider failure; a |
| 22 | // List holding one is incomplete. |
| 23 | var errBadRef = errors.New("hostedctl: the runner returned a malformed VM ref") |
| 24 | |
| 25 | func (b boundedRunner) Launch(ctx context.Context, s isolatedjob.LaunchSpec) (isolatedjob.Ref, error) { |
| 26 | ctx, cancel := context.WithTimeout(ctx, ProviderLaunchTimeout) |
| 27 | defer cancel() |
| 28 | ref, err := b.r.Launch(ctx, s) |
| 29 | if err == nil && !ref.Valid() { |
| 30 | return "", errBadRef |
| 31 | } |
| 32 | return ref, err |
| 33 | } |
| 34 | |
| 35 | func (b boundedRunner) Get(ctx context.Context, ref isolatedjob.Ref) (isolatedjob.Job, error) { |
| 36 | ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) |
| 37 | defer cancel() |
| 38 | j, err := b.r.Get(ctx, ref) |
| 39 | if err == nil && !j.Ref.Valid() { |
| 40 | return isolatedjob.Job{}, errBadRef |
| 41 | } |
| 42 | return j, err |
| 43 | } |
| 44 | |
| 45 | func (b boundedRunner) Suspend(ctx context.Context, ref isolatedjob.Ref) error { |
| 46 | ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) |
| 47 | defer cancel() |
| 48 | return b.r.Suspend(ctx, ref) |
| 49 | } |
| 50 | |
| 51 | func (b boundedRunner) Resume(ctx context.Context, ref isolatedjob.Ref) error { |
| 52 | ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) |
| 53 | defer cancel() |
| 54 | return b.r.Resume(ctx, ref) |
| 55 | } |
| 56 | |
| 57 | func (b boundedRunner) Stop(ctx context.Context, ref isolatedjob.Ref, reason string) error { |
| 58 | ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) |
| 59 | defer cancel() |
| 60 | return b.r.Stop(ctx, ref, reason) |
| 61 | } |
| 62 | |
| 63 | func (b boundedRunner) List(ctx context.Context) ([]isolatedjob.Job, error) { |
| 64 | ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) |
| 65 | defer cancel() |
| 66 | jobs, err := b.r.List(ctx) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | for _, j := range jobs { |
| 71 | if !j.Ref.Valid() { |
| 72 | return nil, fmt.Errorf("%w: %w", isolatedjob.ErrIncomplete, errBadRef) |
| 73 | } |
| 74 | } |
| 75 | return jobs, nil |
| 76 | } |
| 77 | |
| 78 | func (b boundedRunner) Limits() isolatedjob.Limits { return b.r.Limits() } |
| 79 | |
| 80 | // boundedAuditor gives each network audit, every read in it included, |
| 81 | // ProviderCallTimeout (R27). |
| 82 | type boundedAuditor struct{ a isolatedjob.NetworkAuditor } |
| 83 | |
| 84 | func (b boundedAuditor) AuditNetwork(ctx context.Context) (isolatedjob.NetworkAudit, error) { |
| 85 | ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) |
| 86 | defer cancel() |
| 87 | return b.a.AuditNetwork(ctx) |
| 88 | } |
| 89 | |