BlueGit

projects-timfalken-com meteors

Files

meteors / ci / deploy.py

deploy.py

deploy.py 5.7 KiB Download Raw
1#!/usr/bin/env python3
2"""FTP-upload of build/. Never --delete. Refuses a non-empty foreign folder."""
3import os
4import subprocess
5import sys
6from pathlib import Path
7from urllib.parse import urlsplit
8
9sys.path.insert(0, str(Path(__file__).resolve().parent))
10from deploy_guard import DeployGuardError, project_dir # noqa: E402
11
12ROOT = Path(__file__).resolve().parents[1]
13
14# Files we ourselves publish; a first deploy may only land in an empty/new
15# folder, or a folder that already only contains a previous Meteors deploy.
16OURS = {
17 'index.html', 'game.js', 'framework.js', 'style.css', 'images',
18 '.', '..',
19}
20
21
22def quoted(value):
23 if any(c in value for c in '\r\n\x00'):
24 raise ValueError('Deployment variables must not contain line breaks or NUL bytes.')
25 return '"' + value.replace('\\', '\\\\').replace('"', '\\"') + '"'
26
27
28def host_url(environ):
29 for key in ['timftphost', 'timftpuser', 'timftppassword']:
30 if not environ.get(key):
31 raise ValueError(f'CI variable {key} is not set.')
32 host = environ['timftphost']
33 if '://' not in host:
34 host = 'ftp://' + host
35 parsed = urlsplit(host)
36 if parsed.scheme not in ['ftp', 'ftps', 'sftp'] or not parsed.hostname:
37 raise ValueError('timftphost must be an FTP, FTPS or SFTP host URL.')
38 if parsed.username or parsed.password or parsed.query or parsed.fragment:
39 raise ValueError('Keep login credentials in timftpuser/timftppassword; use a plain host URL.')
40 return host
41
42
43def lftp_preamble(environ, host):
44 return [
45 'set cmd:fail-exit yes',
46 'set net:timeout 20',
47 'set net:max-retries 2',
48 'set net:reconnect-interval-base 3',
49 'set ssl:verify-certificate no',
50 'set ftp:list-options -a',
51 'set sftp:connect-program "ssh -a -x -o StrictHostKeyChecking=accept-new"',
52 'set sftp:auto-confirm yes',
53 'set xfer:use-temp-file yes',
54 f'open --user {quoted(environ["timftpuser"])} --env-password {quoted(host)}',
55 ]
56
57
58def run_lftp(commands, environ):
59 subprocess.run(
60 ['lftp', '--norc'],
61 input='\n'.join(commands) + '\n',
62 text=True,
63 env={**environ, 'LFTP_PASSWORD': environ['timftppassword']},
64 check=True,
65 )
66
67
68def list_remote(environ, target):
69 """Return (exists, names). exists=False means the folder is not there yet."""
70 host = host_url(environ)
71 # Probe with cls; a missing folder makes `cd` fail.
72 probe = lftp_preamble(environ, host) + [
73 'set cmd:fail-exit no',
74 f'cd {quoted(target)} && cls -1 && echo __METEORS_DIR_OK__ || echo __METEORS_DIR_MISSING__',
75 'bye',
76 ]
77 result = subprocess.run(
78 ['lftp', '--norc'],
79 input='\n'.join(probe) + '\n',
80 text=True,
81 env={**environ, 'LFTP_PASSWORD': environ['timftppassword']},
82 check=True,
83 capture_output=True,
84 )
85 out = (result.stdout or '') + (result.stderr or '')
86 if '__METEORS_DIR_MISSING__' in out and '__METEORS_DIR_OK__' not in out:
87 return False, []
88 names = []
89 for line in out.splitlines():
90 name = line.strip().rstrip('/')
91 if not name or name.startswith('__METEORS_') or name.startswith('lftp'):
92 continue
93 # lftp may print status; keep path-like tokens
94 if ' ' in name and not name.startswith('.'):
95 # long listing leftover; take the last field
96 name = name.split()[-1]
97 names.append(posix_basename(name))
98 return True, names
99
100
101def posix_basename(name):
102 return name.rstrip('/').split('/')[-1]
103
104
105def assert_safe_target(exists, names):
106 if not exists:
107 print('Remote meteors folder does not exist yet; it will be created.')
108 return
109 unexpected = []
110 for name in names:
111 leaf = posix_basename(name)
112 if leaf in OURS or leaf == '':
113 continue
114 unexpected.append(leaf)
115 if unexpected:
116 raise ValueError(
117 'DEPLOY GEBLOKKEERD: de map meteors op de FTP-server is niet leeg '
118 'en bevat bestanden die niet bij dit project horen: '
119 + ', '.join(sorted(set(unexpected))[:12])
120 + '. Niets overschreven.'
121 )
122 if names:
123 print('Remote meteors folder already holds a previous Meteors deploy; updating.')
124 else:
125 print('Remote meteors folder is empty; deploying.')
126
127
128def deploy_commands(environ, target, webroot):
129 host = host_url(environ)
130 return lftp_preamble(environ, host) + [
131 f'mkdir -p {quoted(target)} || echo "Using existing project directory"',
132 f'cd {quoted(target)}',
133 # GEEN --delete. Excludes only keep CI/docs out if they were ever placed in build/.
134 'mirror --reverse --no-perms --no-symlinks --max-errors=1 '
135 '--exclude-glob ".git/" --exclude-glob ".git/**" '
136 '--exclude-glob "blue-ci-cd.yml" --exclude-glob "README.md" '
137 f'{quoted(str(webroot))} .',
138 'bye',
139 ]
140
141
142def main():
143 environ = os.environ
144 try:
145 target = project_dir(environ)
146 webroot = ROOT / 'build'
147 if not (webroot / 'index.html').is_file() or not (webroot / 'game.js').is_file():
148 raise ValueError('build/ is incomplete (need index.html and game.js).')
149 if not (webroot / 'framework.js').is_file() or not (webroot / 'images').is_dir():
150 raise ValueError('build/ is incomplete (need framework.js and images/).')
151 exists, names = list_remote(environ, target)
152 assert_safe_target(exists, names)
153 run_lftp(deploy_commands(environ, target, webroot), environ)
154 print('Deploy complete (no --delete).')
155 return 0
156 except (ValueError, DeployGuardError, subprocess.CalledProcessError) as error:
157 print(str(error), file=sys.stderr)
158 return 1
159
160
161if __name__ == '__main__':
162 sys.exit(main())