#coding:utf-8
import time
import sunhpc
class Modules(object):
    """
    Configure base for the sunhpc cluster
    """
    def __init__(self, command):
        self.cmd = command
    @property
    def __help__(self):
        info = """
        #==============================================================
        # %s
        #
        # module_path: %s
        # module_name: %s
        #==============================================================
        """ % (self.__doc__.strip(), self.__module__.strip(), self.__repr__())
        return info
        
    def run(self, args):

        content = []
        content.append(self.__help__)

        # hostname setting
        hostname = self.cmd.db.getHostAttr('localhost', 'Kickstart_PublicHostname')
        hostname_info = "hostnamectl --static set-hostname %s" % hostname
        content.append(hostname_info)

        # selinux config
        selinux = '/etc/selinux/config'
        selinux_info = """
        # 
        # Generated by sunhpc selinux module.
        #
        # This file controls the state of SELinux on the system.
        # SELINUX= can take one of these three values:
        #     enforcing - SELinux security policy is enforced.
        #     permissive - SELinux prints warnings instead of enforcing.
        #     disabled - No SELinux policy is loaded.
        SELINUX=disabled
        # SELINUXTYPE= can take one of three values:
        #     targeted - Targeted processes are protected,
        #     minimum - Modification of targeted policy. Only selected processes are protected. 
        #     mls - Multi Level Security protection.
        SELINUXTYPE=targeted
        """
        content.append(self.cmd.ks.makefile(selinux_info, selinux))

        # hosts
        hostfile = '/etc/hosts'
        hostinfo = self.cmd.command('report.host').strip()
        content.append(self.cmd.ks.makefile(hostinfo, hostfile))


        # resolv
        resolv = '/etc/resolv.conf'
        search_lan = self.cmd.db.getHostAttr('localhost', 'Kickstart_PrivateDNSDomain')
        search_wan = self.cmd.db.getHostAttr('localhost', 'Kickstart_PublicDNSDomain')
        public_dns = self.cmd.db.getHostAttr('localhost', 'Kickstart_PublicDNSServer')
        resolv_info = """
        search %s %s
        nameserver %s
        nameserver 223.5.5.5
        """ % (search_lan, search_wan, public_dns)
        content.append(self.cmd.ks.makefile(resolv_info, resolv))

        # motd
        tnow = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
        motd = '/etc/motd'
        motd_info = """
        SunhpcOS %s (%s)
        Profile built %s

                                _____             _                     
                               / ____|           | |                    
                =======       | (___  _   _ _ __ | |__  _ __   ___     
                \    //        \___ \| | | | '_ \| '_ \| '_ \ / __|    
                 \  //         ___ ) | |_| | | | | | | | |_) | (__  
                  \//         |_____/ \__,_|_| |_|_| |_| .__/ \___|
                                                       | |         
                         Powered by HengPU Technology  |_| By kylins 
                                             
                PlatName : HengPu High Performance Computing Platform
                WeChat   : xiubuzhe
                Version  : %s (%s)
                Email    : info@sunhpc.com
                Phone    : +86 18640977650
                Homepage : https://www.sunhpc.com - @kylins
                -----------------------------------------------------


        """ % (sunhpc.version, sunhpc.release, tnow, sunhpc.version, sunhpc.release)
        content.append(self.cmd.ks.makefile(motd_info, motd))

        # sunhpc-release
        rels = '/etc/sunhpc-release'
        rels_info = "SunhpcOS %s" % sunhpc.version
        content.append(self.cmd.ks.makefile(rels_info, rels))

        # sys-release
        sysrels = '/etc/system-release'
        sysrels_info = "SunhpcOS Linux release %s (%s)" % (sunhpc.version, sunhpc.release)
        content.append(self.cmd.ks.makefile(sysrels_info, sysrels))

        # os-release
        osrels = '/etc/os-release'
        osrels_info = """
        NAME="SunHPC Linux"
        VERSION="%s (%s)"
        ID="sunhpc"
        ID_LIKE="rhel fedora"
        VERSION_ID="%s"
        PRETTY_NAME="SunhpcOS Linux %s (%s)"
        ANSI_COLOR="0;31"
        CPE_NAME="cpe:/o:sunhpc:sunhpc:%s"
        HOME_URL="https://www.sunhpc.com/"
        BUG_REPORT_URL="https://bugs.sunhpc.com/"

        CENTOS_MANTISBT_PROJECT="SunhpcOS-%s"
        CENTOS_MANTISBT_PROJECT_VERSION="%s"
        REDHAT_SUPPORT_PRODUCT="sunhpc"
        REDHAT_SUPPORT_PRODUCT_VERSION="%s"
        """ % (self.cmd.major, sunhpc.release,
            self.cmd.major, self.cmd.major, sunhpc.release,
            self.cmd.major, self.cmd.major, self.cmd.major,
            self.cmd.major)
        content.append(self.cmd.ks.makefile(osrels_info, osrels))

        # issue
        issue      = '/etc/issue'
        issue_net  = '/etc/issue.net'
        issue_info = """
        \S
        Kernel \r on an \m
        Current times \d \t

                                _____             _                     
                               / ____|           | |                    
                =======       | (___  _   _ _ __ | |__  _ __   ___     
                \\    //        \\___ \\| | | | '_ \\| '_ \\| '_ \\ / __|    
                 \\  //         ___ ) | |_| | | | | | | | |_) | (__  
                  \\//         |_____/ \\__,_|_| |_|_| |_| .__/ \\___|
                                                        | |         
                          Powered by HengPU Technology  |_| By kylins 
                                             
                PlatName : HengPu High Performance Computing Platform
                WeChat   : xiubuzhe
                Version  : %s (%s)
                Email    : info@sunhpc.com
                Phone    : +86 18640977650
                Homepage : https://www.sunhpc.com - @kylins
                -----------------------------------------------------

        """ % (sunhpc.version, sunhpc.release)
        content.append(self.cmd.ks.makefile(issue_info, issue))
        content.append(self.cmd.ks.makefile(issue_info, issue_net))

        self.cmd.ks.addMain(content)
    def __repr__(self):
        return "control-base"




























