#!/usr/bin/env python
# -*- encoding: UTF8 -*-

#######################################################################
#
#    Copyright (c) 2018 Stefan Helmert <stefan.helmert@t-online.de>
#
#######################################################################

from simpleloggerplus import simpleloggerplus as log
from subprocess import check_output, CalledProcessError
from cryptdomainmgr.modules.common.cdmstatehelper import isReady

defaultConfig = {'depends': 'dhparam, cert', 'cert': 'auto', 'dhparam': 'auto'}

def prepare(serviceConfig, serviceState, state):
    serviceState.setOpStateDone()

def rollover(serviceConfig, serviceState, state):
    serviceState.setOpStateWaiting()
    if not isReady(serviceConfig, state, ['cert', 'dhparam']):
        return
    serviceState.setOpStateRunning()
    log.info('  -> Dovecot reload')
    try:
        rv = check_output(('sudo', 'systemctl', 'start', 'dovecot'))
        rv = check_output(('sudo', 'systemctl', 'reload', 'dovecot'))
    except CalledProcessError as e:
        log.error(e.output)
        raise(e)
    serviceState.setOpStateDone()

def cleanup(serviceConfig, serviceState, state):
    serviceState.setOpStateDone()


