Skip to content

Commit

Permalink
fixing snapshot header wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
fraidev committed Nov 17, 2023
1 parent a705b08 commit 284e3d7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
36 changes: 26 additions & 10 deletions cmd/photographer/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@ import (
"github.com/marigold-dev/tezos-snapshot/pkg/snapshot"
)

func createSnapshot(historyMode snapshot.HistoryModeType) {
script := "mkdir -p /var/run/tezos/snapshots && cd /var/run/tezos/snapshots && /usr/local/bin/octez-node snapshot export --block head~30 --data-dir /var/run/tezos/node/data"
type SnapshotExec struct {
snapshotsPath string
tezosPath string
octezNodeBinPath string
}

func newSnapshotExec(snapshotsPath, tezosPath, octezNodePath string) *SnapshotExec {
return &SnapshotExec{snapshotsPath, tezosPath, octezNodePath}
}

func (s *SnapshotExec) CreateSnapshot(historyMode snapshot.HistoryModeType) {
script := "mkdir -p " + s.snapshotsPath + " && cd " + s.snapshotsPath + " && " + s.octezNodeBinPath + " snapshot export --data-dir " + s.tezosPath

if historyMode == snapshot.ROLLING {
script = script + " --rolling"
}

_, _ = execScript(script)
_, _ = s.execScript(script)
}

func getSnapshotName(historyMode snapshot.HistoryModeType) (string, error) {
func (s *SnapshotExec) GetSnapshotName(historyMode snapshot.HistoryModeType) (string, error) {
log.Println("Getting snapshot names.")
script := "mkdir -p /var/run/tezos/snapshots && cd /var/run/tezos/snapshots && /bin/ls -1a"
stdout, _ := execScript(script)
script := "mkdir -p " + s.snapshotsPath + " && cd " + s.snapshotsPath + " && /bin/ls -1a"
stdout, _ := s.execScript(script)

snapshotfilenames := strings.Split(stdout.String(), "\n")
log.Printf("All files found: %v \n", snapshotfilenames)
Expand All @@ -38,15 +48,21 @@ func getSnapshotName(historyMode snapshot.HistoryModeType) (string, error) {
return "", fmt.Errorf("Snapshot file not found.")
}

func getSnapshotHeaderOutput(filepath string) string {
func (s *SnapshotExec) GetSnapshotHeaderOutput(filepath string) string {
log.Printf("Getting snapshot header output for file: %q. \n", filepath)
script := "/usr/local/bin/octez-node snapshot info --json /var/run/tezos/snapshots/" + filepath
stdout, _ := execScript(script)
script := s.octezNodeBinPath + " snapshot info --json " + s.snapshotsPath + "/" + filepath
stdout, _ := s.execScript(script)
log.Printf("Snapshot header output: %q. \n", stdout.String())
return stdout.String()
}

func execScript(script string) (bytes.Buffer, bytes.Buffer) {
func (s *SnapshotExec) DeleteLocalSnapshots() {
log.Println("Deleting local snapshots.")
script := "rm -rf " + s.snapshotsPath + "/*"
_, _ = s.execScript(script)
}

func (s *SnapshotExec) execScript(script string) (bytes.Buffer, bytes.Buffer) {
cmd := exec.Command("sh", "-c", script)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
Expand Down
20 changes: 14 additions & 6 deletions cmd/photographer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func task() {
maxDays := util.GetEnvInt("MAX_DAYS", 7)
maxMonths := util.GetEnvInt("MAX_MONTHS", 6)
network := strings.ToLower(os.Getenv("NETWORK"))
snapshotsPath := util.GetEnvString("SNAPSHOTS_PATH", "/var/run/tezos/snapshots")
octezNodepath := util.GetEnvString("OCTEZ_NODE_PATH", "/usr/local/bin/octez-node")
tezosPath := util.GetEnvString("TEZOS_PATH", "/var/run/tezos/node")

snapshotExec := newSnapshotExec(snapshotsPath, octezNodepath, tezosPath)

if bucketName == "" {
log.Fatalln("The BUCKET_NAME environment variable is empty.")
Expand All @@ -59,17 +64,20 @@ func task() {
snapshotStorage := store.NewSnapshotStorage(client, bucketName)

// Check if today the rolling snapshot already exists
execute(ctx, snapshotStorage, snapshot.ROLLING, network)
execute(ctx, snapshotStorage, snapshot.ROLLING, network, snapshotExec)

// Check if today the full snapshot already exists
execute(ctx, snapshotStorage, snapshot.FULL, network)
execute(ctx, snapshotStorage, snapshot.FULL, network, snapshotExec)

snapshotStorage.DeleteExpiredSnapshots(ctx, maxDays, maxMonths)

// Delete local snapshots
snapshotExec.DeleteLocalSnapshots()

log.Printf("Snapshot job took %s", time.Since(start))
}

func execute(ctx context.Context, snapshotStorage *store.SnapshotStorage, historyMode snapshot.HistoryModeType, chain string) {
func execute(ctx context.Context, snapshotStorage *store.SnapshotStorage, historyMode snapshot.HistoryModeType, chain string, snapshotExec *SnapshotExec) {
todayItems := snapshotStorage.GetTodaySnapshotsItems(ctx)

alreadyExist := lo.SomeBy(todayItems, func(item snapshot.SnapshotItem) bool {
Expand All @@ -81,12 +89,12 @@ func execute(ctx context.Context, snapshotStorage *store.SnapshotStorage, histor
return
}

createSnapshot(historyMode)
snapshotfilename, err := getSnapshotName(historyMode)
snapshotExec.CreateSnapshot(historyMode)
snapshotfilename, err := snapshotExec.GetSnapshotName(historyMode)
if err != nil {
log.Fatalf("%v \n", err)
}
snapshotHeaderOutput := getSnapshotHeaderOutput(snapshotfilename)
snapshotHeaderOutput := snapshotExec.GetSnapshotHeaderOutput(snapshotfilename)

snapshotStorage.EphemeralUpload(ctx, snapshotfilename, snapshotHeaderOutput)
}
13 changes: 9 additions & 4 deletions pkg/snapshot/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ type SnapshotHeader struct {
Timestamp string `json:"timestamp"`
}

type WrapperSnapshotHeader struct {
SnapshotHeader SnapshotHeader `json:"snapshot_header"`
}

func SnapshotHeaderFromJson(snapshotHeaderOutput string) (*SnapshotHeader, error) {
var snapshotHeader SnapshotHeader
var snapshotHeader WrapperSnapshotHeader
err := json.Unmarshal([]byte(snapshotHeaderOutput), &snapshotHeader)
if err != nil {
return nil, err
}

return &snapshotHeader, nil
return &snapshotHeader.SnapshotHeader, nil
}

// Example: TEZOS_MAINNET_2021-01-01_00-00 to mainnet
// Example: TEZOS_ITHACANET_2022-01-25T15:00:00Z to ghostnet
// Example: TEZOS_MAINNETrolling to mainnet
func (s *SnapshotHeader) SanitizeChainame() string {
parts := strings.Split(s.ChaiName, "_")
chainName := strings.ToLower(parts[len(parts)-1])
chainName := strings.ToLower(parts[1])
if chainName == "ithacanet" {
chainName = "ghostnet"
}
Expand Down

0 comments on commit 284e3d7

Please sign in to comment.