]> us-delaporte.pagekite.me Git - dotfiles.git/blob - invoke/luanti-remote/tasks.py
Update help text
[dotfiles.git] / invoke / luanti-remote / tasks.py
1 from invoke import task
2 from invoke import tasks
3 import os
4 from datetime import datetime as dt
5
6 IP=os.environ.get('LUANTI_IP')
7 USER=os.environ.get('LUANTI_USER')
8 HOME=os.environ.get('LUANTI_HOME')
9 LOCAL_HOME=os.environ.get('HOME')
10 TODAY = dt.now().strftime('%Y.%m.%d')
11
12 WORLD='world' # World folder
13
14 USER_AT_IP=f'{USER}@{IP}'
15 SSH=f'ssh {USER}@{IP}'
16 SCP=f'scp {USER}@{IP}'
17 BACKUP_FILE=f'world.{TODAY}.tar'
18 LOCAL_BACKUPS=f'{LOCAL_HOME}/backup/luanti'
19
20 LUANTI=f'{HOME}/luanti'
21 LOCAL_LUANTI=f'{LOCAL_HOME}/.var/app/net.minetest.Minetest/.minetest'
22 MODS=f'{LUANTI}/mods'
23 WORLDS=f'{LUANTI}/worlds'
24 WORLD_CONF=f'{LUANTI}/worlds/{WORLD}/{WORLD}.mt'
25
26
27 SERVE=f'screen -S MINETEST -d -m {LUANTI}/bin/luantiserver --gameid minetest --worldname world'
28
29 def browse(c, url):
30 c.run(f"librewolf {url}")
31
32 def codium(c, path):
33 c.run(f'codium {path}')
34
35 @task
36 def list_mods(c):
37 c.run(f'{SSH} -C ls {MODS}')
38
39 @task
40 def lookup_mod(c, mod):
41 browse(c, f'https://content.luanti.org/packages/?q={mod}')
42
43 @task
44 def backup_mod(c, mod):
45 c.run(f'mkdir -p {LOCAL_BACKUPS}/mods')
46 tmpdir = '/tmp/mod_backups'
47 c.run(f'{SSH} -C mkdir -p {tmpdir}')
48 tarfile = f'{tmpdir}/{mod}.tar'
49 c.run(f'{SSH} -C rm -f {tarfile} 2> /dev/null')
50 c.run(f'{SSH} -C tar cf {tarfile} --directory {MODS} {mod}')
51 c.run(f'{SCP}:{tarfile} {LOCAL_BACKUPS}/mods')
52 c.run(f'ls -l {LOCAL_BACKUPS}/mods')
53 c.run(f'tar --list -f {LOCAL_BACKUPS}/mods/{mod}.tar | tail')
54 c.run(f'tar --list -f {LOCAL_BACKUPS}/mods/{mod}.tar | wc')
55
56 @task
57 def unpack_mod(c, mod):
58 tarfile = f'{LOCAL_BACKUPS}/mods/{mod}.tar'
59 unpack = f'{LOCAL_BACKUPS}/mods/unpack'
60 c.run(f'mkdir -p {unpack}')
61 c.run(f'cp {tarfile} {unpack}/')
62 c.run(f'cd {unpack}; tar xf {unpack}/{mod}.tar')
63 c.run(f'ls {unpack}')
64
65 @task
66 def edit_mod(c, mod):
67 unpack = f'{LOCAL_BACKUPS}/mods/unpack'
68 codium(c, f'{unpack}/{mod}')
69
70 updated_mods = f'{LOCAL_BACKUPS}/updated_mods'
71
72 @task
73 def repack_mod(c, mod):
74 unpacked_mods = f'{LOCAL_BACKUPS}/mods/unpack/'
75 tarfile = f'{updated_mods}/{mod}.tar'
76 c.run(f'tar cf {tarfile} --directory={unpacked_mods} {mod}')
77 c.run(f'mkdir -p {updated_mods}')
78 c.run(f'ls -l {updated_mods}')
79 c.run(f'tar --list -f {updated_mods}/{mod}.tar | tail')
80 c.run(f'tar --list -f {updated_mods}/{mod}.tar | wc')
81
82 # @task(pre=[tasks.call(backup_mod, mod=mod)])
83 @task
84 def push_updated_mod(c, mod):
85 tmpdir = '/tmp/updated_mods'
86 c.run(f'{SSH} -C mkdir -p {tmpdir}')
87 c.run(f'scp {updated_mods}/{mod}.tar {USER_AT_IP}:{tmpdir}')
88 c.run(f'{SSH} -C ls -al {tmpdir}')
89 c.run(f'{SSH} -C tar --list -f {tmpdir}/{mod}.tar')
90 c.run(f'{SSH} -C mv {MODS}/{mod} /tmp/{mod}.{TODAY}.bk')
91 c.run(f'{SSH} -C tar xf {tmpdir}/{mod}.tar --directory {MODS}')
92 c.run(f'{SSH} -C ls -l {MODS}/{mod}')
93
94 @task
95 def list_local_mods(c):
96 c.run(f'ls -al {LOCAL_LUANTI}/mods')
97
98 @task
99 def push_local_mod(c, mod):
100 tarfile = f'{LOCAL_BACKUPS}/mods/{mod}.tar'
101 print(f'Compressing local mod {mod} to {tarfile}')
102 c.run(f'tar cf {tarfile} --directory={LOCAL_LUANTI}/mods {mod}')
103 c.run(f'tar --list -f {tarfile} | tail')
104 c.run(f'tar --list -f {tarfile} | wc')
105
106 print(f'Pushing {tarfile} to {USER_AT_IP}:/tmp')
107 c.run(f'scp {tarfile} {USER_AT_IP}:/tmp')
108 c.run(f'{SSH} -C ls /tmp | grep {mod}')
109
110 print(f'Unpacking {mod}.tar...')
111 c.run(f'{SSH} -C tar xf /tmp/{mod}.tar --directory {MODS}')
112 c.run(f'{SSH} -C ls -l {MODS} | grep {mod}')
113 c.run(f'{SSH} -C ls -l {MODS}/{mod}')
114
115 print(f'Enable {mod} in config')
116 print(f'Remember to enable {mod} in config (invoke enable-mod {mod})')
117
118 @task
119 def enable_mod(c, mod):
120 enable_line = f'load_mod_{mod} = mods/{mod}'
121 result = c.run(f'{SSH} -C cat {WORLD_CONF} | grep {mod} &2>/dev/null')
122 print(result.stdout)
123 if(not mod in result.stdout):
124 print(f'Mod {mod} not found in {WORLD_CONF}. Adding.')
125 c.run(f'{SSH} -C "echo {enable_line} >> {WORLD_CONF}"')
126 else:
127 print(f'{MOD} already enabled in {WORLD_CONF}')
128 print(f'Grep result: {result.stdout}')
129 print("Remember to restart the server (invoke serve)!")
130
131 @task
132 def compress_world(c):
133 c.run(f'{SSH} -C tar cf {HOME}/{BACKUP_FILE} --directory={WORLDS} {WORLD}')
134
135 @task(compress_world)
136 def backup(c):
137 stopped = input("Is the server stopped?")
138 if(stopped != 'y'):
139 print("so go stop the server...")
140 exit()
141 print(f'Backup {HOME}/{BACKUP_FILE} to {LOCAL_BACKUPS}')
142 c.run(f'mkdir -p {LOCAL_BACKUPS}')
143 c.run(f'{SCP}:{BACKUP_FILE} {LOCAL_BACKUPS}')
144 print(f'Backedup up {BACKUP_FILE} to {LOCAL_BACKUPS}')
145 c.run(f'ls -lh {LOCAL_BACKUPS} | grep {WORLD}')
146 c.run(f'tar tvf {LOCAL_BACKUPS}/{BACKUP_FILE}')
147 verify_backup(c)
148
149
150 @task
151 def cli_hint(c):
152 browse(c, 'https://console.kamatera.com/cli')
153
154 @task
155 def cheat_sheets(c):
156 browse(c, 'https://edward.delaporte.us/blog/luanti/')
157
158 @task
159 def verify_backup(c):
160 TAR_FILE=f'{LOCAL_BACKUPS}/{BACKUP_FILE}'
161 DB_FILE='map.sqlite'
162 print(f'Extracting {DB_FILE} from {TAR_FILE}')
163 c.run(f'cd /tmp;tar --extract --file={LOCAL_BACKUPS}/{BACKUP_FILE} {WORLD}/{DB_FILE}')
164 result = c.run(f'echo "PRAGMA quick_check;" | sqlite3 /tmp/{DB_FILE}')
165 # print(f'Check on {DB_FILE}: {result}')
166 # print(f'Exit code: {result.exited}')
167
168 if(not 'ok' in result.stdout):
169 print("=========================================")
170 print("database is corrupt, restore from backup!")
171 print("=========================================")
172
173 @task
174 def check_server(c):
175 result = c.run(f'ssh {USER}@{IP} -C screen -ls')
176
177 @task(verify_backup)
178 def serve(c):
179 print(f"Attempting to connect to {IP}")
180 result = c.run(f'ssh {USER}@{IP} -C {SERVE}')
181 print(f'Server should now be running in a remote screen session.')
182 print(result)