Post bootstrap re-assignment of devices #1492
|
hi there, i would like to implement this flow:
is this something that can be already implemented in openwisp or does it require a custom flow ? |
Replies: 1 comment 1 reply
|
Step 1 is supported: devices can register using the shared secret of a generic organization. Step 2 requires a routing rule defined by the deployment. OpenWISP cannot infer the intended organization when every device registers with the same shared secret. If you can express an internal rule that distinguishes devices, you can implement it with a receiver for the The following example routes a newly registered device from the generic organization to organization 2 when its MAC address starts with # organization_routing.py
from uuid import UUID
from django.db import transaction
from django.dispatch import receiver
from swapper import load_model
from openwisp_controller.config.signals import device_registered
GENERIC_ORGANIZATION_ID = UUID("11111111-1111-1111-1111-111111111111")
ORGANIZATION_2_ID = UUID("22222222-2222-2222-2222-222222222222")
ORGANIZATION_3_ID = UUID("33333333-3333-3333-3333-333333333333")
@receiver(
device_registered,
dispatch_uid="organization_routing.route_registered_device",
)
def route_registered_device(sender, instance, is_new, **kwargs):
if not is_new or instance.organization_id != GENERIC_ORGANIZATION_ID:
return
destination_id = (
ORGANIZATION_2_ID
if instance.mac_address.startswith("00:11:22")
else ORGANIZATION_3_ID
)
Organization = load_model("openwisp_users", "Organization")
destination = Organization.objects.get(pk=destination_id)
with transaction.atomic():
instance.organization = destination
# A group belonging to the generic organization cannot be retained.
instance.group = None
instance.full_clean()
instance.save()
config = instance.config
config.device.organization = destination
config.templates.clear()
config.add_default_templates()The receiver runs only for new registrations. It removes templates inherited from the generic organization, re-adds required templates for the destination organization, and adds its default templates. With - hosts: openwisp
vars:
openwisp2_extra_django_settings_instructions:
- "from .organization_routing import route_registered_device"
roles:
- role: openwisp.openwisp2
tasks:
- name: Deploy organization-routing receiver
ansible.builtin.copy:
src: files/organization_routing.py
dest: "{{ openwisp2_path }}/openwisp2/organization_routing.py"
group: "{{ www_group }}"
mode: "0640"
notify: Reload applicationPut the receiver code in With Then import it from from .organization_routing import route_registered_deviceRecreate the application containers after adding the customization: docker compose up -d --force-recreate dashboard apiIf no reliable rule can be defined, the fallback is to leave the device in the generic organization after registration and manually move it to the correct organization in the dashboard, then assign the appropriate templates. |
Step 1 is supported: devices can register using the shared secret of a generic organization.
Step 2 requires a routing rule defined by the deployment. OpenWISP cannot infer the intended organization when every device registers with the same shared secret. If you can express an internal rule that distinguishes devices, you can implement it with a receiver for the
device_registeredsignal.The following example routes a newly registered device from the generic organization to organization 2 when its MAC address starts with
00:11:22, otherwise to organization 3. Replace the UUIDs and MAC prefix with the values and rule used in your deployment.