saltr 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/help.rb +66 -0
- data/lib/salt.rb +10 -0
- data/lib/saltr.rb +205 -0
- data/lib/saltr/version.rb +3 -0
- data/saltr.gemspec +25 -0
- metadata +126 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Saltr
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/saltr`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'saltr'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install saltr
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/saltr.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "saltr"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/help.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module Saltr
|
2
|
+
class Help
|
3
|
+
def self.topics
|
4
|
+
self.instance_methods(false).collect{ |meth| meth.to_s }.join(', ')
|
5
|
+
end
|
6
|
+
|
7
|
+
def top
|
8
|
+
"""
|
9
|
+
help topics: #{self.class.topics}
|
10
|
+
|
11
|
+
l # sys.list-modules
|
12
|
+
l <modulename> # sys.list-functions modulename
|
13
|
+
l <functionname> # sys.doc functionname
|
14
|
+
|
15
|
+
r <cmd.run string>
|
16
|
+
m <new-minion-target>
|
17
|
+
m * and G@os:Ubu* will make a compound minion query
|
18
|
+
G=grains, P=grains regexp, L=list of minions
|
19
|
+
o <new out string>
|
20
|
+
|
21
|
+
salt-cloud
|
22
|
+
salt-key
|
23
|
+
|
24
|
+
grains.item ip4_interfaces
|
25
|
+
"""
|
26
|
+
end
|
27
|
+
|
28
|
+
def ufw
|
29
|
+
"""
|
30
|
+
ufw status
|
31
|
+
ufw allow ssh
|
32
|
+
ufw allow salt # on master
|
33
|
+
ufw allow from 52.6.10.1 to any port 22 # put master's ip address
|
34
|
+
ufw enable
|
35
|
+
|
36
|
+
ufw delete allow from 52.6.10.1 to any port 22
|
37
|
+
ufw deny apache
|
38
|
+
|
39
|
+
ufw app list # finds from /etc/ufw/applications.d/
|
40
|
+
|
41
|
+
log file for intrusion attempts: /var/log/kern.log
|
42
|
+
"""
|
43
|
+
end
|
44
|
+
|
45
|
+
def o
|
46
|
+
"""
|
47
|
+
compact Display compact output data structure
|
48
|
+
highstate Outputter for displaying results of state runs
|
49
|
+
json_out Display return data in JSON format
|
50
|
+
key Display salt-key output
|
51
|
+
nested Recursively display nested data
|
52
|
+
newline_values_only Display values only, separated by newlines
|
53
|
+
no_out Display no output
|
54
|
+
no_return Display output for minions that did not return
|
55
|
+
overstatestage Display clean output of an overstate stage
|
56
|
+
pprint_out Python pretty-print (pprint)
|
57
|
+
progress Display return data as a progress bar
|
58
|
+
raw Display raw output data structure
|
59
|
+
txt Simple text outputter
|
60
|
+
virt_query virt.query outputter
|
61
|
+
yaml_out Display return data in YAML format
|
62
|
+
"""
|
63
|
+
end
|
64
|
+
|
65
|
+
end # Help
|
66
|
+
end
|
data/lib/salt.rb
ADDED
data/lib/saltr.rb
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require 'json'
|
3
|
+
require 'yaml'
|
4
|
+
require 'pp'
|
5
|
+
require 'readline'
|
6
|
+
require 'pathname'
|
7
|
+
|
8
|
+
require_relative 'saltr/version'
|
9
|
+
require_relative 'help'
|
10
|
+
|
11
|
+
module Saltr
|
12
|
+
class Repl
|
13
|
+
attr_accessor :minions, :last_result, :out, :static, :main_minion
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@minions = '*'
|
17
|
+
@last_result = nil
|
18
|
+
@out = :json
|
19
|
+
@static = true
|
20
|
+
@main_minion = 'gru'
|
21
|
+
initialize_readline
|
22
|
+
end
|
23
|
+
|
24
|
+
def repl
|
25
|
+
stty_save = `stty -g`.chomp
|
26
|
+
|
27
|
+
while (1) do
|
28
|
+
print "\n#{'-'*50}\n"
|
29
|
+
prompt = "salt '#{@minions}' --out=#{out}: "
|
30
|
+
# cmd = Readline.readline(prompt, true)
|
31
|
+
cmd = readline_with_hist_management(prompt)
|
32
|
+
break if cmd.nil?
|
33
|
+
result = run(cmd)
|
34
|
+
print_result(result)
|
35
|
+
end
|
36
|
+
rescue Interrupt
|
37
|
+
$stderr.puts "\n\nCtrl-c pressed."
|
38
|
+
ensure
|
39
|
+
store_history
|
40
|
+
system('stty', stty_save)
|
41
|
+
end
|
42
|
+
|
43
|
+
def run_yaml(cmd, debug=false)
|
44
|
+
YAML.load(run(cmd, debug))
|
45
|
+
end
|
46
|
+
|
47
|
+
def run(cmd, debug=false)
|
48
|
+
parsed_cmd = parse(cmd)
|
49
|
+
puts parsed_cmd
|
50
|
+
result = if parsed_cmd.class==Array and parsed_cmd.first==:result
|
51
|
+
parsed_cmd[1]
|
52
|
+
else
|
53
|
+
parsed_cmd = "#{parsed_cmd} --out=#{@out}" unless parsed_cmd.match(/--out/)
|
54
|
+
# parsed_cmd += " --static" if static
|
55
|
+
`#{parsed_cmd}`
|
56
|
+
end
|
57
|
+
puts "result:#{result}:" if debug
|
58
|
+
result
|
59
|
+
end
|
60
|
+
|
61
|
+
def parse(cmd)
|
62
|
+
tokens = cmd.split(/[= ]/, 2)
|
63
|
+
case tokens[0]
|
64
|
+
when 'help'
|
65
|
+
[:result, help(tokens[1])]
|
66
|
+
when 'r'
|
67
|
+
parse_cmd_run(tokens[1])
|
68
|
+
when 'm'
|
69
|
+
[:result, set_minion(tokens[1])]
|
70
|
+
when 'o'
|
71
|
+
[:result, set_out(tokens[1])]
|
72
|
+
when 'l'
|
73
|
+
parse_list(tokens[1])
|
74
|
+
when /^salt-key/,/^salt-cloud/
|
75
|
+
cmd
|
76
|
+
when 'quit'
|
77
|
+
exit(0)
|
78
|
+
else
|
79
|
+
parse_salt(cmd)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def print_result(result)
|
84
|
+
puts result unless result.nil?
|
85
|
+
#YAML.load(result)
|
86
|
+
end
|
87
|
+
|
88
|
+
def parse_salt(cmd, minions=minion_str, out_type=out)
|
89
|
+
"salt #{minions} --out=#{out_type} #{cmd}"
|
90
|
+
end
|
91
|
+
|
92
|
+
def parse_cmd_run(cmd)
|
93
|
+
"salt #{minion_str} cmd.run_all --static '#{cmd}'"
|
94
|
+
end
|
95
|
+
|
96
|
+
def parse_list(cmd)
|
97
|
+
puts cmd.inspect
|
98
|
+
type = 'sys.list_modules'
|
99
|
+
rest = ''
|
100
|
+
out_type = 'raw'
|
101
|
+
unless cmd.nil?
|
102
|
+
args = cmd.split(/\s+/, 2)
|
103
|
+
case args.first
|
104
|
+
when 'functions'
|
105
|
+
type = 'sys.list_functions'
|
106
|
+
rest = args[1]
|
107
|
+
when 'doc'
|
108
|
+
type = 'sys.list_functions'
|
109
|
+
rest = args[1]
|
110
|
+
out_type = 'txt'
|
111
|
+
else
|
112
|
+
if args[0].match(/\./)
|
113
|
+
type = 'sys.doc'
|
114
|
+
out_type = 'txt'
|
115
|
+
else
|
116
|
+
type = 'sys.list_functions'
|
117
|
+
end
|
118
|
+
rest = args[0]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
cmd = type
|
122
|
+
cmd = "#{cmd} #{rest}"
|
123
|
+
parse_salt(cmd, main_minion, out_type)
|
124
|
+
end
|
125
|
+
|
126
|
+
def set_minion(cmd)
|
127
|
+
@minions = cmd
|
128
|
+
cmd
|
129
|
+
end
|
130
|
+
|
131
|
+
def minion_str
|
132
|
+
if minions.match(/ and /)
|
133
|
+
"-C '#{minions}'"
|
134
|
+
else
|
135
|
+
"'#{minions}'"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def set_out(cmd)
|
140
|
+
@out = cmd
|
141
|
+
cmd
|
142
|
+
end
|
143
|
+
|
144
|
+
def help(cmd)
|
145
|
+
cmd = 'top' if cmd=='' or cmd.nil?
|
146
|
+
msg = "Help topic '#{cmd}' doesn't exist."
|
147
|
+
begin
|
148
|
+
msg = Help.new.send(cmd.to_sym)
|
149
|
+
rescue
|
150
|
+
end
|
151
|
+
"HELP #{cmd}\n\n#{msg}"
|
152
|
+
end
|
153
|
+
|
154
|
+
def readline_with_hist_management(prompt='> ')
|
155
|
+
line = Readline.readline(prompt, true)
|
156
|
+
return nil if line.nil?
|
157
|
+
if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
|
158
|
+
Readline::HISTORY.pop
|
159
|
+
end
|
160
|
+
# TODO: limit the history size
|
161
|
+
line
|
162
|
+
end
|
163
|
+
|
164
|
+
def initialize_readline
|
165
|
+
load_history
|
166
|
+
Readline.completion_append_character = ' '
|
167
|
+
Readline.completion_proc = readline_proc
|
168
|
+
end
|
169
|
+
|
170
|
+
def readline_proc
|
171
|
+
cmd_list = [
|
172
|
+
'cmd.run', 'cmd.run_all',
|
173
|
+
#'sys.list_modules', 'sys.list_functions', 'sys.doc',
|
174
|
+
'l', 'l modules', 'l functions', 'l doc',
|
175
|
+
'rbenv', 'rbenv.install'
|
176
|
+
]
|
177
|
+
proc { |s| cmd_list.grep(/^#{Regexp.escape(s)}/) }
|
178
|
+
end
|
179
|
+
|
180
|
+
def load_history
|
181
|
+
history_file.open('r').readlines.each do |line|
|
182
|
+
Readline::HISTORY.push line.chomp
|
183
|
+
end
|
184
|
+
rescue
|
185
|
+
end
|
186
|
+
|
187
|
+
def store_history
|
188
|
+
history_file.open('w') do |out|
|
189
|
+
Readline::HISTORY.each do |line|
|
190
|
+
out.puts line
|
191
|
+
end
|
192
|
+
end
|
193
|
+
rescue
|
194
|
+
end
|
195
|
+
|
196
|
+
def history_file
|
197
|
+
Pathname(__FILE__).dirname + '.history'
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
|
203
|
+
if __FILE__ == $0
|
204
|
+
Saltr::Repl.new.repl
|
205
|
+
end
|
data/saltr.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'saltr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "saltr"
|
8
|
+
spec.version = Saltr::VERSION
|
9
|
+
spec.authors = ['Brian Murphy-Dye']
|
10
|
+
spec.email = ['brian@murphydye.com']
|
11
|
+
|
12
|
+
spec.summary = %q{REPL wrapper for salt.}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = ""
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'guard'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: saltr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brian Murphy-Dye
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-08-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.10'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.10'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '10.0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '10.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: ''
|
79
|
+
email:
|
80
|
+
- brian@murphydye.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- bin/console
|
90
|
+
- bin/setup
|
91
|
+
- lib/help.rb
|
92
|
+
- lib/salt.rb
|
93
|
+
- lib/saltr.rb
|
94
|
+
- lib/saltr/version.rb
|
95
|
+
- saltr.gemspec
|
96
|
+
homepage: ''
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: -4543377424658850655
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
segments:
|
118
|
+
- 0
|
119
|
+
hash: -4543377424658850655
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.23
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: REPL wrapper for salt.
|
126
|
+
test_files: []
|