#!/usr/bin/env python
# -*- 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 pyeole.diagnose import compare_iptables, compare_ipset, NOT_AVAILABLE, OK


from os.path import isfile
from commands 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:
        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()
        if ret != NOT_AVAILABLE:
            cmd.append('printf ".  %${len_pf}s => " "Pare-feu"')
            if ret == OK:
                cmd.append('EchoVert "Ok"')
            else:
                cmd.append('EchoRouge "Erreur"')

        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_string = "\n".join(cmd)
    err, ret = getstatusoutput(cmd_string)
    print ret
