vitess.io/vitess@v0.16.2/go/cmd/vtctldclient/command/cells.go (about)

     1  /*
     2  Copyright 2021 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 command
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"vitess.io/vitess/go/cmd/vtctldclient/cli"
    26  
    27  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    28  	vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
    29  )
    30  
    31  var (
    32  	// AddCellInfo makes an AddCellInfo gRPC call to a vtctld.
    33  	AddCellInfo = &cobra.Command{
    34  		Use:   "AddCellInfo --root <root> [--server-address <addr>] <cell>",
    35  		Short: "Registers a local topology service in a new cell by creating the CellInfo.",
    36  		Long: `Registers a local topology service in a new cell by creating the CellInfo
    37  with the provided parameters.
    38  
    39  The address will be used to connect to the topology service, and Vitess data will
    40  be stored starting at the provided root.`,
    41  		DisableFlagsInUseLine: true,
    42  		Args:                  cobra.ExactArgs(1),
    43  		RunE:                  commandAddCellInfo,
    44  	}
    45  	// AddCellsAlias makes an AddCellsAlias gRPC call to a vtctld.
    46  	AddCellsAlias = &cobra.Command{
    47  		Use:   "AddCellsAlias --cells <cell1,cell2,...> [--cells <cell3> ...] <alias>",
    48  		Short: "Defines a group of cells that can be referenced by a single name (the alias).",
    49  		Long: `Defines a group of cells that can be referenced by a single name (the alias).
    50  
    51  When routing query traffic, replica/rdonly traffic can be routed across cells
    52  within the group (alias). Only primary traffic can be routed across cells not in
    53  the same group (alias).`,
    54  		DisableFlagsInUseLine: true,
    55  		Args:                  cobra.ExactArgs(1),
    56  		RunE:                  commandAddCellsAlias,
    57  	}
    58  	// DeleteCellInfo makes a DeleteCellInfo gRPC call to a vtctld.
    59  	DeleteCellInfo = &cobra.Command{
    60  		Use:                   "DeleteCellInfo [--force] <cell>",
    61  		Short:                 "Deletes the CellInfo for the provided cell.",
    62  		Long:                  "Deletes the CellInfo for the provided cell. The cell cannot be referenced by any Shard record.",
    63  		DisableFlagsInUseLine: true,
    64  		Args:                  cobra.ExactArgs(1),
    65  		RunE:                  commandDeleteCellInfo,
    66  	}
    67  	// DeleteCellsAlias makes a DeleteCellsAlias gRPC call to a vtctld.
    68  	DeleteCellsAlias = &cobra.Command{
    69  		Use:                   "DeleteCellsAlias <alias>",
    70  		Short:                 "Deletes the CellsAlias for the provided alias.",
    71  		Long:                  "Deletes the CellsAlias for the provided alias.",
    72  		DisableFlagsInUseLine: true,
    73  		Args:                  cobra.ExactArgs(1),
    74  		RunE:                  commandDeleteCellsAlias,
    75  	}
    76  	// GetCellInfoNames makes a GetCellInfoNames gRPC call to a vtctld.
    77  	GetCellInfoNames = &cobra.Command{
    78  		Use:                   "GetCellInfoNames",
    79  		Short:                 "Lists the names of all cells in the cluster.",
    80  		DisableFlagsInUseLine: true,
    81  		Args:                  cobra.NoArgs,
    82  		RunE:                  commandGetCellInfoNames,
    83  	}
    84  	// GetCellInfo makes a GetCellInfo gRPC call to a vtctld.
    85  	GetCellInfo = &cobra.Command{
    86  		Use:                   "GetCellInfo <cell>",
    87  		Short:                 "Gets the CellInfo object for the given cell.",
    88  		DisableFlagsInUseLine: true,
    89  		Args:                  cobra.ExactArgs(1),
    90  		RunE:                  commandGetCellInfo,
    91  	}
    92  	// GetCellsAliases makes a GetCellsAliases gRPC call to a vtctld.
    93  	GetCellsAliases = &cobra.Command{
    94  		Use:                   "GetCellsAliases",
    95  		Short:                 "Gets all CellsAlias objects in the cluster.",
    96  		DisableFlagsInUseLine: true,
    97  		Args:                  cobra.NoArgs,
    98  		RunE:                  commandGetCellsAliases,
    99  	}
   100  	// UpdateCellInfo makes an UpdateCellInfo gRPC call to a vtctld.
   101  	UpdateCellInfo = &cobra.Command{
   102  		Use:   "UpdateCellInfo [--root <root>] [--server-address <addr>] <cell>",
   103  		Short: "Updates the content of a CellInfo with the provided parameters, creating the CellInfo if it does not exist.",
   104  		Long: `Updates the content of a CellInfo with the provided parameters, creating the CellInfo if it does not exist.
   105  
   106  If a value is empty, it is ignored.`,
   107  		DisableFlagsInUseLine: true,
   108  		Args:                  cobra.ExactArgs(1),
   109  		RunE:                  commandUpdateCellInfo,
   110  	}
   111  	// UpdateCellsAlias makes an UpdateCellsAlias gRPC call to a vtctld.
   112  	UpdateCellsAlias = &cobra.Command{
   113  		Use:                   "UpdateCellsAlias [--cells <cell1,cell2,...> [--cells <cell4> ...]] <alias>",
   114  		Short:                 "Updates the content of a CellsAlias with the provided parameters, creating the CellsAlias if it does not exist.",
   115  		Long:                  "Updates the content of a CellsAlias with the provided parameters, creating the CellsAlias if it does not exist.",
   116  		DisableFlagsInUseLine: true,
   117  		Args:                  cobra.ExactArgs(1),
   118  		RunE:                  commandUpdateCellsAlias,
   119  	}
   120  )
   121  
   122  var addCellInfoOptions topodatapb.CellInfo
   123  
   124  func commandAddCellInfo(cmd *cobra.Command, args []string) error {
   125  	cli.FinishedParsing(cmd)
   126  
   127  	cell := cmd.Flags().Arg(0)
   128  	_, err := client.AddCellInfo(commandCtx, &vtctldatapb.AddCellInfoRequest{
   129  		Name:     cell,
   130  		CellInfo: &addCellInfoOptions,
   131  	})
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	fmt.Printf("Created cell: %s\n", cell)
   137  	return nil
   138  }
   139  
   140  var addCellsAliasOptions topodatapb.CellsAlias
   141  
   142  func commandAddCellsAlias(cmd *cobra.Command, args []string) error {
   143  	cli.FinishedParsing(cmd)
   144  
   145  	alias := cmd.Flags().Arg(0)
   146  	_, err := client.AddCellsAlias(commandCtx, &vtctldatapb.AddCellsAliasRequest{
   147  		Name:  alias,
   148  		Cells: addCellsAliasOptions.Cells,
   149  	})
   150  	if err != nil {
   151  		return err
   152  	}
   153  
   154  	fmt.Printf("Created cells alias: %s (cells = %v)\n", alias, addCellsAliasOptions.Cells)
   155  	return nil
   156  }
   157  
   158  var deleteCellInfoOptions = struct {
   159  	Force bool
   160  }{}
   161  
   162  func commandDeleteCellInfo(cmd *cobra.Command, args []string) error {
   163  	cli.FinishedParsing(cmd)
   164  
   165  	cell := cmd.Flags().Arg(0)
   166  	_, err := client.DeleteCellInfo(commandCtx, &vtctldatapb.DeleteCellInfoRequest{
   167  		Name:  cell,
   168  		Force: deleteCellInfoOptions.Force,
   169  	})
   170  	if err != nil {
   171  		return err
   172  	}
   173  
   174  	fmt.Printf("Deleted cell %s\n", cell)
   175  	return nil
   176  }
   177  
   178  func commandDeleteCellsAlias(cmd *cobra.Command, args []string) error {
   179  	cli.FinishedParsing(cmd)
   180  
   181  	alias := cmd.Flags().Arg(0)
   182  	_, err := client.DeleteCellsAlias(commandCtx, &vtctldatapb.DeleteCellsAliasRequest{
   183  		Name: alias,
   184  	})
   185  	if err != nil {
   186  		return err
   187  	}
   188  
   189  	fmt.Printf("Delete cells alias %s\n", alias)
   190  	return nil
   191  }
   192  
   193  func commandGetCellInfoNames(cmd *cobra.Command, args []string) error {
   194  	cli.FinishedParsing(cmd)
   195  
   196  	resp, err := client.GetCellInfoNames(commandCtx, &vtctldatapb.GetCellInfoNamesRequest{})
   197  	if err != nil {
   198  		return err
   199  	}
   200  
   201  	fmt.Printf("%s\n", strings.Join(resp.Names, "\n"))
   202  
   203  	return nil
   204  }
   205  
   206  func commandGetCellInfo(cmd *cobra.Command, args []string) error {
   207  	cli.FinishedParsing(cmd)
   208  
   209  	cell := cmd.Flags().Arg(0)
   210  
   211  	resp, err := client.GetCellInfo(commandCtx, &vtctldatapb.GetCellInfoRequest{Cell: cell})
   212  	if err != nil {
   213  		return err
   214  	}
   215  
   216  	data, err := cli.MarshalJSON(resp.CellInfo)
   217  	if err != nil {
   218  		return err
   219  	}
   220  
   221  	fmt.Printf("%s\n", data)
   222  
   223  	return nil
   224  }
   225  
   226  func commandGetCellsAliases(cmd *cobra.Command, args []string) error {
   227  	cli.FinishedParsing(cmd)
   228  
   229  	resp, err := client.GetCellsAliases(commandCtx, &vtctldatapb.GetCellsAliasesRequest{})
   230  	if err != nil {
   231  		return err
   232  	}
   233  
   234  	data, err := cli.MarshalJSON(resp.Aliases)
   235  	if err != nil {
   236  		return err
   237  	}
   238  
   239  	fmt.Printf("%s\n", data)
   240  
   241  	return nil
   242  }
   243  
   244  var updateCellInfoOptions topodatapb.CellInfo
   245  
   246  func commandUpdateCellInfo(cmd *cobra.Command, args []string) error {
   247  	cli.FinishedParsing(cmd)
   248  
   249  	cell := cmd.Flags().Arg(0)
   250  	resp, err := client.UpdateCellInfo(commandCtx, &vtctldatapb.UpdateCellInfoRequest{
   251  		Name:     cell,
   252  		CellInfo: &updateCellInfoOptions,
   253  	})
   254  	if err != nil {
   255  		return err
   256  	}
   257  
   258  	data, err := cli.MarshalJSON(resp.CellInfo)
   259  	if err != nil {
   260  		return err
   261  	}
   262  
   263  	fmt.Printf("Updated cell %s. New CellInfo:\n%s\n", resp.Name, data)
   264  	return nil
   265  }
   266  
   267  var updateCellsAliasOptions topodatapb.CellsAlias
   268  
   269  func commandUpdateCellsAlias(cmd *cobra.Command, args []string) error {
   270  	cli.FinishedParsing(cmd)
   271  
   272  	alias := cmd.Flags().Arg(0)
   273  	resp, err := client.UpdateCellsAlias(commandCtx, &vtctldatapb.UpdateCellsAliasRequest{
   274  		Name:       alias,
   275  		CellsAlias: &updateCellsAliasOptions,
   276  	})
   277  	if err != nil {
   278  		return err
   279  	}
   280  
   281  	data, err := cli.MarshalJSON(resp.CellsAlias)
   282  	if err != nil {
   283  		return err
   284  	}
   285  
   286  	fmt.Printf("Updated cells alias %s. New CellsAlias:\n%s\n", resp.Name, data)
   287  	return nil
   288  }
   289  
   290  func init() {
   291  	AddCellInfo.Flags().StringVarP(&addCellInfoOptions.ServerAddress, "server-address", "a", "", "The address the topology server will connect to for this cell.")
   292  	AddCellInfo.Flags().StringVarP(&addCellInfoOptions.Root, "root", "r", "", "The root path the topology server will use for this cell.")
   293  	AddCellInfo.MarkFlagRequired("root")
   294  	Root.AddCommand(AddCellInfo)
   295  
   296  	AddCellsAlias.Flags().StringSliceVarP(&addCellsAliasOptions.Cells, "cells", "c", nil, "The list of cell names that are members of this alias.")
   297  	Root.AddCommand(AddCellsAlias)
   298  
   299  	DeleteCellInfo.Flags().BoolVarP(&deleteCellInfoOptions.Force, "force", "f", false, "Proceeds even if the cell's topology server cannot be reached. The assumption is that you shut down the entire cell, and just need to update the global topo data.")
   300  	Root.AddCommand(DeleteCellInfo)
   301  	Root.AddCommand(DeleteCellsAlias)
   302  
   303  	Root.AddCommand(GetCellInfoNames)
   304  	Root.AddCommand(GetCellInfo)
   305  	Root.AddCommand(GetCellsAliases)
   306  
   307  	UpdateCellInfo.Flags().StringVarP(&updateCellInfoOptions.ServerAddress, "server-address", "a", "", "The address the topology server will connect to for this cell.")
   308  	UpdateCellInfo.Flags().StringVarP(&updateCellInfoOptions.Root, "root", "r", "", "The root path the topology server will use for this cell.")
   309  	Root.AddCommand(UpdateCellInfo)
   310  
   311  	UpdateCellsAlias.Flags().StringSliceVarP(&updateCellsAliasOptions.Cells, "cells", "c", nil, "The list of cell names that are members of this alias.")
   312  	Root.AddCommand(UpdateCellsAlias)
   313  }