Debian Codegraph @

govc/namespace/cluster/logs.go

raw
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 return c.Client.Do(ctx, req, func(res *http.Response) error {
90 if name == "" {
91 d := res.Header.Get("Content-Disposition")
92 _, params, err := mime.ParseMediaType(d)
93 if err == nil {
94 name = params["filename"]
95 }
96 }
97
98 var w io.Writer
99
100 if name == "-" {
101 w = os.Stdout
102 } else {
103 f, err := os.Create(name)
104 if err != nil {
105 return err
106 }
107 defer f.Close()
108 w = f
109 }
110
111 _, err = io.Copy(w, res.Body)
112 return err
113 })
114 }