#!/usr/bin/python
# A simple tool to test maximal exported directories / mount that can be performed on a machine
# The main idea is to create and export a directory on the server/ mount on the client and repeat 
# the operation until it failed.
#
# Please send bugs to Vincent ROQUETA: vincent.roqueta@ext.bull.net



import shutil
import os, sys
from os import *
from sys import *

SERVER='nfs2_gb'
exportPath='/export'
mountPath='mnt/'

class Machine:
    def mkdir(self,dir):
            self.command="mkdir -p "+dir
            self.do()
    def rmdir(self,dir):
            self.command="rm -rf "+dir
            self.do()

    def printc(self):
            print "->"+self.command
            print "\n"

class Client(Machine):
    
    def __init__(self, mountPath):
        self.command=""
        self.mountPath=mountPath

    def do(self):
            os.system(self.command)

    def isomount(self, dir):
        export=SERVER+":/"+dir
        mntpoint=self.mountPath+"/"+dir
        self.command="mount -t nfs4 "+export+" "+mntpoint
        self.do()
    
    def umount(self, dir):
        mntpoint=self.mountPath+"/"+dir
        self.command="umount "+mntpoint
        self.do()
        





class Serveur(Machine):

    def __init__(self, ip, exportPath):
            self.SERVEUR=ip
            self.exportPath=exportPath
            
    def do(self):
            self.command="ssh "+self.SERVEUR+" "+self.command
            os.system(self.command)

    def configure(self, dir):
            exportDir=self.exportPath+'/'+dir
            self. mkdir(exportDir)
            self.export(exportDir)
    def clean(self, dir):
            unexportDir=self.exportPath+'/'+dir
            self.unexport(unexportDir)
            self.rmdir(unexportDir)

    def unexport(self, dir):
            exportOptions="-u "
            allowedUsers="*"
            self.command="exportfs "+exportOptions+allowedUsers+":"+dir
            self.do()




    def export(self, dir):
            exportOptions="-orw,nohide,insecure,no_subtree_check "
            allowedUsers="*"
            self.command="exportfs "+exportOptions+allowedUsers+":"+dir
            self.do()


            


client=Client(mountPath)
serveur=Serveur("nfs2_gb", "/export_nfs")


def test_mount(max):
    i=0
    while(i<max):
            dir=str(i)
            i+=1
            mnt_pt=mountPath+dir

            print "Mounting directory number "+dir
            try:
                    client.mkdir(mnt_pt)
                    serveur.configure(dir)
                    client.isomount(dir)
            except :
                    print("Mount failed for i="+dir)
                    break

def test_clean(max):
    i=0
    while(i<max):
            dir=str(i)
            i+=1
            mnt_pt=mountPath+dir

            print "Cleaning directory number "+dir
            try:
                    client.umount(dir)
                    client.rmdir(mnt_pt)
                    serveur.clean(dir)
            except :
                    print("Clean failed for i="+dir)
                    break
    
def usage():
        print "test maximum mount available for NFSv4"
        print "-h, --help   :this message"
        print "max=<value>  :maximum exported filesystem to test"
        print "--no-test    :do not perform any test, clean tests only"
        print "--no-clean   :test only, without cleanning the environement"
        print " "
        print "send bugs to Vincent ROQUETA: vincent.roqueta@ext.bull.net"
        
def main():
        args=sys.argv[1:]
        max=1024
        test=True
        clean=True
        for a in args:
            if a in ("-h", "--help"):
                usage()
                sys.exit(1)
            if a[:3]=="max":
                v=a.split("=")
                max=int(v[1])
                continue
            if a=="--no-test":
                test=False
                continue
            if a=="--no-clean":
                clean=False
                continue

        if(test):
            test_mount(max)
            print "test completed"
        if(clean):
            test_clean(max)
            print "Cleanup completed"
                



main()


