#Cache invalidation for host directories

1 messages · Page 1 of 1 (latest)

glad pollen
#

Hello,

I wanted to clarify how cache is calculated / invalidated for directories from the host.

I've talked about this a bit in #python-sdk-dev🐍, but I have a use case where I'm importing objects into terraform. In practice, this means chaining commands, but each individual step needs to be allowed to fail, so the execs are happening in a loop with the state file being exported out on success, and imported in by the next step.

#

I'm conditionally (because the file may not exist) mounting the state using this snippet:

async def terraform_state(client: dagger.Client):
    logger.info('Checking entries')
    tf_dir = client.host().directory("terraform")
    entries = await tf_dir.entries()
    def add_tf_state(image: dagger.Container) -> dagger.Container:
        if 'terraform.tfstate' in entries:
            logger.info('Found terraform state! ')
            image = image.with_file('/terraform/terraform.tfstate',
                                    tf_dir.file("terraform.tfstate"))
        return image

    return add_tf_state

which I the use in a loop as:

for tf_identifier, resource_id in generate_lambda_imports():
                try:
                    result = (
                        tf_image
                        .with_(await terraform_state(client))
                        .with_exec(
                            ['-chdir=/terraform', 'import',
                             tf_identifier, resource_id],
                        )
                        .file("/terraform/terraform.tfstate")
                    )
                    success = await result.export("terraform/terraform.tfstate")
                    if result:
                        logger.info(
                            f"[green]Imported {tf_identifier} {resource_id}![/]"
                        )
                except dagger.ExecError as err:
                    result = err.stderr
                    if 'already managed' in result:
                        logger.info(
                            f"[yellow]{tf_identifier} already imported!"
                        )
                    else:
                        logger.error(
                            f"[bold red]Caught error importing {tf_identifier}: " +
                            f"[/]\n {result}"
                        )
                        success = False
#

But what I'm seeing is, despite exporting a file out to the directory terraform and adding a new entry, the call to client.host().directory('terraform') is cached, so each step in the loop doesn't get the state from the previous one.

#

I could of course replace theclient.host().directory logic with something Python native, I'm just wondering why my intuition is wrong here 🙁

limpid prism
#

👋 client.host().directory is only performed once for the session and then additional calls will give you the same reference you originally created. I think what you want instead of exporting things to the host is to use a cacheVolume between the different loops so the tfstate is shared between of all them.

glad pollen
#

Ahaa, a cache volume is a good idea! I'm already using one for the terraform providers and modules, to speedup subsequent runs

#

Great thanks, that's very clear

limpid prism
#

np, we're here if you have further questions.
happy hacking 🧑‍💻

glad pollen
#

Sorry, one further question; can I mount / copy files into a cache volume to 'hydrate it'?

limpid prism
glad pollen
#

Right, I see; within a container, I can use with_exec to movefiles in / out of the cache, but I can't add to the cache using with_file or pull things out using .file("/cache_mount_point/my_cool_file") for example.