Skip to content

Commit

Permalink
modify mount
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevent-fei committed Feb 27, 2023
1 parent 72cdb75 commit 7b6bffc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 19 deletions.
98 changes: 81 additions & 17 deletions cmd/sealer/cmd/alpha/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ package alpha

import (
"fmt"
"strings"

"github.com/containers/storage"
"github.com/olekukonko/tablewriter"
"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imageengine/buildah"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/define/options"
imagebuildah "github.com/sealerio/sealer/pkg/imageengine/buildah"
)

var longMountCmdDescription = `
Expand All @@ -49,7 +51,7 @@ func NewMountCmd() *cobra.Command {
Long: longMountCmdDescription,
Example: exampleForMountCmd,
RunE: func(cmd *cobra.Command, args []string) error {
engine, err := buildah.NewBuildahImageEngine(options.EngineGlobalConfigurations{})
engine, err := imagebuildah.NewBuildahImageEngine(options.EngineGlobalConfigurations{})
if err != nil {
return err
}
Expand All @@ -73,28 +75,52 @@ func NewMountCmd() *cobra.Command {
return nil
}

for _, name := range args {
cid, err := engine.CreateContainer(&options.FromOptions{
Image: name,
Quiet: false,
})
builders, err := imagebuildah.OpenBuilders(store)
if err != nil {
return err
}

var imageIDList []string
for _, builder := range builders {
mounted, err := builder.Mounted()
if err != nil {
return err
}
mounts, err := engine.Mount(&options.MountOptions{Containers: []string{cid}})
if err != nil {
return err
for _, container := range containers {
if builder.ContainerID == container.ID && mounted {
_, id := getImageInfo(images, container)
imageIDList = append(imageIDList, id)
}
}
}

for _, name := range args {
imageID := getImageID(images, name)
if ok := isContains(imageID, imageIDList); !ok {
cid, err := engine.CreateContainer(&options.FromOptions{
Image: imageID,
Quiet: false,
})
if err != nil {
return err
}
mounts, err := engine.Mount(&options.MountOptions{Containers: []string{cid}})
if err != nil {
return err
}
mountPoint := mounts[0].MountPoint
logrus.Infof("mount cluster image %s to %s successful", name, mountPoint)
} else {
logrus.Infof("this image has already been mounted, please do not repeat the operation")
}
mountPoint := mounts[0].MountPoint
logrus.Infof("mount cluster image %s to %s successful", name, mountPoint)
}
return nil
},
}
return mountCmd
}

func mountLists(images []storage.Image, container storage.Container) (string, string) {
func mountList(images []storage.Image, container storage.Container) (string, string) {
for _, image := range images {
for _, name := range image.Names {
if container.ImageID == image.ID {
Expand All @@ -108,7 +134,7 @@ func mountLists(images []storage.Image, container storage.Container) (string, st
func getMountList(containers []storage.Container, store storage.Store, images []storage.Image) (*tablewriter.Table, error) {
table := tablewriter.NewWriter(common.StdOut)
table.SetHeader([]string{imageName, tableHeaderContainerID, "mountpath"})
builders, err := buildah.OpenBuilders(store)
builders, err := imagebuildah.OpenBuilders(store)
if err != nil {
return nil, fmt.Errorf("reading build Containers: %w", err)
}
Expand All @@ -119,7 +145,7 @@ func getMountList(containers []storage.Container, store storage.Store, images []
}
for _, container := range containers {
if builder.ContainerID == container.ID && mounted {
name, _ := mountLists(images, container)
name, _ := mountList(images, container)
contaninerID := truncateID(builder.ContainerID, true)
table.Append([]string{name, contaninerID, builder.MountPoint})
}
Expand All @@ -139,3 +165,41 @@ func truncateID(id string, truncate bool) string {
}
return id
}

func isContains(imageName string, images []string) bool {
for _, image := range images {
if imageName == image || strings.Contains(image, imageName) {
return true
}
}
return false
}

func getImageInfo(images []storage.Image, container storage.Container) (string, string) {
var imageID string
var imgName string
for _, image := range images {
if container.ImageID == image.ID {
imageID = image.ID
}
for _, name := range image.Names {
imgName = name
}
}
return imgName, imageID
}

func getImageID(images []storage.Image, name string) string {
for _, image := range images {
if strings.Contains(image.ID, name) {
return image.ID
}

for _, n := range image.Names {
if name == n {
return image.ID
}
}
}
return ""
}
4 changes: 2 additions & 2 deletions cmd/sealer/cmd/alpha/umount.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NewUmountCmd() *cobra.Command {
return umountCmd
}

func getContainerID(images []storage.Image, imageName string, contaners []storage.Container) string {
func getContainerID(images []storage.Image, imageName string, containers []storage.Container) string {
var imageID string
var cid string
for _, image := range images {
Expand All @@ -123,7 +123,7 @@ func getContainerID(images []storage.Image, imageName string, contaners []storag
}
}

for _, container := range contaners {
for _, container := range containers {
if container.ImageID == imageID {
cid = container.ID
}
Expand Down

0 comments on commit 7b6bffc

Please sign in to comment.