#!/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, os, shutil
import fcntl
import IPy
from optparse import OptionParser,OptionGroup

HOSTS_FILE = '/etc/hosts'

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
#

#
# erase IP addres from /etc/hosts
#

def erase_ipaddr(name, hosts_file) :
  # 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()

#
# find IP addres from /etc/hosts
#

def find_ipaddr(name, hosts_file) :
  # 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)

#
# check IP address in /etc/hosts
#

def check_ipaddr(ip, hosts_file) :
  # 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

#
# get new IP address 
#

def get_ipaddr(hosts_file):
  # 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"
