Skip to main content

File syncing via Fabric.

Project description

fabsync

Overview

This is a file-syncing tool for Fabric. It's almost as straightforward to use as rsync, but with some of the features that will be familiar from deployment automation tools like Ansible.

Key points:

  • Source files are kept in a simple directory tree, as if you were going to rsync them.
  • Metadata—such as ownership and permissions—is configured in TOML files throughout the tree.
  • Rendering functions can be configured to transform file contents. You can hook these up to template engines or anything else.
  • Paths and tags can be used to sync a subset of the file tree.

The most important thing to note is that this is a library, not a framework. It does one simple thing, which you're welcome to integrate into your own deployment scheme any way you like. For illustration purposes, here's a fragment of a hypothetical fabfile.py that you might write:

import io
from pathlib import Path
from typing import Any, Mapping

from fabric import Connection, task
import fabsync
import pystache
import tomli


def _mustache_renderer(conn: Connection) -> fabsync.Renderer:
    try:
        result = conn.get('/usr/local/etc/fabsync.toml', io.BytesIO())
    except FileNotFoundError:
        host = {}
    else:
        host = tomli.loads(result.local.getvalue().decode())

    renderer = pystache.Renderer(escape=lambda s: s)

    def render(path: Path, vars: Mapping[str, Any]) -> str:
        with path.open('rt') as f:
            return renderer.render(f.read(), host | vars)

    return render


@task(iterable=['tag'], incrementable=['verbose'])
def sync(conn, subpath=None, tag=None, verbose=0):
    root = fabsync.load('files', '/')
    selector = fabsync.ItemSelector.new(subpath=subpath, tags=tag)
    renderers = {'mustache': _mustache_renderer(conn)}

    dry_run = conn['run']['dry']

    for result in fabsync.isync(conn, root, selector, renderers, dry_run=dry_run):
        print(f"{result.path}{' [modified]' if result.modified else ''}")
        if verbose > 0 and result.diff:
            print(result.diff.decode())

Of course you may also wish to save the results and use them to decide what other actions to perform (e.g. restarting services).

Because we're just dealing with files, fabsync can offer a few other convenience functions, including one that renders the source tree into human-readable rows. If you rendered this with PrettyTable, it might look like this:

+-----------------------------------+------+-------+------------+----------+-------+--------+
| Path                              | User | Group | Mode       | Renderer | Diff? | Tags   |
+-----------------------------------+------+-------+------------+----------+-------+--------+
| /usr/                             |      |       |            |          |       |        |
| /usr/local/                       |      |       |            |          |       |        |
| /usr/local/etc/                   |      |       |            |          |       |        |
| /usr/local/etc/mail/              |      |       |            |          |       | mail   |
| /usr/local/etc/mail/smtpd.conf    |      |       |            |          |       | mail   |
| /usr/local/etc/rc.d/              | root | wheel |            |          |       |        |
| /usr/local/etc/rc.d/restic-server | root | wheel | -rwxr-xr-x |          |       | restic |
| /usr/local/etc/smb4.conf          |      |       |            |          |       |        |
| /usr/local/etc/wireguard/         |      |       | drwx------ |          |       | wg     |
| /usr/local/etc/wireguard/wg0.conf |      |       | -rw------- | mustache |       | wg     |
| /usr/local/utils/                 |      |       |            |          |       |        |
+-----------------------------------+------+-------+------------+----------+-------+--------+

Refer to the documentation for more details and a step-by-step guide.

Contributing

I wrote this for my own purposes and published it primarily to enforce the discipline of comprehensive documentation and tests. It's deliberately narrow in scope. New features are not out of the question, but mostly only those that would be considerably more effort to implement externally.

Feel free to reach out with bug reports or suggestions. And note the Unlicense, which means you can also just take the code and do what you like with it.

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fabsync-1.0.0.tar.gz (28.7 kB view details)

Uploaded Source

Built Distribution

fabsync-1.0.0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file fabsync-1.0.0.tar.gz.

File metadata

  • Download URL: fabsync-1.0.0.tar.gz
  • Upload date:
  • Size: 28.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for fabsync-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0f8e154d4e91e94204e556be5076be61d3a0d0394b4e81fef99796a88aa90138
MD5 f0316a9ef476402f4d2711f1cb0d9e5d
BLAKE2b-256 d4305a8fe2647b5a06a3ace2b537f8798693c32d72041cc3416f1275db1dd536

See more details on using hashes here.

Provenance

File details

Details for the file fabsync-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: fabsync-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for fabsync-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3f34e74a08179cd033d6eb242a4e3df7357535995946146154b636ccc3ac1bb
MD5 4b23e36248bf682494f4178d7140d2ee
BLAKE2b-256 dffd94e5fecae17920761f0de3880609c7a1b1190665f04f433709ef77193163

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page