github.com/vmware/govmomi@v0.43.0/govc/host/service/command.go (about) 1 /* 2 Copyright (c) 2016 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 service 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 24 "github.com/vmware/govmomi/govc/cli" 25 "github.com/vmware/govmomi/govc/flags" 26 "github.com/vmware/govmomi/object" 27 "github.com/vmware/govmomi/vim25/types" 28 ) 29 30 type service struct { 31 *flags.ClientFlag 32 *flags.HostSystemFlag 33 } 34 35 func init() { 36 cli.Register("host.service", &service{}) 37 } 38 39 func (cmd *service) Register(ctx context.Context, f *flag.FlagSet) { 40 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 41 cmd.ClientFlag.Register(ctx, f) 42 43 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 44 cmd.HostSystemFlag.Register(ctx, f) 45 } 46 47 func (cmd *service) Process(ctx context.Context) error { 48 if err := cmd.ClientFlag.Process(ctx); err != nil { 49 return err 50 } 51 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 52 return err 53 } 54 return nil 55 } 56 57 func (cmd *service) Usage() string { 58 return "ACTION ID" 59 } 60 61 func (cmd *service) Description() string { 62 return `Apply host service ACTION to service ID. 63 64 Where ACTION is one of: start, stop, restart, status, enable, disable 65 66 Examples: 67 govc host.service enable TSM-SSH 68 govc host.service start TSM-SSH` 69 } 70 71 func (cmd *service) status(ctx context.Context, s *object.HostServiceSystem, id string) (string, error) { 72 services, err := s.Service(ctx) 73 if err != nil { 74 return "", err 75 } 76 77 for _, service := range services { 78 if id == service.Key { 79 return Status(service), nil 80 } 81 } 82 83 return "N/A", nil 84 } 85 86 func (cmd *service) Run(ctx context.Context, f *flag.FlagSet) error { 87 host, err := cmd.HostSystem() 88 if err != nil { 89 return err 90 } 91 92 if f.NArg() != 2 { 93 return flag.ErrHelp 94 } 95 96 arg := f.Arg(0) 97 id := f.Arg(1) 98 99 s, err := host.ConfigManager().ServiceSystem(ctx) 100 if err != nil { 101 return err 102 } 103 104 switch arg { 105 case "start": 106 return s.Start(ctx, id) 107 case "stop": 108 return s.Stop(ctx, id) 109 case "restart": 110 return s.Restart(ctx, id) 111 case "status": 112 ss, err := cmd.status(ctx, s, id) 113 if err != nil { 114 return err 115 } 116 fmt.Println(ss) 117 return nil 118 case "enable": 119 return s.UpdatePolicy(ctx, id, string(types.HostServicePolicyOn)) 120 case "disable": 121 return s.UpdatePolicy(ctx, id, string(types.HostServicePolicyOff)) 122 } 123 124 return flag.ErrHelp 125 }