-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbuild.go
More file actions
102 lines (86 loc) · 3.59 KB
/
build.go
File metadata and controls
102 lines (86 loc) · 3.59 KB
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
/*
* Copyright 2024 The CNAI Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"context"
"fmt"
"time"
"github.com/CloudNativeAI/modctl/pkg/backend"
"github.com/CloudNativeAI/modctl/pkg/config"
"github.com/briandowns/spinner"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var buildConfig = config.NewBuild()
// buildCmd represents the modctl command for build.
var buildCmd = &cobra.Command{
Use: "build [flags] <path>",
Short: "A command line tool for modctl build",
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
SilenceUsage: true,
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
RunE: func(cmd *cobra.Command, args []string) error {
if err := buildConfig.Validate(); err != nil {
return err
}
return runBuild(context.Background(), args[0])
},
}
// init initializes build command.
func init() {
flags := buildCmd.Flags()
flags.IntVarP(&buildConfig.Concurrency, "concurrency", "c", buildConfig.Concurrency, "specify the number of concurrent build operations")
flags.StringVarP(&buildConfig.Target, "target", "t", buildConfig.Target, "target model artifact name")
flags.StringVarP(&buildConfig.Modelfile, "modelfile", "f", buildConfig.Modelfile, "model file path")
flags.BoolVarP(&buildConfig.OutputRemote, "output-remote", "", false, "turning on this flag will output model artifact to remote registry directly")
flags.BoolVarP(&buildConfig.PlainHTTP, "plain-http", "", false, "turning on this flag will use plain HTTP instead of HTTPS")
flags.BoolVarP(&buildConfig.Insecure, "insecure", "", false, "turning on this flag will disable TLS verification")
flags.BoolVar(&buildConfig.Nydusify, "nydusify", false, "[EXPERIMENTAL] nydusify the model artifact")
flags.MarkHidden("nydusify")
flags.StringVar(&buildConfig.SourceURL, "source-url", "", "source URL")
flags.StringVar(&buildConfig.SourceRevision, "source-revision", "", "source revision")
// TODO: set the raw flag to true by default in future.
flags.BoolVar(&buildConfig.Raw, "raw", false, "turning on this flag will build model artifact layers in raw format")
if err := viper.BindPFlags(flags); err != nil {
panic(fmt.Errorf("bind cache list flags to viper: %w", err))
}
}
// runBuild runs the build modctl.
func runBuild(ctx context.Context, workDir string) error {
b, err := backend.New(rootConfig.StoargeDir)
if err != nil {
return err
}
if err := b.Build(ctx, buildConfig.Modelfile, workDir, buildConfig.Target, buildConfig); err != nil {
return err
}
fmt.Printf("Successfully built model artifact: %s\n", buildConfig.Target)
// nydusify the model artifact if needed.
if buildConfig.Nydusify {
sp := spinner.New(spinner.CharSets[39], 100*time.Millisecond, spinner.WithSuffix("Nydusifying..."))
sp.Start()
defer sp.Stop()
nydusName, err := b.Nydusify(ctx, buildConfig.Target)
if err != nil {
err = fmt.Errorf("failed to nydusify %s: %w", buildConfig.Target, err)
sp.FinalMSG = err.Error()
return err
}
sp.FinalMSG = fmt.Sprintf("Successfully nydusify model artifact: %s", nydusName)
}
return nil
}