一个简单的系统状态测试实用程序

preflyt的Python项目详细描述


./res/logo.png

轻量级应用程序环境检查程序。

^{em1}仅限$python 3。未来就是现在。

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

开始

要使用preflyt,请安装它pip install preflyt,然后调用它:

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))

跳棋

开箱即用,提供以下跳棋。

NameDescriptionArgs
envCheck environment state
  • name: Variable name.
  • value: (optional) Variable value.
esCheck Elasticsearch state
  • url: The Elasticsearch endpoint URL.
  • colors: (optional) A collection of acceptable cluster colors (aside from ‘green’).
dirCheck directory state
  • path: Directory path.
  • present: (optional, default=True) False if the directory should be absent.
fileCheck file state
  • path: File path.
  • present: (optional, default=True) False if the file should be absent.
psqlCheck 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.
sqliteCheck SQLite3 state
  • path: (optional, default=db.sqlite3) The path to the SQLite database.
webCheck web service state
  • url: The webservice URL
  • statuses: (optional, default=None) A collection of acceptable status codes (aside from 200).

preflyt的未来版本将添加额外的默认检查程序,同时允许第三方发布自己的检查程序。

理念

你知道什么很烂吗?启动长时间运行的数据摄取/处理任务,但在接近尾声时发现缺少外部依赖项(如webservice、binary)或无法访问。“我知道我会怎么做!“你,这个沮丧的程序员,惊呼道。选择自己的冒险:

  • “我将在开始任务之前手动验证事情是否正常。”

    恭喜你,你只是在玩你自己。你不仅会有忘记检查表项目的风险,而且现在你必须在你的团队中实施这种做法。

  • “我将在脚本启动时进行程序检查。”

    暖和起来!希望您的解决方案是配置驱动的。即便如此,你在剧本中使用这个样板的几率有多大?

    # 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)

    等等。现在,您的代码中有一系列疯狂的if语句,更改检查是代码更改,而不是配置更改。此外,您还生成了应该抽象和重用的样板文件。

  • “我将用preflyt程序检查脚本start…上的内容!“

    对了。上面那一系列难看的代码?

    # 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)

    现在您执行的所有检查都是在配置中定义的,没有样板文件!

贡献

更多的跳棋是非常受欢迎的!我们的目标是使这个包不受依赖关系的影响,因此我们非常感谢它的聪明:—)

请为您希望提交的任何检查程序编写测试。Preflyt用鼻子。开发包可以通过pip install -e .[test]安装,测试可以通过nosetests .运行。

许可证

麻省理工学院,版权所有(c)2016 The HumanGeo Group,LLC。有关详细信息,请参阅许可文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java更改WSO2 IS证书会导致登录失败   安卓 Java随机行读取   JavaJComboBox留下了一条线索   用于嵌套对象的java Jasper报告   java动态生成Id值时,如何检索复选框的值   “拼接(a,b,…”的Java等价方法在JavaScript方法中   java在有子面板的jPanel上绘图   java生成时,在包“安卓”中找不到属性“text color”的资源标识符   java如何使用maven插件正确地构建Spring引导远jar?   readUTF中的java EOFEException   在Java中,按星期一到星期天进行排序   java MultiFormatReader无法读取qr,尽管qr是可扫描的   替换java字符串中未知数量的值   java将MySQL连接到Apache nutch   Java从3个不同的枚举中获取随机值   java我需要将多个JRadioButtongGroup和一个jcombobox连接到一个jbutton   java Android sqlite异常。无法实例化活动   如何使用单流创建Map<String,List<Long>>Java8?