blob: 44d72138d4ba7f4b67597624f4c4c196088192e9 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package commands
18
19import (
Scott Baker6cf525a2019-05-09 12:25:08 -070020 "context"
Matteo Scandolo722930f2019-05-21 15:55:31 -070021
Scott Baker6cf525a2019-05-09 12:25:08 -070022 "github.com/fullstorydev/grpcurl"
Scott Baker2c0ebda2019-05-06 16:55:47 -070023 flags "github.com/jessevdk/go-flags"
Scott Baker6cf525a2019-05-09 12:25:08 -070024 "github.com/jhump/protoreflect/dynamic"
Scott Baker2c0ebda2019-05-06 16:55:47 -070025 "github.com/opencord/cordctl/cli/version"
26 "github.com/opencord/cordctl/format"
27)
28
29type VersionDetails struct {
30 Version string `json:"version"`
31 GoVersion string `json:"goversion"`
32 GitCommit string `json:"gitcommit"`
Scott Baker63ce82e2019-05-15 09:01:42 -070033 GitDirty string `json:"gitdirty"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070034 BuildTime string `json:"buildtime"`
35 Os string `json:"os"`
36 Arch string `json:"arch"`
37}
38
Scott Baker6cf525a2019-05-09 12:25:08 -070039type CoreVersionDetails struct {
40 Version string `json:"version"`
41 PythonVersion string `json:"goversion"`
42 GitCommit string `json:"gitcommit"`
43 BuildTime string `json:"buildtime"`
44 Os string `json:"os"`
45 Arch string `json:"arch"`
Scott Baker87efa392019-06-18 17:09:46 -070046 DjangoVersion string `json:"djangoversion"`
Scott Baker6cf525a2019-05-09 12:25:08 -070047}
48
Scott Baker2c0ebda2019-05-06 16:55:47 -070049type VersionOutput struct {
Scott Baker6cf525a2019-05-09 12:25:08 -070050 Client VersionDetails `json:"client"`
51 Server CoreVersionDetails `json:"server"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070052}
53
54type VersionOpts struct {
Matteo Scandolo722930f2019-05-21 15:55:31 -070055 OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
56 ClientOnly bool `short:"c" long:"client-only" description:"Print only client version"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070057}
58
59var versionOpts = VersionOpts{}
60
61var versionInfo = VersionOutput{
62 Client: VersionDetails{
63 Version: version.Version,
64 GoVersion: version.GoVersion,
65 GitCommit: version.GitCommit,
Scott Baker63ce82e2019-05-15 09:01:42 -070066 GitDirty: version.GitDirty,
Scott Baker2c0ebda2019-05-06 16:55:47 -070067 Os: version.Os,
68 Arch: version.Arch,
69 BuildTime: version.BuildTime,
70 },
Scott Baker6cf525a2019-05-09 12:25:08 -070071 Server: CoreVersionDetails{
72 Version: "unknown",
73 PythonVersion: "unknown",
74 GitCommit: "unknown",
75 Os: "unknown",
76 Arch: "unknown",
77 BuildTime: "unknown",
Scott Baker87efa392019-06-18 17:09:46 -070078 DjangoVersion: "unknown",
Scott Baker6cf525a2019-05-09 12:25:08 -070079 },
Scott Baker2c0ebda2019-05-06 16:55:47 -070080}
81
82func RegisterVersionCommands(parent *flags.Parser) {
83 parent.AddCommand("version", "display version", "Display client version", &versionOpts)
84}
85
Matteo Scandolo722930f2019-05-21 15:55:31 -070086const ClientFormat = `Client:
Scott Baker6cf525a2019-05-09 12:25:08 -070087 Version {{.Client.Version}}
88 Go version: {{.Client.GoVersion}}
89 Git commit: {{.Client.GitCommit}}
Scott Baker63ce82e2019-05-15 09:01:42 -070090 Git dirty: {{.Client.GitDirty}}
Scott Baker6cf525a2019-05-09 12:25:08 -070091 Built: {{.Client.BuildTime}}
92 OS/Arch: {{.Client.Os}}/{{.Client.Arch}}
Matteo Scandolo722930f2019-05-21 15:55:31 -070093`
94const ServerFormat = `
Scott Baker6cf525a2019-05-09 12:25:08 -070095Server:
96 Version {{.Server.Version}}
97 Python version: {{.Server.PythonVersion}}
Scott Baker87efa392019-06-18 17:09:46 -070098 Django version: {{.Server.DjangoVersion}}
Scott Baker6cf525a2019-05-09 12:25:08 -070099 Git commit: {{.Server.GitCommit}}
100 Built: {{.Server.BuildTime}}
101 OS/Arch: {{.Server.Os}}/{{.Server.Arch}}
Scott Baker2c0ebda2019-05-06 16:55:47 -0700102`
103
Matteo Scandolo722930f2019-05-21 15:55:31 -0700104const DefaultFormat = ClientFormat + ServerFormat
105
Scott Baker2c0ebda2019-05-06 16:55:47 -0700106func (options *VersionOpts) Execute(args []string) error {
Matteo Scandolo722930f2019-05-21 15:55:31 -0700107 if !options.ClientOnly {
Scott Baker14c8f182019-05-22 18:05:29 -0700108 conn, descriptor, err := InitReflectionClient()
Matteo Scandolo722930f2019-05-21 15:55:31 -0700109 if err != nil {
110 return err
111 }
112 defer conn.Close()
113
Matteo Scandolo722930f2019-05-21 15:55:31 -0700114 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
115 defer cancel()
116
117 headers := GenerateHeaders()
118
119 h := &RpcEventHandler{}
Scott Baker14c8f182019-05-22 18:05:29 -0700120 err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetVersion", headers, h, h.GetParams)
Matteo Scandolo722930f2019-05-21 15:55:31 -0700121 if err != nil {
122 return err
123 }
124
125 if h.Status != nil && h.Status.Err() != nil {
126 return h.Status.Err()
127 }
128
129 d, err := dynamic.AsDynamicMessage(h.Response)
130 if err != nil {
131 return err
132 }
133
134 versionInfo.Server.Version = d.GetFieldByName("version").(string)
135 versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string)
136 versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string)
137 versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string)
138 versionInfo.Server.Os = d.GetFieldByName("os").(string)
139 versionInfo.Server.Arch = d.GetFieldByName("arch").(string)
Scott Baker87efa392019-06-18 17:09:46 -0700140
141 // djangoVersion was added to GetVersion() in xos-core 3.3.1-dev
142 djangoVersion, err := d.TryGetFieldByName("djangoVersion")
143 if err == nil {
144 versionInfo.Server.DjangoVersion = djangoVersion.(string)
145 }
Scott Baker6cf525a2019-05-09 12:25:08 -0700146 }
Scott Baker6cf525a2019-05-09 12:25:08 -0700147
Scott Baker2c0ebda2019-05-06 16:55:47 -0700148 result := CommandResult{
Matteo Scandolo722930f2019-05-21 15:55:31 -0700149 // Format: format.Format(DefaultFormat),
Scott Baker2c0ebda2019-05-06 16:55:47 -0700150 OutputAs: toOutputType(options.OutputAs),
151 Data: versionInfo,
152 }
153
Matteo Scandolo722930f2019-05-21 15:55:31 -0700154 if options.ClientOnly {
155 result.Format = format.Format(ClientFormat)
156 } else {
157 result.Format = format.Format(DefaultFormat)
158 }
159
Scott Baker2c0ebda2019-05-06 16:55:47 -0700160 GenerateOutput(&result)
161 return nil
162}