Skip to main content

fh-fablib

Project description

Usage

  1. Install pipx

  2. Install fh-fablib

    1. pipx install fh_fablib if you’re happy with the packaged version

    2. pipx install ~/Projects/fh-fablib if you have a local git checkout you want to install from

  3. Add a fabfile.py to your project. A minimal example follows:

    import fh_fablib as fl
    
    fl.require("1.0.20220411")
    fl.config.update(base=fl.Path(__file__).parent, host="www-data@feinheit06.nine.ch")
    
    environments = [
        fl.environment(
            "production",
            {
                "domain": "example.com",
                "branch": "main",
                "remote": "production",
            },
            aliases=["p"],
        ),
    ]
    
    ns = fl.Collection(*fl.GENERAL, *fl.NINE, *environments)
  4. Run fl hook to provide a default pre-commit configuration (or fl hook --force to override the dotfiles).

  5. Run fl --list to get a list of commands.

Configuration values

  • app = "app": Name of primary Django app containing settings, assets etc.

  • base: pathlib.Path object pointing to the base dir of the project.

  • branch: Branch containing code to be deployed.

  • domain: Primary domain of website. The database name and cache key prefix are derived from this value.

  • environments: A dictionary of environments, see below.

  • environment: The name of the active environment or "default".

  • force: Always force-push when deploying.

  • host: SSH connection string (username@server)

  • remote: git remote name for the server. Only used for the fetch task.

Adding or overriding bundled tasks

For the sake of an example, suppose that additional processes should be restarted after deployment. A custom deploy task follows:

# ... continuing the fabfile above

@fl.task
def deploy(ctx):
    """Deploy once 🔥"""
    fl.deploy(ctx)  # Reuse
    with fl.Connection(fl.config.host) as conn:
        fl.run(conn, "systemctl --user restart other.service")

ns.add_task(deploy)

Multiple environments

If you need multiple environments, add environment tasks as follows:

import fh_fablib as fl

fl.require("1.0.20220411")
fl.config.update(base=fl.Path(__file__).parent, host="www-data@feinheit06.nine.ch")

environments = [
    fl.environment(
        "production",
        {
            "domain": "example.com",
            "branch": "main",
            "remote": "production",
        },
        aliases=["p"],
    ),
    fl.environment(
        "next",
        {
            "domain": "next.example.com",
            "branch": "next",
            "remote": "next",
        },
        aliases=["n"],
    ),
]

ns = fl.Collection(*fl.GENERAL, *fl.NINE, *environments)

Now, fl production pull-db, fl next deploy and friends should work as expected.

Available tasks

fh_fablib.GENERAL

  • bitbucket: Create a repository on Bitbucket and push the code

  • check: Check the coding style

  • cm: Compile the translation catalogs

  • deploy: Deploy once 🔥

  • dev: Run the development server for the frontend and backend

  • fetch: Ensure a remote exists for the server and fetch

  • freeze: Freeze the virtualenv’s state

  • github: Create a repository on GitHub and push the code

  • hook: Install the pre-commit hook

  • local: Local environment setup

  • mm: Update the translation catalogs

  • pull-db: Pull a local copy of the remote DB and reset all passwords

  • pull-media: Rsync a folder from the remote to the local environment

  • reset-pw: Set all user passwords to "password"

  • reset-sq: Reset all PostgreSQL sequences

  • update: Update virtualenv and node_modules to match the lockfiles

  • upgrade: Re-create the virtualenv with newest versions of all libraries

fh_fablib.NINE

  • nine: Run all nine🌟 setup tasks in order

  • nine-alias-add: Add aliasses to a nine-manage-vhost virtual host

  • nine-alias-remove: Remove aliasses from a nine-manage-vhost virtual host

  • nine-checkout: Checkout the repository on the server

  • nine-db-dotenv: Create a database and initialize the .env. Currently assumes that the shell user has superuser rights (either through PGUSER and PGPASSWORD environment variables or through peer authentication)

  • nine-disable: Disable a virtual host, dump and remove the DB and stop the gunicorn@ unit

  • nine-reinit-from: Reinitialize an environment from a different environment

  • nine-restart: Restart the application server

  • nine-ssl: Activate SSL

  • nine-unit: Start and enable a gunicorn@ unit

  • nine-venv: Create a venv and install packages from requirements.txt

  • nine-vhost: Create a virtual host using nine-manage-vhosts

Building blocks

The following functions may be used to build your own tasks. They cannot be executed directly from the command line.

Running commands

  • run(c, ...): Wrapper around Context.run or Connection.run which always sets a few useful arguments (echo=True, pty= True and replace_env=False at the time of writing)

Checks

  • _check_branch(ctx): Terminates if checked out branch does not match configuration.

  • _check_no_uncommitted_changes(ctx): Terminates if there are uncommitted changes on the server.

Helpers

  • _local_env(path=".env"): speckenv.env for a local env file

  • _srv_env(conn, path): speckenv.env for a remote env file

  • _python3(): Return the path of a Python 3 executable. Prefers newer Python versions.

  • _local_dotenv_if_not_exists(): Ensure a local .env with a few default values exists. Does nothing if .env exists already.

  • _local_dbname(): Ensure a local .env exists and return the database name.

  • _dbname_from_dsn(dsn): Extract the database name from a DSN.

  • _dbname_from_domain(domain): Mangle the domain to produce a string suitable as a database name, database user and cache key prefix.

  • _concurrently(ctx, jobs): Run a list of shell commands concurrently and wait for all of them to terminate (or Ctrl-C).

  • _random_string(length, chars=None): Return a random string of length, suitable for generating secret keys etc.

  • require(version): Terminate if fh_fablib is older.

  • terminate(msg): Terminate processing with an error message.

Deployment

  • _deploy_django: Update the Git checkout, update the virtualenv.

  • _deploy_staticfiles: Collect staticfiles.

  • _rsync_static: rsync the local static/ folder to the remote, optionally deleting everything which doesn’t exist locally.

  • _nine_restart: Restart the systemd control unit.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

fh_fablib-1.0.20220411.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

fh_fablib-1.0.20220411-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file fh_fablib-1.0.20220411.tar.gz.

File metadata

  • Download URL: fh_fablib-1.0.20220411.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for fh_fablib-1.0.20220411.tar.gz
Algorithm Hash digest
SHA256 a593e089c7ed76d06a93e3d8e0e9686342f3a7fb3925d1065d3703c555df3ffc
MD5 cb25bd4cb786449169d72640b0eed3f2
BLAKE2b-256 068251f3da2ef4129106d0a3b7048c4cfd9b689baa2862f4f8cdfd8d21993c78

See more details on using hashes here.

File details

Details for the file fh_fablib-1.0.20220411-py3-none-any.whl.

File metadata

File hashes

Hashes for fh_fablib-1.0.20220411-py3-none-any.whl
Algorithm Hash digest
SHA256 b5b4109d37ba4d1a1b67c4ee085217e23c6314c6d955eef2a8fc0dc77297db54
MD5 38bd66aef81db1ca518f7dbec6240e2e
BLAKE2b-256 df50ff93721019ab58b76993b6507b7285806e1c87d459d9fff2fc55441adb00

See more details on using hashes here.

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