| 1 | #!/usr/bin/env python3 |
| 2 | """FTP-upload of build/. Never --delete. |
| 3 | |
| 4 | The path guard (deploy_guard) is the only hard abort: wrong target, |
| 5 | FTP root, missing allianties leaf. Extra files already on the remote are |
| 6 | logged and left alone. favicon.ico is never uploaded, so a host- or |
| 7 | Tim-provided icon is not overwritten. |
| 8 | """ |
| 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
| 12 | import tempfile |
| 13 | from pathlib import Path |
| 14 | from urllib.parse import urlsplit |
| 15 | |
| 16 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 17 | from deploy_guard import DeployGuardError, project_dir # noqa: E402 |
| 18 | |
| 19 | ROOT = Path(__file__).resolve().parents[1] |
| 20 | |
| 21 | # Files this project publishes. Used only to label remote leftovers in the |
| 22 | # log; foreign names no longer abort the deploy (we never --delete). |
| 23 | OURS = { |
| 24 | 'index.html', 'game.js', 'style.css', 'images', |
| 25 | 'api.php', 'auth.php', 'callback.php', 'logout.php', '.htaccess', |
| 26 | 'lib', 'data', 'router.php', |
| 27 | '.', '..', |
| 28 | } |
| 29 | |
| 30 | # Never upload this name: the remote icon may belong to Tim or the host. |
| 31 | LEAVE_ON_REMOTE = ('favicon.ico',) |
| 32 | |
| 33 | # OAuth file written from CI secrets. Names match BlueGit project secrets. |
| 34 | # The group and sibling repos (verhalenboek, microchess, …) have no |
| 35 | # auth.timfalken.com client secret — only FTP credentials — so this is a |
| 36 | # allianties project secret. client_id is public and optional to override. |
| 37 | AUTH_SECRET_ENV = 'auth_client_secret' |
| 38 | AUTH_CLIENT_ID_ENV = 'auth_client_id' |
| 39 | DEFAULT_AUTH_URL = 'https://auth.timfalken.com' |
| 40 | DEFAULT_BASE_URL = 'https://projects.timfalken.com/allianties' |
| 41 | DEFAULT_CLIENT_ID = 'projects.timfalken.com' |
| 42 | |
| 43 | |
| 44 | def quoted(value): |
| 45 | if any(c in value for c in '\r\n\x00'): |
| 46 | raise ValueError('Deployment variables must not contain line breaks or NUL bytes.') |
| 47 | return '"' + value.replace('\\', '\\\\').replace('"', '\\"') + '"' |
| 48 | |
| 49 | |
| 50 | def php_single_quoted(value): |
| 51 | """A PHP single-quoted string. Raises if the value has a NUL or newline.""" |
| 52 | if any(c in value for c in '\r\n\x00'): |
| 53 | raise ValueError('auth config values must not contain line breaks or NUL bytes.') |
| 54 | return "'" + value.replace('\\', '\\\\').replace("'", "\\'") + "'" |
| 55 | |
| 56 | |
| 57 | def auth_secret(environ): |
| 58 | """Return the stripped secret, or '' if unset/empty. Never print this.""" |
| 59 | raw = environ.get(AUTH_SECRET_ENV) |
| 60 | if raw is None: |
| 61 | return '' |
| 62 | return str(raw).strip() |
| 63 | |
| 64 | |
| 65 | def auth_config_php(environ): |
| 66 | """PHP source for data/config.php, or None if the secret is empty. |
| 67 | |
| 68 | The secret itself is only placed inside a single-quoted PHP string. |
| 69 | Callers must not log the return value. |
| 70 | """ |
| 71 | secret = auth_secret(environ) |
| 72 | if not secret: |
| 73 | return None |
| 74 | client_id = (environ.get(AUTH_CLIENT_ID_ENV) or '').strip() or DEFAULT_CLIENT_ID |
| 75 | auth_url = DEFAULT_AUTH_URL |
| 76 | base_url = DEFAULT_BASE_URL |
| 77 | # Built as a list so the secret is only interpolated via php_single_quoted. |
| 78 | return ( |
| 79 | '<?php\n' |
| 80 | '// Written by CI from the auth_client_secret project secret. Not in git.\n' |
| 81 | 'return [\n' |
| 82 | f" 'auth_url' => {php_single_quoted(auth_url)},\n" |
| 83 | f" 'base_url' => {php_single_quoted(base_url)},\n" |
| 84 | f" 'client_id' => {php_single_quoted(client_id)},\n" |
| 85 | f" 'client_secret' => {php_single_quoted(secret)},\n" |
| 86 | '];\n' |
| 87 | ) |
| 88 | |
| 89 | |
| 90 | def host_url(environ): |
| 91 | for key in ['timftphost', 'timftpuser', 'timftppassword']: |
| 92 | if not environ.get(key): |
| 93 | raise ValueError(f'CI variable {key} is not set.') |
| 94 | host = environ['timftphost'] |
| 95 | if '://' not in host: |
| 96 | host = 'ftp://' + host |
| 97 | parsed = urlsplit(host) |
| 98 | if parsed.scheme not in ['ftp', 'ftps', 'sftp'] or not parsed.hostname: |
| 99 | raise ValueError('timftphost must be an FTP, FTPS or SFTP host URL.') |
| 100 | if parsed.username or parsed.password or parsed.query or parsed.fragment: |
| 101 | raise ValueError('Keep login credentials in timftpuser/timftppassword; use a plain host URL.') |
| 102 | return host |
| 103 | |
| 104 | |
| 105 | def lftp_preamble(environ, host): |
| 106 | return [ |
| 107 | 'set cmd:fail-exit yes', |
| 108 | 'set net:timeout 20', |
| 109 | 'set net:max-retries 2', |
| 110 | 'set net:reconnect-interval-base 3', |
| 111 | 'set ssl:verify-certificate no', |
| 112 | 'set ftp:list-options -a', |
| 113 | 'set sftp:connect-program "ssh -a -x -o StrictHostKeyChecking=accept-new"', |
| 114 | 'set sftp:auto-confirm yes', |
| 115 | 'set xfer:use-temp-file yes', |
| 116 | f'open --user {quoted(environ["timftpuser"])} --env-password {quoted(host)}', |
| 117 | ] |
| 118 | |
| 119 | |
| 120 | def run_lftp(commands, environ): |
| 121 | subprocess.run( |
| 122 | ['lftp', '--norc'], |
| 123 | input='\n'.join(commands) + '\n', |
| 124 | text=True, |
| 125 | env={**environ, 'LFTP_PASSWORD': environ['timftppassword']}, |
| 126 | check=True, |
| 127 | ) |
| 128 | |
| 129 | |
| 130 | def list_remote(environ, target): |
| 131 | """Return (exists, names). exists=False means the folder is not there yet.""" |
| 132 | host = host_url(environ) |
| 133 | # Probe with cls; a missing folder makes `cd` fail. |
| 134 | probe = lftp_preamble(environ, host) + [ |
| 135 | 'set cmd:fail-exit no', |
| 136 | f'cd {quoted(target)} && cls -1 && echo __ALLIANTIES_DIR_OK__ || echo __ALLIANTIES_DIR_MISSING__', |
| 137 | 'bye', |
| 138 | ] |
| 139 | result = subprocess.run( |
| 140 | ['lftp', '--norc'], |
| 141 | input='\n'.join(probe) + '\n', |
| 142 | text=True, |
| 143 | env={**environ, 'LFTP_PASSWORD': environ['timftppassword']}, |
| 144 | check=True, |
| 145 | capture_output=True, |
| 146 | ) |
| 147 | out = (result.stdout or '') + (result.stderr or '') |
| 148 | if '__ALLIANTIES_DIR_MISSING__' in out and '__ALLIANTIES_DIR_OK__' not in out: |
| 149 | return False, [] |
| 150 | names = [] |
| 151 | for line in out.splitlines(): |
| 152 | name = line.strip().rstrip('/') |
| 153 | if not name or name.startswith('__ALLIANTIES_') or name.startswith('lftp'): |
| 154 | continue |
| 155 | # lftp may print status; keep path-like tokens |
| 156 | if ' ' in name and not name.startswith('.'): |
| 157 | # long listing leftover; take the last field |
| 158 | name = name.split()[-1] |
| 159 | names.append(posix_basename(name)) |
| 160 | return True, names |
| 161 | |
| 162 | |
| 163 | def posix_basename(name): |
| 164 | return name.rstrip('/').split('/')[-1] |
| 165 | |
| 166 | |
| 167 | def assert_safe_target(exists, names): |
| 168 | """Path safety lives in deploy_guard.project_dir. Remote leftovers are fine.""" |
| 169 | if not exists: |
| 170 | print('Remote allianties folder does not exist yet; it will be created.') |
| 171 | return |
| 172 | foreign = [] |
| 173 | for name in names: |
| 174 | leaf = posix_basename(name) |
| 175 | if leaf in OURS or leaf == '' or leaf in LEAVE_ON_REMOTE: |
| 176 | continue |
| 177 | foreign.append(leaf) |
| 178 | if foreign: |
| 179 | print( |
| 180 | 'Opmerking: de remote map bevat ook bestanden die niet bij deze ' |
| 181 | 'deploy horen: ' |
| 182 | + ', '.join(sorted(set(foreign))[:12]) |
| 183 | + '. Die blijven staan (geen --delete) en worden niet aangeraakt.' |
| 184 | ) |
| 185 | reserved = [posix_basename(n) for n in names if posix_basename(n) in LEAVE_ON_REMOTE] |
| 186 | if reserved: |
| 187 | print( |
| 188 | 'Opmerking: ' |
| 189 | + ', '.join(sorted(set(reserved))) |
| 190 | + ' blijft ongemoeid; we uploaden dat bestand niet.' |
| 191 | ) |
| 192 | if names: |
| 193 | print('Remote allianties folder already holds files; updating our own.') |
| 194 | else: |
| 195 | print('Remote allianties folder is empty; deploying.') |
| 196 | |
| 197 | |
| 198 | def deploy_commands(environ, target, webroot, auth_config_path=None): |
| 199 | host = host_url(environ) |
| 200 | commands = lftp_preamble(environ, host) + [ |
| 201 | f'mkdir -p {quoted(target)} || echo "Using existing project directory"', |
| 202 | f'cd {quoted(target)}', |
| 203 | # GEEN --delete. Never upload favicon.ico (leave Tim's / the host's). |
| 204 | # Never mirror data/: that folder holds the sqlite database and |
| 205 | # sessions. We only ensure data/.htaccess afterwards, and optionally |
| 206 | # write data/config.php from auth_client_secret (never from the |
| 207 | # build tree, which must not contain a secret). |
| 208 | 'mirror --reverse --no-perms --no-symlinks --max-errors=1 ' |
| 209 | '--exclude-glob ".git/" --exclude-glob ".git/**" ' |
| 210 | '--exclude-glob "blue-ci-cd.yml" --exclude-glob "README.md" ' |
| 211 | '--exclude-glob "favicon.ico" --exclude-glob "**/favicon.ico" ' |
| 212 | '--exclude-glob "router.php" ' |
| 213 | '--exclude-glob "data/" --exclude-glob "data/**" ' |
| 214 | '--exclude-glob "**/config.php" --exclude-glob "**/*.sqlite" ' |
| 215 | f'{quoted(str(webroot))} .', |
| 216 | # Ensure the deny-all htaccess exists; do not delete any data file. |
| 217 | 'mkdir -p data || echo "data exists"', |
| 218 | f'put {quoted(str(webroot / "data" / ".htaccess"))} -o data/.htaccess', |
| 219 | ] |
| 220 | if auth_config_path: |
| 221 | # The lftp script only has the local path, never the secret. |
| 222 | commands.append( |
| 223 | f'put {quoted(str(auth_config_path))} -o data/config.php' |
| 224 | ) |
| 225 | commands.append('bye') |
| 226 | return commands |
| 227 | |
| 228 | |
| 229 | def main(): |
| 230 | environ = os.environ |
| 231 | try: |
| 232 | target = project_dir(environ) |
| 233 | webroot = ROOT / 'build' |
| 234 | if not (webroot / 'index.html').is_file() or not (webroot / 'game.js').is_file(): |
| 235 | raise ValueError('build/ is incomplete (need index.html and game.js).') |
| 236 | if not (webroot / 'style.css').is_file() or not (webroot / 'images').is_dir(): |
| 237 | raise ValueError('build/ is incomplete (need style.css and images/).') |
| 238 | exists, names = list_remote(environ, target) |
| 239 | assert_safe_target(exists, names) |
| 240 | php = auth_config_php(environ) |
| 241 | auth_path = None |
| 242 | tmpdir = None |
| 243 | try: |
| 244 | if php is not None: |
| 245 | tmpdir = tempfile.TemporaryDirectory(prefix='allianties-auth-') |
| 246 | auth_path = Path(tmpdir.name) / 'config.php' |
| 247 | # 0o600: the secret lives in this file only until lftp puts it. |
| 248 | fd = os.open(auth_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) |
| 249 | try: |
| 250 | os.write(fd, php.encode('utf-8')) |
| 251 | finally: |
| 252 | os.close(fd) |
| 253 | print('Writing data/config.php from CI secret auth_client_secret.') |
| 254 | else: |
| 255 | print( |
| 256 | 'CI secret auth_client_secret is empty or unset; ' |
| 257 | 'leaving data/config.php alone.' |
| 258 | ) |
| 259 | run_lftp( |
| 260 | deploy_commands(environ, target, webroot, auth_config_path=auth_path), |
| 261 | environ, |
| 262 | ) |
| 263 | finally: |
| 264 | if tmpdir is not None: |
| 265 | tmpdir.cleanup() |
| 266 | print('Deploy complete (no --delete).') |
| 267 | return 0 |
| 268 | except (ValueError, DeployGuardError, subprocess.CalledProcessError) as error: |
| 269 | print(str(error), file=sys.stderr) |
| 270 | return 1 |
| 271 | |
| 272 | |
| 273 | if __name__ == '__main__': |
| 274 | sys.exit(main()) |
projects-timfalken-com allianties