Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 21 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 22 | "github.com/fullstorydev/grpcurl" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 23 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 24 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 25 | "github.com/opencord/cordctl/cli/version" |
| 26 | "github.com/opencord/cordctl/format" |
| 27 | ) |
| 28 | |
| 29 | type VersionDetails struct { |
| 30 | Version string `json:"version"` |
| 31 | GoVersion string `json:"goversion"` |
| 32 | GitCommit string `json:"gitcommit"` |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 33 | GitDirty string `json:"gitdirty"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 34 | BuildTime string `json:"buildtime"` |
| 35 | Os string `json:"os"` |
| 36 | Arch string `json:"arch"` |
| 37 | } |
| 38 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 39 | type 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 Baker | 87efa39 | 2019-06-18 17:09:46 -0700 | [diff] [blame^] | 46 | DjangoVersion string `json:"djangoversion"` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 49 | type VersionOutput struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 50 | Client VersionDetails `json:"client"` |
| 51 | Server CoreVersionDetails `json:"server"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | type VersionOpts struct { |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 55 | 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 Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | var versionOpts = VersionOpts{} |
| 60 | |
| 61 | var versionInfo = VersionOutput{ |
| 62 | Client: VersionDetails{ |
| 63 | Version: version.Version, |
| 64 | GoVersion: version.GoVersion, |
| 65 | GitCommit: version.GitCommit, |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 66 | GitDirty: version.GitDirty, |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 67 | Os: version.Os, |
| 68 | Arch: version.Arch, |
| 69 | BuildTime: version.BuildTime, |
| 70 | }, |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 71 | Server: CoreVersionDetails{ |
| 72 | Version: "unknown", |
| 73 | PythonVersion: "unknown", |
| 74 | GitCommit: "unknown", |
| 75 | Os: "unknown", |
| 76 | Arch: "unknown", |
| 77 | BuildTime: "unknown", |
Scott Baker | 87efa39 | 2019-06-18 17:09:46 -0700 | [diff] [blame^] | 78 | DjangoVersion: "unknown", |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 79 | }, |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func RegisterVersionCommands(parent *flags.Parser) { |
| 83 | parent.AddCommand("version", "display version", "Display client version", &versionOpts) |
| 84 | } |
| 85 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 86 | const ClientFormat = `Client: |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 87 | Version {{.Client.Version}} |
| 88 | Go version: {{.Client.GoVersion}} |
| 89 | Git commit: {{.Client.GitCommit}} |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 90 | Git dirty: {{.Client.GitDirty}} |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 91 | Built: {{.Client.BuildTime}} |
| 92 | OS/Arch: {{.Client.Os}}/{{.Client.Arch}} |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 93 | ` |
| 94 | const ServerFormat = ` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 95 | Server: |
| 96 | Version {{.Server.Version}} |
| 97 | Python version: {{.Server.PythonVersion}} |
Scott Baker | 87efa39 | 2019-06-18 17:09:46 -0700 | [diff] [blame^] | 98 | Django version: {{.Server.DjangoVersion}} |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 99 | Git commit: {{.Server.GitCommit}} |
| 100 | Built: {{.Server.BuildTime}} |
| 101 | OS/Arch: {{.Server.Os}}/{{.Server.Arch}} |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 102 | ` |
| 103 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 104 | const DefaultFormat = ClientFormat + ServerFormat |
| 105 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 106 | func (options *VersionOpts) Execute(args []string) error { |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 107 | if !options.ClientOnly { |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 108 | conn, descriptor, err := InitReflectionClient() |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | defer conn.Close() |
| 113 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 114 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 115 | defer cancel() |
| 116 | |
| 117 | headers := GenerateHeaders() |
| 118 | |
| 119 | h := &RpcEventHandler{} |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 120 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetVersion", headers, h, h.GetParams) |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 121 | 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 Baker | 87efa39 | 2019-06-18 17:09:46 -0700 | [diff] [blame^] | 140 | |
| 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 146 | } |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 147 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 148 | result := CommandResult{ |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 149 | // Format: format.Format(DefaultFormat), |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 150 | OutputAs: toOutputType(options.OutputAs), |
| 151 | Data: versionInfo, |
| 152 | } |
| 153 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 154 | if options.ClientOnly { |
| 155 | result.Format = format.Format(ClientFormat) |
| 156 | } else { |
| 157 | result.Format = format.Format(DefaultFormat) |
| 158 | } |
| 159 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 160 | GenerateOutput(&result) |
| 161 | return nil |
| 162 | } |