Skip to main content
./res/logo.png

A lightweight application environment checker.

Python 3 only. The future is now.

https://travis-ci.org/humangeo/preflyt.svg https://coveralls.io/repos/github/humangeo/preflyt/badge.svg?branch=master

Getting started

To use Preflyt, install it pip install preflyt, and then invoke it:

import preflyt

ok, results = preflyt.check([
    # Assert the presence and value of the $APP_ENV environment variable
    {"checker": "env", "name": "APP_ENV", "value": "production"},

    # Assert that a file at the given path exists
    {"checker": "file", "path": DATA_FILE},
])

if ok:
    print("Preflyt checks passed.")
    run()
else:
    print("Preflyt checks failed!")
    for result in results:
        if not result["success"]:
            print("{checker[checker]}: {message}".format(**result))

Checkers

Out of the box, the following checkers are available.

Name

Description

Args

env

Check environment state

  • name: Variable name.

  • value: (optional) Variable value.

es

Check Elasticsearch state

  • url: The Elasticsearch endpoint URL.

  • colors: (optional) A collection of acceptable cluster colors (aside from ‘green’).

dir

Check directory state

  • path: Directory path.

  • present: (optional, default=True) False if the directory should be absent.

file

Check file state

  • path: File path.

  • present: (optional, default=True) False if the file should be absent.

psql

Check PostgreSQL state

  • host: (optional, default=localhost) Postgres hostname.

  • port: (optional, default=5432) Postgres server port.

  • dbname: (optional, default=None) The database name for which to test.

sqlite

Check SQLite3 state

  • path: (optional, default=db.sqlite3) The path to the SQLite database.

web

Check web service state

  • url: The webservice URL

  • statuses: (optional, default=None) A collection of acceptable status codes (aside from 200).

Future versions of Preflyt will add additional default checkers while allowing third parties to ship their own.

Philosophy

You know what sucks? Kicking off a long running data ingestion/processing task only to discover, near the end, that an external dependency (e.g. webservice, binary) is missing or otherwise inaccessible. “I know what I’ll do!” you, the frustrated programmer, exclaims. Choose your own adventure:

  • “I’m going to manually verify things are as they should be before I kick off the task.”

    Congratulations, you just played yourself. Not only do you run the risk of forgetting a checklist item, but now you have to enforce this practice within your team.

  • “I’m going to programatically check things on script start.”

    Getting warm! Hopefully your solution is configuration driven. Even then, what are the odds you wind up with this boilerplate across your scripts?

    # settings.py
    if env_name == "production":
        ES_HOST = "http://example.com"
        POSTGRES_HOST = "10.0.1.120"
        ENABLE_DATA_DIR_CHECK = True
    else:
        ES_HOST = "localhost:9200"
        POSTGRES_HOST = "localhost"
        ENABLE_DATA_DIR_CHECK = False
    DATA_DIR = "/mnt/data/dir"
    DATA_FILE = "/mnt/data/dir/metadata.json"
    POSTGRES_PORT = 5432
    
    # run.py
    if not requests.get(settings.ES_HOST).status_ok: #Now you've got a requests dependency
        print("Elasticsearch is unreachable.")
        sys.exit(1)
    if settings.ENABLE_DATA_DIR_CHECK and not os.path.exists(settings.DATA_DIR): # Whoops, should have used `isdir`
        print("Can't access: ", settings.DATA_DIR)
        sys.exit(1)
    if not os.path.exists(settings.DATA_FILE): # Whoops, should have used `isfile`
        print("Can't access: ", settings.DATA_FILE)
        sys.exit(1)
    try:
        postgres.connect(settings.POSTGRES_HOST, settings.POSTGRES_PORT)
    except Exception as exe:
        print(exe)
        sys.exit(1)

    And so forth. You’ve now got a crazy-long series of if statements in your code, and changing the checks is a code change, not a configuration change. Also, you’ve generated boilerplate that should be abstracted and reused.

  • “I’m going to programatically check things on script start… with Preflyt!”

    Bingo. That ugly series of code above?

    # settings.py
    CHECKS = [
        {"checker": "web", "url": ES_HOST},
        {"checker": "psql", "host": POSTGRES_HOST, "port": POSTGRES_PORT},
        {"checker": "file", "path": DATA_FILE},
    ]
    if ENVNAME == "production":
        CHECKS.append({"checker": "dir", "path": DATA_DIR})
    
    # run.py
    import preflyt
    ok, results = preflyt.check(settings.CHECKS)
    if not ok:
        print([result in results if not result["success"]])
        sys.exit(1)

    Now all the checks you’re performing are defined in configuration, and no boilerplate!

Contributing

Additional checkers are more than welcome! The goal is to keep this package free of dependencies, so cleverness is appreciated :-)

Please write tests for whatever checkers you wish to submit. Preflyt uses nose. Development packages can be installed via pip install -e .[test], and tests can be run via nosetests ..

License

MIT, Copyright (c) 2016 The HumanGeo Group, LLC. See the LICENSE file for more information.

Metadata

Release files for preflyt 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for preflyt 0.4.0
File Size Uploaded Provenance Actions
preflyt-0.4.0.tar.gz 7.6 kB Without provenance Details for preflyt-0.4.0.tar.gz

Release files / preflyt-0.4.0.tar.gz

Download URL preflyt-0.4.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a41a035611e2bd18e9453fc6a4ce1efd76ad22396ede7c961ae6f90e29f0ad91
BLAKE2b-256 checksum
How to use checksums
28b510a2d6fc3a16f58ab6af43e95109e4f281055a0af60fb16a7bf08c8e75f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for preflyt 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page