Skip to content

Commit ab85c59

Browse files
committed
Add an r8.im clone command
this is used to create a copy of a public image for debugging purposes
1 parent b8022c9 commit ab85c59

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

pkg/cli/clone.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/google/go-containerregistry/pkg/authn"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/anotherjesse/r8im/pkg/auth"
11+
"github.com/anotherjesse/r8im/pkg/images"
12+
)
13+
14+
func newCloneCommand() *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "clone",
17+
Short: "copy existing image to a new image",
18+
Hidden: false,
19+
RunE: cloneCommmand,
20+
}
21+
22+
cmd.Flags().StringVarP(&sToken, "token", "t", "", "replicate cog token")
23+
cmd.Flags().StringVarP(&sRegistry, "registry", "r", "r8.im", "registry host")
24+
cmd.Flags().StringVarP(&baseRef, "base", "b", "", "base image reference - include tag: r8.im/username/modelname@sha256:hexdigest")
25+
cmd.MarkFlagRequired("base")
26+
cmd.Flags().StringVarP(&dest, "dest", "d", "", "destination image reference: r8.im/username/modelname")
27+
cmd.MarkFlagRequired("dest")
28+
29+
return cmd
30+
}
31+
32+
func cloneCommmand(cmd *cobra.Command, args []string) error {
33+
if sToken == "" {
34+
sToken = os.Getenv("COG_TOKEN")
35+
}
36+
37+
u, err := auth.VerifyCogToken(sRegistry, sToken)
38+
if err != nil {
39+
fmt.Fprintln(os.Stderr, "authentication error, invalid token or registry host error")
40+
return err
41+
}
42+
auth := authn.FromConfig(authn.AuthConfig{Username: u, Password: sToken})
43+
44+
image_id, err := images.Affix(baseRef, dest, "", auth)
45+
if err != nil {
46+
return err
47+
}
48+
49+
fmt.Println(image_id)
50+
51+
return nil
52+
}

pkg/cli/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func NewRootCommand() (*cobra.Command, error) {
1818

1919
rootCmd.AddCommand(
2020
newAffixCommand(),
21+
newCloneCommand(),
2122
newLayerCommand(),
2223
newExtractCommand(),
2324
newRemixCommand(),

pkg/images/affix.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,30 @@ func Affix(baseRef string, dest string, newLayer string, auth authn.Authenticato
3232

3333
// --- adding new layer ontop of existing image
3434

35-
fmt.Fprintln(os.Stderr, "appending as new layer", newLayer)
36-
37-
start = time.Now()
38-
img, err := appendLayer(base, newLayer)
39-
if err != nil {
40-
return "", fmt.Errorf("appending %v: %w", newLayer, err)
35+
var img v1.Image
36+
37+
if newLayer != "" {
38+
fmt.Fprintln(os.Stderr, "appending as new layer", newLayer)
39+
40+
start = time.Now()
41+
img, err = appendLayer(base, newLayer)
42+
if err != nil {
43+
return "", fmt.Errorf("appending %v: %w", newLayer, err)
44+
}
45+
fmt.Fprintln(os.Stderr, "appending took", time.Since(start))
46+
} else {
47+
cfg, err := base.ConfigFile()
48+
if err != nil {
49+
return "", fmt.Errorf("getting config file: %w", err)
50+
}
51+
52+
cfg.Config.Labels["cloned"] = "true"
53+
54+
img, err = mutate.ConfigFile(base, cfg)
55+
if err != nil {
56+
return "", fmt.Errorf("mutating config file: %w", err)
57+
}
4158
}
42-
fmt.Fprintln(os.Stderr, "appending took", time.Since(start))
43-
4459
// --- pushing image
4560

4661
start = time.Now()

0 commit comments

Comments
 (0)