Upgrade current bootstrap.py with the last released one.

    Indeed, when we first run a buildout, a common source of problem
    is to have a locally stale bootstrap, we just try to grab a new copy

    directory
        directory to execute in

    offline
        are we executing buildout in offline mode

    buildout_ver
        forcing to use a specific buildout version (1 | 2)

    onlyif
        Only execute cmd if statement on the host return 0

    unless
        Do not execute cmd if statement on the host return 0

    CLI Example:

    .. code-block:: bash

        salt '*' buildout.upgrade_bootstrap /srv/mybuildout

def upgrade_bootstrap(directory='.',
                      onlyif=None,
                      unless=None,
                      runas=None,
                      env=(),
                      offline=False,
                      buildout_ver=None):
    '''
    Upgrade current bootstrap.py with the last released one.

    Indeed, when we first run a buildout, a common source of problem
    is to have a locally stale bootstrap, we just try to grab a new copy

    directory
        directory to execute in

    offline
        are we executing buildout in offline mode

    buildout_ver
        forcing to use a specific buildout version (1 | 2)

    onlyif
        Only execute cmd if statement on the host return 0

    unless
        Do not execute cmd if statement on the host return 0

    CLI Example:

    .. code-block:: bash

        salt '*' buildout.upgrade_bootstrap /srv/mybuildout
    '''
    if buildout_ver:
        booturl = _URL_VERSIONS[buildout_ver]
    else:
        buildout_ver = _get_buildout_ver(directory)
        booturl = _get_bootstrap_url(directory)
    LOG.debug('Using {0}'.format(booturl))  # pylint: disable=str-format-in-logging
    # try to download an up-to-date bootstrap
    # set defaulttimeout
    # and add possible content
    directory = os.path.abspath(directory)
    b_py = os.path.join(directory, 'bootstrap.py')
    comment = ''
    try:
        oldcontent = _get_bootstrap_content(directory)
        dbuild = _dot_buildout(directory)
        data = oldcontent
        updated = False
        dled = False
        if not offline:
            try:
                if not os.path.isdir(dbuild):
                    os.makedirs(dbuild)
                # only try to download once per buildout checkout
                with salt.utils.files.fopen(os.path.join(
                        dbuild,
                        '{0}.updated_bootstrap'.format(buildout_ver))):
                    pass
            except (OSError, IOError):
                LOG.info('Bootstrap updated from repository')
                data = _urlopen(booturl).read()
                updated = True
                dled = True
        if 'socket.setdefaulttimeout' not in data:
            updated = True
            ldata = data.splitlines()
            ldata.insert(1, 'import socket;socket.setdefaulttimeout(2)')
            data = '\n'.join(ldata)
        if updated:
            comment = 'Bootstrap updated'
            with salt.utils.files.fopen(b_py, 'w') as fic:
                fic.write(salt.utils.stringutils.to_str(data))
        if dled:
            with salt.utils.files.fopen(os.path.join(dbuild,
                                               '{0}.updated_bootstrap'.format(
                                                   buildout_ver)), 'w') as afic:
                afic.write('foo')
    except (OSError, IOError):
        if oldcontent:
            with salt.utils.files.fopen(b_py, 'w') as fic:
                fic.write(salt.utils.stringutils.to_str(oldcontent))

    return {'comment': comment}