#coding:utf-8

import os
import sys
import sunhpc
import shutil
import textwrap
import subprocess
class Command(sunhpc.commands.pxelinux.build.command):
    """
    Build the autofs service for the sunhpc cluster.

    <param type='Bool' name='Quiet'>
    Whether to output detailed information, default: no
    </param>
    
    <example cmd='pxelinux build autofs'>
    In local build the autofs service.
    </example>

    <example cmd='pxelinux build autofs'>
    In local build the autofs service.
    </example>
    """
    def run(self, params, args):

        (quiet, ) = self.fillParams([('quiet', 'no')])

        cwd = os.getcwd()
        q = self.str2bool(quiet)
        self.msg('Starting build the autofs service...', q=q)
        
        isInstalled  = True
        autofs_files = ['/etc/auto.master', '/etc/auto.home', '/etc/auto.share']
        for autofs in autofs_files:
            if not os.path.exists(autofs):
                isInstalled = False


        # The httpd is not installed.
        if not isInstalled:
            self.shcmd('yum install -y autofs rpcbind nfs-utils --enablerepo=Sunhpc-%s' % sunhpc.version)

        self.fw.addports(['111', '2049', '30001-30004'], ['tcp', 'udp'])

        address = self.db.getHostAttr('localhost', 'Kickstart_PrivateAddress')
        network = self.db.getHostAttr('localhost', 'Kickstart_PrivateNetwork')
        netmask = self.db.getHostAttr('localhost', 'Kickstart_PrivateNetmask')

        export_file = '/etc/exports'
        with open(export_file, 'w') as f:
            f.write(textwrap.dedent(f"""\
            /export {address}(rw,async,no_root_squash) {network}/{netmask}(rw,async)
            """))

        sysconfig = '/etc/sysconfig/nfs'
        if self.matchText(sysconfig, 'RQUOTAD_PORT=30001'):
            with open(sysconfig, 'a') as f:
                f.write(textwrap.dedent(f"""\
                    RQUOTAD_PORT=30001
                    LOCKD_TCPPORT=30002
                    LOCKD_UDPPORT=30002
                    MOUNTD_PORT=30003
                    STATD_PORT=30004
                """))

        lockd = '/etc/modprobe.d/lockd.conf'
        if self.matchText(lockd, 'nlm_tcpport=30002'):
            
            with open(lockd, 'a') as f:
                f.write(textwrap.dedent(f"""\
                    options lockd nlm_tcpport=30002
                    options lockd nlm_udpport=30002
                """))

        autofs_config = "/etc/autofs.conf"
        os.system("sed -i 's/mount_nfs_default_protocol = 4/mount_nfs_default_protocol = 3/g' %s" % autofs_config)

        apps = '/export/apps'
        if not os.path.exists(apps):
            os.makedirs(apps)

        home = '/export/home'
        if not os.path.exists(home):
            os.makedirs(home)

        self.command('sync.users')

        self.shcmd('systemctl daemon-reload')
        self.shcmd('systemctl stop autofs')
        self.shcmd('systemctl start autofs')
        self.shcmd('systemctl enable autofs')
        self.shcmd('systemctl stop nfs')
        self.shcmd('systemctl start nfs')
        self.shcmd('systemctl enable nfs')

RollName = "base"



























