Welcome to mirror list, hosted at ThFree Co, Russian Federation.

subcmd_metadata.go « praefect « cmd - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e913a0eec37e1421592ad1e0c99ad716f96c9002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main

import (
	"context"
	"errors"
	"flag"
	"fmt"
	"io"

	"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config"
	"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
)

const metadataCmdName = "metadata"

type metadataSubcommand struct {
	stdout         io.Writer
	repositoryID   int64
	virtualStorage string
	relativePath   string
}

func newMetadataSubcommand(stdout io.Writer) *metadataSubcommand {
	return &metadataSubcommand{stdout: stdout}
}

func (cmd *metadataSubcommand) FlagSet() *flag.FlagSet {
	fs := flag.NewFlagSet(metadataCmdName, flag.ContinueOnError)
	fs.Int64Var(&cmd.repositoryID, "repository-id", 0, "the repository's ID")
	fs.StringVar(&cmd.virtualStorage, "virtual-storage", "", "the repository's virtual storage")
	fs.StringVar(&cmd.relativePath, "relative-path", "", "the repository's relative path in the virtual storage")
	return fs
}

func (cmd *metadataSubcommand) println(format string, args ...interface{}) {
	fmt.Fprintf(cmd.stdout, format+"\n", args...)
}

func (cmd *metadataSubcommand) Exec(flags *flag.FlagSet, cfg config.Config) error {
	if flags.NArg() > 0 {
		return unexpectedPositionalArgsError{Command: flags.Name()}
	}

	var request gitalypb.GetRepositoryMetadataRequest
	switch {
	case cmd.repositoryID != 0:
		if cmd.virtualStorage != "" || cmd.relativePath != "" {
			return errors.New("virtual storage and relative path can't be provided with a repository ID")
		}
		request.Query = &gitalypb.GetRepositoryMetadataRequest_RepositoryId{RepositoryId: cmd.repositoryID}
	case cmd.virtualStorage != "" || cmd.relativePath != "":
		if cmd.virtualStorage == "" {
			return errors.New("virtual storage is required with relative path")
		} else if cmd.relativePath == "" {
			return errors.New("relative path is required with virtual storage")
		}
		request.Query = &gitalypb.GetRepositoryMetadataRequest_Path_{
			Path: &gitalypb.GetRepositoryMetadataRequest_Path{
				VirtualStorage: cmd.virtualStorage,
				RelativePath:   cmd.relativePath,
			},
		}
	default:
		return errors.New("repository id or virtual storage and relative path required")
	}

	nodeAddr, err := getNodeAddress(cfg)
	if err != nil {
		return fmt.Errorf("get node address: %w", err)
	}

	ctx := context.TODO()
	conn, err := subCmdDial(ctx, nodeAddr, cfg.Auth.Token, defaultDialTimeout)
	if err != nil {
		return fmt.Errorf("dial: %w", err)
	}
	defer conn.Close()

	metadata, err := gitalypb.NewPraefectInfoServiceClient(conn).GetRepositoryMetadata(ctx, &request)
	if err != nil {
		return fmt.Errorf("get metadata: %w", err)
	}

	cmd.println("Repository ID: %d", metadata.RepositoryId)
	cmd.println("Virtual Storage: %q", metadata.VirtualStorage)
	cmd.println("Relative Path: %q", metadata.RelativePath)
	cmd.println("Replica Path: %q", metadata.ReplicaPath)
	cmd.println("Primary: %q", metadata.Primary)
	cmd.println("Generation: %d", metadata.Generation)
	cmd.println("Replicas:")
	for _, replica := range metadata.Replicas {
		cmd.println("- Storage: %q", replica.Storage)
		cmd.println("  Assigned: %v", replica.Assigned)

		generationText := fmt.Sprintf("%d, fully up to date", replica.Generation)
		if replica.Generation == -1 {
			generationText = "replica not yet created"
		} else if replica.Generation < metadata.Generation {
			generationText = fmt.Sprintf("%d, behind by %d changes", replica.Generation, metadata.Generation-replica.Generation)
		}

		cmd.println("  Generation: %s", generationText)
		cmd.println("  Healthy: %v", replica.Healthy)
		cmd.println("  Valid Primary: %v", replica.ValidPrimary)
	}
	return nil
}