forked from pachyderm/pachyderm
/
version.go
77 lines (69 loc) · 2.19 KB
/
version.go
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
package pachyderm
import (
"context"
"errors"
"fmt"
"time"
"github.com/gogo/protobuf/types"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
pclient "github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/version"
)
func (b *backend) versionPath() *framework.Path {
return &framework.Path{
Pattern: "version(?P<clientonly>/client-only)?",
Fields: map[string]*framework.FieldSchema{
"clientonly": &framework.FieldSchema{
Type: framework.TypeString,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathVersion,
},
}
}
func (b *backend) pathVersion(ctx context.Context, req *logical.Request, d *framework.FieldData) (resp *logical.Response, retErr error) {
b.Logger().Debug(fmt.Sprintf("(%s) %s received at %s", req.ID, req.Operation, req.Path))
defer func() {
b.Logger().Debug(fmt.Sprintf("(%s) %s finished at %s (success=%t)", req.ID, req.Operation, req.Path, retErr == nil && !resp.IsError()))
}()
response := &logical.Response{
Data: map[string]interface{}{
"client-version": version.PrettyPrintVersion(version.Version),
},
}
// Determine if caller only wants client version
clientOnlyIface, ok, err := d.GetOkErr("clientonly")
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("%v: could not extract 'clientonly' from request", err)), nil
}
if ok {
if clientOnly, ok := clientOnlyIface.(string); ok && clientOnly != "" {
return response, nil
}
}
// Get Pachd address from config
config, err := getConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if len(config.PachdAddress) == 0 {
return nil, errors.New("plugin is missing pachd_address")
}
// Create version API client
client, err := pclient.NewFromAddress(config.PachdAddress)
if err != nil {
return nil, err
}
defer client.Close() // avoid leaking connections
// Retrieve server version
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
serverVersion, err := client.GetVersion(ctx, &types.Empty{})
if err != nil {
return nil, err
}
response.Data["server-version"] = version.PrettyPrintVersion(serverVersion)
return response, nil
}