github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/engine/virt/image.go (about) 1 package virt 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "io" 8 "strings" 9 10 enginetypes "github.com/projecteru2/core/engine/types" 11 "github.com/projecteru2/core/log" 12 "github.com/projecteru2/core/types" 13 virttypes "github.com/projecteru2/libyavirt/types" 14 ) 15 16 // ImageList lists images. 17 func (v *Virt) ImageList(ctx context.Context, imageName string) (imgs []*enginetypes.Image, err error) { 18 images, err := v.client.ListImage(ctx, imageName) 19 if err != nil { 20 return nil, err 21 } 22 23 imgs = []*enginetypes.Image{} 24 25 for _, image := range images { 26 imgs = append(imgs, &enginetypes.Image{ 27 ID: image.ID, 28 Tags: []string{image.Name}, 29 }) 30 } 31 32 return 33 } 34 35 // ImageRemove removes a specific image. 36 func (v *Virt) ImageRemove(ctx context.Context, tag string, force, prune bool) (names []string, err error) { 37 user, imgName, err := splitUserImage(tag) 38 if err != nil { 39 return nil, err 40 } 41 42 return v.client.RemoveImage(ctx, imgName, user, force, prune) 43 } 44 45 // ImagesPrune prunes one. 46 func (v *Virt) ImagesPrune(ctx context.Context) (err error) { 47 log.WithFunc("engine.virt.ImagesPrune").Warnf(ctx, "ImagesPrune does not implement") 48 return 49 } 50 51 // ImagePull pulls an image to local virt-node. 52 func (v *Virt) ImagePull(ctx context.Context, ref string, all bool) (rc io.ReadCloser, err error) { 53 // ref is a simple image name without username for now 54 _, imgName, err := splitUserImage(ref) 55 if err != nil { 56 return nil, err 57 } 58 59 msg, err := v.client.PullImage(ctx, imgName, all) 60 if err != nil { 61 return nil, err 62 } 63 64 rc = io.NopCloser(strings.NewReader(msg)) 65 defer rc.Close() 66 67 return rc, err 68 } 69 70 // ImagePush pushes to central image registry. 71 func (v *Virt) ImagePush(ctx context.Context, ref string) (rc io.ReadCloser, err error) { 72 user, imgName, err := splitUserImage(ref) 73 if err != nil { 74 return nil, err 75 } 76 77 msg, err := v.client.PushImage(ctx, imgName, user) 78 if err != nil { 79 return nil, err 80 } 81 82 reply, err := json.Marshal(&types.BuildImageMessage{Error: msg}) 83 if err != nil { 84 return nil, err 85 } 86 87 rc = io.NopCloser(bytes.NewReader(reply)) 88 defer rc.Close() 89 90 return rc, nil 91 } 92 93 // ImageBuild captures from a guest. 94 func (v *Virt) ImageBuild(ctx context.Context, _ io.Reader, _ []string, _ string) (rc io.ReadCloser, err error) { 95 log.WithFunc("engine.virt.ImageBuild").Warnf(ctx, "imageBuild does not implement") 96 return 97 } 98 99 // ImageBuildFromExist builds vm image from running vm 100 func (v *Virt) ImageBuildFromExist(ctx context.Context, ID string, refs []string, user string) (string, error) { 101 if len(user) < 1 { 102 return "", types.ErrNoImageUser 103 } 104 if len(refs) != 1 { 105 return "", types.ErrInvaildRefs 106 } 107 108 _, imgName, err := splitUserImage(refs[0]) 109 if err != nil { 110 return "", err 111 } 112 113 req := virttypes.CaptureGuestReq{Name: imgName, User: user} 114 req.ID = ID 115 116 uimg, err := v.client.CaptureGuest(ctx, req) 117 if err != nil { 118 return "", err 119 } 120 121 return uimg.ID, nil 122 } 123 124 // ImageBuildCachePrune prunes cached one. 125 func (v *Virt) ImageBuildCachePrune(ctx context.Context, _ bool) (reclaimed uint64, err error) { 126 log.WithFunc("engine.virt.ImageBuildCachePrune").Warnf(ctx, "ImageBuildCachePrune does not implement and not required by vm") 127 return 128 } 129 130 // ImageLocalDigests shows local images' digests. 131 // If local image file not exists return error 132 // If exists return digests 133 // Same for remote digest 134 func (v *Virt) ImageLocalDigests(ctx context.Context, image string) ([]string, error) { 135 // If not exists return error 136 // If exists return digests 137 _, imgName, err := splitUserImage(image) 138 if err != nil { 139 return nil, err 140 } 141 142 return v.client.DigestImage(ctx, imgName, true) 143 } 144 145 // ImageRemoteDigest shows remote one's digest. 146 func (v *Virt) ImageRemoteDigest(ctx context.Context, image string) (string, error) { 147 _, imgName, err := splitUserImage(image) 148 if err != nil { 149 return "", err 150 } 151 152 digests, err := v.client.DigestImage(ctx, imgName, false) 153 switch { 154 case err != nil: 155 return "", err 156 case len(digests) < 1: 157 return "", types.ErrInvaildRemoteDigest 158 default: 159 return digests[0], nil 160 } 161 }