Skip to content

Commit 48d0036

Browse files
committed
[chores] Minor refactoring, update docs
Signed-off-by: DragnEmperor <[email protected]>
1 parent eaa0194 commit 48d0036

File tree

3 files changed

+32
-50
lines changed

3 files changed

+32
-50
lines changed

docs/user/whois.rst

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@ met:
4545
- There is **no existing WHOIS record** for that IP.
4646
- WHOIS lookup is **enabled** for the device's organization.
4747

48-
Handling Existing Devices
49-
-------------------------
50-
51-
For creating WHOIS records for existing devices, run the ``clear_last_ip``
52-
management command, to clear the last IP address of **all active devices
53-
across organizations**. Active devices will update their last IP address,
54-
triggering the WHOIS lookup automatically.
55-
56-
It accepts an optional flag ``--whois-related`` to exclude devices with
57-
WHOIS records.
58-
5948
Managing WHOIS Records
6049
----------------------
6150

@@ -88,6 +77,26 @@ Setup Instructions
8877
- Set :ref:`OPENWISP_CONTROLLER_WHOIS_GEOIP_ACCOUNT` to **Account ID**.
8978
- Set :ref:`OPENWISP_CONTROLLER_WHOIS_GEOIP_KEY` to **License Key**.
9079

80+
6. Restart the application/containers if using ansible-openwisp2 or
81+
docker.
82+
7. Run the ``clear_last_ip`` management command to clear the last IP
83+
address of **all active devices across organizations**.
84+
85+
- If using ansible-openwisp2 (default directory is /opt/openwisp2,
86+
unless changed in Ansible playbook configuration):
87+
88+
.. code-block:: bash
89+
90+
source /opt/openwisp2/env/bin/activate
91+
python /opt/openwisp2/src/manage.py clear_last_ip
92+
93+
- If using docker:
94+
95+
.. code-block:: bash
96+
97+
docker exec -it <openwisp_container_name> sh
98+
python manage.py clear_last_ip
99+
91100
Viewing WHOIS Lookup Data
92101
-------------------------
93102

openwisp_controller/config/management/commands/clear_last_ip.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ def add_arguments(self, parser):
1616
dest="interactive",
1717
help="Do NOT prompt the user for input of any kind.",
1818
)
19-
parser.add_argument(
20-
"--whois-related",
21-
action="store_true",
22-
help="Clear only those IPs having no WHOIS information.",
23-
)
2419
return super().add_arguments(parser)
2520

2621
def handle(self, *args, **options):
22+
if not app_settings.WHOIS_CONFIGURED:
23+
self.stdout.write("WHOIS must be configured to use this option.")
24+
return
25+
2726
Device = load_model("config", "Device")
2827
WHOISInfo = load_model("config", "WHOISInfo")
2928

@@ -39,20 +38,15 @@ def handle(self, *args, **options):
3938
if input("".join(message)) != "yes":
4039
raise CommandError("Operation cancelled by user.")
4140

42-
devices = Device.objects.filter(_is_deactivated=False).exclude(last_ip=None)
43-
devices = devices.only("last_ip")
44-
if options["whois_related"]:
45-
if not app_settings.WHOIS_CONFIGURED:
46-
self.stdout.write("WHOIS must be configured to use this option.")
47-
return
48-
# Filter devices that have no WHOIS information for their last IP
49-
devices = devices.exclude(
50-
last_ip__in=Subquery(
51-
WHOISInfo.objects.filter(ip_address=OuterRef("last_ip")).values(
52-
"ip_address"
53-
)
41+
devices = Device.objects.filter(_is_deactivated=False).only("last_ip")
42+
# Filter devices that have no WHOIS information for their last IP
43+
devices = devices.exclude(last_ip=None).exclude(
44+
last_ip__in=Subquery(
45+
WHOISInfo.objects.filter(ip_address=OuterRef("last_ip")).values(
46+
"ip_address"
5447
)
55-
)
48+
),
49+
)
5650

5751
updated_devices = devices.update(last_ip=None)
5852
if updated_devices:

openwisp_controller/config/whois/test_whois.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -279,27 +279,6 @@ def test_last_ip_management_command(self):
279279
device.refresh_from_db()
280280
self.assertIsNone(device.last_ip)
281281

282-
device.last_ip = "172.217.22.11"
283-
device.save()
284-
self._create_whois_info(ip_address=device.last_ip)
285-
device2 = self._create_device(
286-
name="11:22:33:44:55:66",
287-
mac_address="11:22:33:44:55:66",
288-
last_ip="172.217.22.12",
289-
)
290-
args.append("--whois-related")
291-
call_command("clear_last_ip", *args, stdout=out, stderr=StringIO())
292-
self.assertIn(
293-
"Cleared last IP addresses for 1 active device(s).", out.getvalue()
294-
)
295-
device.refresh_from_db()
296-
device2.refresh_from_db()
297-
self.assertIsNotNone(device.last_ip)
298-
self.assertIsNone(device2.last_ip)
299-
300-
device.last_ip = None
301-
WHOISInfo.objects.all().delete()
302-
device.save()
303282
call_command("clear_last_ip", *args, stdout=out, stderr=StringIO())
304283
self.assertIn("No active devices with last IP to clear.", out.getvalue())
305284

0 commit comments

Comments
 (0)