Skip to content

Commit

Permalink
Add cluster.group and cluster.rule commands (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm authored Nov 21, 2017
1 parent 2a8a516 commit 3be5f1d
Show file tree
Hide file tree
Showing 20 changed files with 1,566 additions and 49 deletions.
21 changes: 21 additions & 0 deletions find/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,15 @@ func (f *Finder) ClusterComputeResourceList(ctx context.Context, path string) ([
return ccrs, nil
}

func (f *Finder) DefaultClusterComputeResource(ctx context.Context) (*object.ClusterComputeResource, error) {
cr, err := f.ClusterComputeResource(ctx, "*")
if err != nil {
return nil, toDefaultError(err)
}

return cr, nil
}

func (f *Finder) ClusterComputeResource(ctx context.Context, path string) (*object.ClusterComputeResource, error) {
ccrs, err := f.ClusterComputeResourceList(ctx, path)
if err != nil {
Expand All @@ -638,6 +647,18 @@ func (f *Finder) ClusterComputeResource(ctx context.Context, path string) (*obje
return ccrs[0], nil
}

func (f *Finder) ClusterComputeResourceOrDefault(ctx context.Context, path string) (*object.ClusterComputeResource, error) {
if path != "" {
cr, err := f.ClusterComputeResource(ctx, path)
if err != nil {
return nil, err
}
return cr, nil
}

return f.DefaultClusterComputeResource(ctx)
}

func (f *Finder) HostSystemList(ctx context.Context, path string) ([]*object.HostSystem, error) {
s := &spec{
Relative: f.hostFolder,
Expand Down
172 changes: 171 additions & 1 deletion govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Examples:
govc cluster.add -cluster ClusterB -hostname 10.0.6.1 -username root -password pass -noverify
Options:
-cluster=* Path to cluster
-cluster= Cluster [GOVC_CLUSTER]
-connect=true Immediately connect to host
-force=false Force when host is managed by another VC
-hostname= Hostname or IP address of the host
Expand Down Expand Up @@ -131,6 +131,158 @@ Options:
-folder= Inventory folder [GOVC_FOLDER]
```

## cluster.group.change

```
Usage: govc cluster.group.change [OPTIONS] NAME...
Set cluster group members.
Examples:
govc cluster.group.change -name my_group vm_a vm_b vm_c # set
govc cluster.group.change -name my_group vm_a vm_b vm_c $(govc cluster.group.ls -name my_group) vm_d # add
govc cluster.group.ls -name my_group | grep -v vm_b | xargs govc cluster.group.change -name my_group vm_a vm_b vm_c # remove
Options:
-cluster= Cluster [GOVC_CLUSTER]
-name= Cluster group name
```

## cluster.group.create

```
Usage: govc cluster.group.create [OPTIONS]
Create cluster group.
One of '-vm' or '-host' must be provided to specify the group type.
Examples:
govc cluster.group.create -name my_vm_group -vm vm_a vm_b vm_c
govc cluster.group.create -name my_host_group -host host_a host_b host_c
Options:
-cluster= Cluster [GOVC_CLUSTER]
-host=false Create cluster Host group
-name= Cluster group name
-vm=false Create cluster VM group
```

## cluster.group.ls

```
Usage: govc cluster.group.ls [OPTIONS]
List cluster groups and group members.
Examples:
govc cluster.group.ls -cluster my_cluster
govc cluster.group.ls -cluster my_cluster -name my_group
Options:
-cluster= Cluster [GOVC_CLUSTER]
-name= Cluster group name
```

## cluster.group.remove

```
Usage: govc cluster.group.remove [OPTIONS]
Remove cluster group.
Examples:
govc cluster.group.remove -cluster my_cluster -name my_group
Options:
-cluster= Cluster [GOVC_CLUSTER]
-name= Cluster group name
```

## cluster.rule.change

```
Usage: govc cluster.rule.change [OPTIONS] NAME...
Change cluster rule.
Examples:
govc cluster.rule.change -cluster my_cluster -name my_rule -enable=false
Options:
-cluster= Cluster [GOVC_CLUSTER]
-enable=<nil> Enable rule
-host-affine-group= Host affine group name
-host-anti-affine-group= Host anti-affine group name
-mandatory=<nil> Enforce rule compliance
-name= Cluster rule name
-vm-group= VM group name
```

## cluster.rule.create

```
Usage: govc cluster.rule.create [OPTIONS] NAME...
Create cluster rule.
Rules are not enabled by default, use the 'enable' flag to enable upon creation or cluster.rule.change after creation.
One of '-affinity', '-anti-affinity' or '-vm-host' must be provided to specify the rule type.
With '-affinity' or '-anti-affinity', at least 2 vm NAME arguments must be specified.
With '-vm-host', use the '-vm-group' flag combined with the '-host-affine-group' and/or '-host-anti-affine-group' flags.
Examples:
govc cluster.rule.create -name pod1 -enable -affinity vm_a vm_b vm_c
govc cluster.rule.create -name pod2 -enable -anti-affinity vm_d vm_e vm_f
govc cluster.rule.create -name pod3 -enable -mandatory -vm-host -vm-group my_vms -host-affine-group my_hosts
Options:
-affinity=false Keep Virtual Machines Together
-anti-affinity=false Separate Virtual Machines
-cluster= Cluster [GOVC_CLUSTER]
-enable=<nil> Enable rule
-host-affine-group= Host affine group name
-host-anti-affine-group= Host anti-affine group name
-mandatory=<nil> Enforce rule compliance
-name= Cluster rule name
-vm-group= VM group name
-vm-host=false Virtual Machines to Hosts
```

## cluster.rule.ls

```
Usage: govc cluster.rule.ls [OPTIONS]
List cluster rules and rule members.
Examples:
govc cluster.rule.ls -cluster my_cluster
govc cluster.rule.ls -cluster my_cluster -name my_rule
Options:
-cluster= Cluster [GOVC_CLUSTER]
-name= Cluster rule name
```

## cluster.rule.remove

```
Usage: govc cluster.rule.remove [OPTIONS]
Remove cluster rule.
Examples:
govc cluster.group.remove -cluster my_cluster -name my_rule
Options:
-cluster= Cluster [GOVC_CLUSTER]
-name= Cluster rule name
```

## datacenter.create

```
Expand Down Expand Up @@ -200,6 +352,7 @@ Create VMDK on DS.
Examples:
govc datastore.mkdir disks
govc datastore.disk.create -size 24G disks/disk1.vmdk
govc datastore.disk.create disks/parent.vmdk disk/child.vmdk
Options:
-ds= Datastore [GOVC_DATASTORE]
Expand Down Expand Up @@ -3178,6 +3331,23 @@ Remove VM from inventory without removing any of the VM files on disk.
Options:
```

## vm.upgrade

```
Usage: govc vm.upgrade [OPTIONS]
Upgrade VMs to latest hardware version
Examples:
govc vm.upgrade -vm $vm_name
govc vm.upgrade -version=$version -vm $vm_name
govc vm.upgrade -version=$version -vm.uuid $vm_uuid
Options:
-version=0 Target vm hardware version, by default -- latest available
-vm= Virtual machine [GOVC_VM]
```

## vm.vnc

```
Expand Down
18 changes: 5 additions & 13 deletions govc/cluster/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import (
)

type add struct {
*flags.DatacenterFlag
*flags.ClusterFlag
*flags.HostConnectFlag

cluster string
connect bool
license string
}
Expand All @@ -40,21 +39,19 @@ func init() {
}

func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
cmd.DatacenterFlag.Register(ctx, f)
cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx)
cmd.ClusterFlag.Register(ctx, f)

cmd.HostConnectFlag, ctx = flags.NewHostConnectFlag(ctx)
cmd.HostConnectFlag.Register(ctx, f)

f.StringVar(&cmd.cluster, "cluster", "*", "Path to cluster")

f.StringVar(&cmd.license, "license", "", "Assign license key")

f.BoolVar(&cmd.connect, "connect", true, "Immediately connect to host")
}

func (cmd *add) Process(ctx context.Context) error {
if err := cmd.DatacenterFlag.Process(ctx); err != nil {
if err := cmd.ClusterFlag.Process(ctx); err != nil {
return err
}
if err := cmd.HostConnectFlag.Process(ctx); err != nil {
Expand Down Expand Up @@ -108,12 +105,7 @@ func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
return flag.ErrHelp
}

finder, err := cmd.Finder()
if err != nil {
return err
}

cluster, err := finder.ClusterComputeResource(ctx, cmd.cluster)
cluster, err := cmd.Cluster()
if err != nil {
return err
}
Expand Down
75 changes: 75 additions & 0 deletions govc/cluster/group/change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
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 group

import (
"context"
"flag"

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

type change struct {
*InfoFlag
}

func init() {
cli.Register("cluster.group.change", &change{})
}

func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
cmd.InfoFlag, ctx = NewInfoFlag(ctx)
cmd.InfoFlag.Register(ctx, f)
}

func (cmd *change) Process(ctx context.Context) error {
if cmd.name == "" {
return flag.ErrHelp
}
return cmd.InfoFlag.Process(ctx)
}

func (cmd *change) Usage() string {
return `NAME...`
}

func (cmd *change) Description() string {
return `Set cluster group members.
Examples:
govc cluster.group.change -name my_group vm_a vm_b vm_c # set
govc cluster.group.change -name my_group vm_a vm_b vm_c $(govc cluster.group.ls -name my_group) vm_d # add
govc cluster.group.ls -name my_group | grep -v vm_b | xargs govc cluster.group.change -name my_group vm_a vm_b vm_c # remove`
}

func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
update := types.ArrayUpdateSpec{Operation: types.ArrayUpdateOperationEdit}
group, err := cmd.Group(ctx)
if err != nil {
return err
}

refs, err := cmd.ObjectList(ctx, group.kind, f.Args())
if err != nil {
return err
}

*group.refs = refs

return cmd.Apply(ctx, update, group.info)
}
Loading

0 comments on commit 3be5f1d

Please sign in to comment.