github.com/vmware/govmomi@v0.43.0/govc/vcsa/shutdown/poweroff.go (about) 1 /* 2 Copyright (c) 2022 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package shutdown 18 19 import ( 20 "context" 21 "flag" 22 23 "github.com/vmware/govmomi/govc/cli" 24 "github.com/vmware/govmomi/govc/flags" 25 "github.com/vmware/govmomi/vapi/appliance/shutdown" 26 ) 27 28 type powerOff struct { 29 *flags.ClientFlag 30 31 reason string 32 delay int // in minutes 33 } 34 35 func init() { 36 cli.Register("vcsa.shutdown.poweroff", &powerOff{}) 37 } 38 39 func (cmd *powerOff) Register(ctx context.Context, f *flag.FlagSet) { 40 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 41 cmd.ClientFlag.Register(ctx, f) 42 43 f.IntVar(&cmd.delay, 44 "delay", 45 0, 46 "Minutes after which poweroff should start.") 47 } 48 49 func (cmd *powerOff) Usage() string { 50 return "REASON" 51 } 52 53 func (cmd *powerOff) Description() string { 54 return `Power off the appliance. 55 56 Note: This command requires vCenter 7.0.2 or higher. 57 58 Examples: 59 govc vcsa.shutdown.poweroff -delay 10 "powering off for maintenance"` 60 } 61 62 func (cmd *powerOff) Run(ctx context.Context, f *flag.FlagSet) error { 63 if f.NArg() != 1 { 64 return flag.ErrHelp 65 } 66 67 c, err := cmd.RestClient() 68 if err != nil { 69 return err 70 } 71 72 m := shutdown.NewManager(c) 73 74 if err = m.PowerOff(ctx, f.Arg(0), cmd.delay); err != nil { 75 return err 76 } 77 78 return nil 79 }