|
| 1 | +package inference |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "reflect" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/fatih/color" |
| 12 | + "github.com/scaleway/scaleway-cli/v2/core" |
| 13 | + "github.com/scaleway/scaleway-cli/v2/core/human" |
| 14 | + "github.com/scaleway/scaleway-sdk-go/api/inference/v1" |
| 15 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + deploymentActionTimeout = 60 * time.Minute |
| 20 | + deploymentActionCreate = 1 |
| 21 | + deploymentActionDelete = 2 |
| 22 | +) |
| 23 | + |
| 24 | +var deploymentStateMarshalSpecs = human.EnumMarshalSpecs{ |
| 25 | + inference.DeploymentStatusCreating: &human.EnumMarshalSpec{Attribute: color.FgBlue}, |
| 26 | + inference.DeploymentStatusDeploying: &human.EnumMarshalSpec{Attribute: color.FgBlue}, |
| 27 | + inference.DeploymentStatusDeleting: &human.EnumMarshalSpec{Attribute: color.FgBlue}, |
| 28 | + inference.DeploymentStatusError: &human.EnumMarshalSpec{Attribute: color.FgRed}, |
| 29 | + inference.DeploymentStatusReady: &human.EnumMarshalSpec{Attribute: color.FgGreen}, |
| 30 | + inference.DeploymentStatusLocked: &human.EnumMarshalSpec{Attribute: color.FgRed}, |
| 31 | +} |
| 32 | + |
| 33 | +func DeploymentMarshalerFunc(i any, opt *human.MarshalOpt) (string, error) { |
| 34 | + type tmp inference.Deployment |
| 35 | + deployment := tmp(i.(inference.Deployment)) |
| 36 | + opt.Sections = []*human.MarshalSection{ |
| 37 | + { |
| 38 | + FieldName: "Endpoints", |
| 39 | + Title: "Endpoints", |
| 40 | + }, |
| 41 | + } |
| 42 | + str, err := human.Marshal(deployment, opt) |
| 43 | + if err != nil { |
| 44 | + return "", err |
| 45 | + } |
| 46 | + |
| 47 | + return str, nil |
| 48 | +} |
| 49 | + |
| 50 | +func deploymentDeleteBuilder(c *core.Command) *core.Command { |
| 51 | + c.WaitFunc = waitForDeploymentFunc(deploymentActionDelete) |
| 52 | + |
| 53 | + return c |
| 54 | +} |
| 55 | + |
| 56 | +func deploymentCreateBuilder(c *core.Command) *core.Command { |
| 57 | + type llmInferenceEndpointSpecCustom struct { |
| 58 | + *inference.EndpointSpec |
| 59 | + IsPublic bool `json:"is-public"` |
| 60 | + } |
| 61 | + |
| 62 | + type llmInferenceCreateDeploymentRequestCustom struct { |
| 63 | + *inference.CreateDeploymentRequest |
| 64 | + Endpoints []*llmInferenceEndpointSpecCustom `json:"endpoints"` |
| 65 | + } |
| 66 | + |
| 67 | + c.ArgSpecs.AddBefore("endpoints.{index}.private-network.private-network-id", &core.ArgSpec{ |
| 68 | + Name: "endpoints.{index}.is-public", |
| 69 | + Short: "Will configure your public endpoint if true", |
| 70 | + Required: false, |
| 71 | + Default: core.DefaultValueSetter("true"), |
| 72 | + }) |
| 73 | + |
| 74 | + c.ArgsType = reflect.TypeOf(llmInferenceCreateDeploymentRequestCustom{}) |
| 75 | + |
| 76 | + c.WaitFunc = waitForDeploymentFunc(deploymentActionCreate) |
| 77 | + |
| 78 | + c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) { |
| 79 | + deploymentCreateCustomRequest := argsI.(*llmInferenceCreateDeploymentRequestCustom) |
| 80 | + deploymentRequest := deploymentCreateCustomRequest.CreateDeploymentRequest |
| 81 | + if deploymentCreateCustomRequest.Endpoints == nil { |
| 82 | + publicEndpoint := &inference.EndpointPublicNetworkDetails{} |
| 83 | + endpoint := inference.EndpointSpec{ |
| 84 | + PublicNetwork: publicEndpoint, |
| 85 | + PrivateNetwork: nil, |
| 86 | + DisableAuth: false, |
| 87 | + } |
| 88 | + deploymentRequest.Endpoints = append(deploymentRequest.Endpoints, &endpoint) |
| 89 | + |
| 90 | + return runner(ctx, deploymentRequest) |
| 91 | + } |
| 92 | + for _, ep := range deploymentCreateCustomRequest.Endpoints { |
| 93 | + if ep.IsPublic { |
| 94 | + deploymentRequest.Endpoints = append( |
| 95 | + deploymentRequest.Endpoints, |
| 96 | + &inference.EndpointSpec{ |
| 97 | + PublicNetwork: &inference.EndpointPublicNetworkDetails{}, |
| 98 | + DisableAuth: ep.DisableAuth, |
| 99 | + }, |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + if ep.PrivateNetwork != nil { |
| 104 | + deploymentRequest.Endpoints = append( |
| 105 | + deploymentRequest.Endpoints, |
| 106 | + &inference.EndpointSpec{ |
| 107 | + PrivateNetwork: &inference.EndpointPrivateNetworkDetails{ |
| 108 | + PrivateNetworkID: ep.PrivateNetwork.PrivateNetworkID, |
| 109 | + }, |
| 110 | + DisableAuth: ep.DisableAuth, |
| 111 | + }, |
| 112 | + ) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return runner(ctx, deploymentRequest) |
| 117 | + } |
| 118 | + |
| 119 | + return c |
| 120 | +} |
| 121 | + |
| 122 | +func waitForDeploymentFunc(action int) core.WaitFunc { |
| 123 | + return func(ctx context.Context, _, respI any) (any, error) { |
| 124 | + deployment, err := inference.NewAPI(core.ExtractClient(ctx)). |
| 125 | + WaitForDeployment(&inference.WaitForDeploymentRequest{ |
| 126 | + DeploymentID: respI.(*inference.Deployment).ID, |
| 127 | + Region: respI.(*inference.Deployment).Region, |
| 128 | + Timeout: scw.TimeDurationPtr(deploymentActionTimeout), |
| 129 | + RetryInterval: core.DefaultRetryInterval, |
| 130 | + }) |
| 131 | + |
| 132 | + switch action { |
| 133 | + case deploymentActionCreate: |
| 134 | + return deployment, err |
| 135 | + case deploymentActionDelete: |
| 136 | + if err != nil { |
| 137 | + // if we get a 404 here, it means the resource was successfully deleted |
| 138 | + notFoundError := &scw.ResourceNotFoundError{} |
| 139 | + responseError := &scw.ResponseError{} |
| 140 | + if errors.As(err, &responseError) && |
| 141 | + responseError.StatusCode == http.StatusNotFound || |
| 142 | + errors.As(err, ¬FoundError) { |
| 143 | + return fmt.Sprintf( |
| 144 | + "Server %s successfully deleted.", |
| 145 | + respI.(*inference.Deployment).ID, |
| 146 | + ), nil |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return nil, err |
| 152 | + } |
| 153 | +} |
0 commit comments