/
metastore.go
95 lines (85 loc) · 2.68 KB
/
metastore.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cmd
import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/glue"
"github.com/spf13/cobra"
"github.com/treeverse/lakefs/pkg/metastore"
metastoreglue "github.com/treeverse/lakefs/pkg/metastore/glue"
"github.com/treeverse/lakefs/pkg/metastore/hive"
"golang.org/x/net/context"
)
var metastoreCmd = &cobra.Command{
Use: "metastore",
Short: "Manage metastore commands",
Deprecated: "Upcoming releases of lakectl will no longer support this command.",
}
func getGlueClient(c *Configuration) *glue.Client {
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion(string(c.Metastore.Glue.Region)),
config.WithSharedCredentialsFiles([]string{string(c.Metastore.Glue.CredentialsFile)}),
config.WithSharedConfigProfile(string(c.Metastore.Glue.Profile)),
)
if err != nil {
return nil
}
client := glue.NewFromConfig(cfg, func(options *glue.Options) {
if c.Metastore.Glue.Credentials != nil {
options.Credentials = credentials.NewStaticCredentialsProvider(
string(c.Metastore.Glue.Credentials.AccessKeyID),
string(c.Metastore.Glue.Credentials.AccessSecretKey),
string(c.Metastore.Glue.Credentials.SessionToken),
)
}
})
return client
}
func getMetastoreClient(msType, hiveAddress string) (metastore.Client, func()) {
if msType == "" {
msType = cfg.Metastore.Type.String()
}
switch msType {
case "hive":
if len(hiveAddress) == 0 {
hiveAddress = cfg.Metastore.Hive.URI.String()
}
hiveClient, err := hive.NewMSClient(hiveAddress, false, cfg.Metastore.Hive.DBLocationURI.String())
if err != nil {
DieErr(err)
}
deferFunc := func() {
err := hiveClient.Close()
if err != nil {
DieErr(err)
}
}
return hiveClient, deferFunc
case "glue":
glueClient := getGlueClient(cfg)
client, err := metastoreglue.NewMSClient(glueClient, cfg.Metastore.Glue.CatalogID.String(), cfg.Metastore.Glue.DBLocationURI.String())
if err != nil {
DieErr(err)
}
return client, func() {}
default:
Die("unknown type, expected hive or glue got: "+msType, 1)
}
return nil, nil
}
func getClients(fromClientType, toClientType, fromAddress, toAddress string) (metastore.Client, metastore.Client, func(), func()) {
msType := cfg.Metastore.Type.String()
if len(fromClientType) == 0 {
fromClientType = msType
}
if len(toClientType) == 0 {
toClientType = msType
}
fromClient, fromClientDeferFunc := getMetastoreClient(fromClientType, fromAddress)
toClient, toClientDeferFunc := getMetastoreClient(toClientType, toAddress)
return fromClient, toClient, fromClientDeferFunc, toClientDeferFunc
}
//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(metastoreCmd)
}