Skip to content

Commit 2dad1f0

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

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
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: 13 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,16 @@ 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(
44+
last_ip=None,
45+
last_ip__in=Subquery(
46+
WHOISInfo.objects.filter(ip_address=OuterRef("last_ip")).values(
47+
"ip_address"
5448
)
55-
)
49+
),
50+
)
5651

5752
updated_devices = devices.update(last_ip=None)
5853
if updated_devices:

openwisp_controller/config/whois/test_whois.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ def test_last_ip_management_command(self):
287287
mac_address="11:22:33:44:55:66",
288288
last_ip="172.217.22.12",
289289
)
290-
args.append("--whois-related")
291290
call_command("clear_last_ip", *args, stdout=out, stderr=StringIO())
292291
self.assertIn(
293292
"Cleared last IP addresses for 1 active device(s).", out.getvalue()

0 commit comments

Comments
 (0)