import os
import re 
import sys
import sunhpc.commands

from sunhpc.configs import Config

class Command(sunhpc.commands.init.command):
    """
    这个命令是初始化系统的相关配置，并且写入到配置文件.
    <arg type='string' name='iface'>
    提供一个内网的接口、例如: eth1
    </arg>

    <param type='string' name='iface'>
    提供一个内网的接口、例如: eth1
    </param>

    <example cmd='init config eth1 '>
    使用eth1网络接口
    </example>

    <example cmd='init config iface=eth1'>
    使用eth1网络接口
    </example>
    """
    def run(self, params, args):
        (args, iface) = self.fillPostArgs(('iface',))

        if not iface:
            self.abort('must supply an interface')

        self.net.setiface(iface)

        ipaddr = self.net.getip()
        if ipaddr is None:
            self.abort('interface "%s" has no ip address' % iface)

        config_file = self.conf.getConfFile()
        config_dict = {
            'iface': iface,
            'ipaddr': ipaddr,
            'netmask': self.net.getnetmask(),
            'network': self.net.getnetwork(),
            'mac': self.net.getmac(),
            'cidr': self.net.getcidr(),
            'dnsmasq': {
                'enabled': True,
                'config': '/etc/dnsmasq.d/sunhpc.conf',
                'server': self.net.getip(),
                'start_ip': '.'.join(ipaddr.split('.')[:-1]) + '.100',
                'end_ip': '.'.join(ipaddr.split('.')[:-1]) + '.200',
                'dns': '223.5.5.5',
                'systemd_name': 'dnsmasq',
            },
            'http': {
                'enabled': True,
                'wwwroot': '/var/www/html',
                'config': '/etc/httpd/conf.d/sunhpc.conf',
                'httpaddr': 'http://%s' % ipaddr,
                'systemd_name': 'httpd',
            },
            'tftp': {
                'enabled': False,
                'tftpboot': '/srv/pxelinux/tftpboot',
                'systemd_name': 'tftp-server',
            },
            'nfs': {
                'enabled': True,
                'paths': [
                    {
                        'path': '/home',
                        'options': 'rw,sync'
                    },
                    {
                        'path': '/opt',
                        'options': 'rw,sync,no_root_squash'
                    },
                ],
                'systemd_name': 'nfs-server',
            },
            'ssh': {
                'key_types': [
                    'ssh-rsa',
                    'ssh-dss',
                    'ssh-ed25519',
                ]
            },
            'firewall': {
                'enabled': True,
                'backend': 'firewalld',
            },
            'osinfo': {
                'basename': 'sunhpc',
                'os': 'rocky',
                'version': '9.7',
            },
            'pxe':{
                'bios': 'boot/undionly.kpxe',
                'uefi': 'boot/bootx64.efi',
                'ipxe': 'boot/ipxe.efi',
                'ksfile': 'scripts/rhel.ks',
                'ipxeboot': 'scripts/ipxe.sh',
                'repo': 'sunhpc/rockylinux/9.7',
                'initrd': 'images/pxeboot/initrd.img',
                'vmlinuz': 'images/pxeboot/vmlinuz',
            },
            'paths': {
                'bin': '/opt/sunhpc/bin',
                'etc': '/opt/sunhpc/etc',
                'lib': '/opt/sunhpc/lib',
                'var': '/opt/sunhpc/var',
                'pkgs': '/srv/repos/pkgs',
                'repo': '/srv/repos/rockylinux/9.7',
            }
        }

        self.conf.save(config_dict=config_dict)
