yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/shell/image.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package shell
    16  
    17  import (
    18  	"os"
    19  
    20  	"yunion.io/x/cloudmux/pkg/multicloud/zstack"
    21  	"yunion.io/x/onecloud/pkg/util/shellutils"
    22  )
    23  
    24  func init() {
    25  	type ImageListOptions struct {
    26  		ZoneId  string
    27  		ImageId string
    28  	}
    29  	shellutils.R(&ImageListOptions{}, "image-list", "List images", func(cli *zstack.SRegion, args *ImageListOptions) error {
    30  		images, err := cli.GetImages(args.ZoneId, args.ImageId)
    31  		if err != nil {
    32  			return err
    33  		}
    34  		printList(images, 0, 0, 0, []string{})
    35  		return nil
    36  	})
    37  
    38  	type ImageCreateOptions struct {
    39  		ZONE     string
    40  		FILE     string
    41  		FORMAT   string `choices:"qcow2|raw|iso"`
    42  		PLATFORM string `choices:"Linux|Windows|Other"`
    43  		Desc     string
    44  	}
    45  
    46  	shellutils.R(&ImageCreateOptions{}, "image-create", "Create image", func(cli *zstack.SRegion, args *ImageCreateOptions) error {
    47  		f, err := os.Open(args.FILE)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		defer f.Close()
    52  		finfo, err := f.Stat()
    53  		if err != nil {
    54  			return err
    55  		}
    56  		image, err := cli.CreateImage(args.ZONE, args.FILE, args.FORMAT, args.PLATFORM, args.Desc, f, finfo.Size(), nil)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		printObject(image)
    61  		return nil
    62  	})
    63  
    64  	type ImageIdOptions struct {
    65  		ID string
    66  	}
    67  
    68  	shellutils.R(&ImageIdOptions{}, "image-delete", "Delete image", func(cli *zstack.SRegion, args *ImageIdOptions) error {
    69  		return cli.DeleteImage(args.ID)
    70  	})
    71  
    72  }