Skip to content

Commit

Permalink
Add vsan and disk commands / helpers (#672)
Browse files Browse the repository at this point in the history
* Add DatastoreFileManager API wrapper

* Add HostVsanInternalSystem API wrappers

* Add govc datastore.disk.create command

* Add govc datastore.vsan.dom.ls and datastore.vsan.dom.rm commands

* Use DatastoreFileManager in govc datastore.rm command
  • Loading branch information
dougm committed Feb 27, 2017
1 parent 1f82c28 commit f4a3ffe
Show file tree
Hide file tree
Showing 18 changed files with 872 additions and 35 deletions.
8 changes: 8 additions & 0 deletions govc/datastore/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func (cmd *cp) Usage() string {
return "SRC DST"
}

func (cmd *cp) Description() string {
return `Copy SRC to DST on DATASTORE.
Examples:
govc datastore.cp foo/foo.vmx foo/foo.vmx.old
govc datastore.cp -f my.vmx foo/foo.vmx`
}

func (cmd *cp) Run(ctx context.Context, f *flag.FlagSet) error {
args := f.Args()
if len(args) != 2 {
Expand Down
98 changes: 98 additions & 0 deletions govc/datastore/disk/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package disk

import (
"context"
"flag"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/units"
"github.com/vmware/govmomi/vim25/types"
)

type create struct {
*flags.DatastoreFlag

Bytes units.ByteSize
}

func init() {
cli.Register("datastore.disk.create", &create{})
}

func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
cmd.DatastoreFlag.Register(ctx, f)

_ = cmd.Bytes.Set("10G")
f.Var(&cmd.Bytes, "size", "Size of new disk")
}

func (cmd *create) Process(ctx context.Context) error {
if err := cmd.DatastoreFlag.Process(ctx); err != nil {
return err
}
return nil
}

func (cmd *create) Usage() string {
return "VMDK"
}

func (cmd *create) Description() string {
return `Create VMDK on DS.
Examples:
govc datastore.mkdir disks
govc datastore.disk.create -size 24G disks/disk1.vmdk`
}

func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() == 0 {
return flag.ErrHelp
}

dc, err := cmd.Datacenter()
if err != nil {
return err
}

ds, err := cmd.Datastore()
if err != nil {
return err
}

m := object.NewVirtualDiskManager(ds.Client())

spec := &types.FileBackedVirtualDiskSpec{
VirtualDiskSpec: types.VirtualDiskSpec{
AdapterType: string(types.VirtualDiskAdapterTypeLsiLogic),
DiskType: string(types.VirtualDiskTypeThin),
},
CapacityKb: int64(cmd.Bytes) / 1024,
}

task, err := m.CreateVirtualDisk(ctx, ds.Path(f.Arg(0)), dc, spec)
if err != nil {
return err
}

return task.Wait(ctx)
}
15 changes: 13 additions & 2 deletions govc/datastore/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ func (cmd *ls) Usage() string {
return "[FILE]..."
}

func isInvalid(err error) bool {
if f, ok := err.(types.HasFault); ok {
switch f.Fault().(type) {
case *types.InvalidArgument:
return true
}
}

return false
}

func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
ds, err := cmd.Datastore()
if err != nil {
Expand Down Expand Up @@ -113,7 +124,7 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
r, err := cmd.ListPath(b, arg, spec)
if err != nil {
// Treat the argument as a match pattern if not found as directory
if i == 0 && types.IsFileNotFound(err) {
if i == 0 && types.IsFileNotFound(err) || isInvalid(err) {
spec.MatchPattern[0] = path.Base(arg)
arg = path.Dir(arg)
continue
Expand Down Expand Up @@ -187,7 +198,7 @@ func (o *listOutput) add(r types.HostDatastoreBrowserSearchResults) {
}

for _, p := range path {
if p[0] == '.' {
if len(p) != 0 && p[0] == '.' {
return
}
}
Expand Down
8 changes: 8 additions & 0 deletions govc/datastore/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ func (cmd *mv) Usage() string {
return "SRC DST"
}

func (cmd *mv) Description() string {
return `Move SRC to DST on DATASTORE.
Examples:
govc datastore.mv foo/foo.vmx foo/foo.vmx.old
govc datastore.mv -f my.vmx foo/foo.vmx`
}

func (cmd *mv) Run(ctx context.Context, f *flag.FlagSet) error {
args := f.Args()
if len(args) != 2 {
Expand Down
37 changes: 22 additions & 15 deletions govc/datastore/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package datastore

import (
"context"
"errors"
"flag"

"github.com/vmware/govmomi/govc/cli"
Expand All @@ -30,6 +29,7 @@ import (
type rm struct {
*flags.DatastoreFlag

kind bool
force bool
isNamespace bool
}
Expand All @@ -43,6 +43,7 @@ func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
cmd.DatastoreFlag.Register(ctx, f)

f.BoolVar(&cmd.kind, "t", true, "Use file type to choose disk or file manager")
f.BoolVar(&cmd.force, "f", false, "Force; ignore nonexistent files and arguments")
f.BoolVar(&cmd.isNamespace, "namespace", false, "Path is uuid of namespace on vsan datastore")
}
Expand All @@ -58,10 +59,19 @@ func (cmd *rm) Usage() string {
return "FILE"
}

func (cmd *rm) Description() string {
return `Remove FILE from DATASTORE.
Examples:
govc datastore.rm vm/vmware.log
govc datastore.rm vm
govc datastore.rm -f images/base.vmdk`
}

func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
args := f.Args()
if len(args) == 0 {
return errors.New("missing operand")
return flag.ErrHelp
}

c, err := cmd.Client()
Expand All @@ -75,28 +85,25 @@ func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}

ds, err := cmd.Datastore()
if err != nil {
return err
}

if cmd.isNamespace {
path := args[0]

nm := object.NewDatastoreNamespaceManager(c)
err = nm.DeleteDirectory(ctx, dc, path)
} else {
var path string
var task *object.Task

// TODO(PN): Accept multiple args
path, err = cmd.DatastorePath(args[0])
if err != nil {
return err
}
fm := ds.NewFileManager(dc, cmd.force)

m := object.NewFileManager(c)
task, err = m.DeleteDatastoreFile(ctx, path, dc)
if err != nil {
return err
remove := fm.DeleteFile // File delete
if cmd.kind {
remove = fm.Delete // VirtualDisk or File delete
}

err = task.Wait(ctx)
err = remove(ctx, args[0])
}

if err != nil {
Expand Down
Loading

1 comment on commit f4a3ffe

@linanjing1105
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we just miss vm.clone and vm.migrate

Please sign in to comment.