#!/usr/bin/python
"""
Binary to check if cdsls that are defined in the inventoryfile really exists on filesystem. 
Outputs a message if check was succesfull or not. 
In case of failure the tool documents which of the tested cdsls have failed.
"""
__version__ = "$revision$"

import getopt
import sys
import logging
import re
import os.path

from comoonics import ComLog
#from comoonics import GetOpts

import comoonics.cluster
from comoonics.cluster.ComClusterInfo import ClusterInfo
from comoonics.cluster.ComClusterRepository import ClusterRepository, RedHatClusterRepository
from comoonics.cdsl.ComCdslRepository import CdslRepository
from comoonics.cdsl.ComCdslValidate import CdslValidate
from comoonics.cdsl import dirtrim, commonoptparseroptions

ComLog.setLevel(logging.INFO)
import logging

logging.basicConfig()

from optparse import OptionParser

parser = OptionParser(description=__doc__)
parser.add_option("-U", "--update", dest="update", default=False, action="store_true", help="Also updates the repository")
parser.add_option("-f", "--filesystem", dest="filesystem", default=False, action="store_true", help="Validates a given path (--path) on the filesystem instead of the repository")
parser.add_option("-p", "--path", dest="path", default=".", help="Set the path to be searched. Defaults to the base cdslpath.")
parser.add_option("-C", "--cdsl", dest="cdsls", default=[], type="string", action="append", help="Defines the cdsl to be validated. Only available when used not in filesystemmode." )
parser.add_option("-R", "--norecursive", dest="norecursive", default=False, action="store_true", help="If filesystem selected it will not run recursively")

parser=commonoptparseroptions(parser)

ComLog.setLevel(logging.INFO)
(options, args) = parser.parse_args()

inventoryfile = os.path.join(options.root,dirtrim(options.inventoryfile))
clusterconf = os.path.join(options.root, dirtrim(options.clusterconf))

doc=comoonics.cluster.parseClusterConf(options.clusterconf)
          
#create needed cluster objects
clusterRepository = ClusterRepository(doc.documentElement,doc)
clusterInfo = ClusterInfo(clusterRepository)

cdslRepository = CdslRepository(dirtrim(options.inventoryfile), None, False, usenodeids="True", root=options.root)

validator=CdslValidate(cdslRepository, clusterInfo)

_added, _removed=validator.validate(update=options.update, root=options.root, onfilesystem=options.filesystem, filesystempath=options.path, cdsls=options.cdsls, recursive=not options.norecursive)
if _added and len(_added) > 0:
    for _addedcdsl in _added:
        print "Added cdsl %s type %s" %(_addedcdsl, _addedcdsl.type)
if _removed and len(_removed) > 0:
    for _removedcdsl in _removed:
        print "Removed cdsl %s type %s" %(_removedcdsl, _removedcdsl.type)
if ((_added and len(_added) > 0) or (_removed and len(_removed) > 0)) and not options.update:
    print >>sys.stderr, "Cdslrepository is inconsistent to the filesystem. Please run this command with -U/--update option to fix this." 
    sys.exit(len(_added)+len(_removed))
