| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostguard" |
| 6 | "github.com/carlosframework/platform/internal/instancectl" |
| 7 | "github.com/carlosframework/platform/internal/release" |
| 8 | "os" |
| 9 | ) |
| 10 | |
| 11 | func claimLaunchPlacement(units ClaimLaunchUnits, host string) (string, string, error) { |
| 12 | if bound, ok := units.(interface { |
| 13 | Placement(string) (string, string, error) |
| 14 | }); ok { |
| 15 | return bound.Placement(host) |
| 16 | } |
| 17 | return instancectl.UnitName(instancectl.RoleRun, host), instancectl.ClaimUnitControlGroup(host), nil |
| 18 | } |
| 19 | |
| 20 | func claimStopInvocation(ctx context.Context, units ClaimLaunchUnits, host, invocation string) error { |
| 21 | if exact, ok := units.(interface { |
| 22 | StopInvocation(context.Context, string, string) error |
| 23 | }); ok { |
| 24 | return exact.StopInvocation(ctx, host, invocation) |
| 25 | } |
| 26 | return units.Stop(ctx, host) |
| 27 | } |
| 28 | |
| 29 | func generationBindingGroup(b release.GenerationBinding) string { |
| 30 | if b.Unit == "" { |
| 31 | return instancectl.ClaimUnitControlGroup(b.Binding.Host) |
| 32 | } |
| 33 | if !instancectl.ValidBoundClaimUnit(b.Unit) { |
| 34 | return "" |
| 35 | } |
| 36 | return instancectl.BoundClaimUnitControlGroup(b.Binding.Host, b.Unit) |
| 37 | } |
| 38 | |
| 39 | func (h *ClaimLaunchRoot) checkUnitBinding(ctx context.Context, t hostguard.FenceTarget, cleanup bool) error { |
| 40 | unit, _, err := claimLaunchPlacement(h.Units, t.Host) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | if unit == instancectl.UnitName(instancectl.RoleRun, t.Host) { |
| 45 | return nil |
| 46 | } |
| 47 | if h.unitBindings == nil { |
| 48 | return os.ErrPermission |
| 49 | } |
| 50 | var b claimUnitBinding |
| 51 | if cleanup { |
| 52 | b, err = h.unitBindings.read(t.Host) |
| 53 | } else { |
| 54 | b, err = h.unitBindings.check(ctx, t.Host, nil) |
| 55 | } |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if b.Owner.Host != t.Host || b.Owner.Incarnation != t.Incarnation || b.Owner.Stamp != t.Stamp || b.Owner.Box != t.Box || b.Owner.Enrollment != t.Enrollment || b.Shape.Unit != unit { |
| 60 | return os.ErrPermission |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |