X-Git-Url: https://apps.delaporte.us/gitweb/dotfiles.git/blobdiff_plain/fe6a19ad1ef52f133f8c3522aa5062e1db39b4e4..8900458299fe6eb84828299e3a3ae46d6ad944f8:/invoke/luanti-remote/tasks.py diff --git a/invoke/luanti-remote/tasks.py b/invoke/luanti-remote/tasks.py index e69de29..e6d92d7 100644 --- a/invoke/luanti-remote/tasks.py +++ b/invoke/luanti-remote/tasks.py @@ -0,0 +1,182 @@ +from invoke import task +from invoke import tasks +import os +from datetime import datetime as dt + +IP=os.environ.get('LUANTI_IP') +USER=os.environ.get('LUANTI_USER') +HOME=os.environ.get('LUANTI_HOME') +LOCAL_HOME=os.environ.get('HOME') +TODAY = dt.now().strftime('%Y.%m.%d') + +WORLD='world' # World folder + +USER_AT_IP=f'{USER}@{IP}' +SSH=f'ssh {USER}@{IP}' +SCP=f'scp {USER}@{IP}' +BACKUP_FILE=f'world.{TODAY}.tar' +LOCAL_BACKUPS=f'{LOCAL_HOME}/backup/luanti' + +LUANTI=f'{HOME}/luanti' +LOCAL_LUANTI=f'{LOCAL_HOME}/.var/app/net.minetest.Minetest/.minetest' +MODS=f'{LUANTI}/mods' +WORLDS=f'{LUANTI}/worlds' +WORLD_CONF=f'{LUANTI}/worlds/{WORLD}/{WORLD}.mt' + + +SERVE=f'screen -S MINETEST -d -m {LUANTI}/bin/luantiserver --gameid minetest --worldname world' + +def browse(c, url): + c.run(f"librewolf {url}") + +def codium(c, path): + c.run(f'codium {path}') + +@task +def list_mods(c): + c.run(f'{SSH} -C ls {MODS}') + +@task +def lookup_mod(c, mod): + browse(c, f'https://content.luanti.org/packages/?q={mod}') + +@task +def backup_mod(c, mod): + c.run(f'mkdir -p {LOCAL_BACKUPS}/mods') + tmpdir = '/tmp/mod_backups' + c.run(f'{SSH} -C mkdir -p {tmpdir}') + tarfile = f'{tmpdir}/{mod}.tar' + c.run(f'{SSH} -C rm -f {tarfile} 2> /dev/null') + c.run(f'{SSH} -C tar cf {tarfile} --directory {MODS} {mod}') + c.run(f'{SCP}:{tarfile} {LOCAL_BACKUPS}/mods') + c.run(f'ls -l {LOCAL_BACKUPS}/mods') + c.run(f'tar --list -f {LOCAL_BACKUPS}/mods/{mod}.tar | tail') + c.run(f'tar --list -f {LOCAL_BACKUPS}/mods/{mod}.tar | wc') + +@task +def unpack_mod(c, mod): + tarfile = f'{LOCAL_BACKUPS}/mods/{mod}.tar' + unpack = f'{LOCAL_BACKUPS}/mods/unpack' + c.run(f'mkdir -p {unpack}') + c.run(f'cp {tarfile} {unpack}/') + c.run(f'cd {unpack}; tar xf {unpack}/{mod}.tar') + c.run(f'ls {unpack}') + +@task +def edit_mod(c, mod): + unpack = f'{LOCAL_BACKUPS}/mods/unpack' + codium(c, f'{unpack}/{mod}') + +updated_mods = f'{LOCAL_BACKUPS}/updated_mods' + +@task +def repack_mod(c, mod): + unpacked_mods = f'{LOCAL_BACKUPS}/mods/unpack/' + tarfile = f'{updated_mods}/{mod}.tar' + c.run(f'tar cf {tarfile} --directory={unpacked_mods} {mod}') + c.run(f'mkdir -p {updated_mods}') + c.run(f'ls -l {updated_mods}') + c.run(f'tar --list -f {updated_mods}/{mod}.tar | tail') + c.run(f'tar --list -f {updated_mods}/{mod}.tar | wc') + +# @task(pre=[tasks.call(backup_mod, mod=mod)]) +@task +def push_updated_mod(c, mod): + tmpdir = '/tmp/updated_mods' + c.run(f'{SSH} -C mkdir -p {tmpdir}') + c.run(f'scp {updated_mods}/{mod}.tar {USER_AT_IP}:{tmpdir}') + c.run(f'{SSH} -C ls -al {tmpdir}') + c.run(f'{SSH} -C tar --list -f {tmpdir}/{mod}.tar') + c.run(f'{SSH} -C mv {MODS}/{mod} /tmp/{mod}.{TODAY}.bk') + c.run(f'{SSH} -C tar xf {tmpdir}/{mod}.tar --directory {MODS}') + c.run(f'{SSH} -C ls -l {MODS}/{mod}') + +@task +def list_local_mods(c): + c.run(f'ls -al {LOCAL_LUANTI}/mods') + +@task +def push_local_mod(c, mod): + tarfile = f'{LOCAL_BACKUPS}/mods/{mod}.tar' + print(f'Compressing local mod {mod} to {tarfile}') + c.run(f'tar cf {tarfile} --directory={LOCAL_LUANTI}/mods {mod}') + c.run(f'tar --list -f {tarfile} | tail') + c.run(f'tar --list -f {tarfile} | wc') + + print(f'Pushing {tarfile} to {USER_AT_IP}:/tmp') + c.run(f'scp {tarfile} {USER_AT_IP}:/tmp') + c.run(f'{SSH} -C ls /tmp | grep {mod}') + + print(f'Unpacking {mod}.tar...') + c.run(f'{SSH} -C tar xf /tmp/{mod}.tar --directory {MODS}') + c.run(f'{SSH} -C ls -l {MODS} | grep {mod}') + c.run(f'{SSH} -C ls -l {MODS}/{mod}') + + print(f'Enable {mod} in config') + print(f'Remember to enable {mod} in config (invoke enable-mod {mod})') + +@task +def enable_mod(c, mod): + enable_line = f'load_mod_{mod} = mods/{mod}' + result = c.run(f'{SSH} -C cat {WORLD_CONF} | grep {mod} &2>/dev/null') + print(result.stdout) + if(not mod in result.stdout): + print(f'Mod {mod} not found in {WORLD_CONF}. Adding.') + c.run(f'{SSH} -C "echo {enable_line} >> {WORLD_CONF}"') + else: + print(f'{MOD} already enabled in {WORLD_CONF}') + print(f'Grep result: {result.stdout}') + print("Remember to restart the server (invoke serve)!") + +@task +def compress_world(c): + c.run(f'{SSH} -C tar cf {HOME}/{BACKUP_FILE} --directory={WORLDS} {WORLD}') + +@task(compress_world) +def backup(c): + stopped = input("Is the server stopped?") + if(stopped != 'y'): + print("so go stop the server...") + exit() + print(f'Backup {HOME}/{BACKUP_FILE} to {LOCAL_BACKUPS}') + c.run(f'mkdir -p {LOCAL_BACKUPS}') + c.run(f'{SCP}:{BACKUP_FILE} {LOCAL_BACKUPS}') + print(f'Backedup up {BACKUP_FILE} to {LOCAL_BACKUPS}') + c.run(f'ls -lh {LOCAL_BACKUPS} | grep {WORLD}') + c.run(f'tar tvf {LOCAL_BACKUPS}/{BACKUP_FILE}') + verify_backup(c) + + +@task +def cli_hint(c): + browse(c, 'https://console.kamatera.com/cli') + +@task +def cheat_sheets(c): + browse(c, 'https://edward.delaporte.us/blog/luanti/') + +@task +def verify_backup(c): + TAR_FILE=f'{LOCAL_BACKUPS}/{BACKUP_FILE}' + DB_FILE='map.sqlite' + print(f'Extracting {DB_FILE} from {TAR_FILE}') + c.run(f'cd /tmp;tar --extract --file={LOCAL_BACKUPS}/{BACKUP_FILE} {WORLD}/{DB_FILE}') + result = c.run(f'echo "PRAGMA quick_check;" | sqlite3 /tmp/{DB_FILE}') + # print(f'Check on {DB_FILE}: {result}') + # print(f'Exit code: {result.exited}') + + if(not 'ok' in result.stdout): + print("=========================================") + print("database is corrupt, restore from backup!") + print("=========================================") + +@task +def check_server(c): + result = c.run(f'ssh {USER}@{IP} -C screen -ls') + +@task(verify_backup) +def serve(c): + print(f"Attempting to connect to {IP}") + result = c.run(f'ssh {USER}@{IP} -C {SERVE}') + print(f'Server should now be running in a remote screen session.') + print(result)