Skip to content

Commit

Permalink
Block-sync update. (#790)
Browse files Browse the repository at this point in the history
* Do nothing if secret is empty.
* add newline at bottom of file.
* Renamed key.yml to object-config.yml file.
* Replaced key with path and other parts of the file.

Signed-off-by: Kushal Shukla <[email protected]>
  • Loading branch information
kushalShukla-web authored Nov 24, 2024
1 parent 8958a49 commit b5e7c50
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion tools/block-sync/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ RUN chmod +x /scripts/download.sh

RUN chmod +x /bin/block-sync

ENTRYPOINT [ "/scripts/download.sh" ]
ENTRYPOINT [ "/scripts/download.sh" ]
10 changes: 5 additions & 5 deletions tools/block-sync/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

# block-sync - TSDB Data Synchronization Tool


The `block-sync` command is a CLI tool designed to synchronize TSDB data with an object storage system.

## Table of Contents
Expand All @@ -14,7 +12,9 @@ The `block-sync` command is a CLI tool designed to synchronize TSDB data with an
- ``` -h , --help```:Displays context-sensitive help
- ``` - tsdb-path```: Path for The TSDB data in prometheus
- ```- objstore.config-file```: Path for The Config file
- ```- Key```: Path for the Key where to store block data , i.e a Directory.
- ```- path```: Path within the objectstorage where to store block data , i.e a Directory.
## Note
> Use the `path` flag with a file specifying the path: *your directory name*.
## Upload

Expand All @@ -23,7 +23,7 @@ The `upload` command allows you to upload TSDB data from a specified path to an
### Usage

```bash
./block-sync upload --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --key=<object-key>
./block-sync upload --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --path=<path-to-bucket-config-file>


```
Expand All @@ -34,7 +34,7 @@ The `download` command allows you to retrieve TSDB data from an object storage b
### Usage

```bash
./block-sync download --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --key=<object-key>
./block-sync download --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --path=<path-to-bucket-config-file>
```
## Config File

Expand Down
12 changes: 6 additions & 6 deletions tools/block-sync/download.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/sh

KEY_FILE="/key/key.yml"
PATH_FILE="/key/bucket-config.yml"

if [[ -f "$KEY_FILE" ]]; then
echo "Found key.yml, proceeding with download..."
/bin/block-sync download --tsdb-path=/data --objstore.config-file=/config/object-config.yml --key=$KEY_FILE
if [[ -f "$PATH_FILE" ]]; then
echo "Found bucket-config.yml, proceeding with download..."
/bin/block-sync download --tsdb-path=/data --objstore.config-file=/config/object-config.yml --path=$PATH_FILE
else
echo "key.yml not found, skipping download."
echo "bucket-config.yml not found, skipping download."
exit 0
fi
fi
15 changes: 7 additions & 8 deletions tools/block-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ func main() {
var (
tsdbPath string
objectConfig string
objectKey string
objectPath string
)
uploadCmd := flag.NewFlagSet("upload", flag.ExitOnError)
downloadCmd := flag.NewFlagSet("download", flag.ExitOnError)

uploadCmd.StringVar(&tsdbPath, "tsdb-path", "", "Uploading data to objstore")
uploadCmd.StringVar(&objectConfig, "objstore.config-file", "", "Path for The Config file")
uploadCmd.StringVar(&objectKey, "key", "", "Path for the Key where to store block data")
uploadCmd.StringVar(&objectPath, "path", "", "Path within the objectstorage")

downloadCmd.StringVar(&tsdbPath, "tsdb-path", "", "Downloading data to objstore")
downloadCmd.StringVar(&objectConfig, "objstore.config-file", "", "Path for The Config file")
downloadCmd.StringVar(&objectKey, "key", "", "Path from the Key where to download the block data")
downloadCmd.StringVar(&objectPath, "path", "", "Path within the objectstorage")
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

flag.Usage = func() {
Expand All @@ -46,9 +46,8 @@ func main() {
fmt.Println("Flags:")
fmt.Println(" --tsdb-path Path to TSDB data")
fmt.Println(" --objstore.config-file Path to the object store config file")
fmt.Println(" --key Key path for storing or downloading data")
fmt.Println(" --path Directory in objectstorage for uploading or downloading data")
fmt.Println()
fmt.Println("Use 'block-sync [command] --help' for more information about a command.")
}

if len(os.Args) < 2 {
Expand All @@ -74,13 +73,13 @@ func main() {
os.Exit(1)
}

if tsdbPath == "" || objectConfig == "" || objectKey == "" {
fmt.Println("error: all flags --tsdb-path, --objstore.config-file, and --key are required.")
if tsdbPath == "" || objectConfig == "" || objectPath == "" {
fmt.Println("error: all flags --tsdb-path, --objstore.config-file, and --path are required.")
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := newStore(tsdbPath, objectConfig, objectKey, logger)
store, err := newStore(tsdbPath, objectConfig, objectPath, logger)
if err != nil {
logger.Error("Failed to create store", "error", err)
os.Exit(1)
Expand Down
34 changes: 18 additions & 16 deletions tools/block-sync/upload_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,61 +28,63 @@ import (
type Store struct {
bucket objstore.Bucket
tsdbpath string
objectkey string
objectpath string
objectconfig string
bucketlogger *slog.Logger
}

func newStore(tsdbPath, objectConfig, objectKey string, logger *slog.Logger) (*Store, error) {
func newStore(tsdbPath, objectConfig, objectPath string, logger *slog.Logger) (*Store, error) {
configBytes, err := os.ReadFile(objectConfig)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
if len(configBytes) == 0 {
fmt.Println("Config file is empty, exiting container.")
os.Exit(0)
}

bucket, err := client.NewBucket(log.NewNopLogger(), configBytes, "block-sync")
if err != nil {
return nil, fmt.Errorf("failed to create bucket existence:%w", err)
}
key, err := os.ReadFile(objectKey)
path, err := os.ReadFile(objectPath)
if err != nil {
return nil, fmt.Errorf("failed to read objectKey file: %w", err)
}

content := strings.TrimSpace(string(key))
content := strings.TrimSpace(string(path))
lines := strings.Split(content, "\n")
var value string
var directory string

// Loop through each line to find the key
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "key:") {
// Extract the value after "key:"
value = strings.TrimSpace(strings.TrimPrefix(line, "key:"))
if strings.HasPrefix(line, "path:") {
directory = strings.TrimSpace(strings.TrimPrefix(line, "path:"))
break
}
}

if value == "" {
return nil, fmt.Errorf("expected 'key:' prefix not found")
if directory == "" {
return nil, fmt.Errorf("expected 'path:' prefix not found")
}

return &Store{
bucket: bucket,
tsdbpath: tsdbPath,
objectkey: value,
objectpath: directory,
objectconfig: objectConfig,
bucketlogger: logger,
}, nil
}

func (c *Store) upload(ctx context.Context) error {
exists, err := c.bucket.Exists(ctx, c.objectkey)
exists, err := c.bucket.Exists(ctx, c.objectpath)
if err != nil {
return fmt.Errorf("failed to check new bucket:%w", err)
}
c.bucketlogger.Info("Bucket checked Successfully", "Bucket name", exists)

err = objstore.UploadDir(ctx, log.NewNopLogger(), c.bucket, c.tsdbpath, c.objectkey)
err = objstore.UploadDir(ctx, log.NewNopLogger(), c.bucket, c.tsdbpath, c.objectpath)
if err != nil {
c.bucketlogger.Error("Failed to upload directory", "path", c.tsdbpath, "error", err)
return fmt.Errorf("failed to upload directory from path %s to bucket: %w", c.tsdbpath, err)
Expand All @@ -93,13 +95,13 @@ func (c *Store) upload(ctx context.Context) error {
}

func (c *Store) download(ctx context.Context) error {
exists, err := c.bucket.Exists(ctx, c.objectkey)
exists, err := c.bucket.Exists(ctx, c.objectpath)
if err != nil {
return fmt.Errorf("failed to check new bucket:%w", err)
}
c.bucketlogger.Info("Bucket checked Successfully", "Bucket name", exists)

err = objstore.DownloadDir(ctx, log.NewNopLogger(), c.bucket, "dir/", c.objectkey, c.tsdbpath)
err = objstore.DownloadDir(ctx, log.NewNopLogger(), c.bucket, "dir/", c.objectpath, c.tsdbpath)
if err != nil {
c.bucketlogger.Error("Failed to download directory", "path", c.tsdbpath, "error", err)
return fmt.Errorf("failed to download directory from path %s to bucket: %w", c.tsdbpath, err)
Expand Down

0 comments on commit b5e7c50

Please sign in to comment.