Package: preflyt
A lightweight application environment checker.Python 3 only. The future is now.Getting startedTo use Preflyt, install itpip install preflyt, and then invoke it:importpreflytok,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},])ifok:print("Preflyt checks passed.")run()else:print("Preflyt checks failed!")forresultinresults:ifnotresult["success"]:print("{checker[checker]}:{message}".format(**result))CheckersOut of the box, the following checkers are available.NameDescriptionArgsenvCheck environment statename: Variable name.value: (optional) Variable value.esCheck Elasticsearch stateurl: The Elasticsearch endpoint URL.colors: (optional) A collection of acceptable cluster colors (aside from ‘green’).dirCheck directory statepath: Directory path.present: (optional, default=True) False if the directory should be absent.fileCheck file statepath: File path.present: (optional, default=True) False if the file should be absent.psqlCheck PostgreSQL statehost: (optional, default=localhost) Postgres hostname.port: (optional, default=5432) Postgres server port.dbname: (optional, default=None) The database name for which to test.sqliteCheck SQLite3 statepath: (optional, default=db.sqlite3) The path to the SQLite database.webCheck web service stateurl: The webservice URLstatuses: (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.PhilosophyYou 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.pyifenv_name=="production":ES_HOST="http://example.com"POSTGRES_HOST="10.0.1.120"ENABLE_DATA_DIR_CHECK=Trueelse:ES_HOST="localhost:9200"POSTGRES_HOST="localhost"ENABLE_DATA_DIR_CHECK=FalseDATA_DIR="/mnt/data/dir"DATA_FILE="/mnt/data/dir/metadata.json"POSTGRES_PORT=5432# run.pyifnotrequests.get(settings.ES_HOST).status_ok:#Now you've got a requests dependencyprint("Elasticsearch is unreachable.")sys.exit(1)ifsettings.ENABLE_DATA_DIR_CHECKandnotos.path.exists(settings.DATA_DIR):# Whoops, should have used `isdir`print("Can't access: ",settings.DATA_DIR)sys.exit(1)ifnotos.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)exceptExceptionasexe: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.pyCHECKS=[{"checker":"web","url":ES_HOST},{"checker":"psql","host":POSTGRES_HOST,"port":POSTGRES_PORT},{"checker":"file","path":DATA_FILE},]ifENVNAME=="production":CHECKS.append({"checker":"dir","path":DATA_DIR})# run.pyimportpreflytok,results=preflyt.check(settings.CHECKS)ifnotok:print([resultinresultsifnotresult["success"]])sys.exit(1)Now all the checks you’re performing are defined in configuration, and no boilerplate!ContributingAdditional 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 viapip install -e .[test], and tests can be run vianosetests ..LicenseMIT, Copyright (c) 2016 The HumanGeo Group, LLC. See the LICENSE file for more information.