-
Notifications
You must be signed in to change notification settings - Fork 104
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 new "pdk console" command #758
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
module PDK::CLI | ||
@console_cmd = @base_cmd.define_command do | ||
name 'console' | ||
usage _('console [console_options]') | ||
summary _('(Experimental) Start a session of the puppet debugger console.') | ||
default_subcommand 'help' | ||
description _(<<-EOF | ||
The pdk console runs a interactive session of the puppet debugger tool to test out snippets of code, run | ||
language evaluations, datatype prototyping and much more. A virtual playground for your puppet code! | ||
For usage details see the puppet debugger docs at https://docs.puppet-debugger.com. | ||
|
||
EOF | ||
) | ||
PDK::CLI.puppet_version_options(self) | ||
PDK::CLI.puppet_dev_option(self) | ||
# we have to skip option parsing because it is expected the user | ||
# will be passing additional args that are passed to the debugger | ||
skip_option_parsing | ||
|
||
# TODO: using -h or --help skips the pdk help and passes to puppet debugger help | ||
run do |_opts, args, _cmd| | ||
require 'pdk/cli/util' | ||
require 'pdk/util' | ||
|
||
PDK::CLI::Util.ensure_in_module!( | ||
message: _('Console can only be run from inside a valid module directory'), | ||
log_level: :fatal, | ||
) | ||
processed_options, processed_args = process_opts(args) | ||
|
||
PDK::CLI::Util.validate_puppet_version_opts(processed_options) | ||
|
||
PDK::CLI::Util.analytics_screen_view('console', args) | ||
|
||
# TODO: figure out if we need to remove default configs set by puppet | ||
logicminds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# so it is scoped for the module only | ||
# "--environmentpath"... | ||
flags = if PDK::Util.in_module_root? | ||
["--basemodulepath=#{base_module_path}", | ||
"--modulepath=#{base_module_path}"] | ||
else | ||
[] | ||
end | ||
debugger_args = ['debugger'] + processed_args + flags | ||
result = run_in_module(processed_options, debugger_args) | ||
|
||
exit result[:exit_code] | ||
end | ||
|
||
# Logs a fatal message about the gem missing and how to add it | ||
def inform_user_for_missing_gem(gem_name = 'puppet-debugger', version = '~> 0.14') | ||
PDK.logger.fatal(<<-EOF | ||
Your Gemfile is missing the #{gem_name} gem. You can add the missing gem | ||
by updating your #{File.join(PDK::Util.module_root, '.sync.yml')} file with the following | ||
and running pdk update. | ||
|
||
Gemfile: | ||
required: | ||
":development": | ||
- gem: #{gem_name} | ||
version: \"#{version}\" | ||
|
||
EOF | ||
) | ||
end | ||
|
||
# @return [Boolean] - true if the gem was found in the lockfile | ||
# @param [String] - name of ruby gem to check in bundle lockfile | ||
def gem_in_bundle_lockfile?(gem_name) | ||
require 'bundler' | ||
require 'pdk/util/bundler' | ||
|
||
lock_file_path = PDK::Util::Bundler::BundleHelper.new.gemfile_lock | ||
PDK.logger.debug("Checking lockfile #{lock_file_path} for #{gem_name}") | ||
lock_file = ::Bundler::LockfileParser.new(::Bundler.read_file(lock_file_path)) | ||
!lock_file.specs.find { |spec| spec.name.eql?(gem_name) }.nil? | ||
rescue ::Bundler::GemfileNotFound => e | ||
PDK.logger.debug _(e.message) | ||
false | ||
end | ||
|
||
def check_fixtures_dir | ||
existing_path = base_module_path.split(':').find do |path| | ||
Dir.exist?(path) && Dir.entries(path).length > 2 | ||
end | ||
PDK.logger.warn _('Module fixtures not found, please run pdk bundle exec rake spec_prep.') unless existing_path | ||
end | ||
|
||
# @return [Array] - array of split options [{:"puppet-version"=>"6.9.0"}, ['--loglevel=debug']] | ||
# options are for the pdk and debugger pass through | ||
def process_opts(opts) | ||
args = opts.map do |e| | ||
if e =~ %r{\A-{2}puppet|pe\-version|dev} | ||
value = e.split('=') | ||
(value.count < 2) ? value + [''] : value | ||
end | ||
end | ||
args = args.compact.to_h | ||
# symbolize keys | ||
args = args.inject({}) do |memo, (k, v)| # rubocop:disable Style/EachWithObject | ||
memo[k.sub('--', '').to_sym] = v | ||
memo | ||
end | ||
# pass through all other args that are bound for puppet debugger | ||
processed_args = opts.map { |e| e unless e =~ %r{\A-{2}puppet|pe\-version|dev} }.compact | ||
[args, processed_args] | ||
end | ||
|
||
# @param opts [Hash] - the options passed into the CRI command | ||
# @param bundle_args [Array] array of bundle exec args and puppet debugger args | ||
# @return [Hash] - a command result hash | ||
def run_in_module(opts, bundle_args) | ||
require 'pdk/cli/exec' | ||
require 'pdk/cli/exec/interactive_command' | ||
require 'pdk/util/ruby_version' | ||
require 'pdk/util/bundler' | ||
|
||
check_fixtures_dir | ||
output = opts[:debug].nil? | ||
puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts, output) | ||
gemfile_env = PDK::Util::Bundler::BundleHelper.gemfile_env(puppet_env[:gemset]) | ||
PDK::Util::RubyVersion.use(puppet_env[:ruby_version]) | ||
PDK::Util::RubyVersion.instance(puppet_env[:ruby_version]) | ||
PDK::Util::Bundler.ensure_bundle!(puppet_env[:gemset]) | ||
unless gem_in_bundle_lockfile?('puppet-debugger') | ||
inform_user_for_missing_gem | ||
return { exit_code: 1 } | ||
end | ||
|
||
debugger_args = %w[exec puppet] + bundle_args | ||
command = PDK::CLI::Exec::InteractiveCommand.new(PDK::CLI::Exec.bundle_bin, *debugger_args).tap do |c| | ||
c.context = :pwd | ||
c.update_environment(gemfile_env) | ||
end | ||
command.execute! | ||
end | ||
|
||
# @return [String] - the basemodulepath of the fixtures and modules from the current module | ||
# also includes ./modules in case librarian puppet is used | ||
def base_module_path | ||
base_module_path = File.join(PDK::Util.module_fixtures_dir, 'modules') | ||
"#{base_module_path}:#{File.join(PDK::Util.module_root, 'modules')}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
source ENV['GEM_SOURCE'] || 'https://rubygems.org' | ||
|
||
def location_for(place_or_version, fake_version = nil) | ||
git_url_regex = %r{\A(?<url>(https?|git)[:@][^#]*)(#(?<branch>.*))?} | ||
file_url_regex = %r{\Afile:\/\/(?<path>.*)} | ||
|
||
if place_or_version && (git_url = place_or_version.match(git_url_regex)) | ||
[fake_version, { git: git_url[:url], branch: git_url[:branch], require: false }].compact | ||
elsif place_or_version && (file_url = place_or_version.match(file_url_regex)) | ||
['>= 0', { path: File.expand_path(file_url[:path]), require: false }] | ||
else | ||
[place_or_version, { require: false }] | ||
end | ||
end | ||
|
||
ruby_version_segments = Gem::Version.new(RUBY_VERSION.dup).segments | ||
minor_version = ruby_version_segments[0..1].join('.') | ||
|
||
group :development do | ||
gem "fast_gettext", '1.1.0', require: false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.1.0') | ||
gem "fast_gettext", require: false if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1.0') | ||
gem "json_pure", '<= 2.0.1', require: false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0') | ||
gem "json", '= 1.8.1', require: false if Gem::Version.new(RUBY_VERSION.dup) == Gem::Version.new('2.1.9') | ||
gem "json", '= 2.0.4', require: false if Gem::Requirement.create('~> 2.4.2').satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) | ||
gem "json", '= 2.1.0', require: false if Gem::Requirement.create(['>= 2.5.0', '< 2.7.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) | ||
gem "rb-readline", '= 0.5.5', require: false, platforms: [:mswin, :mingw, :x64_mingw] | ||
gem "puppet-module-posix-default-r#{minor_version}", '~> 0.3', require: false, platforms: [:ruby] | ||
gem "puppet-module-posix-dev-r#{minor_version}", '~> 0.3', require: false, platforms: [:ruby] | ||
gem "puppet-module-win-default-r#{minor_version}", '~> 0.3', require: false, platforms: [:mswin, :mingw, :x64_mingw] | ||
gem "puppet-module-win-dev-r#{minor_version}", '~> 0.3', require: false, platforms: [:mswin, :mingw, :x64_mingw] | ||
gem "puppet-debugger", '~> 0.14.0', require: false | ||
gem "puppet-debugger-playbooks", require: false | ||
end | ||
|
||
puppet_version = ENV['PUPPET_GEM_VERSION'] | ||
facter_version = ENV['FACTER_GEM_VERSION'] | ||
hiera_version = ENV['HIERA_GEM_VERSION'] | ||
|
||
gems = {} | ||
|
||
gems['puppet'] = location_for(puppet_version) | ||
|
||
# If facter or hiera versions have been specified via the environment | ||
# variables | ||
|
||
gems['facter'] = location_for(facter_version) if facter_version | ||
gems['hiera'] = location_for(hiera_version) if hiera_version | ||
|
||
if Gem.win_platform? && puppet_version =~ %r{^(file:///|git://)} | ||
# If we're using a Puppet gem on Windows which handles its own win32-xxx gem | ||
# dependencies (>= 3.5.0), set the maximum versions (see PUP-6445). | ||
gems['win32-dir'] = ['<= 0.4.9', require: false] | ||
gems['win32-eventlog'] = ['<= 0.6.5', require: false] | ||
gems['win32-process'] = ['<= 0.7.5', require: false] | ||
gems['win32-security'] = ['<= 0.2.5', require: false] | ||
gems['win32-service'] = ['0.8.8', require: false] | ||
end | ||
|
||
gems.each do |gem_name, gem_params| | ||
gem gem_name, *gem_params | ||
end | ||
|
||
# Evaluate Gemfile.local and ~/.gemfile if they exist | ||
extra_gemfiles = [ | ||
"#{__FILE__}.local", | ||
File.join(Dir.home, '.gemfile'), | ||
] | ||
|
||
extra_gemfiles.each do |gemfile| | ||
if File.file?(gemfile) && File.readable?(gemfile) | ||
eval(File.read(gemfile), binding) | ||
end | ||
end | ||
# vim: syntax=ruby |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbondpdx Can you give these docs a quick look.
To use just execute
avoidjust
andeasy
~/modules/ntp $ ...
Avoid shell specific things e.g. a PowerShell user would see this prefixed differently.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the README with the fixes and also added some extra bits I thought were important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I didn't see this sooner, @glennsarti , @logicminds ... I think it got lost in a mass of GitHub emails. Thank you for both for these docs!