#coding:utf-8
import textwrap
class Modules(object):
    """
    Configure sshd, authorized, rsa and so on. 
    """
    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):

        text = ''
        # get root's public keys
        public = '/etc/ssh/authorized_keys/id_rsa.pub'
        try:
            with open(public, 'r') as f:
                for key in f.readlines():
                    if len(key) > 0:
                        text = key[:-1]
        except:
            pass

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

        parts = """
        [ ! -e /root/.ssh ] && mkdir -p /root/.ssh
        chmod -R 700 /root/.ssh

        cat > /root/.ssh/authorized_keys << 'EOF'
        %s
        EOF
        chmod 600 /root/.ssh/authorized_keys

        ifconfig virbr0 down
        brctl delbr virbr0
        systemctl disable libvirtd.service

        sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config

        systemctl set-default multi-user.target

        systemctl enable autofs
        """ % (text)

        content.append(parts)
        self.cmd.ks.addPost(content)

    def __repr__(self):
        return "kickstart-secret"
