braintree / runbook

A framework for gradual system automation
MIT License
734 stars 43 forks source link

"Unable to retrieve source code" when using Runbook::Viewer #19

Closed fwolfst closed 4 years ago

fwolfst commented 4 years ago

I want to create and save markdown-rendered versions of my runbook. The runbook is defined in it/add_ldap_user.rb. Running runbook view it/it/add_ldap_user.rb works fine, but if I use md = Runbook::Viewer.new(eval(File.read book)).generate(view: :markdown) (where book is 'it/add_ldap_user.rb`) the markdown output contains "Unable to retrieve source code" instead of the relevant Ruby Code.

The project can be found here (last file in the commit is the relevant source) https://github.com/ecovillage/operations/commit/6c2dd147ca7d51fa3773d9e8d55bf62b6e1337c3 .

Now, I could easily fire up the runbook view commands, but I thought there might be a bug hidden here. Alternatively it could have something to do with the load path and stuff. ./require 'it/add_ldap_user' in the file worked but didnt help.

pblesi commented 4 years ago

This is a known issue with using eval to load the runbook code (it is a shortcoming of https://github.com/banister/method_source). See https://github.com/braintree/runbook#load-vs-eval for more details. Essentially, you need to do the following in order to have the ruby source show up in views:

load book
md = Runbook::Viewer.new(Runbook.books.last).generate(view: :markdown)
fwolfst commented 4 years ago

Damn, sorry for not rtfm.

fwolfst commented 4 years ago

Ah, but I remember why I didnt load and then loop over the runbooks via Runbook.books.each {...}: I want to know the name of the original runbooks definition file name, to create a .md file with the same name (more or less). Is there a way to access the "source" location from Runbook.books.last? Inspecting the object (p Runbook.last.inspect), I see the source location in the blocks but do not know how to programmatically access it.

fwolfst commented 4 years ago

I guess https://github.com/ecovillage/operations/commit/2313250c78f68d690ee7b9b1e8ca61526314b1c3#diff-ccbed9de3d03765bf1528a07c80646ee is a workaround for that. Loop over the books file locations, load them and inspect the respectively last Runbook. Thanks again.