Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/air/air.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ see your active runs with "databricks air list".`
wrapRunErrorWithDebugTip(runCommand)
addToGroup(cmd, runGroupID, runCommand)
addToGroup(cmd, manageGroupID, newGetCommand(), newListCommand(), newLogsCommand(), newCancelCommand())
addToGroup(cmd, packageGroupID, newConvertToDabsCommand(), newImagesCommand())
addToGroup(cmd, packageGroupID, newRegisterCommand(), newConvertToDabsCommand(), newImagesCommand())
addToGroup(cmd, capacityGroupID, newPoolsCommand())

// Subcommands inherit this help func, so the footer is printed only for `air` itself.
Expand Down
2 changes: 1 addition & 1 deletion cmd/air/air_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNewRegistersAllSubcommands(t *testing.T) {
registered[c.Name()] = true
}

want := []string{"run", "get", "list", "pools", "logs", "cancel", "convert-to-dabs", "images"}
want := []string{"run", "get", "list", "pools", "logs", "cancel", "register", "convert-to-dabs", "images"}
for _, name := range want {
assert.True(t, registered[name], "subcommand %q is not registered", name)
}
Expand Down
90 changes: 88 additions & 2 deletions cmd/air/modelcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package aircmd
import (
"context"
"errors"
"fmt"
"net/http"
"regexp"
"sync"
"unicode/utf16"

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/retries"
"github.com/spf13/cobra"
)

// This file holds what `air register` and `air serve` share: their JSON error codes
// and how they report a wait they stopped.
// This file holds what `air register` and `air serve` share: their JSON error codes,
// how they report a wait they stopped, and the names and numbers both files take.

// Errors a model command wraps when it stops waiting, as "air register was
// interrupted". The server keeps working, and the error says how to continue.
Expand Down Expand Up @@ -94,5 +97,88 @@ func modelErrorCode(err error) (code string, retryable bool) {
return codeInternalError, true
}

// serveCommandPath is the command air register's output points to for serving a
// version, and whose -h documents serveConfig.
const serveCommandPath = "databricks air serve"

// Flags the model commands share.
const (
ucModelFlag = "uc-model"
endpointNameFlag = "endpoint-name"
modelVersionFlag = "model-version"
modelSourceFlag = "model-source"
)

// changedFlags reports which of names were passed on the command line.
func changedFlags(cmd *cobra.Command, names ...string) map[string]bool {
changed := map[string]bool{}
for _, name := range names {
changed[name] = cmd.Flags().Changed(name)
}
return changed
}

// renderModelError renders a model command's failure, if any, with its JSON error
// code and whether retrying can help.
func renderModelError(cmd *cobra.Command, err error) error {
if err == nil {
return nil
}
code, retryable := modelErrorCode(err)
kind := "PERMANENT"
if retryable {
kind = "TRANSIENT"
}
return renderError(cmd.Context(), cmd, code, kind, retryable, err)
}

// schemaHelp is the help of a model command whose YAML schema documents a field
// given as a positional argument to -h, as in `-h config.compute`. cobra passes
// -h's positional args to the help func before Args/required-flag validation, so
// a field is documented without needing -f.
func schemaHelp(schema func() configField, commandPath string) func(*cobra.Command, []string) {
return func(c *cobra.Command, args []string) {
fields := c.Flags().Args()
if len(fields) == 0 {
// Parent() is nil for a detached command (unit tests).
if parent := c.Parent(); parent != nil {
parent.HelpFunc()(c, args)
return
}
_ = c.Usage()
return
}
field, err := resolveSchemaField(schema(), fields[0])
if err != nil {
c.PrintErrln("Error:", err)
return
}
renderConfigField(c.OutOrStdout(), commandPath, field, nil)
}
}

// registeredModelRe matches <catalog>.<schema>.<model> with the characters the
// server accepts in each part.
var registeredModelRe = sync.OnceValue(func() *regexp.Regexp { return regexp.MustCompile("^[^\\s./`]+\\.[^\\s./`]+\\.[^\\s./`]+$") })

// modelVersionRe matches a Unity Catalog model version number.
var modelVersionRe = sync.OnceValue(func() *regexp.Regexp { return regexp.MustCompile(`^[1-9][0-9]*$`) })

// validateUCModelName checks a Unity Catalog model name from source, the YAML field
// or a flag.
func validateUCModelName(name, source string) error {
if !registeredModelRe().MatchString(name) {
return fmt.Errorf("%s must be a Unity Catalog model name as <catalog>.<schema>.<model>, got %q", source, name)
}
return nil
}

// serverLength is the length of s as the server's limits count it, in UTF-16
// code units.
func serverLength(s string) int {
n := 0
for _, r := range s {
n += utf16.RuneLen(r)
}
return n
}
7 changes: 7 additions & 0 deletions cmd/air/modelcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ func TestModelErrorCode(t *testing.T) {
assert.Equal(t, tt.retryable, retryable, tt.name)
}
}

func TestValidateUCModelName(t *testing.T) {
assert.NoError(t, validateUCModelName("main.serving.defect-detector", "uc_model"))
for _, name := range []string{"", "qwen", "a.b", "a.b.c.d", "a.b/c.d", "a. b.c", "main.models.`qwen`"} {
assert.ErrorContains(t, validateUCModelName(name, "--uc-model"), "--uc-model must be a Unity Catalog model name", name)
}
}
Loading
Loading