| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strconv" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "github.com/carlosframework/platform/internal/provision" |
| 10 | "github.com/carlosframework/platform/internal/release" |
| 11 | ) |
| 12 | |
| 13 | func (a ProductionAuthority) TouchSidecarWake(ctx context.Context, account, app, name, appID, by, reason string) error { |
| 14 | if a.Control == nil { |
| 15 | return ErrContract |
| 16 | } |
| 17 | ctx, cancel := context.WithTimeout(ctx, 15*time.Second) |
| 18 | defer cancel() |
| 19 | record, err := ReadRecord(ctx, a.Store, provision.SidecarRecordKey(account, app, name)) |
| 20 | if err != nil { |
| 21 | return err |
| 22 | } |
| 23 | obs, err := hostclaim.Read(ctx, a.Store, record.Holder.Host) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | if obs.Absent || !matches(record, obs.Claim.Holder) || obs.Claim.Op.Kind != hostclaim.KindServing { |
| 28 | return ErrPending |
| 29 | } |
| 30 | admission, err := ReadAdmission(ctx, a.Store, account, app, appID) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | id, err := operationToken() |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | controller, err := operationToken() |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | owner := RegistrationOwner{Console: a.ConsoleID, Enrollment: a.Enrollment, Controller: controller} |
| 43 | reg := Registration{Kind: "wake", Host: obs.Host, Owner: owner, AppID: appID, Epoch: admission.Value.Epoch, Claim: SavedObservation(obs), Incarnation: obs.Claim.Holder.Incarnation, CreatedAt: obs.Claim.Holder.CreatedAt} |
| 44 | start, err := a.Control.RegisterStart(ctx, account, app, appID, id, reg, a.Now()) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | ctx = start.Lifecycle(ctx) |
| 49 | if _, err = hostclaim.Update(ctx, a.Store, obs, func(*hostclaim.Claim) error { return nil }); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | key := release.SidecarWakeKey(account, app, name) |
| 53 | _, err = start.Runner(a.Now).Run(ctx, Effect{ID: id, Resource: "control-object:" + key, Action: "wake", Inputs: map[string]string{"key": key, "incarnation": reg.Incarnation, "request_id": id, "admission_epoch": strconv.FormatUint(reg.Epoch, 10), "created_at": reg.CreatedAt.UTC().Format(time.RFC3339Nano)}}, func(ctx context.Context) (string, error) { |
| 54 | if err := release.TouchSidecarWake(ctx, a.Store, account, app, name, by, reason, release.SidecarWakeIdentity{AppID: appID, Incarnation: reg.Incarnation, RequestID: id}); err != nil { |
| 55 | return "", err |
| 56 | } |
| 57 | return id, nil |
| 58 | }) |
| 59 | return err |
| 60 | } |
| 61 | |