|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "log/slog" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/go-kit/log" |
| 23 | + "github.com/thanos-io/objstore" |
| 24 | + "github.com/thanos-io/objstore/client" |
| 25 | + "gopkg.in/alecthomas/kingpin.v2" |
| 26 | +) |
| 27 | + |
| 28 | +type Store struct { |
| 29 | + Bucket objstore.Bucket |
| 30 | + TsdbPath string |
| 31 | + ObjectKey string |
| 32 | + ObjectConfig string |
| 33 | +} |
| 34 | + |
| 35 | +func newstore(tsdbPath, objectConfig, objectKey string) *Store { |
| 36 | + logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) |
| 37 | + configBytes, err := os.ReadFile(objectConfig) |
| 38 | + if err != nil { |
| 39 | + logger.Error("failed to read config file", "error", err) |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + bucket, err := client.NewBucket(log.NewNopLogger(), configBytes, "block-sync") |
| 44 | + if err != nil { |
| 45 | + logger.Error("Failed to create bucket existence", "error", err) |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + exists, err := bucket.Exists(context.Background(), objectKey) |
| 50 | + if err != nil { |
| 51 | + logger.Error("Failed to create new bucket", "error", err) |
| 52 | + return nil |
| 53 | + } |
| 54 | + logger.Info("Bucket existence check", "bucket_name", objectKey, "exists", exists) |
| 55 | + |
| 56 | + return &Store{ |
| 57 | + Bucket: bucket, |
| 58 | + TsdbPath: tsdbPath, |
| 59 | + ObjectConfig: objectConfig, |
| 60 | + ObjectKey: objectKey, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (c *Store) upload(*kingpin.ParseContext) error { |
| 65 | + |
| 66 | + logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) |
| 67 | + |
| 68 | + err := objstore.UploadDir(context.Background(), log.NewNopLogger(), c.Bucket, c.TsdbPath, c.ObjectKey) |
| 69 | + if err != nil { |
| 70 | + logger.Error("Failed to upload directory", "path", c.TsdbPath, "error", err) |
| 71 | + return fmt.Errorf("failed to upload directory from path %s to bucket: %v", c.TsdbPath, err) |
| 72 | + } |
| 73 | + |
| 74 | + logger.Info("Successfully uploaded directory", "path", c.TsdbPath, "bucket", c.Bucket.Name()) |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func (c *Store) download(*kingpin.ParseContext) error { |
| 79 | + |
| 80 | + logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) |
| 81 | + |
| 82 | + err := objstore.DownloadDir(context.Background(), log.NewNopLogger(), c.Bucket, "dir/", c.ObjectKey, c.TsdbPath) |
| 83 | + if err != nil { |
| 84 | + logger.Error("Failed to download directory", "path", c.TsdbPath, "error", err) |
| 85 | + return fmt.Errorf("failed to download directory from path %s to bucket: %v", c.TsdbPath, err) |
| 86 | + } |
| 87 | + |
| 88 | + logger.Info("Successfully downloaded directory", "path", c.TsdbPath, "bucket", c.Bucket.Name()) |
| 89 | + return nil |
| 90 | +} |
0 commit comments