ridley-exec 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/ridley-exec ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # vim: set filetype=ruby:
4
+
5
+ require 'ridley-exec'
6
+
7
+ exit RidleyExec::CLI.run(ARGV)
@@ -0,0 +1,8 @@
1
+ module RidleyExec
2
+ module CLI
3
+ def self.run args
4
+ api, target, params = Configuration.resolve_args(args)
5
+ Runner.run_target(api, target, params)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,142 @@
1
+ require 'ridley'
2
+ require 'optparse'
3
+ require 'ostruct'
4
+
5
+ module RidleyExec
6
+ module Configuration
7
+ def self.env_default(name, default, opt={})
8
+ val = ENV[name.to_s]
9
+ return default if val.nil? || (val.size == 0 && opt[:no_blank])
10
+ if block_given?
11
+ val = yield val
12
+ end
13
+
14
+ val
15
+ end
16
+
17
+
18
+ def self.parse_args(args)
19
+ options = {}
20
+ opt_parser = OptionParser.new do |opts|
21
+ opts.banner = "Usage: ridley-exec [options]"
22
+
23
+ opts.separator ""
24
+ opts.separator "Specific options:"
25
+
26
+ options[:server_url] = env_default(:CHEF_SERVER_URL, nil, :no_blank => true)
27
+ opts.on('-u [URL]', '--server-url [URL]', String, "The URL of the chef server to query") do |url|
28
+ options[:server_url] = url
29
+ end
30
+
31
+
32
+ options[:client_name] = env_default(:CHEF_CLIENT_NAME, nil, :no_blank => true)
33
+ opts.on('-n [CLIENT_NAME]', '--client-name [CLIENT_NAME]', String, "The name of the client to use for chef API operations.") do |client|
34
+ options[:client_name] = client
35
+ end
36
+
37
+
38
+ options[:client_key] = env_default(:CHEF_CLIENT_KEY, nil, :no_blank => true)
39
+ opts.on('-k [CLIENT_KEY]', '--client-key [CLIENT_KEY]', String, "The path to the client key file used for chef API operations.") do |key|
40
+ options[:client_key] = key
41
+ end
42
+
43
+ options[:encrypted_data_bag_secret] = env_default(:CHEF_ENCRYPTED_DATA_BAG_SECRET, nil, :no_blank => true)
44
+ opts.on('-s [SECRET]', '--encrypted-data-bag-secret [SECRET]', String, "The secret string for encrypting data bags.") do |secret|
45
+ options[:encrypted_data_bag_secret] = secret
46
+ end
47
+
48
+ options[:knife_path] = env_default(:CHEF_KNIFE_PATH, nil)
49
+ puts "Knife path nil? #{options[:knife_path].nil?}"
50
+ opts.on('-p [KNIFE_PATH]', '--knife-path [KNIFE_PATH]', String, "Path to the knife.rb file to use for config (empty string does search)") do |path|
51
+ path = '' if path.nil?
52
+ options[:knife_path] = path
53
+ end
54
+
55
+ opts.on('-e [SCRIPT_STRING]', String, 'Execute the ruby in SCRIPT_STRING') do |script|
56
+ options[:target] = prepare_script_string(script)
57
+ end
58
+
59
+ opts.on('-I', 'Execute the ruby from STDIN.') do
60
+ options[:target] = prepare_stdin_script
61
+ end
62
+
63
+ opts.on('--pry', 'Run pry REPL with api in context') do
64
+ options[:target] = prepare_pry_session
65
+ end
66
+
67
+ opts.separator ""
68
+ opts.separator "Common options:"
69
+ opts.on_tail("-h", "--help", "Show this help message.") do
70
+ puts opts
71
+ exit 0
72
+ end
73
+
74
+ opts.on_tail("-v", "--version", "Print the version.") do
75
+ puts RidleyExec::VERSION
76
+ exit 0
77
+ end
78
+ end
79
+
80
+ opt_parser.parse!(args)
81
+ [options, args]
82
+ end
83
+
84
+ def self.api_from_knife(path)
85
+ args = []
86
+ args << path unless path.nil?
87
+ Ridley.from_chef_config(*args)
88
+ end
89
+
90
+ def self.api_from_options(options)
91
+ Ridley.new(options)
92
+ end
93
+
94
+
95
+ def self.prepare_script(path)
96
+ Proc.new do |scope|
97
+ eval File.read(path), scope, path
98
+ end
99
+ end
100
+
101
+ def self.prepare_stdin_script
102
+ Proc.new do |scope|
103
+ eval STDIN.read, scope, '-stdin-'
104
+ end
105
+ end
106
+
107
+ def self.prepare_script_string(script)
108
+ Proc.new do |scope|
109
+ eval script, scope, '-'
110
+ end
111
+ end
112
+
113
+ def self.prepare_pry_session
114
+ Proc.new do |scope|
115
+ require "pry"
116
+ scope.pry
117
+ end
118
+ end
119
+
120
+ def self.resolve_args(args)
121
+ options, remaining = parse_args(args)
122
+ options = options.inject({}) do |a, kv|
123
+ a[kv[0]] = kv[1] unless kv[1].nil?
124
+ a
125
+ end
126
+
127
+ if options.has_key? :target
128
+ target = options.delete(:target)
129
+ else
130
+ target = prepare_script(remaining.shift)
131
+ end
132
+
133
+ if options.has_key? :knife_path
134
+ api = api_from_knife(options[:knife_path])
135
+ else
136
+ api = api_from_options(options)
137
+ end
138
+
139
+ [api, target, remaining]
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,22 @@
1
+ module RidleyExec
2
+ module Runner
3
+
4
+ class Context
5
+ attr_reader :api, :parameters
6
+
7
+ def initialize(api, args)
8
+ @api = api
9
+ @parameters = args
10
+ end
11
+
12
+ def run(&target)
13
+ instance_eval { target.call(binding) }
14
+ end
15
+ end
16
+
17
+ def self.run_target(api, target, args)
18
+ Context.new(api, args).run(&target)
19
+ 0
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module RidleyExec
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ module RidleyExec
2
+ end
3
+
4
+ require "ridley-exec/version"
5
+ require "ridley-exec/cli"
6
+ require "ridley-exec/configuration"
7
+ require "ridley-exec/runner"
@@ -0,0 +1,21 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'ridley-exec/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'ridley-exec'
6
+ s.version = RidleyExec::VERSION
7
+ s.authors = ['Ethan Rowe']
8
+ s.email = ['ethanrowe000@gmail.com']
9
+ s.homepage = 'https://github.com/ethanrowe/ridley-exec'
10
+ s.summary = 'Basic utility for executing ruby scripts with ridley chef API in scope'
11
+ s.description = s.summary
12
+
13
+ s.add_dependency 'ridley', '~> 4.0'
14
+ s.add_dependency 'pry', '~> 0.10'
15
+
16
+ s.files = `git ls-files`.split("\n").reject {|n| ["Gemfile", "Gemfile.lock"].include? n}
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+
19
+ s.require_paths = ['lib']
20
+ end
21
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ridley-exec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ethan Rowe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-10-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ridley
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '4.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: pry
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.10'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.10'
46
+ description: Basic utility for executing ruby scripts with ridley chef API in scope
47
+ email:
48
+ - ethanrowe000@gmail.com
49
+ executables:
50
+ - ridley-exec
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - bin/ridley-exec
55
+ - lib/ridley-exec.rb
56
+ - lib/ridley-exec/cli.rb
57
+ - lib/ridley-exec/configuration.rb
58
+ - lib/ridley-exec/runner.rb
59
+ - lib/ridley-exec/version.rb
60
+ - ridley-exec.gemspec
61
+ homepage: https://github.com/ethanrowe/ridley-exec
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.23
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Basic utility for executing ruby scripts with ridley chef API in scope
85
+ test_files: []