Update tasks.py to deploy via rsync

This commit is contained in:
Jez Cope 2021-09-04 21:19:05 +01:00
parent f71bb12247
commit ab7f933714
2 changed files with 21 additions and 70 deletions

View File

@ -24,8 +24,7 @@ let
py.rich py.rich
py.requests py.requests
py.ruamel_yaml py.ruamel_yaml
py.sh
my_yaspin my_yaspin
]); ]);
in pkgs.mkShell { in pkgs.mkShell { buildInputs = with pkgs; [ pythonWithPackages yarn hugo ]; }
buildInputs = with pkgs; [ pythonWithPackages yarn hugo ];
}

View File

@ -1,75 +1,27 @@
from invoke import task from invoke import task
from yaspin import yaspin
import requests as rq
from pathlib import Path from pathlib import Path
from ruamel.yaml import YAML import sh
yaml = YAML() DEPLOY_DIR = Path(__file__).parent / "public"
REMOTE_DIR = "jemisin.petrichor.me:/srv/www/erambler.co.uk/"
CONFIG = yaml.load(Path("deploy.yaml"))
@task @task
def deploy(c, clean=False): def deploy(c, clean=False):
with yaspin().yellow as s: if clean:
if clean: print("clean and rebuild...")
s.text = "clean and rebuild" sh.rm(DEPLOY_DIR, recursive=True, force=True)
c.run(f"rm --recursive --force {CONFIG['deploy-dir']}", hide=True) sh.hugo()
c.run("nikola build", hide=True) print("done")
s.ok("[done]")
s.text = "add to IPFS" print("push via rsync...")
s.start() sh.rsync(
result = c.run( f"{DEPLOY_DIR}/",
f"ipfs add --recursive --hidden --quieter {CONFIG['deploy-dir']}", REMOTE_DIR,
hide=True, checksum=True,
) archive=True,
new_hash = result.stdout.rstrip() verbose=True,
s.ok("[done]") delete=True,
s.write(f"- published at {new_hash}") _fg=True,
)
pinata(c, new_hash, "erambler") print("done")
update_dnslink(c, f"/ipfs/{new_hash}")
@task
def pinata(c, new_hash, name):
with yaspin(text="pin at pinata").yellow as s:
result = rq.post(
"https://api.pinata.cloud/pinning/pinByHash",
headers={"Authorization": f'Bearer {CONFIG["pinata-key"]}'},
json={
"pinataMetadata": {"name": name},
"hashToPin": new_hash,
},
)
result.raise_for_status()
s.ok("[done]")
details = result.json()
s.write(f'- pin status: {details["status"]}')
@task
def update_dnslink(c, ipfs_path):
with yaspin(text="update dnslink record").yellow as s:
cf_config = CONFIG["cloudflare"]
token = cf_config["token"]
zone_id = cf_config["zone_id"]
record_id = cf_config["record_id"]
result = rq.put(
f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}",
headers={"Authorization": f"Bearer {token}"},
json={
"type": "TXT",
"name": "_dnslink.erambler.co.uk",
"content": f"dnslink={ipfs_path}",
"ttl": 1,
},
)
result.raise_for_status()
details = result.json()
if details["success"]:
s.ok("[done]")
else:
s.fail()