#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#########################################################################
# pyeole.service - manage EOLE services
# Copyright © 2014 Pôle de Compétence EOLE <eole@ac-dijon.fr>
#
# License CeCILL:
#  * in french: http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html
#  * in english http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
#########################################################################
from creole.client import CreoleClient
from creole.config import VIRTMASTER
from pyeole.diagnose import compare_iptables, compare_ipset, NOT_AVAILABLE, OK


from os.path import isfile
from subprocess import getstatusoutput


STATUS_FILE = '/var/lib/eole/reports/bastion.log'


if __name__ == "__main__":
    client = CreoleClient()

    cmd = [". /usr/lib/eole/diagnose.sh"]
    cmd.append('EchoGras "*** Pare-feu"')
    if client.get_creole('activer_firewall') == 'non':
        cmd.append('Inactif "Pare-feu"')
    else:
        if client.get_creole('activer_firewall'):
            cmd.append('printf ".  %$((len_pf+3))s => " "Génération des règles"')
            if isfile(STATUS_FILE):
                cmd.append('. /var/lib/eole/reports/bastion.log')
                cmd.append('msg="(`date +%T\' \'%d/%m/%y -d @$DATE`)"')
                cmd.append('[ "$STATUS" = "0" ] && EchoVert "Ok $msg"')
                cmd.append('[ "$STATUS" != "0" ] && EchoRouge "Erreur : $MSG $msg"')
            else:
                cmd.append('EchoOrange "Aucune information"')
            ret = compare_iptables()
            mode_conteneur_actif = client.get_creole('mode_conteneur_actif')
            if mode_conteneur_actif == 'oui':
                cmd.append('printf ".  %${len_pf}s => " "maitre"')
            else:
                cmd.append('printf ".  %${len_pf}s => " "Pare-feu"')
            if ret != NOT_AVAILABLE:
                if ret == OK:
                    cmd.append('EchoVert "Ok"')
                else:
                    cmd.append('EchoRouge "Erreur"')
            else:
                cmd.append('EchoOrange "Non généré"')
            if mode_conteneur_actif == 'oui':
                for container in client.get_groups():
                    if container not in [VIRTMASTER, 'all']:
                        ret = compare_iptables(container)
                        cmd.append('printf ".  %${{len_pf}}s => " "{0}"'.format(container))
                        if ret != NOT_AVAILABLE:
                            if ret == OK:
                                cmd.append('EchoVert "Ok"')
                            else:
                                cmd.append('EchoRouge "Erreur"')
                        else:
                            cmd.append('EchoOrange "Non généré"')

            ret = compare_ipset()
            if ret != NOT_AVAILABLE:
                cmd.append('printf ".  %${len_pf}s => " "IPSet"')
                if ret == OK:
                    cmd.append('EchoVert "Ok"')
                else:
                    cmd.append('EchoRouge "Erreur"')
                cmd.append('echo')
        else:
            cmd.append('EchoOrange "désactivé"')
    cmd_string = "\n".join(cmd)
    err, ret = getstatusoutput(cmd_string)
    print(ret)
