Skip to content

Commit

Permalink
Pipelines automation
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiimk committed Apr 19, 2024
1 parent ae6ba4a commit 71d0aaf
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 17 deletions.
70 changes: 70 additions & 0 deletions .ci/example-datasets-mt/demo-env-force-update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python

import os
import sys
import subprocess
import json

sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from common import *

###############################################################################

S3_DEMO_REPO_URL = "s3://repo.demo.stg.kamu.dev.us-west-2/"

###############################################################################

def list_datasets_in_s3(repo_url):
for did in s3_listdir(repo_url):
url = repo_url + did
alias = s3_cat(f"{repo_url}{did}info/alias")
account, name = alias.split('/', 1)
yield url, did, account, name

def push_local_dataset_to_s3(dataset, repo_url):
id = dataset["ID"].removeprefix("did:odf:")
account = dataset["Owner"]
name = dataset["Name"]

url = f"{repo_url}{id}/"

subprocess.run(
f"kamu --account {account} push {name} --to {url}",
shell=True,
check=True,
)

# Set account and alias
subprocess.run(
f"aws s3 cp - {url}info/alias",
input=f"{account}/{name}".encode("utf8"),
shell=True,
check=True,
)


# List local datasets
local_datasets = {
(ds["Owner"], ds["Name"]): ds
for ds in json.loads(subprocess.check_output(
"kamu list --all-accounts --output-format json --wide",
shell=True,
))
}

# Replace remote datasets of matching names with local versions, keeping the others
for (remote_ds_url, did, account, name) in list_datasets_in_s3(S3_DEMO_REPO_URL):
local_dataset = local_datasets.get((account, name))
if local_dataset is not None:
print(f">>> Deleting {account}/{name} ({did})")
subprocess.run(
f"aws s3 rm --recursive {remote_ds_url}",
input=f"{account}/{name}".encode("utf8"),
shell=True,
check=True,
)
new_id = local_dataset["ID"]
print(f">>> Pushing {account}/{name} ({new_id})")
push_local_dataset_to_s3(local_dataset, S3_DEMO_REPO_URL)
else:
print(f">>> Keeping {account}/{name} ({did})")
61 changes: 44 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,42 +1,69 @@
KAMU_WORKSPACE=.priv/workspace-contrib
DATE:=$(shell date +%F)
S3_CONTRIB_BASE_URL=s3://datasets.kamu.dev/odf/v2/contrib/
S3_BACKUP_BASE_URL=s3://datasets.kamu.dev/odf/backup/v2/contrib/

###############################################################################
# Repo maintenance
###############################################################################

.PHONY: strip-notebooks
strip-notebooks:
find . -name '.ipynb_checkpoints' -type d -prune -exec rm -rf {} \;
find . -name '*.ipynb' -type f -exec nbstripout {} \;


.PHONY: backup
backup:
###############################################################################
# Datasets upkeep
###############################################################################

.PHONY: local-compact-hard
local-compact-hard:
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu system compact --hard "net.rocketpool.reth.mint-burn"
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu system compact --hard "com.cryptocompare.ohlcv.eth-usd"
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu system compact --hard "co.alphavantage.tickers.daily.spy"


.PHONY: local-pull
local-pull:
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu pull --all --fetch-uncacheable


###############################################################################
# S3
###############################################################################

.PHONY: s3-backup
s3-backup:
aws s3 cp --recursive "$(S3_CONTRIB_BASE_URL)net.rocketpool.reth.mint-burn" "$(S3_BACKUP_BASE_URL)net.rocketpool.reth.mint-burn.$(DATE)"
aws s3 cp --recursive "$(S3_CONTRIB_BASE_URL)com.cryptocompare.ohlcv.eth-usd" "$(S3_BACKUP_BASE_URL)com.cryptocompare.ohlcv.eth-usd.$(DATE)"
aws s3 cp --recursive "$(S3_CONTRIB_BASE_URL)co.alphavantage.tickers.daily.spy" "$(S3_BACKUP_BASE_URL)co.alphavantage.tickers.daily.spy.$(DATE)"


.PHONY: purge
purge:
.PHONY: s3-purge
s3-purge:
aws s3 rm --recursive "$(S3_CONTRIB_BASE_URL)net.rocketpool.reth.mint-burn"
aws s3 rm --recursive "$(S3_CONTRIB_BASE_URL)com.cryptocompare.ohlcv.eth-usd"
aws s3 rm --recursive "$(S3_CONTRIB_BASE_URL)co.alphavantage.tickers.daily.spy"

.PHONY: sync-from-s3
sync-from-s3:
mkdir -p $(KAMU_WORKSPACE)
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu init --exists-ok
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu pull "$(S3_CONTRIB_BASE_URL)net.rocketpool.reth.mint-burn"
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu pull "$(S3_CONTRIB_BASE_URL)com.cryptocompare.ohlcv.eth-usd"
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu pull "$(S3_CONTRIB_BASE_URL)co.alphavantage.tickers.daily.spy"

.PHONY: pull
pull:
kamu pull "$(S3_CONTRIB_BASE_URL)net.rocketpool.reth.mint-burn"
kamu pull "$(S3_CONTRIB_BASE_URL)com.cryptocompare.ohlcv.eth-usd"
kamu pull "$(S3_CONTRIB_BASE_URL)co.alphavantage.tickers.daily.spy"
.PHONY: sync-to-s3
sync-to-s3:
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu push net.rocketpool.reth.mint-burn --to "$(S3_CONTRIB_BASE_URL)net.rocketpool.reth.mint-burn" --no-alias
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu push com.cryptocompare.ohlcv.eth-usd --to "$(S3_CONTRIB_BASE_URL)com.cryptocompare.ohlcv.eth-usd" --no-alias
KAMU_WORKSPACE=$(KAMU_WORKSPACE) kamu push co.alphavantage.tickers.daily.spy --to "$(S3_CONTRIB_BASE_URL)co.alphavantage.tickers.daily.spy" --no-alias

.PHONY: update
update:
kamu pull net.rocketpool.reth.mint-burn com.cryptocompare.ohlcv.eth-usd co.alphavantage.tickers.daily.spy

.PHONY: push
push:
kamu push net.rocketpool.reth.mint-burn --to "$(S3_CONTRIB_BASE_URL)net.rocketpool.reth.mint-burn" --no-alias
kamu push com.cryptocompare.ohlcv.eth-usd --to "$(S3_CONTRIB_BASE_URL)com.cryptocompare.ohlcv.eth-usd" --no-alias
kamu push co.alphavantage.tickers.daily.spy --to "$(S3_CONTRIB_BASE_URL)co.alphavantage.tickers.daily.spy" --no-alias
###############################################################################
# IPFS
###############################################################################

.PHONY: push-ipfs
push-ipfs:
Expand Down

0 comments on commit 71d0aaf

Please sign in to comment.