-
Notifications
You must be signed in to change notification settings - Fork 9
/
invoke.go
71 lines (59 loc) · 2.49 KB
/
invoke.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package tools
import (
"context"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build/binary"
"namespacelabs.dev/foundation/internal/build/buildkit"
"namespacelabs.dev/foundation/internal/build/multiplatform"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/planning/tool/protocol"
"namespacelabs.dev/foundation/internal/runtime/rtypes"
"namespacelabs.dev/foundation/internal/versions"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
"namespacelabs.dev/foundation/std/types"
)
func InvokeWithBinary(ctx context.Context, env pkggraph.SealedContext, inv *types.DeferredInvocation, prepared *binary.Prepared) (compute.Computable[*protocol.InvokeResponse], error) {
c, err := buildkit.Client(ctx, env.Configuration(), nil)
if err != nil {
return nil, err
}
plan := prepared.Plan
plan.Platforms = []specs.Platform{c.BuildkitOpts().HostPlatform}
image, err := multiplatform.PrepareMultiPlatformImage(ctx, env, plan)
if err != nil {
return nil, err
}
ximage := oci.ResolveImagePlatform(image, c.BuildkitOpts().HostPlatform)
req := &protocol.ToolRequest{
ApiVersion: int32(versions.Builtin().APIVersion),
ToolPackage: inv.BinaryRef.AsPackageName().String(),
RequestType: &protocol.ToolRequest_InvokeRequest{
InvokeRequest: &protocol.InvokeRequest{},
},
}
if inv.GetWithInput().GetTypeUrl() != "" {
req.Input = append(req.Input, inv.WithInput)
}
workingDir := "/"
if inv.BinaryConfig.WorkingDir != "" {
workingDir = inv.BinaryConfig.WorkingDir
}
run := rtypes.RunBinaryOpts{
WorkingDir: workingDir,
Command: inv.BinaryConfig.Command,
Args: inv.BinaryConfig.Args,
Env: append(slices.Clone(inv.BinaryConfig.Env), &schema.BinaryConfig_EnvEntry{Name: "HOME", Value: &schema.Resolvable{Value: "/tmp"}}),
}
return compute.Transform("get-response",
InvokeOnBuildkit[*protocol.ToolResponse](c, nil, "foundation.provision.tool.protocol.InvocationService/Invoke",
inv.BinaryRef.AsPackageName(), ximage, run, req, LowLevelInvokeOptions{}),
func(_ context.Context, resp *protocol.ToolResponse) (*protocol.InvokeResponse, error) {
return resp.InvokeResponse, nil
}), nil
}