vitess.io/vitess@v0.16.2/go/vt/wrangler/version.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     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 wrangler
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  
    25  	"context"
    26  
    27  	"vitess.io/vitess/go/vt/log"
    28  	"vitess.io/vitess/go/vt/topo/topoproto"
    29  
    30  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    31  	vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
    32  )
    33  
    34  var getVersionFromTabletDebugVars = func(tabletAddr string) (string, error) {
    35  	resp, err := http.Get("http://" + tabletAddr + "/debug/vars")
    36  	if err != nil {
    37  		return "", err
    38  	}
    39  	defer resp.Body.Close()
    40  	body, err := io.ReadAll(resp.Body)
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  
    45  	var vars struct {
    46  		BuildHost      string
    47  		BuildUser      string
    48  		BuildTimestamp int64
    49  		BuildGitRev    string
    50  	}
    51  	err = json.Unmarshal(body, &vars)
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  
    56  	version := fmt.Sprintf("%v", vars)
    57  	return version, nil
    58  }
    59  
    60  var getVersionFromTablet = getVersionFromTabletDebugVars
    61  
    62  // ResetDebugVarsGetVersion is used by tests to reset the
    63  // getVersionFromTablet variable to the default one. That way we can
    64  // run the unit tests in testlib/ even when another implementation of
    65  // getVersionFromTablet is used.
    66  func ResetDebugVarsGetVersion() {
    67  	getVersionFromTablet = getVersionFromTabletDebugVars
    68  }
    69  
    70  // GetVersion returns the version string from a tablet
    71  func (wr *Wrangler) GetVersion(ctx context.Context, tabletAlias *topodatapb.TabletAlias) (string, error) {
    72  	resp, err := wr.VtctldServer().GetVersion(ctx, &vtctldatapb.GetVersionRequest{
    73  		TabletAlias: tabletAlias,
    74  	})
    75  	log.Infof("Tablet %v is running version '%v'", topoproto.TabletAliasString(tabletAlias), resp.Version)
    76  	return resp.Version, err
    77  }
    78  
    79  // ValidateVersionShard validates all versions are the same in all
    80  // tablets in a shard
    81  func (wr *Wrangler) ValidateVersionShard(ctx context.Context, keyspace, shard string) error {
    82  	res, err := wr.VtctldServer().ValidateVersionShard(ctx, &vtctldatapb.ValidateVersionShardRequest{
    83  		Keyspace: keyspace,
    84  		Shard:    shard,
    85  	})
    86  
    87  	if len(res.Results) > 0 {
    88  		return fmt.Errorf("version diffs: %v", res.Results)
    89  	}
    90  	return err
    91  }
    92  
    93  // ValidateVersionKeyspace validates all versions are the same in all
    94  // tablets in a keyspace
    95  func (wr *Wrangler) ValidateVersionKeyspace(ctx context.Context, keyspace string) error {
    96  	res, err := wr.VtctldServer().ValidateVersionKeyspace(ctx, &vtctldatapb.ValidateVersionKeyspaceRequest{
    97  		Keyspace: keyspace,
    98  	})
    99  
   100  	if len(res.Results) > 0 {
   101  		return fmt.Errorf("version diffs: %v", res.Results)
   102  	}
   103  	return err
   104  }