BlueGit

projects-timfalken-com meteors

Files

meteors / ci / deploy.py

deploy.py

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