fs adapter

This module offers some resource to manage files and directories on a host.

Features

The module currently offers the possibility to manage:

  • Text files (fs::File resource, with textual content in content attribute)

  • Binary files (fs::File resource, with binary file reference in content attribute, using std::file plugin)

  • Directories (fs::Directory resource)

  • Symlinks (fs::Symlink resource)

  • Json files (fs::JsonFile resource)

On top of that, the module makes sure that any dependency in between resources of this type are automatically added when the resources are exported. This means that if the resources fs::Directory[localhost,path=/tmp/test] and fs::File[localhost,path=/tmp/test/a.txt] are both exported, the module will make sure that:

  1. If the directory is purged, the file is purged as well

  2. If the directory is not purged, it is in the dependencies of the file

Usage examples

Json files

The following model will manage a file at the path /tmp/example.json on the host of the orchestrator, and will make sure that:

  1. In the json object of the file, there is a key people, which has as value a list of objects. Within that list, there should be exactly one dict with the key name and value bob, and this dict should be equal to `{“name”: “bob”, “age”: 20}.

{
    "animals": [], // This will stay untouched
    "people": [
        {
            "name": "bob",
            "age": 20
        },
        {
            "name": "fred" // This will stay untouched
        }
    ]
}
  1. In the json object of the file, there is a key people, which has as value a list of objects. Within that list, there should be exactly one dict with the key name and value alice, and this dict should have at least the following keys set to the desired values: {"name": "alice", "age": 20}.

{
    "people": [
        {
            "name": "alice",
            "age": 20,
            "nickname": "al" // This will stay untouched
        }
    ]
}
  1. In the json object of the file, there is no object with key name and value eve in a list at the key people, at the root of the object.

{
    "people": [
        // The resource will make sure the dict below isn't part of
        // the file
        // {
        //     "name": "eve"
        // }
    ]
}
import mitogen
import fs

import std

host = std::Host(
    name="localhost",
    os=std::linux,
    via=mitogen::Local(),
)

fs::JsonFile(
    host=host,
    path="/tmp/example.json",
    format="json",
    values=[
        fs::JsonObject(
            path="people[name=bob]",
            operation="replace",
            value={"name": "bob", "age": 20},
        ),
        fs::JsonObject(
            path="people[name=alice]",
            operation="merge",
            value={"name": "alice", "age": 20},
        ),
        fs::JsonObject(
            path="people[name=eve]",
            operation="remove",
            value={},
        ),
    ],
)

Similarly to the other file resources, the file permissions, owner and group can be managed too.