From 4c3c31476b9e5e2741dd220ac3b1ba5bcf43d343 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Fri, 17 May 2024 14:58:14 +0200 Subject: [PATCH] ci: delete branch snapshot (#508) * ci: delete branch snapshot * ci: increase expiration time for oracle in upgrade-assure --- .github/workflows/delete-branch-snapshot.yml | 43 +++++++++++ .github/workflows/software-upgrade-test.yml | 9 +-- Makefile | 8 ++- cmd/delete-snapshot/main.go | 76 ++++++++++++++++++++ cmd/upgrade-assure/update-genesis.go | 4 +- 5 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/delete-branch-snapshot.yml create mode 100644 cmd/delete-snapshot/main.go diff --git a/.github/workflows/delete-branch-snapshot.yml b/.github/workflows/delete-branch-snapshot.yml new file mode 100644 index 000000000..7febeab6d --- /dev/null +++ b/.github/workflows/delete-branch-snapshot.yml @@ -0,0 +1,43 @@ +name: Delete Snapshot on Branch Deletion + +on: + delete: + branches: + - "**" + +jobs: + delete-snapshot: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.21" + + - name: Cache delete-snapshot binary + id: cache-delete-snapshot + uses: actions/cache@v4 + with: + path: | + ./build + key: ${{ runner.os }}-delete-snapshot-${{ hashFiles('cmd/delete-snapshot/*.go') }} + + - name: Build delete-snapshot binary + if: steps.cache-delete-snapshot.outputs.cache-hit != 'true' + run: | + make build-delete-snapshot + + - name: Run delete-snapshot + env: + R2_ACCESS_KEY: ${{ secrets.R2_ACCESS_KEY }} + R2_SECRET_KEY: ${{ secrets.R2_SECRET_KEY }} + R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }} + R2_BUCKET_NAME: ${{ secrets.R2_BUCKET_NAME }} + run: | + SANITIZED_HEAD_REF=${{ github.head_ref || github.ref }} + SANITIZED_HEAD_REF=$(echo "$SANITIZED_HEAD_REF" | sed 's|refs/heads/||; s|/|_|g') + ./build/delete_snapshot "$SANITIZED_HEAD_REF" diff --git a/.github/workflows/software-upgrade-test.yml b/.github/workflows/software-upgrade-test.yml index 3423c8e28..e483c5965 100644 --- a/.github/workflows/software-upgrade-test.yml +++ b/.github/workflows/software-upgrade-test.yml @@ -778,11 +778,12 @@ jobs: tar -cf - .elys | lz4 -z - > "$NEW_SNAPSHOT_PATH" - name: Upload snapshot + env: + R2_ACCESS_KEY: ${{ secrets.R2_ACCESS_KEY }} + R2_SECRET_KEY: ${{ secrets.R2_SECRET_KEY }} + R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }} + R2_BUCKET_NAME: ${{ secrets.R2_BUCKET_NAME }} run: | - export R2_ACCESS_KEY=${{ secrets.R2_ACCESS_KEY }} - export R2_SECRET_KEY=${{ secrets.R2_SECRET_KEY }} - export R2_ENDPOINT=${{ secrets.R2_ENDPOINT }} - export R2_BUCKET_NAME=${{ secrets.R2_BUCKET_NAME }} ${{ needs.build-new-binary.outputs.UPLOAD_SNAPSHOT_BINARY_PATH }} $NEW_SNAPSHOT_PATH - name: Info about the snapshot diff --git a/Makefile b/Makefile index 34b85abd6..b7fb04091 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,12 @@ build-upload-snapshot: check-version go.sum @-mkdir -p $(BUILD_FOLDER) 2> /dev/null @GOFLAGS=$(GOFLAGS) go build -o $(BUILD_FOLDER) ./cmd/upload-snapshot +## build-delete-snapshot: Build the binary for delete snapshot +build-delete-snapshot: check-version go.sum + @echo Building Delete snapshot binary... + @-mkdir -p $(BUILD_FOLDER) 2> /dev/null + @GOFLAGS=$(GOFLAGS) go build -o $(BUILD_FOLDER) ./cmd/delete-snapshot + ## build-all: Build binaries for all platforms build-all: @echo Building Elysd binaries for all platforms... @@ -91,7 +97,7 @@ do-checksum: ## build-with-checksum: Build binaries for all platforms and generate checksums build-with-checksum: build-all do-checksum -.PHONY: install build build-all do-checksum build-with-checksum build-upgrade-assure build-upload-snapshot +.PHONY: install build build-all do-checksum build-with-checksum build-upgrade-assure build-upload-snapshot build-delete-snapshot ## mocks: Generate mocks mocks: diff --git a/cmd/delete-snapshot/main.go b/cmd/delete-snapshot/main.go new file mode 100644 index 000000000..a7c7bf618 --- /dev/null +++ b/cmd/delete-snapshot/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "context" + "fmt" + "os" + "strings" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + "github.com/aws/aws-sdk-go-v2/service/s3" +) + +func main() { + if len(os.Args) != 2 { + fmt.Println("Usage: ") + os.Exit(1) + } + + // Fetch credentials and configuration from environment variables + accessKey := os.Getenv("R2_ACCESS_KEY") + secretKey := os.Getenv("R2_SECRET_KEY") + s3URL := os.Getenv("R2_ENDPOINT") + bucketName := os.Getenv("R2_BUCKET_NAME") + branchName := os.Args[1] + + // Ensure all required environment variables are set + if accessKey == "" || secretKey == "" || s3URL == "" || bucketName == "" { + fmt.Println("Please set R2_ACCESS_KEY, R2_SECRET_KEY, R2_ENDPOINT, and R2_BUCKET_NAME environment variables") + os.Exit(1) + } + + // Load AWS configuration with credentials + cfg, err := config.LoadDefaultConfig( + context.TODO(), + config.WithCredentialsProvider( + credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""), + ), + config.WithRegion("ENAM"), // Ensure this region is appropriate or set it via environment variable if needed + config.WithEndpointResolverWithOptions( + aws.EndpointResolverWithOptionsFunc( + func(service, region string, options ...interface{}) (aws.Endpoint, error) { + return aws.Endpoint{ + URL: s3URL, + }, nil + }, + ), + ), + ) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to load configuration, %v", err) + os.Exit(1) + } + + // Create an S3 client + client := s3.NewFromConfig(cfg) + + // Replace '/' with '_' in the branch name + safeBranchName := strings.ReplaceAll(branchName, "/", "_") + + // Construct the key for the snapshot file + key := fmt.Sprintf("elys-snapshot-%s.tar.lz4", safeBranchName) + + // Delete the file + _, err = client.DeleteObject(context.TODO(), &s3.DeleteObjectInput{ + Bucket: &bucketName, + Key: &key, + }) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to delete file %q from bucket %q, %v", key, bucketName, err) + os.Exit(1) + } + + fmt.Printf("Successfully deleted %q from %q\n", key, bucketName) +} diff --git a/cmd/upgrade-assure/update-genesis.go b/cmd/upgrade-assure/update-genesis.go index 5d50636e8..0d1638399 100644 --- a/cmd/upgrade-assure/update-genesis.go +++ b/cmd/upgrade-assure/update-genesis.go @@ -101,8 +101,8 @@ func updateGenesis(validatorBalance, homePath, genesisFilePath string) { genesis.AppState.Parameter.Params.BrokerAddress = "elys1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrqau4f4q" // update oracle price expiration - genesis.AppState.Oracle.Params.PriceExpiryTime = "604800" - genesis.AppState.Oracle.Params.LifeTimeInBlocks = "1000000" + genesis.AppState.Oracle.Params.PriceExpiryTime = "31536000" + genesis.AppState.Oracle.Params.LifeTimeInBlocks = "8000000" outputFilePath := homePath + "/config/genesis.json" if err := writeGenesisFile(outputFilePath, genesis); err != nil {