# Buggy code
def setupToil(options, userScript=None):
    """
    Creates the data-structures needed for running a toil.

    :type userScript: toil.resource.ModuleDescriptor
    """
    # Make the default config object
    config = Config()
    # Get options specified by the user
    config.setOptions(options)
    if not options.restart:  # Create for the first time
        batchSystemClass, kwargs = loadBatchSystemClass(config)
        # Load the jobStore
        jobStore = loadJobStore(config.jobStore, config=config)
    else:
        # Reload the workflow
        jobStore = loadJobStore(config.jobStore)
        config = jobStore.config
        # Update the earlier config with any options that have been set
        config.setOptions(options)
        # Write these new options back to disk
        jobStore.writeConfigToStore()
        # Get the batch system class
        batchSystemClass, kwargs = loadBatchSystemClass(config)
    if (
        userScript is not None
        and not userScript.belongsToToil
        and batchSystemClass.supportsHotDeployment()
    ):
        kwargs["userScript"] = userScript.saveAsResourceTo(jobStore)
        # TODO: toil distribution

    batchSystem = createBatchSystem(config, batchSystemClass, kwargs)
    try:
        serialiseEnvironment(jobStore)
        yield (config, batchSystem, jobStore)
    finally:
        logger.debug("Shutting down batch system")
        batchSystem.shutdown()


# Traceback (ConfigParser.NoSectionError)
DEBUG:toil.batchSystems.mesos.executor:Starting executor
I0924 22:47:08.587648 43191 exec.cpp:132] Version: 0.22.0
I0924 22:47:08.592193 43192 exec.cpp:206] Executor registered on slave 20150924-214557-83886090-5050-44087-S8
INFO:toil.batchSystems.mesos.executor:Registered with framework
DEBUG:toil.batchSystems.mesos.executor:Running task 0
DEBUG:toil.batchSystems.mesos.executor:Sending status update ...
DEBUG:toil.batchSystems.mesos.executor:Sent status update
DEBUG:toil.batchSystems.mesos.executor:Invoking command: '/usr/bin/python2.7 -E /usr/local/lib/python2.7/dist-packages/toil/worker.py azure:hgvm:tree2 683b1010_55c3_4978_a7e7_853f17ed1803'
DEBUG:toil.batchSystems.mesos.executor:Sent stats message
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/toil/worker.py", line 408, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/toil/worker.py", line 89, in main
jobStore = loadJobStore(jobStoreString)
File "/usr/local/lib/python2.7/dist-packages/toil/common.py", line 366, in loadJobStore
return AzureJobStore( account, namePrefix, config=config )
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/azureJobStore.py", line 78, in __init__
account_key = _fetchAzureAccountKey(accountName)
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/azureJobStore.py", line 63, in _fetchAzureAccountKey
return configParser.get('AzureStorageCredentials', accountName)
File "/usr/lib/python2.7/ConfigParser.py", line 330, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'AzureStorageCredentials'
DEBUG:toil.batchSystems.mesos.executor:Sending status update ...
DEBUG:toil.batchSystems.mesos.executor:Sent status update
I0924 22:47:09.738494 43200 exec.cpp:379] Executor asked to shutdown
CRITICAL:toil.batchSystems.mesos.executor:Shutting down executor...
CRITICAL:toil.batchSystems.mesos.executor:Executor shut down

# Fixed code
def setupToil(options, userScript=None):
    """
    Creates the data-structures needed for running a toil.

    :type userScript: toil.resource.ModuleDescriptor
    """
    # Make the default config object
    config = Config()
    # Get options specified by the user
    config.setOptions(options)
    if not options.restart:  # Create for the first time
        batchSystemClass, kwargs = loadBatchSystemClass(config)
        # Load the jobStore
        jobStore = loadJobStore(config.jobStore, config=config)
    else:
        # Reload the workflow
        jobStore = loadJobStore(config.jobStore)
        config = jobStore.config
        # Update the earlier config with any options that have been set
        config.setOptions(options)
        # Write these new options back to disk
        jobStore.writeConfigToStore()
        # Get the batch system class
        batchSystemClass, kwargs = loadBatchSystemClass(config)
    if (
        userScript is not None
        and not userScript.belongsToToil
        and batchSystemClass.supportsHotDeployment()
    ):
        kwargs["userScript"] = userScript.saveAsResourceTo(jobStore)
        # TODO: toil distribution

    batchSystem = createBatchSystem(config, batchSystemClass, kwargs)
    try:
        # Set environment variables required by job store
        for k, v in jobStore.getEnv().iteritems():
            batchSystem.setEnv(k, v)
        # Set environment variables passed on command line
        for k, v in config.environment.iteritems():
            batchSystem.setEnv(k, v)
        serialiseEnvironment(jobStore)
        yield (config, batchSystem, jobStore)
    finally:
        logger.debug("Shutting down batch system")
        batchSystem.shutdown()