Files
2026-11-11 14:22:02 +08:00

44 lines
1.8 KiB
Python

def sha(p):
return hashlib.sha256(open(p, 'rb').read()).hexdigest()
def run(args, timeout=90):
p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=timeout)
if p.returncode:
with open(WORK + '/last-command-error.log', 'w') as f:
f.write(p.stderr)
os.chmod(WORK + '/last-command-error.log', 384)
raise RuntimeError('command failed: ' + args[0] + ', private error log saved')
return p.stdout
def health(port, expected_enabled):
for i in range(60):
try:
with urllib.request.urlopen('http://127.0.0.1:' + str(port) + '/api/v2/health', timeout=2) as r:
assert r.status == 200
with urllib.request.urlopen('http://127.0.0.1:' + str(port) + '/api/v2/auth/group/config', timeout=2) as r:
c = json.load(r)
assert c.get('enabled') is expected_enabled
if expected_enabled:
assert c.get('redirect_uri') == 'http://im.zhenyangtang.cn:8369/auth/group-login' and c.get('http_callback_allowed') is True
return
except Exception:
time.sleep(1)
raise RuntimeError('API readiness timeout on port ' + str(port))
def replace_file(path, content):
st = os.stat(path)
temporary = path + '.group-identity-staged'
assert not os.path.lexists(temporary)
with open(temporary, 'wb') as f:
f.write(content.encode('utf-8') if isinstance(content, str) else content)
os.chmod(temporary, st.st_mode & 511)
os.chown(temporary, st.st_uid, st.st_gid)
os.replace(temporary, path)
def reload_nginx():
run([NGINX, '-t'])
run([NGINX, '-s', 'reload'])
def env_yaml(env):
return ''.join((' ' + k + ': ' + json.dumps(v.replace('$', '$$')) + '\n' for k, v in env.items()))