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