Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module lib/ to LOAD_PATH #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

nmaludy
Copy link

@nmaludy nmaludy commented Oct 25, 2019

Currently, when writing ruby tasks, there can be a bit of an issue with require statements for things in a module's lib directory.

Normally these files do a require puppet/whatever. Puppet makes this possible by adding the lib directory to Ruby's LOAD_PATH when it compiles its catalogs.

In Bolt however the LOAD_PATH isn't modified in the same way and it causes a bit of grief if you try to require something in lib. This is especially evident when that class/module in lib also does a require for something else in lib.

To fix this problem i noticed that the cisco_ios module solves this by modifying the LOAD_PATH early on in the Task's lifetime:

https://github.com/puppetlabs/cisco_ios/blob/master/lib/puppet/util/task_helper.rb#L34-L39

I've taken this idea and expanded upon it by allowing it to be used in two different ways in the Ruby TaskHelper:

Option 1 - Global access using PT__installdir

At the top of a Ruby task file you could do something like so:

#!/usr/bin/env ruby
require_relative '../../ruby_task_helper/files/task_helper.rb'
# adds all <module>/lib path's into LOAD_PATH by inspecting PT__installdir
TaskHelper.add_module_lib_paths

require 'puppet_x/owner/module/myclass'

Option 2 - Using _installdir from stdin before task() is called

Alternatively, if that environment variable doesn't exist, we can allow the user to do the require inside of the task() method in their ruby code.

We can accomplish this by reading in the parameters from stdin, parsing out _installdir and then modifying LOAD_PATH before task() is called.

#!/usr/bin/env ruby
require_relative '../../ruby_task_helper/files/task_helper.rb'

# Bolt task for enabling/disabling monitoring alerts in SolarWinds
class MonitoringSolarwindsTask < TaskHelper

  def task(nodes: nil,
           action: nil,
           **kwargs) 
    require 'puppet_x/encore/patching/orion_client'
    # do something with the required class
  end
end

MonitoringSolarwindsTask.run if __FILE__ == $0


@nmaludy nmaludy requested a review from a team as a code owner October 25, 2019 16:49
@nmaludy nmaludy changed the title Add module' Add module lib/ to LOAD_PATH Oct 25, 2019
Copy link
Contributor

@adreyer adreyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this module should rely entirely on STDIN rather than using the environment variables.
I'm not 100% sure whether we should magically add lib to the load path or force the user to opt into that behavior.

I think ideally we should be parsing stdin and saving it to a class variable with a new class method params perhaps. That raises some complexity around handling though which may blow up the scope of ticket too much.

  • new class variables params and error
  • new class method params which returns the params class variable if set otherwise it reads and parses params from stdin. If something goes wrong it rescues the exception and saves it in error
  • All class methods(except run) check if error is set and immediately exit if so.
  • run raises the error if set.
  • new reset class method to clear out all the class variables when testing.

@nmaludy
Copy link
Author

nmaludy commented Oct 25, 2019

@adreyer i was able to get the params idea working so it's always going to try and read from stdin and not rely on ENV.

I'll work on the "capturing errors" part next.

@@ -1,6 +1,8 @@
require 'json'

class TaskHelper
@@params = nil # rubocop:disable Style/ClassVars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe to just add this to .rubocop.yml. I trust codereview to catch misuse of class variables.

@nicklewis nicklewis requested a review from a team September 16, 2020 21:10
Base automatically changed from master to main January 21, 2021 17:55
@CLAassistant
Copy link

CLAassistant commented Sep 1, 2021

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants