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

ci: delete branch snapshot #508

Merged
merged 2 commits into from
May 17, 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
43 changes: 43 additions & 0 deletions .github/workflows/delete-branch-snapshot.yml
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 5 additions & 4 deletions .github/workflows/software-upgrade-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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:
Expand Down
76 changes: 76 additions & 0 deletions cmd/delete-snapshot/main.go
Original file line number Diff line number Diff line change
@@ -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: <executable> <branch name>")
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)
}
4 changes: 2 additions & 2 deletions cmd/upgrade-assure/update-genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading