Skip to content

Commit

Permalink
Fix the OTA upgrade command (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdlambert authored Mar 25, 2020
1 parent e503383 commit ae4b6b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bond livelog --level info
You can upgrade your selected bond:
```bash
bond upgrade --release beta
bond upgrade beta
```
## Getting Help
Expand Down
2 changes: 0 additions & 2 deletions bond/commands/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ def run(self, args):
print("Set %s port %s" % (bond_id, args.ip))
print("Selected Bond: %s" % BondDatabase().get("selected_bondid"))
token = check_unlocked_token()
if token:
print("Set token: %s" % token)
elif args.clear:
BondDatabase.pop("selected_bondid", None)
print("Cleared selected Bond")
Expand Down
53 changes: 24 additions & 29 deletions bond/commands/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ def register():
UpgradeCommand()


def get_branch(args, target):
release = args.release
if release:
return "trunk" if release == "trunk" else f"{release}-{target}"
def get_branch_string(branch, target):
if branch in ("alpha", "beta", "master"):
return f"{branch}-{target}"
elif branch == "trunk":
return "trunk"
else:
return args.branch.replace("/", "-")
return branch.replace("/", "-")


def get_latest_version(target, branch):
url = f"https://s3.amazonaws.com/bond-updates/v2/{target}/{branch}/versions_internal.json"
rsp = requests.get(url)
if rsp.status_code != 200:
raise Exception(
f"Failed to access version info on S3: {url}, {rsp.status_code}"
)
print("Failed to find an upgrade for target %s on branch %s" % (target, branch))
exit(1)
return json.loads(rsp.content)["versions"][0]


Expand All @@ -42,13 +42,17 @@ def do_upgrade(bondid, version_obj):
pass
for _ in range(120):
time.sleep(1)
rsp = bond.proto.get(bondid, topic="sys/upgrade")
try:
rsp = bond.proto.get(bondid, topic="sys/upgrade")
except requests.exceptions.ReadTimeout:
sys.stdout.write(".")
sys.stdout.flush()
continue
progress = rsp["b"]["progress"]
assert rsp["s"] in (200, 204)
print(f"Progress: {progress / 10}%")
if rsp["s"] == 204 or progress == 1000:
print("Upgrade installed.")
break
print(f"Progress: {progress / 10}%")
else:
raise Exception("Download timeout")
print("Rebooting...")
Expand All @@ -62,14 +66,12 @@ def do_upgrade(bondid, version_obj):
# check for in progress return get on upgrade
try:
rsp = bond.proto.get(bondid, topic="sys/version", timeout=2)
except:
except requests.exceptions.ConnectTimeout:
sys.stdout.write(".")
sys.stdout.flush()
rsp = None

if rsp:
print("Reconnected!")
break
continue
print("Reconnected!")
break
else:
raise Exception("Download timeout")

Expand All @@ -80,25 +82,18 @@ class UpgradeCommand(BaseCommand):
subcmd = "upgrade"
help = "Upgrade your Bond. Choose either a released firmware or a firmware from a specific branch"
arguments = {
"--release": {
"help": """the release to use. Using trunk or alpha is not recommended unless
you really know what you're doing, master is the branch released to
the store mobile apps, beta is released to the public beta mobile apps""",
"choices": ["trunk", "alpha", "beta", "master"],
"branch": {
"help": "The branch. Release branches are 'trunk', 'alpha', 'beta', and 'master'. "
"'master' is the version distributed by the mobile apps in the app stores, "
"'alpha' and 'trunk' are for internal development use, and may be unstable."
},
"--target": {
"help": "override detected target. Useful in development, but may cause irreversible device malfunction!"
},
"--branch": {
"help": "choose a specific firmware branch, same deal as alpha: don't use unless you know what you're doing."
},
}

def run(self, args):
bondid = BondDatabase.get_assert_selected_bondid()
if (args.release and args.branch) or not (args.release or args.branch):
print("Unable to proceed: choose one and only one of release or branch")
exit(1)
print("Connecting to BOND...")
sys_version = bond.proto.get(bondid, topic="sys/version")["b"]
target = sys_version["target"]
Expand All @@ -123,7 +118,7 @@ def run(self, args):
target = args.target
print("Target manually overriden.")

branch = get_branch(args, target)
branch = get_branch_string(args.branch, target)
print(f"Selected Branch: \t{branch}")
print(f"Current Version: \t{current_ver}")
version_obj = get_latest_version(target, branch)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='bond-cli',
version='0.0.7',
version='0.0.8',
author='Olibra',
packages=find_packages(),
scripts=['bond/bond'],
Expand Down

0 comments on commit ae4b6b4

Please sign in to comment.