#!/usr/bin/python
# copyright (C) 2013 FUJITSU LIMITED All Rights Reserved

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

# -*- coding: utf-8 -*-

import sys
import os
import fcntl
import IPy
import ConfigParser
from optparse import OptionParser

CONFIG_FILE = '/etc/lxcf/lxcf.conf'
HOSTS_FILE = '/etc/hosts'

# check root
if os.geteuid() != 0:
    print "error: Because you are not root, you cannot execute this command. "
    exit(-1)


parser = OptionParser()
(options, args) = parser.parse_args(sys.argv)

argc = len(args)

if (argc != 2):
    print "usage : lxcf-erase-setup  LXCNAME"
    quit()

domain_name = args[1]

#
# Function for IP address
#


def erase_ipaddr(name, hosts_file):
    """erase IP addres from /etc/hosts"""
    # read hosts file and check ip addr
    try:
        f = open(hosts_file, "r")
    except:
        return

    Lines = f.readlines()

    f = open(hosts_file, "w")
    fcntl.flock(f.fileno(), fcntl.LOCK_EX)

    for l in Lines:
        S = l.split()
        if (len(S) >= 1):
            if ((S[0])[0] == '#'):
                continue
            if (name != S[1]):
                f.write(l)

    # unlock hosts file
    fcntl.flock(f.fileno(), fcntl.LOCK_UN)

    f.close()


def find_ipaddr(name, hosts_file):
    """find IP addres from /etc/hosts"""
    # read hosts file and check ip addr
    try:
        f = open(hosts_file, "r")
    except:
        return IPy.IP(1)

    while True:
        Line = f.readline()
        if (Line):
            S = Line.split()

            if (len(S) >= 1):
                if ((S[0])[0] == '#'):
                    continue

                try:
                    ip_s = IPy.IP(S[0])
                except:
                    continue

                if (name == S[1]):
                    f.close()
                    return ip_s
        else:
            break

    return IPy.IP(1)


def check_ipaddr(ip, hosts_file):
    """check IP address in /etc/hosts"""
    # read hosts file and check ip addr
    try:
        f = open(hosts_file, "r")
    except:
        return False

    while True:
        Line = f.readline()
        if (Line):
            S = Line.split()

            if (len(S) >= 1):
                if ((S[0])[0] == '#'):
                    continue

                try:
                    ip_s = IPy.IP(S[0])
                except:
                    continue

            if (ip_s == ip):
                f.close()
            return False
        else:
            break

    return True


def get_ipaddr(hosts_file):
    """get new IP address"""
    # read the config file
    conf = ConfigParser.SafeConfigParser()
    conf.read(CONFIG_FILE)

    ipaddr_start = IPy.IP(conf.get('ipaddr_range', 'ipaddr_start'))
    ipaddr_end = IPy.IP(conf.get('ipaddr_range', 'ipaddr_end'))

    ip1 = ipaddr_start.int()
    ip2 = ipaddr_end.int()

    # looks for empty IP.address
    for i in range(ip1, ip2+1):
        ipa = IPy.IP(i)
        if (check_ipaddr(ipa, hosts_file)):
            break

    try:
        with open(hosts_file, 'a') as f:
            f.write(ipa.strNormal()+'\t'+domain_name+'\n')
    except:
        pass

    return ipa

erase_ipaddr(domain_name, HOSTS_FILE)
for hfile in os.listdir('/opt/lxcf/'):
    hf = '/opt/lxcf/'+hfile+'/etc/hosts'
    erase_ipaddr(domain_name, hf)

print "erase ip address from /etc/hosts"
