#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__)

        # create rsa keys
        rsa_command = "sunhpc create security keyname=rsa"
        content.append(rsa_command)

        ssh_config = '/etc/ssh/ssh_config'
        ssh_config_info = """
        Host *
            CheckHostIP             no
            ForwardX11              yes
            ForwardAgent            yes
            StrictHostKeyChecking   no
            UsePrivilegedPort       no
            Protocol                2,1
            HostbasedAuthentication yes
            EnableSSHKeySign        yes
        """
        content.append(self.cmd.ks.makefile(ssh_config_info, ssh_config))


        sshd_config = '/etc/ssh/sshd_config'
        sshd_config_info = """
        Match User root
                HostbasedAuthentication no
        """
        content.append(self.cmd.ks.makefile(sshd_config_info, sshd_config, mode="append"))

        root_info = """
        ROOTRSAKEY=/root/.ssh/id_rsa
        if [ ! -f $ROOTRSAKEY ]; then
            /usr/bin/ssh-keygen -q -t rsa -P '' -f  $ROOTRSAKEY
        fi

        mkdir -p /etc/ssh/authorized_keys
        AUTHKEYS = /etc/ssh/authorized_keys/id_rsa.pub
        if [ ! -f $AUTHKEYS ]; then
            /usr/bin/cat /root/.ssh/id_rsa.pub > $AUTHKEYS
        fi
        """
        content.append(root_info)


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




























