|
| 1 | +/* |
| 2 | + * Copyright 2025 The CNAI Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/briandowns/spinner" |
| 25 | + "github.com/spf13/cobra" |
| 26 | + "github.com/spf13/viper" |
| 27 | + |
| 28 | + "github.com/CloudNativeAI/modctl/pkg/backend" |
| 29 | + "github.com/CloudNativeAI/modctl/pkg/config" |
| 30 | +) |
| 31 | + |
| 32 | +var attachConfig = config.NewAttach() |
| 33 | + |
| 34 | +// attachCmd represents the modctl command for attach. |
| 35 | +var attachCmd = &cobra.Command{ |
| 36 | + Use: "attach [flags] <file>", |
| 37 | + Short: "A command line tool for modctl attach", |
| 38 | + Args: cobra.ExactArgs(1), |
| 39 | + DisableAutoGenTag: true, |
| 40 | + SilenceUsage: true, |
| 41 | + FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true}, |
| 42 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 43 | + if err := attachConfig.Validate(); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + return runAttach(context.Background(), args[0]) |
| 48 | + }, |
| 49 | +} |
| 50 | + |
| 51 | +// init initializes build command. |
| 52 | +func init() { |
| 53 | + flags := attachCmd.Flags() |
| 54 | + flags.StringVarP(&attachConfig.Source, "source", "s", "", "source model artifact name") |
| 55 | + flags.StringVarP(&attachConfig.Target, "target", "t", "", "target model artifact name") |
| 56 | + flags.BoolVarP(&attachConfig.OutputRemote, "output-remote", "", false, "turning on this flag will output model artifact to remote registry directly") |
| 57 | + flags.BoolVarP(&attachConfig.PlainHTTP, "plain-http", "", false, "turning on this flag will use plain HTTP instead of HTTPS") |
| 58 | + flags.BoolVarP(&attachConfig.Insecure, "insecure", "", false, "turning on this flag will disable TLS verification") |
| 59 | + flags.BoolVarP(&attachConfig.Force, "force", "f", false, "turning on this flag will force the attach, which will overwrite the layer if it already exists with same filepath") |
| 60 | + flags.BoolVar(&attachConfig.Nydusify, "nydusify", false, "[EXPERIMENTAL] nydusify the model artifact") |
| 61 | + flags.MarkHidden("nydusify") |
| 62 | + |
| 63 | + if err := viper.BindPFlags(flags); err != nil { |
| 64 | + panic(fmt.Errorf("bind cache list flags to viper: %w", err)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// runAttach runs the attach modctl. |
| 69 | +func runAttach(ctx context.Context, filepath string) error { |
| 70 | + b, err := backend.New(rootConfig.StoargeDir) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + if err := b.Attach(ctx, filepath, attachConfig); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + fmt.Printf("Successfully attached model artifact: %s\n", attachConfig.Target) |
| 80 | + |
| 81 | + // nydusify the model artifact if needed. |
| 82 | + if attachConfig.Nydusify { |
| 83 | + sp := spinner.New(spinner.CharSets[39], 100*time.Millisecond, spinner.WithSuffix("Nydusifying...")) |
| 84 | + sp.Start() |
| 85 | + defer sp.Stop() |
| 86 | + |
| 87 | + nydusName, err := b.Nydusify(ctx, attachConfig.Target) |
| 88 | + if err != nil { |
| 89 | + err = fmt.Errorf("failed to nydusify %s: %w", attachConfig.Target, err) |
| 90 | + sp.FinalMSG = err.Error() |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + sp.FinalMSG = fmt.Sprintf("Successfully nydusify model artifact: %s", nydusName) |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments