-
Notifications
You must be signed in to change notification settings - Fork 150
/
custom_deployment.go
170 lines (151 loc) · 5.65 KB
/
custom_deployment.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package llm_inference
import (
"context"
"errors"
"net/http"
"reflect"
"strings"
"time"
"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/human"
llm_inference "github.com/scaleway/scaleway-sdk-go/api/llm_inference/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
deploymentActionTimeout = 40 * time.Minute
)
var (
deployementStateMarshalSpecs = human.EnumMarshalSpecs{
llm_inference.DeploymentStatusCreating: &human.EnumMarshalSpec{Attribute: color.FgBlue},
llm_inference.DeploymentStatusDeploying: &human.EnumMarshalSpec{Attribute: color.FgBlue},
llm_inference.DeploymentStatusDeleting: &human.EnumMarshalSpec{Attribute: color.FgBlue},
llm_inference.DeploymentStatusError: &human.EnumMarshalSpec{Attribute: color.FgRed},
llm_inference.DeploymentStatusReady: &human.EnumMarshalSpec{Attribute: color.FgGreen},
llm_inference.DeploymentStatusLocked: &human.EnumMarshalSpec{Attribute: color.FgRed},
}
)
func DeploymentMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
type tmp llm_inference.Deployment
deployment := tmp(i.(llm_inference.Deployment))
opt.Sections = []*human.MarshalSection{
{
FieldName: "Endpoints",
Title: "Endpoints",
},
}
str, err := human.Marshal(deployment, opt)
if err != nil {
return "", err
}
return str, nil
}
func deploymentCreateBuilder(c *core.Command) *core.Command {
c.ArgSpecs.GetByName("node-type").AutoCompleteFunc = autocompleteDeploymentNodeType
type llmInferenceEndpointSpecCustom struct {
*llm_inference.EndpointSpec
IsPublic bool `json:"is-public"`
}
type llmInferenceCreateDeploymentRequestCustom struct {
*llm_inference.CreateDeploymentRequest
Endpoints []*llmInferenceEndpointSpecCustom `json:"endpoints"`
}
c.ArgSpecs.AddBefore("endpoints.{index}.private-network.private-network-id", &core.ArgSpec{
Name: "endpoints.{index}.is-public",
Short: "Will configure your public endpoint if true",
Required: false,
Default: core.DefaultValueSetter("false"),
})
c.ArgsType = reflect.TypeOf(llmInferenceCreateDeploymentRequestCustom{})
c.WaitFunc = func(ctx context.Context, _, respI interface{}) (interface{}, error) {
api := llm_inference.NewAPI(core.ExtractClient(ctx))
return api.WaitForDeployment(&llm_inference.WaitForDeploymentRequest{
DeploymentId: respI.(*llm_inference.Deployment).ID,
Region: respI.(*llm_inference.Deployment).Region,
Status: respI.(*llm_inference.Deployment).Status,
Timeout: scw.TimeDurationPtr(deploymentActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
deploymentCreateCustomRequest := argsI.(*llmInferenceCreateDeploymentRequestCustom)
deploymentRequest := deploymentCreateCustomRequest.CreateDeploymentRequest
if deploymentCreateCustomRequest.Endpoints == nil {
publicEndpoint := &llm_inference.EndpointSpecPublic{}
endpoint := llm_inference.EndpointSpec{
Public: publicEndpoint,
PrivateNetwork: nil,
DisableAuth: false,
}
deploymentRequest.Endpoints = append(deploymentRequest.Endpoints, &endpoint)
return runner(ctx, deploymentRequest)
}
for _, endpoint := range deploymentCreateCustomRequest.Endpoints {
publicEndpoint := &llm_inference.EndpointSpecPublic{}
if !endpoint.IsPublic {
publicEndpoint = nil
}
privateNetwork := &llm_inference.EndpointSpecPrivateNetwork{}
if endpoint.EndpointSpec == nil {
privateNetwork = nil
} else {
privateNetwork.PrivateNetworkID = endpoint.PrivateNetwork.PrivateNetworkID
}
endpoint := llm_inference.EndpointSpec{
Public: publicEndpoint,
PrivateNetwork: privateNetwork,
DisableAuth: endpoint.DisableAuth,
}
deploymentRequest.Endpoints = append(deploymentRequest.Endpoints, &endpoint)
}
return runner(ctx, deploymentRequest)
}
return c
}
func deploymentDeleteBuilder(c *core.Command) *core.Command {
c.WaitFunc = func(ctx context.Context, _, respI interface{}) (interface{}, error) {
api := llm_inference.NewAPI(core.ExtractClient(ctx))
deployment, err := api.WaitForDeployment(&llm_inference.WaitForDeploymentRequest{
DeploymentId: respI.(*llm_inference.Deployment).ID,
Region: respI.(*llm_inference.Deployment).Region,
Status: respI.(*llm_inference.Deployment).Status,
Timeout: scw.TimeDurationPtr(deploymentActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
notFoundError := &scw.ResourceNotFoundError{}
responseError := &scw.ResponseError{}
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, ¬FoundError) {
return &core.SuccessResult{
Resource: "deployment",
Verb: "delete",
}, nil
}
return nil, err
}
return deployment, nil
}
return c
}
var completeListNodeTypesCache *llm_inference.ListNodeTypesResponse
func autocompleteDeploymentNodeType(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
req := request.(*llm_inference.CreateDeploymentRequest)
suggestions := core.AutocompleteSuggestions(nil)
client := core.ExtractClient(ctx)
api := llm_inference.NewAPI(client)
if completeListNodeTypesCache == nil {
res, err := api.ListNodeTypes(&llm_inference.ListNodeTypesRequest{
Region: req.Region,
})
if err != nil {
return nil
}
completeListNodeTypesCache = res
}
for _, nodeType := range completeListNodeTypesCache.NodeTypes {
if strings.HasPrefix(nodeType.Name, prefix) {
suggestions = append(suggestions, nodeType.Name)
}
}
return suggestions
}