yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/shell/store.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  	"context"
    19  	"fmt"
    20  	"os"
    21  
    22  	"yunion.io/x/cloudmux/pkg/multicloud/esxi"
    23  	"yunion.io/x/onecloud/pkg/util/printutils"
    24  	"yunion.io/x/onecloud/pkg/util/shellutils"
    25  )
    26  
    27  func getDatastore(cli *esxi.SESXiClient, dcId string, dsId string) (*esxi.SDatastore, error) {
    28  	dc, err := cli.FindDatacenterByMoId(dcId)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	ds, err := dc.GetIStorageByMoId(dsId)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return ds.(*esxi.SDatastore), nil
    37  }
    38  
    39  func init() {
    40  	type DatastoreListOptions struct {
    41  		DATACENTER string `help:"List datastores in datacenter"`
    42  	}
    43  	shellutils.R(&DatastoreListOptions{}, "ds-list", "List datastores in datacenter", func(cli *esxi.SESXiClient, args *DatastoreListOptions) error {
    44  		dc, err := cli.FindDatacenterByMoId(args.DATACENTER)
    45  		if err != nil {
    46  			return err
    47  		}
    48  		ds, err := dc.GetIStorages()
    49  		if err != nil {
    50  			return err
    51  		}
    52  		printList(ds, nil)
    53  		return nil
    54  	})
    55  
    56  	type DatastoreShowOptions struct {
    57  		DATACENTER string `help:"Datacenter"`
    58  		DSID       string `help:"Datastore ID"`
    59  	}
    60  	shellutils.R(&DatastoreShowOptions{}, "ds-show", "Show details of a datastore", func(cli *esxi.SESXiClient, args *DatastoreShowOptions) error {
    61  		ds, err := getDatastore(cli, args.DATACENTER, args.DSID)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		printObject(ds)
    66  		return nil
    67  	})
    68  
    69  	shellutils.R(&DatastoreShowOptions{}, "ds-cache-show", "Show details of a datastore image cache", func(cli *esxi.SESXiClient, args *DatastoreShowOptions) error {
    70  		ds, err := getDatastore(cli, args.DATACENTER, args.DSID)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		cache := ds.GetIStoragecache()
    75  		printObject(cache)
    76  		return nil
    77  	})
    78  
    79  	shellutils.R(&DatastoreShowOptions{}, "ds-cache-list", "Show image list of a datastore image cache", func(cli *esxi.SESXiClient, args *DatastoreShowOptions) error {
    80  		ds, err := getDatastore(cli, args.DATACENTER, args.DSID)
    81  		if err != nil {
    82  			return err
    83  		}
    84  		cache := ds.GetIStoragecache()
    85  		images, err := cache.GetICloudImages()
    86  		if err != nil {
    87  			return err
    88  		}
    89  		printList(images, []string{})
    90  		return nil
    91  	})
    92  
    93  	type DatastoreListDirOptions struct {
    94  		DATACENTER string `help:"Datacenter"`
    95  		DSID       string `help:"Datastore ID"`
    96  		DIR        string `help:"directory"`
    97  	}
    98  	shellutils.R(&DatastoreListDirOptions{}, "ds-list-dir", "List directory of a datastore", func(cli *esxi.SESXiClient, args *DatastoreListDirOptions) error {
    99  		dsObj, err := getDatastore(cli, args.DATACENTER, args.DSID)
   100  		if err != nil {
   101  			return err
   102  		}
   103  		ctx := context.Background()
   104  		fileList, err := dsObj.ListDir(ctx, args.DIR)
   105  		if err != nil {
   106  			return err
   107  		}
   108  		printutils.PrintInterfaceList(fileList, 0, 0, 0, []string{"Name", "Date", "Size"})
   109  		return nil
   110  	})
   111  
   112  	shellutils.R(&DatastoreListDirOptions{}, "ds-check-file", "Check file status in a datastore", func(cli *esxi.SESXiClient, args *DatastoreListDirOptions) error {
   113  		dsObj, err := getDatastore(cli, args.DATACENTER, args.DSID)
   114  		if err != nil {
   115  			return err
   116  		}
   117  		ctx := context.Background()
   118  		file, err := dsObj.CheckFile(ctx, args.DIR)
   119  		if err != nil {
   120  			return err
   121  		}
   122  		printutils.PrintInterfaceObject(file)
   123  		return nil
   124  	})
   125  
   126  	shellutils.R(&DatastoreListDirOptions{}, "ds-delete-file", "Delete file in a datastore", func(cli *esxi.SESXiClient, args *DatastoreListDirOptions) error {
   127  		dsObj, err := getDatastore(cli, args.DATACENTER, args.DSID)
   128  		if err != nil {
   129  			return err
   130  		}
   131  		ctx := context.Background()
   132  		err = dsObj.Delete(ctx, args.DIR)
   133  		if err != nil {
   134  			return err
   135  		}
   136  		fmt.Println("success")
   137  		return nil
   138  	})
   139  
   140  	shellutils.R(&DatastoreListDirOptions{}, "ds-check-vmdk", "Check vmdk file status in a datastore", func(cli *esxi.SESXiClient, args *DatastoreListDirOptions) error {
   141  		dsObj, err := getDatastore(cli, args.DATACENTER, args.DSID)
   142  		if err != nil {
   143  			return err
   144  		}
   145  		ctx := context.Background()
   146  		err = dsObj.CheckVmdk(ctx, args.DIR)
   147  		if err != nil {
   148  			return err
   149  		}
   150  		fmt.Println("valid")
   151  		return nil
   152  	})
   153  
   154  	shellutils.R(&DatastoreListDirOptions{}, "ds-delete-vmdk", "Delete vmdk file from a datastore", func(cli *esxi.SESXiClient, args *DatastoreListDirOptions) error {
   155  		dsObj, err := getDatastore(cli, args.DATACENTER, args.DSID)
   156  		if err != nil {
   157  			return err
   158  		}
   159  		ctx := context.Background()
   160  		err = dsObj.DeleteVmdk(ctx, args.DIR)
   161  		if err != nil {
   162  			return err
   163  		}
   164  		fmt.Println("success")
   165  		return nil
   166  	})
   167  
   168  	shellutils.R(&DatastoreListDirOptions{}, "ds-mkdir", "Delete vmdk directory from a datastore", func(cli *esxi.SESXiClient, args *DatastoreListDirOptions) error {
   169  		dsObj, err := getDatastore(cli, args.DATACENTER, args.DSID)
   170  		if err != nil {
   171  			return err
   172  		}
   173  		err = dsObj.MakeDir(args.DIR)
   174  		if err != nil {
   175  			return err
   176  		}
   177  		fmt.Println("Make dir success")
   178  		return nil
   179  	})
   180  
   181  	shellutils.R(&DatastoreListDirOptions{}, "ds-rmdir", "Remove vmdk directory from a datastore", func(cli *esxi.SESXiClient, args *DatastoreListDirOptions) error {
   182  		dsObj, err := getDatastore(cli, args.DATACENTER, args.DSID)
   183  		if err != nil {
   184  			return err
   185  		}
   186  		ctx := context.Background()
   187  		err = dsObj.RemoveDir(ctx, args.DIR)
   188  		if err != nil {
   189  			return err
   190  		}
   191  		fmt.Println("Remove dir success")
   192  		return nil
   193  	})
   194  
   195  	type DatastoreDownloadOptions struct {
   196  		DATACENTER string `help:"Datacenter"`
   197  		DSID       string `help:"Datastore ID"`
   198  		DIR        string `help:"directory"`
   199  		LOCAL      string `help:"local file"`
   200  	}
   201  	shellutils.R(&DatastoreDownloadOptions{}, "ds-download", "Download file from a datastore", func(cli *esxi.SESXiClient, args *DatastoreDownloadOptions) error {
   202  		dc, err := cli.FindDatacenterByMoId(args.DATACENTER)
   203  		if err != nil {
   204  			return err
   205  		}
   206  		ds, err := dc.GetIStorageByMoId(args.DSID)
   207  		if err != nil {
   208  			return err
   209  		}
   210  		ctx := context.Background()
   211  		dsObj := ds.(*esxi.SDatastore)
   212  
   213  		file, err := os.Create(args.LOCAL)
   214  		if err != nil {
   215  			return err
   216  		}
   217  		defer file.Close()
   218  
   219  		err = dsObj.Download(ctx, args.DIR, file)
   220  		if err != nil {
   221  			return err
   222  		}
   223  
   224  		return nil
   225  	})
   226  
   227  	shellutils.R(&DatastoreDownloadOptions{}, "ds-upload", "Upload local file to datastore", func(cli *esxi.SESXiClient, args *DatastoreDownloadOptions) error {
   228  		dc, err := cli.FindDatacenterByMoId(args.DATACENTER)
   229  		if err != nil {
   230  			return err
   231  		}
   232  		ds, err := dc.GetIStorageByMoId(args.DSID)
   233  		if err != nil {
   234  			return err
   235  		}
   236  		ctx := context.Background()
   237  		dsObj := ds.(*esxi.SDatastore)
   238  
   239  		file, err := os.Open(args.LOCAL)
   240  		if err != nil {
   241  			return err
   242  		}
   243  		defer file.Close()
   244  
   245  		err = dsObj.Upload(ctx, args.DIR, file)
   246  		if err != nil {
   247  			return err
   248  		}
   249  
   250  		return nil
   251  	})
   252  
   253  }