Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 | package commands |
| 17 | |
| 18 | import ( |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 19 | "bufio" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 20 | b64 "encoding/base64" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 21 | "fmt" |
| 22 | "github.com/fullstorydev/grpcurl" |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 23 | versionUtils "github.com/hashicorp/go-version" |
| 24 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 25 | "github.com/jhump/protoreflect/grpcreflect" |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame^] | 26 | corderrors "github.com/opencord/cordctl/error" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 27 | "golang.org/x/net/context" |
| 28 | "google.golang.org/grpc" |
| 29 | reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 30 | "google.golang.org/grpc/status" |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 31 | "log" |
| 32 | "os" |
| 33 | "strings" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 36 | // Flags for calling the InitReflectionClient Method |
| 37 | const ( |
| 38 | INIT_DEFAULT = 0 |
| 39 | INIT_NO_VERSION_CHECK = 1 // Do not check whether server is allowed version |
| 40 | ) |
| 41 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 42 | func GenerateHeaders() []string { |
| 43 | username := GlobalConfig.Username |
| 44 | password := GlobalConfig.Password |
| 45 | sEnc := b64.StdEncoding.EncodeToString([]byte(username + ":" + password)) |
| 46 | headers := []string{"authorization: basic " + sEnc} |
| 47 | return headers |
| 48 | } |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 49 | |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 50 | // Perform the GetVersion API call on the core to get the version |
| 51 | func GetVersion(conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource) (*dynamic.Message, error) { |
| 52 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 53 | defer cancel() |
| 54 | |
| 55 | headers := GenerateHeaders() |
| 56 | |
| 57 | h := &RpcEventHandler{} |
| 58 | err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetVersion", headers, h, h.GetParams) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | |
| 63 | if h.Status != nil && h.Status.Err() != nil { |
| 64 | return nil, h.Status.Err() |
| 65 | } |
| 66 | |
| 67 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 68 | |
| 69 | return d, err |
| 70 | } |
| 71 | |
| 72 | // Initialize client connection |
| 73 | // flags is a set of optional flags that may influence how the connection is setup |
| 74 | // INIT_DEFAULT - default behavior (0) |
| 75 | // INIT_NO_VERSION_CHECK - do not perform core version check |
| 76 | |
| 77 | func InitClient(flags uint32) (*grpc.ClientConn, grpcurl.DescriptorSource, error) { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 78 | conn, err := NewConnection() |
| 79 | if err != nil { |
| 80 | return nil, nil, err |
| 81 | } |
| 82 | |
| 83 | refClient := grpcreflect.NewClient(context.Background(), reflectpb.NewServerReflectionClient(conn)) |
| 84 | defer refClient.Reset() |
| 85 | |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 86 | // Intended method of use is to download the protos via reflection API. Loading the |
| 87 | // protos from a file is supported for unit testing, as the mock server does not |
| 88 | // support the reflection API. |
| 89 | |
| 90 | var descriptor grpcurl.DescriptorSource |
| 91 | if GlobalConfig.Protoset != "" { |
| 92 | descriptor, err = grpcurl.DescriptorSourceFromProtoSets(GlobalConfig.Protoset) |
| 93 | if err != nil { |
| 94 | return nil, nil, err |
| 95 | } |
| 96 | } else { |
| 97 | descriptor = grpcurl.DescriptorSourceFromServer(context.Background(), refClient) |
| 98 | } |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 99 | |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 100 | if flags&INIT_NO_VERSION_CHECK == 0 { |
| 101 | d, err := GetVersion(conn, descriptor) |
| 102 | if err != nil { |
| 103 | return nil, nil, err |
| 104 | } |
| 105 | // Note: NewVersion doesn't like the `-dev` suffix, so strip it off. |
| 106 | serverVersion, err := versionUtils.NewVersion(strings.Split(d.GetFieldByName("version").(string), "-")[0]) |
| 107 | if err != nil { |
| 108 | return nil, nil, err |
| 109 | } |
| 110 | |
| 111 | constraint, err := versionUtils.NewConstraint(CORE_VERSION_CONSTRAINT) |
| 112 | if err != nil { |
| 113 | return nil, nil, err |
| 114 | } |
| 115 | |
| 116 | if !constraint.Check(serverVersion) { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame^] | 117 | return nil, nil, corderrors.WithStackTrace(&corderrors.VersionConstraintError{ |
| 118 | Name: "xos-core", |
| 119 | Version: serverVersion.String(), |
| 120 | Constraint: CORE_VERSION_CONSTRAINT}) |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } |
| 124 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 125 | return conn, descriptor, nil |
| 126 | } |
| 127 | |
| 128 | // A makeshift substitute for C's Ternary operator |
| 129 | func Ternary_uint32(condition bool, value_true uint32, value_false uint32) uint32 { |
| 130 | if condition { |
| 131 | return value_true |
| 132 | } else { |
| 133 | return value_false |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // call printf only if visible is True |
| 138 | func conditional_printf(visible bool, format string, args ...interface{}) { |
| 139 | if visible { |
| 140 | fmt.Printf(format, args...) |
| 141 | } |
| 142 | } |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 143 | |
| 144 | // Print a confirmation prompt and get a response from the user |
| 145 | func Confirmf(format string, args ...interface{}) bool { |
| 146 | if GlobalOptions.Yes { |
| 147 | return true |
| 148 | } |
| 149 | |
| 150 | reader := bufio.NewReader(os.Stdin) |
| 151 | |
| 152 | for { |
| 153 | msg := fmt.Sprintf(format, args...) |
| 154 | fmt.Print(msg) |
| 155 | |
| 156 | response, err := reader.ReadString('\n') |
| 157 | if err != nil { |
| 158 | log.Fatal(err) |
| 159 | } |
| 160 | |
| 161 | response = strings.ToLower(strings.TrimSpace(response)) |
| 162 | |
| 163 | if response == "y" || response == "yes" { |
| 164 | return true |
| 165 | } else if response == "n" || response == "no" { |
| 166 | return false |
| 167 | } |
| 168 | } |
| 169 | } |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 170 | |
| 171 | func HumanReadableError(err error) string { |
| 172 | st, ok := status.FromError(err) |
| 173 | if ok { |
| 174 | grpc_message := st.Message() |
| 175 | if strings.HasPrefix(grpc_message, "Exception calling application: ") { |
| 176 | return st.Message()[31:] |
| 177 | } else { |
| 178 | return st.Message() |
| 179 | } |
| 180 | } |
| 181 | return err.Error() |
| 182 | } |