openwisp / openwisp-firmware-upgrader

Firmware upgrade solution for OpenWRT with possibility to add support for other embedded OSes. Provides features like automatic retry for network failures, mass upgrades, REST API and more.
https://openwisp.io/docs/dev/firmware-upgrader/
Other
53 stars 60 forks source link

[admin] Redirect to mass upgrade operation page after launch #43

Closed nemesifier closed 4 years ago

nemesifier commented 4 years ago

After launching the mass upgrade operation in the admin, the system should directly redirect to its page where users can track the progress of the upgrade.

This will require changing a bit of model code, because right now the BatchUpgradeOperation instance is created in the celery task which launches the upgrade, while we need to create the object before and then only trigger the mass upgrade in the celery task, by passing the primary key of the BatchUpgradeOperation object just created; this way we'll know the id of the BatchUpgradeOperation and we'll be able to redirect to its page.

nemesifier commented 4 years ago

Update.

I think that my initial mistake here was to put these methods in the Build model:

A solution could be to move those two methods to AbstractBatchUpgradeOperation.

batch_upgrade could be renamed to just upgrade().

Eg:

# this creates a `BatchUpgradeOperation` instance and initializes a mass upgrade
batch_upgrade_operation = BatchUpgradeOperation(build=build)
batch_upgrade_operation.full_clean()
batch_upgrade_operation.save()
batch_upgrade_operation.upgrade(firmwareless=True)

A dry_run class method would just execute the queries and return the list of the devices that would be affected by the upgrade (we can then use this method in the admin and in the API):

result = BatchUpgradeOperation.dry_run(build=build, firmwareless=True)

# list of device firmwares instances that can be upgraded
result['device_firmwares']

# list of devices which do not have a related device firmware yet
result['devices']

So the batch_upgrade_operation celery task would have to be changed to something like:

try:
    batch = load_model('BatchUpgradeOperation').objects.get(pk=pk)
    batch.upgrade(firmwareless=firmwareless)
except SoftTimeLimitExceeded:
    # etc

The difference is that the BatchUpgradeOperation object is created in the DB before the celery task is launched, that way we now its ID and we can build the URL to which we'll redirect the user to.

We also need to change the success message to something like:

You can track the progress of this mass upgrade operation in this page, refresh the page from time to time to find out how the upgrade is going

It would be cool to avoid that the user has to refresh but we can do this later on.