editor.py 3.77 KB
Newer Older
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
'''
Utilities for adding or deleting entries on hosts file
as /etc/hosts.
'''
import os
import sys
import json
import subprocess
import socket
import argparse


def is_valid_ip_address(ip):
    '''
    Check whether an ip address is valid, both for ipv4
    and ipv6.
    '''
    try:
        socket.inet_pton(socket.AF_INET, ip)
        return True
    except socket.error:
        pass

    try:
        socket.inet_pton(socket.AF_INET, ip)
        return True
    except socket.error:
        pass
    return False


def parse_line(line):
    pos = line.find("#")
    new_line = line[:pos].strip() if pos != -1 else line.strip()
    comment = line[pos:] if pos != -1 else ''
    if new_line:
        parts = list(map(lambda x: x.strip(), new_line.split()))
        return (line, parts, comment)
    else:
        return (line, None, comment)
        

class HostEditor(object):
    def __init__(self, filename='/etc/hosts'):
        self.filename = filename
        self._parse()

    def add(self, ip, *hostnames):
        '''
        Add an entry to hosts file.
        '''
        if not self.entries:
            return
        ret = []
        added = False
        if not self.entries:
            return
        for (line, parts, comment) in self.entries:
            if parts and parts[0] == ip and not added:
                for hostname in hostnames:
                    if hostname not in parts[1:]:
                        parts.append(hostname)
                line = ' '.join(['\t'.join(parts), comment])
                added = True
            ret.append((line, parts, comment))
        if not added:
            parts = [ip] + list(hostnames)
            line = '\t'.join(parts)
            ret.append((line, parts, comment))
        self.entries = ret

    def delete(self, ip, hostname):
        '''
        Delete an entry from hosts file.
        '''
        if not is_valid_ip_address(ip):
            raise Exception("Ip %s is not valid." % ip)
        ret = []
        for (line, parts, comment) in self.entries:
            if parts and parts[0] == ip:
                parts = list(filter(lambda x: x != hostname, parts))
                if not parts[1:]:
                    continue
                line = ' '.join(['\t'.join(parts), comment])
            ret.append((line, parts, comment))
        self.entries = ret

    def _parse(self):
        '''
        Parse the files into entries.
        '''
        self.entries = []
        for line in open(self.filename).readlines():
            self.entries.append(parse_line(line))

    def output(self, fd=None):
        if fd is None:
            fd = sys.stdout
        fd.write('\n'.join(map(lambda x: x[0].strip(), self.entries)))

    def write(self):
        fd = open(self.filename, 'w')
        self.output(fd=fd)
        fd.close()

    def output_docker_ip(self, container):
        proc = subprocess.Popen("docker inspect %s" % container,
                                shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        if proc.returncode == 0:
            ret = json.loads(stdout.decode('utf-8'))
            ip = ret[0]['NetworkSettings']['IPAddress']
            sys.stdout.write(ip)


def main():
    he = HostEditor()
    if len(sys.argv) >= 4 and sys.argv[1] == 'add':
        he.add(sys.argv[2], *sys.argv[3:])
        he.write()
        he.output()
    elif len(sys.argv) == 4 and sys.argv[1] in ('rm', 'remove',
                                                'del', 'delete'):
        he.delete(sys.argv[2], sys.argv[3])
        he.write()
        he.output()
    elif len(sys.argv) == 3 and sys.argv[1] == 'docker':
        he.output_docker_ip(sys.argv[2])
    else:
        he.output()


if __name__ == '__main__':
    main()