Last active
March 28, 2017 08:43
-
-
Save matthijskooijman/1046d42f42a77571adc341a102cf605d to your computer and use it in GitHub Desktop.
TTN migration script, see https://www.thethingsnetwork.org/forum/t/seamless-migration-from-staging-to-production-for-otaa-devices/6196
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import subprocess | |
| import sys | |
| import re | |
| import os | |
| ttnctl_staging = ["ttnctl-staging", "--ttn-account-server", "https://v1.account.thethingsnetwork.org"] | |
| ttnctl_production = ["ttnctl"] | |
| mapping = {} | |
| id_prefix = 'meetstation-' | |
| def run(ttnctl, args): | |
| print(' '.join(ttnctl + args)) | |
| out = subprocess.check_output(ttnctl + args) | |
| out = out.decode('utf8') | |
| sys.stdout.write(out) | |
| return out | |
| def find(out, field): | |
| matches = re.search("^ *" + field + ": *(.*)$", out, re.MULTILINE) | |
| if matches is None: | |
| raise Exception("Failed to find {} in output\n".format((field,))) | |
| return matches.group(1) | |
| def migrate(dev_eui): | |
| out = run(ttnctl_staging, ['devices', 'info', dev_eui]) | |
| app_eui = find(out, "AppEUI") | |
| app_key = find(out, "AppKey") | |
| if "DevAddr" in out: | |
| app_skey = find(out, "AppSKey") | |
| nwk_skey = find(out, "NwkSKey") | |
| dev_addr = find(out, "DevAddr") | |
| dev_id = id_prefix + str(int(dev_eui, 16)) | |
| run(ttnctl_staging, ['devices', 'delete', dev_eui]) | |
| if "DevAddr" in out: | |
| run(ttnctl_staging, ['devices', 'register', 'personalized', '--relax-fcnt', dev_addr, nwk_skey, app_skey]) | |
| mapping[dev_addr] = dev_eui | |
| run(ttnctl_production, ['devices', 'register', dev_id, dev_eui, app_key]) | |
| try: | |
| for dev_eui in sys.argv[1:]: | |
| migrate(dev_eui) | |
| except subprocess.CalledProcessError as e: | |
| sys.stdout.write(e.output.decode('utf8')) | |
| raise | |
| finally: | |
| print("mapping = {") | |
| for (dev_addr, dev_eui) in mapping.items(): | |
| print(" '00000000" + dev_addr + "': '" + dev_eui + "',") | |
| print("}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment