github.com/vmware/govmomi@v0.37.1/govc/namespace/cluster/logs.go (about) 1 /* 2 Copyright (c) 2020 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 cluster 18 19 import ( 20 "context" 21 "flag" 22 "io" 23 "mime" 24 "net/http" 25 "os" 26 27 "github.com/vmware/govmomi/govc/cli" 28 "github.com/vmware/govmomi/govc/flags" 29 "github.com/vmware/govmomi/vapi/namespace" 30 ) 31 32 type logs struct { 33 *flags.ClusterFlag 34 } 35 36 func init() { 37 cli.Register("namespace.logs.download", &logs{}) 38 } 39 40 func (cmd *logs) Register(ctx context.Context, f *flag.FlagSet) { 41 cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx) 42 cmd.ClusterFlag.Register(ctx, f) 43 } 44 45 func (cmd *logs) Usage() string { 46 return "[NAME]" 47 } 48 49 func (cmd *logs) Description() string { 50 return `Download namespace cluster support bundle. 51 52 If NAME name is "-", bundle is written to stdout. 53 54 See also: govc logs.download 55 56 Examples: 57 govc namespace.logs.download -cluster k8s 58 govc namespace.logs.download -cluster k8s - | tar -xvf - 59 govc namespace.logs.download -cluster k8s logs.tar` 60 } 61 62 func (cmd *logs) Run(ctx context.Context, f *flag.FlagSet) error { 63 c, err := cmd.RestClient() 64 if err != nil { 65 return err 66 } 67 68 cluster, err := cmd.Cluster() 69 if err != nil { 70 return err 71 } 72 73 id := cluster.Reference().Value 74 75 name := f.Arg(0) 76 77 m := namespace.NewManager(c) 78 79 bundle, err := m.CreateSupportBundle(ctx, id) 80 if err != nil { 81 return err 82 } 83 84 req, err := m.SupportBundleRequest(ctx, bundle) 85 if err != nil { 86 return err 87 } 88 89 if id := c.SessionID(); id != "" { 90 req.Header.Set("vmware-api-session-id", id) 91 } 92 93 return c.Client.Do(ctx, req, func(res *http.Response) error { 94 if name == "" { 95 d := res.Header.Get("Content-Disposition") 96 _, params, err := mime.ParseMediaType(d) 97 if err == nil { 98 name = params["filename"] 99 } 100 } 101 102 var w io.Writer 103 104 if name == "-" { 105 w = os.Stdout 106 } else { 107 f, err := os.Create(name) 108 if err != nil { 109 return err 110 } 111 defer f.Close() 112 w = f 113 } 114 115 _, err = io.Copy(w, res.Body) 116 return err 117 }) 118 }