提交 25f08a71 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!28 message: add info to client when image is deleted successfully

Merge pull request !28 from DCCooper/master
......@@ -90,6 +90,8 @@ func NewContainerImageBuildCmd() *cobra.Command {
NewSaveCmd(),
)
disableFlags(ctrImgBuildCmd)
return ctrImgBuildCmd
}
......@@ -97,7 +99,7 @@ func NewContainerImageBuildCmd() *cobra.Command {
func NewBuildCmd() *cobra.Command {
// buildCmd represents the "build" command
buildCmd := &cobra.Command{
Use: "build",
Use: "build [FLAGS] PATH",
Short: "Build container images",
Example: buildExample,
RunE: buildCommand,
......
// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
// isula-build licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: Zekun Liu
// Create: 2020-07-16
// Description: This file is used for command import
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
dockerref "github.com/containers/image/v5/docker/reference"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
pb "isula.org/isula-build/api/services"
"isula.org/isula-build/util"
)
const (
bufferSize = 32 * 1024
maxTarballSize = 10 * 1024 * 1024 * 1024 // support tarball max size at most 10G
importExample = `isula-build ctr-img import file [REPOSITORY[:TAG]]`
importArgsLen = 1
)
type importOptions struct {
source string
reference string
}
var importOpts importOptions
// NewImportCmd returns import command
func NewImportCmd() *cobra.Command {
importCmd := &cobra.Command{
Use: "import",
Short: "Import the base image from a tarball to the image store",
Example: importExample,
RunE: importCommand,
}
return importCmd
}
func importCommand(c *cobra.Command, args []string) error {
if len(args) < importArgsLen {
return errors.New("requires at least one argument")
}
if err := util.CheckFileSize(args[0], maxTarballSize); err != nil {
return err
}
importOpts.source = args[0]
if len(args) > importArgsLen {
importOpts.reference = args[1]
}
ctx := context.TODO()
cli, err := NewClient(ctx)
if err != nil {
return err
}
return runImport(ctx, cli)
}
func runImport(ctx context.Context, cli Cli) error {
if importOpts.reference != "" {
if _, err := dockerref.Parse(importOpts.reference); err != nil {
return err
}
}
file, err := os.Open(importOpts.source)
if err != nil {
return err
}
defer func() {
if ferr := file.Close(); ferr != nil {
logrus.Warnf("Close file %s failed", importOpts.source)
}
}()
rpcCli, err := cli.Client().Import(ctx)
if err != nil {
return err
}
reader := bufio.NewReader(file)
buf := make([]byte, bufferSize, bufferSize)
var length int
for {
length, err = reader.Read(buf)
if err != nil && err != io.EOF {
return err
}
if length == 0 {
break
}
if err = rpcCli.Send(&pb.ImportRequest{
Data: buf[0:length],
Reference: importOpts.reference,
}); err != nil {
return err
}
}
resp, err := rpcCli.CloseAndRecv()
if err != nil {
return err
}
if resp == nil {
return errors.New("import failed, got nil response")
}
fmt.Printf("Import success with image id: %s\n", resp.ImageID)
return nil
}
// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
// isula-build licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: Zekun Liu
// Create: 2020-07-16
// Description: This file is used for command import
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
dockerref "github.com/containers/image/v5/docker/reference"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
pb "isula.org/isula-build/api/services"
"isula.org/isula-build/util"
)
const (
bufferSize = 32 * 1024
maxTarballSize = 10 * 1024 * 1024 * 1024 // support tarball max size at most 10G
importExample = `isula-build ctr-img import busybox.tar busybox:isula`
importArgsLen = 1
)
type importOptions struct {
source string
reference string
}
var importOpts importOptions
// NewImportCmd returns import command
func NewImportCmd() *cobra.Command {
importCmd := &cobra.Command{
Use: "import FILE [REPOSITORY[:TAG]]",
Short: "Import the base image from a tarball to the image store",
Example: importExample,
RunE: importCommand,
}
return importCmd
}
func importCommand(c *cobra.Command, args []string) error {
if len(args) < importArgsLen {
return errors.New("requires at least one argument")
}
if err := util.CheckFileSize(args[0], maxTarballSize); err != nil {
return err
}
importOpts.source = args[0]
if len(args) > importArgsLen {
importOpts.reference = args[1]
}
ctx := context.TODO()
cli, err := NewClient(ctx)
if err != nil {
return err
}
return runImport(ctx, cli)
}
func runImport(ctx context.Context, cli Cli) error {
if importOpts.reference != "" {
if _, err := dockerref.Parse(importOpts.reference); err != nil {
return err
}
}
file, err := os.Open(importOpts.source)
if err != nil {
return err
}
defer func() {
if ferr := file.Close(); ferr != nil {
logrus.Warnf("Close file %s failed", importOpts.source)
}
}()
rpcCli, err := cli.Client().Import(ctx)
if err != nil {
return err
}
reader := bufio.NewReader(file)
buf := make([]byte, bufferSize, bufferSize)
var length int
for {
length, err = reader.Read(buf)
if err != nil && err != io.EOF {
return err
}
if length == 0 {
break
}
if err = rpcCli.Send(&pb.ImportRequest{
Data: buf[0:length],
Reference: importOpts.reference,
}); err != nil {
return err
}
}
resp, err := rpcCli.CloseAndRecv()
if err != nil {
return err
}
if resp == nil {
return errors.New("import failed, got nil response")
}
fmt.Printf("Import success with image id: %s\n", resp.ImageID)
return nil
}
......@@ -38,7 +38,7 @@ var infoOpts infoOptions
func NewInfoCmd() *cobra.Command {
// infoCmd represents the "info" command
infoCmd := &cobra.Command{
Use: "info",
Use: "info [FLAGS]",
Short: "Show isula-build system information",
RunE: infoCommand,
Args: util.NoArgs,
......
......@@ -42,7 +42,7 @@ const (
// NewLoadCmd returns image load command
func NewLoadCmd() *cobra.Command {
loadCmd := &cobra.Command{
Use: "load",
Use: "load [FLAGS]",
Short: "Load images",
Example: loadExample,
Args: util.NoArgs,
......
......@@ -63,7 +63,7 @@ type passReader func() ([]byte, error)
func NewLoginCmd() *cobra.Command {
// loginCmd represents the "login" command
loginCmd := &cobra.Command{
Use: "login",
Use: "login SERVER [FLAGS]",
Short: "Login to an image registry",
Example: loginExample,
RunE: loginCommand,
......
......@@ -40,7 +40,7 @@ var logoutOpts logoutOptions
func NewLogoutCmd() *cobra.Command {
// logoutCmd represents the "logout" command
logoutCmd := &cobra.Command{
Use: "logout",
Use: "logout [SERVER] [FLAGS]",
Short: "Logout from an image registry",
Example: logoutExample,
RunE: logoutCommand,
......
......@@ -45,12 +45,14 @@ func newCliCommand() *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return before(cmd)
},
SilenceUsage: true,
SilenceErrors: true,
Version: fmt.Sprintf("%s, build %s", version.Version, version.GitCommit),
SilenceUsage: true,
SilenceErrors: true,
DisableFlagsInUseLine: true,
Version: fmt.Sprintf("%s, build %s", version.Version, version.GitCommit),
}
setupRootCmd(rootCmd)
addCommands(rootCmd)
disableFlags(rootCmd)
return rootCmd
}
......@@ -117,3 +119,9 @@ var completionCmd = &cobra.Command{
cmd.Root().GenBashCompletion(os.Stdout) // nolint
},
}
func disableFlags(root *cobra.Command) {
for _, c := range root.Commands() {
c.DisableFlagsInUseLine = true
}
}
......@@ -41,7 +41,7 @@ isula-build ctr-img rm --all`
func NewRemoveCmd() *cobra.Command {
// removeCmd represents the "rm" command
removeCmd := &cobra.Command{
Use: "rm",
Use: "rm IMAGE [IMAGE...] [FLAGS]",
Short: "Remove one or more locally stored images",
Example: removeExample,
RunE: removeCommand,
......
......@@ -48,7 +48,7 @@ isula-build ctr-img save 21c3e96ac411 -o myimage.tar`
// NewSaveCmd cmd for container image saving
func NewSaveCmd() *cobra.Command {
saveCmd := &cobra.Command{
Use: "save",
Use: "save IMAGE [FLAGS]",
Short: "Save image to tarball",
Example: saveExample,
RunE: saveCommand,
......
......@@ -23,15 +23,15 @@ import (
)
const (
tagExample = `isula-build ctr-img tag <imageID> busybox:latest
isula-build ctr-img tag <imageName> busybox:latest`
tagExample = `isula-build ctr-img tag a24bb4013296 busybox:latest
isula-build ctr-img tag busybox:v1.0 busybox:latest`
)
// NewTagCmd returns tag command
func NewTagCmd() *cobra.Command {
// tagCmd represents the "tag" command
tagCmd := &cobra.Command{
Use: "tag",
Use: "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]",
Short: "create a tag for source image",
RunE: tagCommand,
Example: tagExample,
......
......@@ -57,12 +57,19 @@ func (b *Backend) Remove(req *pb.RemoveRequest, stream pb.Control_RemoveServer)
}
for _, layer := range layers {
layerString := fmt.Sprintf("Deleted: sha256:%v", layer)
layerString := fmt.Sprintf("Deleted layer: sha256:%v", layer)
logrus.Debug(layerString)
if err = stream.Send(&pb.RemoveResponse{LayerMessage: layerString}); err != nil {
return err
}
}
// after image is deleted successfully, print it out
imageString := fmt.Sprintf("Deleted image: %v", imageID)
logrus.Debug(imageString)
if err = stream.Send(&pb.RemoveResponse{LayerMessage: imageString}); err != nil {
return err
}
}
return nil
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册