Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Docker support #861

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Docker Publish

on:
workflow_dispatch:
push:
tags:
- 'v*'

jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=semver,pattern={{version}}
type=raw,value=latest

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o redis-shake ./cmd/redis-shake

FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/redis-shake .
COPY shake_sync_env.toml .
COPY shake_scan_env.toml .
COPY entrypoint.sh .

RUN chmod +x entrypoint.sh

ENTRYPOINT ["/bin/sh", "entrypoint.sh"]
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ RedisShake is a tool designed for processing and migrating Redis data. It offers

Download the binary package directly from the [Releases](https://github.com/tair-opensource/RedisShake/releases) page.

#### Docker

```shell
docker run --network host \
-e SYNC=true \
-e SHAKE_SRC_ADDRESS=127.0.0.1:6379 \
-e SHAKE_DST_ADDRESS=127.0.0.1:6380 \
ghcr.io/tair-opensource/redisshake:latest
```

#### Compile from Source

To compile from source, ensure that you have a Golang environment set up on your local machine:

```shell
git clone https://github.com/alibaba/RedisShake
git clone https://github.com/tair-opensource/RedisShake
cd RedisShake
sh build.sh
```
Expand Down
9 changes: 9 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
if [ "$SYNC" = "true" ]; then
./redis-shake shake_sync_env.toml
elif [ "$SCAN" = "true" ]; then
./redis-shake shake_scan_env.toml
else
echo "Error: Neither SYNC nor SCAN environment variable is set to true"
exit 1
fi
28 changes: 17 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,31 @@ require (
github.com/gofrs/flock v0.8.1
github.com/mcuadros/go-defaults v1.2.0
github.com/rs/zerolog v1.28.0
github.com/spf13/viper v1.15.0
github.com/spf13/viper v1.18.1
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/a8m/envsubst v1.4.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
502 changes: 51 additions & 451 deletions go.sum

Large diffs are not rendered by default.

42 changes: 23 additions & 19 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package config

import (
"bytes"
"fmt"
"os"
"strings"

"RedisShake/internal/log"

"github.com/a8m/envsubst"
"github.com/mcuadros/go-defaults"
"github.com/rs/zerolog"
"github.com/spf13/viper"
)

type FilterOptions struct {
AllowKeyPrefix []string `mapstructure:"allow_key_prefix" default:"[]"`
AllowKeySuffix []string `mapstructure:"allow_key_suffix" default:"[]"`
BlockKeyPrefix []string `mapstructure:"block_key_prefix" default:"[]"`
BlockKeySuffix []string `mapstructure:"block_key_suffix" default:"[]"`
AllowDB []int `mapstructure:"allow_db" default:"[]"`
BlockDB []int `mapstructure:"block_db" default:"[]"`
AllowCommand []string `mapstructure:"allow_command" default:"[]"`
BlockCommand []string `mapstructure:"block_command" default:"[]"`
AllowKeyPrefix []string `mapstructure:"allow_key_prefix" default:"[]"`
AllowKeySuffix []string `mapstructure:"allow_key_suffix" default:"[]"`
BlockKeyPrefix []string `mapstructure:"block_key_prefix" default:"[]"`
BlockKeySuffix []string `mapstructure:"block_key_suffix" default:"[]"`
AllowDB []int `mapstructure:"allow_db" default:"[]"`
BlockDB []int `mapstructure:"block_db" default:"[]"`
AllowCommand []string `mapstructure:"allow_command" default:"[]"`
BlockCommand []string `mapstructure:"block_command" default:"[]"`
AllowCommandGroup []string `mapstructure:"allow_command_group" default:"[]"`
BlockCommandGroup []string `mapstructure:"block_command_group" default:"[]"`
Function string `mapstructure:"function" default:""`
Expand Down Expand Up @@ -73,7 +75,7 @@ func (opt *AdvancedOptions) GetPSyncCommand(address string) string {
}

type ShakeOptions struct {
Filter FilterOptions
Filter FilterOptions
Advanced AdvancedOptions
Module ModuleOptions
}
Expand All @@ -97,18 +99,20 @@ func LoadConfig() *viper.Viper {
if len(os.Args) == 2 {
logger.Info().Msgf("load config from file: %s", os.Args[1])
configFile := os.Args[1]
v.SetConfigFile(configFile)
err := v.ReadInConfig()
buf, err := envsubst.ReadFile(configFile)
if err != nil {
panic(err)
logger.Error().Msgf("failed to read config file: %v", err)
os.Exit(1)
}
}

// load config from environment variables
if len(os.Args) == 1 {
logger.Warn().Msg("load config from environment variables")
v.SetConfigType("env")
v.AutomaticEnv()
v.SetConfigType("toml")
err = v.ReadConfig(bytes.NewReader(buf))
if err != nil {
logger.Error().Msgf("failed to read config file: %v", err)
os.Exit(1)
}
} else {
logger.Error().Msg("config file not found")
os.Exit(1)
}

// unmarshal config
Expand Down
5 changes: 5 additions & 0 deletions shake_scan_env.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[scan_reader]
address = "${SHAKE_SRC_ADDRESS}" # export SHAKE_SRC_ADDRESS=127.0.0.1:6379

[redis_writer]
address = "${SHAKE_DST_ADDRESS}" # export SHAKE_DST_ADDRESS=127.0.0.1:6380
5 changes: 5 additions & 0 deletions shake_sync_env.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[sync_reader]
address = "${SHAKE_SRC_ADDRESS}" # export SHAKE_SRC_ADDRESS=127.0.0.1:6379

[redis_writer]
address = "${SHAKE_DST_ADDRESS}" # export SHAKE_DST_ADDRESS=127.0.0.1:6380
Loading