.. versionchanged:: 2015.5.0
        The :mod:`lxc.started <salt.states.lxc.started>` state has been renamed
        to ``lxc.running``

    Ensure that a container is running

    .. note::

        This state does not enforce the existence of the named container, it
        just starts the container if it is not running. To ensure that the
        named container exists, use :mod:`lxc.present
        <salt.states.lxc.present>`.

    name
        The name of the container

    path
        path to the container parent
        default: /var/lib/lxc (system default)

        .. versionadded:: 2015.8.0

    restart : False
        Restart container if it is already running

    .. code-block:: yaml

        web01:
          lxc.running

        web02:
          lxc.running:
            - restart: True

def running(name, restart=False, path=None):
    '''
    .. versionchanged:: 2015.5.0
        The :mod:`lxc.started <salt.states.lxc.started>` state has been renamed
        to ``lxc.running``

    Ensure that a container is running

    .. note::

        This state does not enforce the existence of the named container, it
        just starts the container if it is not running. To ensure that the
        named container exists, use :mod:`lxc.present
        <salt.states.lxc.present>`.

    name
        The name of the container

    path
        path to the container parent
        default: /var/lib/lxc (system default)

        .. versionadded:: 2015.8.0

    restart : False
        Restart container if it is already running

    .. code-block:: yaml

        web01:
          lxc.running

        web02:
          lxc.running:
            - restart: True
    '''
    ret = {'name': name,
           'result': True,
           'comment': 'Container \'{0}\' is already running'.format(name),
           'changes': {}}

    state = {'old': __salt__['lxc.state'](name, path=path)}
    if state['old'] is None:
        ret['result'] = False
        ret['comment'] = 'Container \'{0}\' does not exist'.format(name)
        return ret
    elif state['old'] == 'running' and not restart:
        return ret
    elif state['old'] == 'stopped' and restart:
        # No need to restart since container is not running
        restart = False

    if restart:
        if state['old'] != 'stopped':
            action = ('restart', 'restarted')
        else:
            action = ('start', 'started')
    else:
        if state['old'] == 'frozen':
            action = ('unfreeze', 'unfrozen')
        else:
            action = ('start', 'started')

    if __opts__['test']:
        ret['result'] = None
        ret['comment'] = ('Container \'{0}\' would be {1}'
                          .format(name, action[1]))
        return ret

    try:
        if state['old'] == 'frozen' and not restart:
            result = __salt__['lxc.unfreeze'](name, path=path)
        else:
            if restart:
                result = __salt__['lxc.restart'](name, path=path)
            else:
                result = __salt__['lxc.start'](name, path=path)
    except (CommandExecutionError, SaltInvocationError) as exc:
        ret['result'] = False
        ret['comment'] = exc.strerror
        state['new'] = __salt__['lxc.state'](name, path=path)
    else:
        state['new'] = result['state']['new']
        if state['new'] != 'running':
            ret['result'] = False
            ret['comment'] = ('Unable to {0} container \'{1}\''
                              .format(action[0], name))
        else:
            ret['comment'] = ('Container \'{0}\' was successfully {1}'
                              .format(name, action[1]))
        try:
            ret['changes']['restarted'] = result['restarted']
        except KeyError:
            pass

    if state['old'] != state['new']:
        ret['changes']['state'] = state
    return ret