Ensure that a CIB-file with the content of the current live CIB is created

    Should be run on one cluster node only
    (there may be races)

    name
        Irrelevant, not used (recommended: {{formulaname}}__cib_present_{{cibname}})
    cibname
        name/path of the file containing the CIB
    scope
        specific section of the CIB (default:
    extra_args
        additional options for creating the CIB-file

    Example:

    .. code-block:: yaml

        mysql_pcs__cib_present_cib_for_galera:
            pcs.cib_present:
                - cibname: cib_for_galera
                - scope: None
                - extra_args: None

def cib_present(name, cibname, scope=None, extra_args=None):
    '''
    Ensure that a CIB-file with the content of the current live CIB is created

    Should be run on one cluster node only
    (there may be races)

    name
        Irrelevant, not used (recommended: {{formulaname}}__cib_present_{{cibname}})
    cibname
        name/path of the file containing the CIB
    scope
        specific section of the CIB (default:
    extra_args
        additional options for creating the CIB-file

    Example:

    .. code-block:: yaml

        mysql_pcs__cib_present_cib_for_galera:
            pcs.cib_present:
                - cibname: cib_for_galera
                - scope: None
                - extra_args: None
    '''
    ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}

    cib_hash_form = 'sha256'

    cib_create_required = False
    cib_cksum_required = False
    cib_required = False

    cibpath = _get_cibpath()
    cibfile = _get_cibfile(cibname)
    cibfile_tmp = _get_cibfile_tmp(cibname)
    cibfile_cksum = _get_cibfile_cksum(cibname)

    if not os.path.exists(cibpath):
        os.makedirs(cibpath)

    if not isinstance(extra_args, (list, tuple)):
        extra_args = []

    if os.path.exists(cibfile_tmp):
        __salt__['file.remove'](cibfile_tmp)

    cib_create = __salt__['pcs.cib_create'](cibfile=cibfile_tmp, scope=scope, extra_args=extra_args)
    log.trace('Output of pcs.cib_create: %s', cib_create)

    if cib_create['retcode'] not in [0] or not os.path.exists(cibfile_tmp):
        ret['result'] = False
        ret['comment'] += 'Failed to get live CIB\n'
        return ret

    cib_hash_live = '{0}:{1}'.format(cib_hash_form, __salt__['file.get_hash'](path=cibfile_tmp, form=cib_hash_form))
    log.trace('cib_hash_live: %s', cib_hash_live)

    cib_hash_cur = _file_read(path=cibfile_cksum)

    if cib_hash_cur not in [cib_hash_live]:
        cib_cksum_required = True

    log.trace('cib_hash_cur: %s', cib_hash_cur)

    if not os.path.exists(cibfile) or not __salt__['file.check_hash'](path=cibfile, file_hash=cib_hash_live):
        cib_create_required = True

    if cib_cksum_required or cib_create_required:
        cib_required = True

    if not cib_create_required:
        __salt__['file.remove'](cibfile_tmp)
        ret['comment'] += 'CIB {0} is already equal to the live CIB\n'.format(cibname)

    if not cib_cksum_required:
        ret['comment'] += 'CIB {0} checksum is correct\n'.format(cibname)

    if not cib_required:
        return ret

    if __opts__['test']:
        __salt__['file.remove'](cibfile_tmp)
        ret['result'] = None
        if cib_create_required:
            ret['comment'] += 'CIB {0} is set to be created/updated\n'.format(cibname)
        if cib_cksum_required:
            ret['comment'] += 'CIB {0} checksum is set to be created/updated\n'.format(cibname)
        return ret

    if cib_create_required:
        __salt__['file.move'](cibfile_tmp, cibfile)

        if __salt__['file.check_hash'](path=cibfile, file_hash=cib_hash_live):
            ret['comment'] += 'Created/updated CIB {0}\n'.format(cibname)
            ret['changes'].update({'cibfile': cibfile})
        else:
            ret['result'] = False
            ret['comment'] += 'Failed to create/update CIB {0}\n'.format(cibname)

    if cib_cksum_required:
        _file_write(cibfile_cksum, cib_hash_live)

        if _file_read(cibfile_cksum) in [cib_hash_live]:
            ret['comment'] += 'Created/updated checksum {0} of CIB {1}\n'.format(cib_hash_live, cibname)
            ret['changes'].update({'cibcksum': cib_hash_live})
        else:
            ret['result'] = False
            ret['comment'] += 'Failed to create/update checksum {0} CIB {1}\n'.format(cib_hash_live, cibname)

    log.trace('ret: %s', ret)

    return ret