BlueGit

projects-timfalken-com meteors

Files

meteors / ci / deploy.py

deploy.py

deploy.py 7.0 KiB Download Raw
1#!/usr/bin/env python3
2"""FTP-upload of build/. Never --delete.
3
4The path guard (deploy_guard) is the only hard abort: wrong target,
5FTP root, missing meteors leaf. Extra files already on the remote are
6logged and left alone. favicon.ico is never uploaded, so a host- or
7Tim-provided icon is not overwritten.
8"""
9import os
10import subprocess
11import sys
12from pathlib import Path
13from urllib.parse import urlsplit
14
15sys.path.insert(0, str(Path(__file__).resolve().parent))
16from deploy_guard import DeployGuardError, project_dir # noqa: E402
17
18ROOT = Path(__file__).resolve().parents[1]
19
20# Files this project publishes. Used only to label remote leftovers in the
21# log; foreign names no longer abort the deploy (we never --delete).
22OURS = {
23 'index.html', 'game.js', 'framework.js', 'style.css', 'images',
24 'api.php', 'auth.php', 'callback.php', 'logout.php', '.htaccess',
25 'lib', 'data', 'router.php',
26 '.', '..',
27}
28
29# Never upload this name: the remote icon may belong to Tim or the host.
30LEAVE_ON_REMOTE = ('favicon.ico',)
31
32
33def quoted(value):
34 if any(c in value for c in '\r\n\x00'):
35 raise ValueError('Deployment variables must not contain line breaks or NUL bytes.')
36 return '"' + value.replace('\\', '\\\\').replace('"', '\\"') + '"'
37
38
39def host_url(environ):
40 for key in ['timftphost', 'timftpuser', 'timftppassword']:
41 if not environ.get(key):
42 raise ValueError(f'CI variable {key} is not set.')
43 host = environ['timftphost']
44 if '://' not in host:
45 host = 'ftp://' + host
46 parsed = urlsplit(host)
47 if parsed.scheme not in ['ftp', 'ftps', 'sftp'] or not parsed.hostname:
48 raise ValueError('timftphost must be an FTP, FTPS or SFTP host URL.')
49 if parsed.username or parsed.password or parsed.query or parsed.fragment:
50 raise ValueError('Keep login credentials in timftpuser/timftppassword; use a plain host URL.')
51 return host
52
53
54def lftp_preamble(environ, host):
55 return [
56 'set cmd:fail-exit yes',
57 'set net:timeout 20',
58 'set net:max-retries 2',
59 'set net:reconnect-interval-base 3',
60 'set ssl:verify-certificate no',
61 'set ftp:list-options -a',
62 'set sftp:connect-program "ssh -a -x -o StrictHostKeyChecking=accept-new"',
63 'set sftp:auto-confirm yes',
64 'set xfer:use-temp-file yes',
65 f'open --user {quoted(environ["timftpuser"])} --env-password {quoted(host)}',
66 ]
67
68
69def run_lftp(commands, environ):
70 subprocess.run(
71 ['lftp', '--norc'],
72 input='\n'.join(commands) + '\n',
73 text=True,
74 env={**environ, 'LFTP_PASSWORD': environ['timftppassword']},
75 check=True,
76 )
77
78
79def list_remote(environ, target):
80 """Return (exists, names). exists=False means the folder is not there yet."""
81 host = host_url(environ)
82 # Probe with cls; a missing folder makes `cd` fail.
83 probe = lftp_preamble(environ, host) + [
84 'set cmd:fail-exit no',
85 f'cd {quoted(target)} && cls -1 && echo __METEORS_DIR_OK__ || echo __METEORS_DIR_MISSING__',
86 'bye',
87 ]
88 result = subprocess.run(
89 ['lftp', '--norc'],
90 input='\n'.join(probe) + '\n',
91 text=True,
92 env={**environ, 'LFTP_PASSWORD': environ['timftppassword']},
93 check=True,
94 capture_output=True,
95 )
96 out = (result.stdout or '') + (result.stderr or '')
97 if '__METEORS_DIR_MISSING__' in out and '__METEORS_DIR_OK__' not in out:
98 return False, []
99 names = []
100 for line in out.splitlines():
101 name = line.strip().rstrip('/')
102 if not name or name.startswith('__METEORS_') or name.startswith('lftp'):
103 continue
104 # lftp may print status; keep path-like tokens
105 if ' ' in name and not name.startswith('.'):
106 # long listing leftover; take the last field
107 name = name.split()[-1]
108 names.append(posix_basename(name))
109 return True, names
110
111
112def posix_basename(name):
113 return name.rstrip('/').split('/')[-1]
114
115
116def assert_safe_target(exists, names):
117 """Path safety lives in deploy_guard.project_dir. Remote leftovers are fine."""
118 if not exists:
119 print('Remote meteors folder does not exist yet; it will be created.')
120 return
121 foreign = []
122 for name in names:
123 leaf = posix_basename(name)
124 if leaf in OURS or leaf == '' or leaf in LEAVE_ON_REMOTE:
125 continue
126 foreign.append(leaf)
127 if foreign:
128 print(
129 'Opmerking: de remote map bevat ook bestanden die niet bij deze '
130 'deploy horen: '
131 + ', '.join(sorted(set(foreign))[:12])
132 + '. Die blijven staan (geen --delete) en worden niet aangeraakt.'
133 )
134 reserved = [posix_basename(n) for n in names if posix_basename(n) in LEAVE_ON_REMOTE]
135 if reserved:
136 print(
137 'Opmerking: '
138 + ', '.join(sorted(set(reserved)))
139 + ' blijft ongemoeid; we uploaden dat bestand niet.'
140 )
141 if names:
142 print('Remote meteors folder already holds files; updating our own.')
143 else:
144 print('Remote meteors folder is empty; deploying.')
145
146
147def deploy_commands(environ, target, webroot):
148 host = host_url(environ)
149 return lftp_preamble(environ, host) + [
150 f'mkdir -p {quoted(target)} || echo "Using existing project directory"',
151 f'cd {quoted(target)}',
152 # GEEN --delete. Never upload favicon.ico (leave Tim's / the host's).
153 # Never mirror data/: that folder holds Tim's config.php and the
154 # sqlite database. We only ensure data/.htaccess afterwards.
155 'mirror --reverse --no-perms --no-symlinks --max-errors=1 '
156 '--exclude-glob ".git/" --exclude-glob ".git/**" '
157 '--exclude-glob "blue-ci-cd.yml" --exclude-glob "README.md" '
158 '--exclude-glob "favicon.ico" --exclude-glob "**/favicon.ico" '
159 '--exclude-glob "router.php" '
160 '--exclude-glob "data/" --exclude-glob "data/**" '
161 '--exclude-glob "**/config.php" --exclude-glob "**/*.sqlite" '
162 f'{quoted(str(webroot))} .',
163 # Ensure the deny-all htaccess exists; do not touch any other data file.
164 'mkdir -p data || echo "data exists"',
165 f'put {quoted(str(webroot / "data" / ".htaccess"))} -o data/.htaccess',
166 'bye',
167 ]
168
169
170def main():
171 environ = os.environ
172 try:
173 target = project_dir(environ)
174 webroot = ROOT / 'build'
175 if not (webroot / 'index.html').is_file() or not (webroot / 'game.js').is_file():
176 raise ValueError('build/ is incomplete (need index.html and game.js).')
177 if not (webroot / 'framework.js').is_file() or not (webroot / 'images').is_dir():
178 raise ValueError('build/ is incomplete (need framework.js and images/).')
179 exists, names = list_remote(environ, target)
180 assert_safe_target(exists, names)
181 run_lftp(deploy_commands(environ, target, webroot), environ)
182 print('Deploy complete (no --delete).')
183 return 0
184 except (ValueError, DeployGuardError, subprocess.CalledProcessError) as error:
185 print(str(error), file=sys.stderr)
186 return 1
187
188
189if __name__ == '__main__':
190 sys.exit(main())