Restart OpenStack service immediately, or only if it's running longer than
    specified value

    CLI Example:

    .. code-block:: bash

        salt '*' openstack_mng.restart_service neutron
        salt '*' openstack_mng.restart_service neutron minimum_running_time=600

def restart_service(service_name, minimum_running_time=None):
    '''
    Restart OpenStack service immediately, or only if it's running longer than
    specified value

    CLI Example:

    .. code-block:: bash

        salt '*' openstack_mng.restart_service neutron
        salt '*' openstack_mng.restart_service neutron minimum_running_time=600
    '''

    if minimum_running_time:
        ret_code = False
        # get system services list for interesting openstack service
        services = __salt__['cmd.run'](['/usr/bin/openstack-service', 'list', service_name]).split('\n')
        for service in services:
            service_info = __salt__['service.show'](service)
            with salt.utils.files.fopen('/proc/uptime') as rfh:
                boot_time = float(
                    salt.utils.stringutils.to_unicode(
                        rfh.read()
                    ).split(' ')[0]
                )

            expr_time = int(service_info.get('ExecMainStartTimestampMonotonic', 0)) / 1000000 < boot_time - minimum_running_time
            expr_active = service_info.get('ActiveState') == "active"

            if expr_time or not expr_active:
                # restart specific system service
                ret = __salt__['service.restart'](service)
                if ret:
                    ret_code = True

        return ret_code
    else:
        # just restart
        os_cmd = ['/usr/bin/openstack-service', 'restart', service_name]
        return __salt__['cmd.retcode'](os_cmd) == 0