puppet-repl 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f50d114eed718108fb705d0a03a06afcc2e44a94
4
- data.tar.gz: 37e67568fffa41aeec376da36f2d7b50453965d0
3
+ metadata.gz: 264126ccdd7fafe72a52274c967c28ef18d53679
4
+ data.tar.gz: 31d01dd0eaa97e130ec51d9679424839d48cb73d
5
5
  SHA512:
6
- metadata.gz: c1d732e9ddc6878fbb875b818b67fe3eeaf04cc17492dd2a7eb78f611c619f80c9559434815d03691f44463e6d63c6b53f692a13e21c3b32314d11f9429d143f
7
- data.tar.gz: b7673de9a0f6fc739230f8e4f2881495a93fc56eb346205d347a9354b32dfdcdc766eb5d5200534a80efebd03d332ae65af4f561100cf5f0308a6a5a80d3db2a
6
+ metadata.gz: c12edea95534472ad07ba667ecdebd21a6d5f61eeccf01950b587e9e43eb6ea23563ccd5743731f91aba7f0245bb0898fdbe1580e9cf8b5839518cfe438f5910
7
+ data.tar.gz: 023f8e084288ca93a16366d8d8dfb473adadb0baa53c83f87951d2e99f14ecc3e55f9bd140622cb0b5fd4f531b9dee76482be9830c46745f63a26a0689c53957
data/README.md CHANGED
@@ -8,6 +8,12 @@ Requires Puppet 3.8+ and only uses the future parser.
8
8
  ## Installation
9
9
  `gem install puppet-repl`
10
10
 
11
+ ## Load path
12
+ puppet-repl will load all functions from your basemodulepath and environmentpath.
13
+
14
+ This means if you run `puppet module install puppetlabs-stdlib` and they will be available
15
+ in the repl.
16
+
11
17
  ## Usage
12
18
  Puppet-repl will only parse and evaulate your code. It will not build a catalog
13
19
  and try to enforce the catalog. This has a few side affects.
@@ -34,7 +40,8 @@ Type "exit", "functions", "types", "reset", "help" for more information.
34
40
 
35
41
  ```
36
42
 
37
- Using Variables
43
+ ## Using Variables
44
+
38
45
  ```
39
46
  MacBook-Pro-2/tmp % prepl
40
47
  Puppet Version: 4.2.2
@@ -49,16 +56,13 @@ Type "exit", "functions", "types", "reset", "help" for more information.
49
56
  >>
50
57
  ```
51
58
 
52
- Using functions: functions will run and produce the desired output.
59
+ ## Using functions
60
+ Functions will run and produce the desired output. If you type the word `functions`
61
+ a list of available functions will be displayed on the screen.
53
62
 
54
63
  ```
55
64
  >> split('hello/there/one/two/three','/')
56
- =>
57
- hello
58
- there
59
- one
60
- two
61
- three
65
+ => ["hello", "there", "one", "two", "three"]
62
66
 
63
67
  ```
64
68
  ## Duplicate resource error
@@ -72,6 +76,7 @@ Just like normal puppet code you cannot create duplicate resources.
72
76
  You can reset the parser by running `reset` within the repl without having to exit.
73
77
 
74
78
  ## Troubleshooting
79
+
75
80
  ## Forward
76
81
  I was just playing around and created this simple tool. Its alpha quality,
77
82
  and a ton of features need to be added. Please create a issue if you see a bug or feature that should be added.
@@ -1,42 +1,17 @@
1
1
  require 'puppet'
2
2
  require 'puppet/pops'
3
- require "readline"
4
-
3
+ require 'readline'
4
+ require_relative 'support'
5
5
  module PuppetRepl
6
6
  class Cli
7
+ include PuppetRepl::Support
7
8
 
8
- # returns a future parser for evaluating code
9
- def parser
10
- @parser || ::Puppet::Pops::Parser::EvaluatingParser.new
11
- end
12
-
13
- def module_dirs
14
- Puppet.settings[:basemodulepath].split(':')
15
- end
16
-
17
- # creates a puppet environment given a module path and environment name
18
- def puppet_environment
19
- @puppet_environment ||= Puppet::Node::Environment.create('production', module_dirs)
20
- end
21
-
22
- def scope
23
- unless @scope
24
- begin
25
- Puppet.initialize_settings
26
- rescue
27
- # do nothing otherwise calling init twice raises an error
28
- end
29
- #options['parameters']
30
- #options['facts']
31
- #options[:classes]
32
- node_name = 'node_name'
33
- node = Puppet::Node.new(node_name, :environment => puppet_environment)
34
- compiler = Puppet::Parser::Compiler.new(node)
35
- @scope = Puppet::Parser::Scope.new(compiler)
36
- @scope.source = Puppet::Resource::Type.new(:node, node_name)
37
- @scope.parent = compiler.topscope
9
+ def initialize
10
+ begin
11
+ Puppet.initialize_settings
12
+ rescue
13
+ # do nothing otherwise calling init twice raises an error
38
14
  end
39
- @scope
40
15
  end
41
16
 
42
17
  def puppet_eval(input)
@@ -59,21 +34,25 @@ module PuppetRepl
59
34
  end
60
35
  result
61
36
  end
62
- #scope.environment.known_resource_types
37
+
63
38
  def handle_input(input)
64
39
  case input
65
40
  when 'help'
66
41
  PuppetRepl::Cli.print_repl_desc
67
42
  when 'functions'
68
- puts "list of functions coming soon"
43
+ puts function_map.keys.sort
69
44
  when 'types'
70
45
  puts "list of types coming soon"
71
46
  when '_'
72
47
  puts(" => #{@last_item}")
48
+ when 'environment'
49
+ puts "Puppet Environment: #{puppet_env_name}"
73
50
  when 'exit'
74
51
  exit 0
75
52
  when 'reset'
76
53
  @scope = nil
54
+ when 'current_resources'
55
+ compiler.known_resource_types
77
56
  else
78
57
  result = puppet_eval(input)
79
58
  @last_item = result
@@ -87,6 +66,7 @@ module PuppetRepl
87
66
 
88
67
  def self.print_repl_desc
89
68
  puts(<<-EOT)
69
+ Ruby Version: #{RUBY_VERSION}
90
70
  Puppet Version: #{Puppet.version}
91
71
  Puppet Repl Version: #{PuppetRepl::VERSION}
92
72
  Created by: NWOps <corey@nwops.io>
@@ -0,0 +1,138 @@
1
+ module PuppetRepl
2
+ module Support
3
+
4
+ # returns an array of module directories
5
+ def module_dirs
6
+ dirs = []
7
+ dirs << File.join(Puppet[:environmentpath],puppet_env_name,'modules')
8
+ dirs << Puppet.settings[:basemodulepath].split(':')
9
+ dirs.flatten
10
+ end
11
+
12
+ def puppet_lib_dir
13
+ # returns something like "/Library/Ruby/Gems/2.0.0/gems/puppet-4.2.2/lib/puppet.rb"
14
+ @puppet_lib_dir ||= File.dirname(Puppet.method(:[]).source_location.first)
15
+ end
16
+
17
+ # returns a array of function files
18
+ def function_files
19
+ search_dirs = lib_dirs.map do |lib_dir|
20
+ [File.join(lib_dir, 'puppet', 'functions', '**', '*.rb'),
21
+ File.join(lib_dir, 'functions', '**', '*.rb'),
22
+ File.join(lib_dir, 'puppet', 'parser', 'functions', '*.rb')
23
+ ]
24
+ end
25
+ # add puppet lib directories
26
+ search_dirs << [File.join(puppet_lib_dir, 'puppet', 'functions', '**', '*.rb'),
27
+ File.join(puppet_lib_dir, 'puppet', 'parser', 'functions', '*.rb')
28
+ ]
29
+ Dir.glob(search_dirs.flatten)
30
+ end
31
+
32
+ # returns either the module name or puppet version
33
+ def mod_finder
34
+ @mod_finder ||= Regexp.new('\/([\w\-\.]+)\/lib')
35
+ end
36
+
37
+ # returns a map of functions
38
+ def function_map
39
+ unless @functions
40
+ @functions = {}
41
+ function_files.each do |file|
42
+ obj = {}
43
+ name = File.basename(file, '.rb')
44
+ obj[:name] = name
45
+ obj[:parent] = mod_finder.match(file)[1]
46
+ @functions["#{obj[:parent]}::#{name}"] = obj
47
+ end
48
+ end
49
+ @functions
50
+ end
51
+
52
+ def lib_dirs
53
+ module_dirs.map do |mod_dir|
54
+ Dir["#{mod_dir}/*/lib"].entries
55
+ end.flatten
56
+ end
57
+
58
+ def load_lib_dirs
59
+ lib_dirs.each do |lib|
60
+ $LOAD_PATH << lib
61
+ end
62
+ end
63
+
64
+ # returns a future parser for evaluating code
65
+ def parser
66
+ @parser || ::Puppet::Pops::Parser::EvaluatingParser.new
67
+ end
68
+
69
+ # the cached name of the environment
70
+ def puppet_env_name
71
+ @penv ||= ENV['PUPPET_ENV'] || Puppet[:environment]
72
+ end
73
+
74
+ # creates a puppet environment given a module path and environment name
75
+ # this is cached
76
+ def puppet_environment
77
+ @puppet_environment ||= Puppet::Node::Environment.create(
78
+ puppet_env_name,
79
+ module_dirs,
80
+ manifests_dir
81
+ )
82
+ end
83
+
84
+ # def functions
85
+ # @functions = []
86
+ # @functions << compiler.loaders.static_loader.loaded.keys.find_all {|l| l.type == :function}
87
+ # end
88
+
89
+ def environment_loaders
90
+ name = compiler.loaders.public_environment_loader.loader_name
91
+ end
92
+
93
+ def compiler
94
+ @compiler
95
+ end
96
+
97
+ def create_scope(node_name)
98
+ #options['parameters']
99
+ #options['facts']
100
+ #options[:classes]
101
+ node = create_node(node_name)
102
+ @compiler = create_compiler(node)
103
+ scope = Puppet::Parser::Scope.new(compiler)
104
+ scope.source = Puppet::Resource::Type.new(:node, node_name)
105
+ scope.parent = @compiler.topscope
106
+ load_lib_dirs
107
+ scope
108
+ end
109
+
110
+ def create_compiler(node)
111
+ Puppet::Parser::Compiler.new(node)
112
+ end
113
+
114
+ def create_node(node_name)
115
+ Puppet::Node.new(node_name, :environment => puppet_environment)
116
+ end
117
+
118
+ def scope
119
+ begin
120
+ Puppet.initialize_settings
121
+ rescue
122
+ # do nothing otherwise calling init twice raises an error
123
+ end
124
+ @scope ||= create_scope('node_name')
125
+ end
126
+
127
+ def manifests_dir
128
+ File.join(Puppet[:environmentpath],puppet_env_name,'manifests')
129
+ end
130
+
131
+ def build_node(name, opts = {})
132
+ opts.merge!({:environment => node_environment})
133
+ Puppet::Node.new(name, opts)
134
+ end
135
+
136
+ end
137
+ end
138
+ #scope.environment.known_resource_types
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module PuppetRepl
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/puppet-repl.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "puppet-repl"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Corey Osman"]
12
- s.date = "2016-02-19"
12
+ s.date = "2016-02-25"
13
13
  s.description = "A interactive command line tool for evaluating the puppet language"
14
14
  s.email = "corey@nwops.io"
15
15
  s.executables = ["prepl"]
@@ -29,10 +29,12 @@ Gem::Specification.new do |s|
29
29
  "bin/prepl",
30
30
  "lib/puppet-repl.rb",
31
31
  "lib/puppet-repl/cli.rb",
32
+ "lib/puppet-repl/support.rb",
32
33
  "lib/version.rb",
33
34
  "puppet-repl.gemspec",
34
35
  "spec/puppet-repl_spec.rb",
35
- "spec/spec_helper.rb"
36
+ "spec/spec_helper.rb",
37
+ "spec/support_spec.rb"
36
38
  ]
37
39
  s.homepage = "http://github.com/nwops/puppet-repl"
38
40
  s.licenses = ["MIT"]
@@ -14,7 +14,7 @@ describe "PuppetRepl" do
14
14
  'help'
15
15
  end
16
16
  it 'can show the help screen' do
17
- repl_output = "Puppet Version: 4.3.2\nPuppet Repl Version: 0.0.1\nCreated by: NWOps <corey@nwops.io>\nType \"exit\", \"functions\", \"types\", \"reset\", \"help\" for more information.\n\n"
17
+ repl_output = "Ruby Version: #{RUBY_VERSION}\nPuppet Version: 4.3.2\nPuppet Repl Version: 0.0.1\nCreated by: NWOps <corey@nwops.io>\nType \"exit\", \"functions\", \"types\", \"reset\", \"help\" for more information.\n\n"
18
18
  expect{repl.handle_input(input)}.to output(repl_output).to_stdout
19
19
  end
20
20
  end
@@ -49,6 +49,22 @@ describe "PuppetRepl" do
49
49
  end
50
50
  end
51
51
 
52
+ describe 'reset' do
53
+ before(:each) do
54
+ repl.handle_input(input)
55
+ end
56
+ let(:input) do
57
+ "file{'/tmp/reset': ensure => present}"
58
+ end
59
+
60
+ it 'can process a each block' do
61
+ repl.handle_input('reset')
62
+ repl_output = " => File['/tmp/reset']\n"
63
+ expect{repl.handle_input(input)}.to output(repl_output).to_stdout
64
+ end
65
+ end
66
+
67
+
52
68
  describe 'each block' do
53
69
  let(:input) do
54
70
  "['/tmp/test3', '/tmp/test4'].each |String $path| { file{$path: ensure => present} }"
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe 'support' do
5
+
6
+ let(:repl) do
7
+ PuppetRepl::Cli.new
8
+ end
9
+
10
+ let(:scope) do
11
+ repl.scope
12
+ end
13
+
14
+ let(:puppet_version) do
15
+ repl.mod_finder.match(repl.puppet_lib_dir)[1]
16
+ end
17
+
18
+ context '#function_map' do
19
+
20
+ it 'should list functions' do
21
+ func = repl.function_map["#{puppet_version}::hiera"]
22
+ expect(repl.function_map).to be_instance_of(Hash)
23
+ expect(func).to eq({:name => 'hiera', :parent => puppet_version})
24
+ end
25
+
26
+ end
27
+
28
+ it 'should return a puppet version' do
29
+ expect(puppet_version).to match(/puppet-\d\.\d.\d/)
30
+ end
31
+
32
+ it 'should return lib dirs' do
33
+ expect(repl.lib_dirs.count).to be >= 1
34
+
35
+
36
+ end
37
+
38
+ it 'should return module dirs' do
39
+ expect(repl.module_dirs.count).to be >= 1
40
+
41
+ end
42
+
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-repl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Osman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-19 00:00:00.000000000 Z
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet
@@ -142,10 +142,12 @@ files:
142
142
  - bin/prepl
143
143
  - lib/puppet-repl.rb
144
144
  - lib/puppet-repl/cli.rb
145
+ - lib/puppet-repl/support.rb
145
146
  - lib/version.rb
146
147
  - puppet-repl.gemspec
147
148
  - spec/puppet-repl_spec.rb
148
149
  - spec/spec_helper.rb
150
+ - spec/support_spec.rb
149
151
  homepage: http://github.com/nwops/puppet-repl
150
152
  licenses:
151
153
  - MIT