Git Product home page Git Product logo

runbook.py's Introduction

runbook.py (v0.2)

pip3 install runbook

Inspired by this blog post by Dan Slimmon.

Define your own run-book in a class extending from Runbook. Every method that doesn't begin with an underscore is read in as a step to be completed, in order. The step name will be built from the method name, and the description is taken either from the method's own docstring or from any data returned from invoking the method.

from runbook import Runbook


class CustomRunbook(Runbook):
   
    def first_step(self):
        """
        Do ABC now.
        """
    
    def second_step():
        """
        Do EFG then wait 1 hour.
        """

    def third_step():
        task = "reboot"
        return f"perform a {task}"
    
    @staticmethod
    def last_step():
        """Everything ok?"""

Every Runbook object comes with a default main method that you can use to execute the script.

if __name__ == '__main__':
    CustomRunbook.main()

The run-book object can also be instantiated and run directly.

book = CustomRunbook(file_path="path/to/file")
book.run()

You should avoid using the step names run and main, which are already defined. If you need to override these methods to define custom behavior then that is fine.

As steps are completed, the results are written out to a log file. You can set a custom log file path by passing an argument to main, as in:

python3 my_runbook.py output.log

When reusing the same log file, already completed steps will be skipped. Any new steps found in the Runbook and not already in the log will be processed as normal, with results appended to the end of the file.

License

Licensed under the Apache Software License 2.0 (ASL 2.0).

runbook.py's People

Contributors

unquietcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

runbook.py's Issues

pypy deploy

  • dependency inclusion from requirements file
  • GPG signing using code signing key
  • list signing key somewhere like keybase and linked to from pypy
  • deploy runbook-1.0 package to test pypy
  • deploy runbook-1.0 package to prod pypy

styling improvements

  • better spacing between tasks, around repeatable tasks in a resume flow
  • reduce wait time for long text, consider a non-linear curve
  • add a 'done' message to the screen output
  • consider printing the book name in screen output

support for Acceptance Criteria

Consider supporting the notion of Acceptance Criteria, or otherwise answering the question "How do you know it's done?" for each step of a runbook.

something like...

Did you do the thing?
  > yes

Are you sure? You can perform the following action to confirm:
  > Make a text record in the new DNS zone and use dig to request it.
  > dig xyz.potatosalad.com TXT
  > It should return the expected text value

Were you able to confirm?
  > yes

better support for dynamic docs

relates to #40

  • remove 'dynamic documentation' return string
  • add method parameter docs
    • captures either a string or a callable
    • (turn strings into callables)
  • add annotation @with_docs which takes a similar arguments
    • string or callable as sole argument
    • annotation must be imported from another module, not in the immediate
      namespace
    • ensure decorated steps are still able to be located within the class

โ˜€ This issue was created by email; some cleanup may be required.

specify licensing

In theory, since there are no dependencies this can use an un-license.

  • in setup.py
  • in readme
  • in license file
  • whatever github needs to show it

support dynamic class definition ordering

The current implementation sorts methods by class hierarchy and the line number of the file in which they were defined. This works ok, but breaks down when defining methods on a class outside of the class body.

There may or may not be a way to support this kind of behavior. A subclass init hook or other hook which is called when a member is defined could help if it triggers in the right order.

support using default values in place of decorator

#7 defines a decorator annotation that can be used to customize a step. In theory it would be possible to instead use keyword arguments with defaults to decorate the methods.

This decorated step

@step(repeatable=True)
def second_step():
    pass

could instead be written as

def second_step(repeatable=True):
    pass

  • name
  • repeatable
  • skippable
  • critical

unit testing framework

Set up basic unit testing.

  • test framework
  • tests for step ordering
  • tests for step repetition
  • tests for inheritence

prompt improvements

Some feedback based on others' experiences so far.

  • maybe remark about supported input options on the first encounter
  • add ['yeah+', ''yep+', 'nah+', 'affirmative', 'negative'] to options
  • support looser acceptance when starts with an option
  • consider 4 spaces instead of tabs for prompt text (too indented)
  • different interpretations of 'done', so consider altering the prompt text to read 'Did you successfully do the thing?"

support markdown in step descriptions

Support for rendering basic markdown to styled terminal text would be nice. The step descriptions would benefit from being able to leverage markdown, such as for bulleted lists, links, title text, bold and italic text, etc.

pip packaging

setup.py or whatever the kids are using these days

step timeout

new step attribute timeout that limits how long to wait for a step to be completed before giving up or failing

Localization

There aren't many prompts or error texts in the code. It should be possible to implement localized version of these messages without much effort. Consider supporting localization and include Spanish and German as the initial offerings.

requires #14 first for full unicode support

repeated task text correctness

  • seems like every task after resuming is prefixed with 'resuming xxx'
  • text for a repeated task inside of a group of resumed tasks

support for automatic steps

In the path towards automation, the idea of the runbook is to slowly swap out individual steps / methods in the class for automated actions, eventually cutting out the humans entirely. To facilitate this we need the concept of an automated step, one which does not prompt but performs an action and logs it.

Considerations include acceptance criteria, whether the user should confirm the action or just let it happen, repeatability, etc. Also right now the methods are invoked to return dynamic doc strings, so this behavior might be in conflict if the method has to run.

Some possible implementations:

def _deploy_cf_stack():
    pass

def deploy_cf_stack(action='_deploy_cf_stack'):
    """
    Deploys the CloudFormation stack with parameters
    derived from the previous steps.
    """
def deploy_cf_stack(auto=True):
    """
    Deploys the CloudFormation stack with parameters
    derived from the previous steps.
    """
    def action_fn():
        self.helper.deployStack()

    return StepInfo(description="dynamic docs!", action=action_fn)
def deploy_cf_stack(with_action):
    """
    Deploys the CloudFormation stack with parameters
    derived from the previous steps.
    """
    def action_fn():
        self.helper.deployStack()
    
    with_action(action_fn)
    return "dynamic docs"
def deploy_cf_stack(action):
    """
    Deploys the CloudFormation stack with parameters
    derived from the previous steps.
    """
    @action
    def do_deploy():
        self.helper.deployStack()
    
    return "dynamic docs"

colored terminal messages

  • a cprint("some text") method
  • output colors by default
  • command line option to disable colors (#5 seems relevant)

more info in readme

  • add example of import unquietcode.tools.runbook.Runbook
  • step attributes
  • inheritence
  • license info
  • output file format
  • using existing log files
  • module shortcuts (new / show)

support a starts_with model for responses

Support for responses that start with an accepted value but then deviate. Here is an example interaction that went poorly:

Did you do the thing?

    -> yes, and it kind of worked

Invalid response.
```

So when checking responses this could have matched 'yes', and then the rest would be captured and placed in the log file, but still be valid as a yes answer.

better existing step matching logic

do instead something like:

existing_step_names = {}

for step in steps
  if name in existing_names
    skip

this handles the case of reordering better, while preserving additions and subtractions

unicode / emoji support

  • ensure unicode works fine
  • try to get emojii working as well
  • consider sparse use of some unicode symbols in the log file

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.