| 1 | #!/usr/bin/env python3 |
| 2 | """Upload a signed IPA for one device to the OTA service and print its install link.""" |
| 3 | import argparse |
| 4 | import plistlib |
| 5 | import re |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import zipfile |
| 9 | |
| 10 | from deploy import ssh |
| 11 | |
| 12 | SERVER = "https://ota.paperclover.net" |
| 13 | PROFILE = re.compile(r"Payload/[^/]+\.app/embedded\.mobileprovision\Z") |
| 14 | |
| 15 | |
| 16 | def provisioned(ipa): |
| 17 | with zipfile.ZipFile(ipa) as archive: |
| 18 | name = next((name for name in archive.namelist() if PROFILE.fullmatch(name)), None) |
| 19 | if name is None: |
| 20 | return None |
| 21 | signed = archive.read(name) |
| 22 | return plistlib.loads(signed[signed.index(b"<?xml"):signed.rindex(b"</plist>") + 8]) |
| 23 | |
| 24 | |
| 25 | def main(): |
| 26 | parser = argparse.ArgumentParser(description=__doc__) |
| 27 | parser.add_argument("ipa") |
| 28 | parser.add_argument("udid") |
| 29 | parser.add_argument("--server", default=SERVER) |
| 30 | args = parser.parse_args() |
| 31 | |
| 32 | profile = provisioned(args.ipa) |
| 33 | if profile is None: |
| 34 | sys.exit(f"{args.ipa} has no embedded.mobileprovision; sign it with a profile that includes the device") |
| 35 | udid = args.udid.replace("-", "").upper() |
| 36 | if not profile.get("ProvisionsAllDevices") and udid not in {device.replace("-", "").upper() for device in profile.get("ProvisionedDevices", [])}: |
| 37 | sys.exit(f"the profile in {args.ipa} does not include {args.udid}; register the device and re-sign") |
| 38 | |
| 39 | token = ssh("NOMAD_TOKEN=$(cat /var/lib/snowglobe/nomad.token) nomad var get -item=upload_token nomad/jobs/ota", capture=True).stdout.strip() |
| 40 | subprocess.run( |
| 41 | ["curl", "--fail-with-body", "--progress-bar", "--config", "-", "--upload-file", args.ipa, f"{args.server}/builds"], |
| 42 | input=f'header = "Authorization: Bearer {token}"\n', text=True, check=True, |
| 43 | ) |
| 44 | |
| 45 | |
| 46 | if __name__ == "__main__": |
| 47 | main() |