summaryrefslogtreecommitdiff
path: root/src/op_mode/clear_dhcp_lease.py
blob: f372d3af0b6a9320c75d7f9c77b56d87b8e71767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3

import argparse
import re

from isc_dhcp_leases import Lease
from isc_dhcp_leases import IscDhcpLeases

from vyos.configquery import ConfigTreeQuery
from vyos.utils.io import ask_yes_no
from vyos.utils.process import call
from vyos.utils.commit import commit_in_progress


config = ConfigTreeQuery()
base = ['service', 'dhcp-server']
lease_file = '/config/dhcpd.leases'


def del_lease_ip(address):
    """
    Read lease_file and write data to this file
    without specific section "lease ip"
    Delete section "lease x.x.x.x { x;x;x; }"
    """
    with open(lease_file, encoding='utf-8') as f:
        data = f.read().rstrip()
        lease_config_ip = '{(?P<config>[\s\S]+?)\n}'
        pattern = rf"lease {address} {lease_config_ip}"
        # Delete lease for ip block
        data = re.sub(pattern, '', data)

    # Write new data to original lease_file
    with open(lease_file, 'w', encoding='utf-8') as f:
        f.write(data)

def is_ip_in_leases(address):
    """
    Return True if address found in the lease file
    """
    leases = IscDhcpLeases(lease_file)
    lease_ips = []
    for lease in leases.get():
        lease_ips.append(lease.ip)
    if address not in lease_ips:
        print(f'Address "{address}" not found in "{lease_file}"')
        return False
    return True


if not config.exists(base):
    print('DHCP-server not configured!')
    exit(0)

if config.exists(base + ['failover']):
    print('Lease cannot be reset in failover mode!')
    exit(0)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--ip', help='IPv4 address', action='store', required=True)

    args = parser.parse_args()
    address = args.ip

    if not is_ip_in_leases(address):
        exit(1)

    if commit_in_progress():
        print('Cannot clear DHCP lease while a commit is in progress')
        exit(1)

    if not ask_yes_no(f'This will restart DHCP server.\nContinue?'):
        exit(1)
    else:
        del_lease_ip(address)
        call('systemctl restart isc-dhcp-server.service')