adamwiggins-rush 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +87 -0
- data/Rakefile +61 -0
- data/VERSION +1 -0
- data/bin/rush +13 -0
- data/bin/rushd +7 -0
- data/lib/rush/access.rb +130 -0
- data/lib/rush/array_ext.rb +19 -0
- data/lib/rush/box.rb +112 -0
- data/lib/rush/commands.rb +55 -0
- data/lib/rush/config.rb +154 -0
- data/lib/rush/dir.rb +160 -0
- data/lib/rush/embeddable_shell.rb +26 -0
- data/lib/rush/entry.rb +185 -0
- data/lib/rush/exceptions.rb +31 -0
- data/lib/rush/file.rb +85 -0
- data/lib/rush/find_by.rb +39 -0
- data/lib/rush/fixnum_ext.rb +18 -0
- data/lib/rush/head_tail.rb +11 -0
- data/lib/rush/local.rb +402 -0
- data/lib/rush/process.rb +59 -0
- data/lib/rush/process_set.rb +62 -0
- data/lib/rush/remote.rb +156 -0
- data/lib/rush/search_results.rb +58 -0
- data/lib/rush/server.rb +117 -0
- data/lib/rush/shell.rb +187 -0
- data/lib/rush/ssh_tunnel.rb +122 -0
- data/lib/rush/string_ext.rb +3 -0
- data/lib/rush.rb +87 -0
- data/spec/access_spec.rb +134 -0
- data/spec/array_ext_spec.rb +15 -0
- data/spec/base.rb +24 -0
- data/spec/box_spec.rb +64 -0
- data/spec/commands_spec.rb +47 -0
- data/spec/config_spec.rb +108 -0
- data/spec/dir_spec.rb +164 -0
- data/spec/embeddable_shell_spec.rb +17 -0
- data/spec/entry_spec.rb +133 -0
- data/spec/file_spec.rb +83 -0
- data/spec/find_by_spec.rb +58 -0
- data/spec/fixnum_ext_spec.rb +19 -0
- data/spec/local_spec.rb +352 -0
- data/spec/process_set_spec.rb +50 -0
- data/spec/process_spec.rb +73 -0
- data/spec/remote_spec.rb +140 -0
- data/spec/rush_spec.rb +28 -0
- data/spec/search_results_spec.rb +44 -0
- data/spec/shell_spec.rb +23 -0
- data/spec/ssh_tunnel_spec.rb +122 -0
- data/spec/string_ext_spec.rb +23 -0
- metadata +141 -0
data/README.rdoc
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
= rush -- manage your unix systems with pure Ruby
|
2
|
+
|
3
|
+
rush is a unix integration library and an interactive shell which uses pure Ruby syntax. Walk directory trees; create, copy, search, and destroy files; find and kill processes - everything you'd normally do with shell commands, now in the strict and elegant world of Ruby.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
Count the number of classes in your project using bash:
|
8
|
+
|
9
|
+
find myproj -name \*.rb | xargs grep '^\s*class' | wc -l
|
10
|
+
|
11
|
+
In rush, this is:
|
12
|
+
|
13
|
+
myproj['**/*.rb'].search(/^\s*class/).lines.size
|
14
|
+
|
15
|
+
Pesky stray mongrels? In bash:
|
16
|
+
|
17
|
+
kill `ps aux | grep mongrel_rails | grep -v grep | cut -c 10-20`
|
18
|
+
|
19
|
+
In rush:
|
20
|
+
|
21
|
+
processes.filter(:cmdline => /mongrel_rails/).kill
|
22
|
+
|
23
|
+
== As a library
|
24
|
+
|
25
|
+
require 'rubygems'
|
26
|
+
require 'rush'
|
27
|
+
|
28
|
+
file = Rush['/tmp/myfile']
|
29
|
+
file.write "hello"
|
30
|
+
puts file.contents
|
31
|
+
file.destroy
|
32
|
+
|
33
|
+
puts Rush.my_process.pid
|
34
|
+
puts Rush.processes.size
|
35
|
+
puts Rush.bash("echo SHELL COMMAND | tr A-Z a-z")
|
36
|
+
puts Rush.launch_dir['*.rb'].search(/Rush/).entries.inspect
|
37
|
+
|
38
|
+
== Invoking the shell
|
39
|
+
|
40
|
+
Run the "rush" binary to enter the interactive shell.
|
41
|
+
|
42
|
+
== Remote access and clustering
|
43
|
+
|
44
|
+
rush can control any number of remote machines from a single location. Copy files or directories between servers as seamlessly as if it was all local.
|
45
|
+
|
46
|
+
Example of remote access:
|
47
|
+
|
48
|
+
local = Rush::Box.new('localhost')
|
49
|
+
remote = Rush::Box.new('my.remote.server.com')
|
50
|
+
local_dir = local['/Users/adam/myproj/']
|
51
|
+
remote_dir = remote['/home/myproj/app/']
|
52
|
+
|
53
|
+
local_dir.copy_to remote_dir
|
54
|
+
remote_dir['**/.svn/'].each { |d| d.destroy }
|
55
|
+
|
56
|
+
Clustering:
|
57
|
+
|
58
|
+
local_dir = Rush::Box.new('localhost')['/Users/adam/server_logs/'].create
|
59
|
+
servers = %w(www1 www2 www3).map { |n| Rush::Box.new(n) }
|
60
|
+
servers.each { |s| s['/var/log/nginx/access.log'].copy_to local_dir["#{s.host}_access.log"] }
|
61
|
+
|
62
|
+
== Reference
|
63
|
+
|
64
|
+
For more details on syntax and commands, see:
|
65
|
+
|
66
|
+
* Rush
|
67
|
+
* Rush::Entry
|
68
|
+
* Rush::File
|
69
|
+
* Rush::Dir
|
70
|
+
* Rush::Commands
|
71
|
+
* Rush::Box
|
72
|
+
* Rush::Process
|
73
|
+
|
74
|
+
== Meta
|
75
|
+
|
76
|
+
Created by Adam Wiggins
|
77
|
+
|
78
|
+
Patches contributed by Chihiro Ito, Gabriel Ware, Michael Schutte, Ricardo Chimal Jr., and Nicholas Schlueter, Pedro Belo, and Martin Kuehl
|
79
|
+
|
80
|
+
Logo by James Lindenbaum
|
81
|
+
|
82
|
+
Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
|
83
|
+
|
84
|
+
http://rush.heroku.com
|
85
|
+
|
86
|
+
http://groups.google.com/group/ruby-shell
|
87
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
require 'jeweler'
|
4
|
+
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "rush"
|
7
|
+
s.summary = "A Ruby replacement for bash+ssh."
|
8
|
+
s.description = "A Ruby replacement for bash+ssh, providing both an interactive shell and a library. Manage both local and remote unix systems from a single client."
|
9
|
+
s.author = "Adam Wiggins"
|
10
|
+
s.email = "adam@heroku.com"
|
11
|
+
s.homepage = "http://rush.heroku.com/"
|
12
|
+
s.executables = [ "rush", "rushd" ]
|
13
|
+
s.rubyforge_project = "ruby-shell"
|
14
|
+
s.has_rdoc = true
|
15
|
+
|
16
|
+
s.add_dependency 'mongrel'
|
17
|
+
s.add_dependency 'session'
|
18
|
+
|
19
|
+
s.files = FileList["[A-Z]*", "{bin,lib,spec}/**/*"]
|
20
|
+
end
|
21
|
+
|
22
|
+
Jeweler::RubyforgeTasks.new
|
23
|
+
|
24
|
+
######################################################
|
25
|
+
|
26
|
+
require 'spec/rake/spectask'
|
27
|
+
|
28
|
+
desc "Run all specs"
|
29
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
30
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "Print specdocs"
|
34
|
+
Spec::Rake::SpecTask.new(:doc) do |t|
|
35
|
+
t.spec_opts = ["--format", "specdoc", "--dry-run"]
|
36
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
37
|
+
end
|
38
|
+
|
39
|
+
desc "Run all examples with RCov"
|
40
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
41
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
42
|
+
t.rcov = true
|
43
|
+
t.rcov_opts = ['--exclude', 'examples']
|
44
|
+
end
|
45
|
+
|
46
|
+
task :default => :spec
|
47
|
+
|
48
|
+
######################################################
|
49
|
+
|
50
|
+
require 'rake/rdoctask'
|
51
|
+
|
52
|
+
Rake::RDocTask.new do |t|
|
53
|
+
t.rdoc_dir = 'rdoc'
|
54
|
+
t.title = "rush, a Ruby replacement for bash+ssh"
|
55
|
+
t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
|
56
|
+
t.options << '--charset' << 'utf-8'
|
57
|
+
t.rdoc_files.include('README.rdoc')
|
58
|
+
t.rdoc_files.include('lib/rush.rb')
|
59
|
+
t.rdoc_files.include('lib/rush/*.rb')
|
60
|
+
end
|
61
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.6.1
|
data/bin/rush
ADDED
data/bin/rushd
ADDED
data/lib/rush/access.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
# A class to hold permissions (read, write, execute) for files and dirs.
|
2
|
+
# See Rush::Entry#access= for information on the public-facing interface.
|
3
|
+
class Rush::Access
|
4
|
+
attr_accessor :user_can_read, :user_can_write, :user_can_execute
|
5
|
+
attr_accessor :group_can_read, :group_can_write, :group_can_execute
|
6
|
+
attr_accessor :other_can_read, :other_can_write, :other_can_execute
|
7
|
+
|
8
|
+
def self.roles
|
9
|
+
%w(user group other)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.permissions
|
13
|
+
%w(read write execute)
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse(options)
|
17
|
+
options.each do |key, value|
|
18
|
+
next unless m = key.to_s.match(/(.*)_can$/)
|
19
|
+
key = m[1].to_sym
|
20
|
+
roles = extract_list('role', key, self.class.roles)
|
21
|
+
perms = extract_list('permission', value, self.class.permissions)
|
22
|
+
set_matrix(perms, roles)
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.parse(options)
|
28
|
+
new.parse(options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def apply(full_path)
|
32
|
+
FileUtils.chmod(octal_permissions, full_path)
|
33
|
+
rescue Errno::ENOENT
|
34
|
+
raise Rush::DoesNotExist, full_path
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_hash
|
38
|
+
hash = {}
|
39
|
+
self.class.roles.each do |role|
|
40
|
+
self.class.permissions.each do |perm|
|
41
|
+
key = "#{role}_can_#{perm}".to_sym
|
42
|
+
hash[key] = send(key) ? 1 : 0
|
43
|
+
end
|
44
|
+
end
|
45
|
+
hash
|
46
|
+
end
|
47
|
+
|
48
|
+
def display_hash
|
49
|
+
hash = {}
|
50
|
+
to_hash.each do |key, value|
|
51
|
+
hash[key] = true if value == 1
|
52
|
+
end
|
53
|
+
hash
|
54
|
+
end
|
55
|
+
|
56
|
+
def from_hash(hash)
|
57
|
+
self.class.roles.each do |role|
|
58
|
+
self.class.permissions.each do |perm|
|
59
|
+
key = "#{role}_can_#{perm}"
|
60
|
+
send("#{key}=".to_sym, hash[key.to_sym].to_i == 1 ? true : false)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.from_hash(hash)
|
67
|
+
new.from_hash(hash)
|
68
|
+
end
|
69
|
+
|
70
|
+
def octal_permissions
|
71
|
+
perms = [ 0, 0, 0 ]
|
72
|
+
perms[0] += 4 if user_can_read
|
73
|
+
perms[0] += 2 if user_can_write
|
74
|
+
perms[0] += 1 if user_can_execute
|
75
|
+
|
76
|
+
perms[1] += 4 if group_can_read
|
77
|
+
perms[1] += 2 if group_can_write
|
78
|
+
perms[1] += 1 if group_can_execute
|
79
|
+
|
80
|
+
perms[2] += 4 if other_can_read
|
81
|
+
perms[2] += 2 if other_can_write
|
82
|
+
perms[2] += 1 if other_can_execute
|
83
|
+
|
84
|
+
eval("0" + perms.join)
|
85
|
+
end
|
86
|
+
|
87
|
+
def from_octal(mode)
|
88
|
+
perms = octal_integer_array(mode)
|
89
|
+
|
90
|
+
self.user_can_read = (perms[0] & 4) > 0 ? true : false
|
91
|
+
self.user_can_write = (perms[0] & 2) > 0 ? true : false
|
92
|
+
self.user_can_execute = (perms[0] & 1) > 0 ? true : false
|
93
|
+
|
94
|
+
self.group_can_read = (perms[1] & 4) > 0 ? true : false
|
95
|
+
self.group_can_write = (perms[1] & 2) > 0 ? true : false
|
96
|
+
self.group_can_execute = (perms[1] & 1) > 0 ? true : false
|
97
|
+
|
98
|
+
self.other_can_read = (perms[2] & 4) > 0 ? true : false
|
99
|
+
self.other_can_write = (perms[2] & 2) > 0 ? true : false
|
100
|
+
self.other_can_execute = (perms[2] & 1) > 0 ? true : false
|
101
|
+
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
def octal_integer_array(mode)
|
106
|
+
mode %= 01000 # filter out everything but the bottom three digits
|
107
|
+
mode = sprintf("%o", mode) # convert to string
|
108
|
+
mode.split("").map { |p| p.to_i } # and finally, array of integers
|
109
|
+
end
|
110
|
+
|
111
|
+
def set_matrix(perms, roles)
|
112
|
+
perms.each do |perm|
|
113
|
+
roles.each do |role|
|
114
|
+
meth = "#{role}_can_#{perm}=".to_sym
|
115
|
+
send(meth, true)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def extract_list(type, value, choices)
|
121
|
+
list = parts_from(value)
|
122
|
+
list.each do |value|
|
123
|
+
raise(Rush::BadAccessSpecifier, "Unrecognized #{type}: #{value}") unless choices.include? value
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def parts_from(value)
|
128
|
+
value.to_s.split('_').reject { |r| r == 'and' }
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Rush mixes in Rush::Commands in order to allow operations on groups of
|
2
|
+
# Rush::Entry items. For example, dir['**/*.rb'] returns an array of files, so
|
3
|
+
# dir['**/*.rb'].destroy would destroy all the files specified.
|
4
|
+
#
|
5
|
+
# One cool tidbit: the array can contain entries anywhere, so you can create
|
6
|
+
# collections of entries from different servers and then operate across them:
|
7
|
+
#
|
8
|
+
# [ box1['/var/log/access.log'] + box2['/var/log/access.log'] ].search /#{url}/
|
9
|
+
class Array
|
10
|
+
include Rush::Commands
|
11
|
+
|
12
|
+
def entries
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
include Rush::FindBy
|
17
|
+
|
18
|
+
include Rush::HeadTail
|
19
|
+
end
|
data/lib/rush/box.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# A rush box is a single unix machine - a server, workstation, or VPS instance.
|
2
|
+
#
|
3
|
+
# Specify a box by hostname (default = 'localhost'). If the box is remote, the
|
4
|
+
# first action performed will attempt to open an ssh tunnel. Use square
|
5
|
+
# brackets to access the filesystem, or processes to access the process list.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
#
|
9
|
+
# local = Rush::Box.new
|
10
|
+
# local['/etc/hosts'].contents
|
11
|
+
# local.processes
|
12
|
+
#
|
13
|
+
class Rush::Box
|
14
|
+
attr_reader :host
|
15
|
+
|
16
|
+
# Instantiate a box. No action is taken to make a connection until you try
|
17
|
+
# to perform an action. If the box is remote, an ssh tunnel will be opened.
|
18
|
+
# Specify a username with the host if the remote ssh user is different from
|
19
|
+
# the local one (e.g. Rush::Box.new('user@host')).
|
20
|
+
def initialize(host='localhost')
|
21
|
+
@host = host
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s # :nodoc:
|
25
|
+
host
|
26
|
+
end
|
27
|
+
|
28
|
+
def inspect # :nodoc:
|
29
|
+
host
|
30
|
+
end
|
31
|
+
|
32
|
+
# Access / on the box.
|
33
|
+
def filesystem
|
34
|
+
Rush::Entry.factory('/', self)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Look up an entry on the filesystem, e.g. box['/path/to/some/file'].
|
38
|
+
# Returns a subclass of Rush::Entry - either Rush::Dir if you specifiy
|
39
|
+
# trailing slash, or Rush::File otherwise.
|
40
|
+
def [](key)
|
41
|
+
filesystem[key]
|
42
|
+
end
|
43
|
+
|
44
|
+
# Get the list of processes running on the box, not unlike "ps aux" in bash.
|
45
|
+
# Returns a Rush::ProcessSet.
|
46
|
+
def processes
|
47
|
+
Rush::ProcessSet.new(
|
48
|
+
connection.processes.map do |ps|
|
49
|
+
Rush::Process.new(ps, self)
|
50
|
+
end
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Execute a command in the standard unix shell. Returns the contents of
|
55
|
+
# stdout if successful, or raises Rush::BashFailed with the output of stderr
|
56
|
+
# if the shell returned a non-zero value. Options:
|
57
|
+
#
|
58
|
+
# :user => unix username to become via sudo
|
59
|
+
# :env => hash of environment variables
|
60
|
+
# :background => run in the background (returns Rush::Process instead of stdout)
|
61
|
+
#
|
62
|
+
# Examples:
|
63
|
+
#
|
64
|
+
# box.bash '/etc/init.d/mysql restart', :user => 'root'
|
65
|
+
# box.bash 'rake db:migrate', :user => 'www', :env => { :RAILS_ENV => 'production' }
|
66
|
+
# box.bash 'mongrel_rails start', :background => true
|
67
|
+
#
|
68
|
+
def bash(command, options={})
|
69
|
+
cmd_with_env = command_with_environment(command, options[:env])
|
70
|
+
|
71
|
+
if options[:background]
|
72
|
+
pid = connection.bash(cmd_with_env, options[:user], true)
|
73
|
+
processes.find_by_pid(pid)
|
74
|
+
else
|
75
|
+
connection.bash(cmd_with_env, options[:user], false)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def command_with_environment(command, env) # :nodoc:
|
80
|
+
return command unless env
|
81
|
+
|
82
|
+
vars = env.map do |key, value|
|
83
|
+
"export #{key}='#{value}'"
|
84
|
+
end
|
85
|
+
vars.push(command).join("\n")
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns true if the box is responding to commands.
|
89
|
+
def alive?
|
90
|
+
connection.alive?
|
91
|
+
end
|
92
|
+
|
93
|
+
# This is called automatically the first time an action is invoked, but you
|
94
|
+
# may wish to call it manually ahead of time in order to have the tunnel
|
95
|
+
# already set up and running. You can also use this to pass a timeout option,
|
96
|
+
# either :timeout => (seconds) or :timeout => :infinite.
|
97
|
+
def establish_connection(options={})
|
98
|
+
connection.ensure_tunnel(options)
|
99
|
+
end
|
100
|
+
|
101
|
+
def connection # :nodoc:
|
102
|
+
@connection ||= make_connection
|
103
|
+
end
|
104
|
+
|
105
|
+
def make_connection # :nodoc:
|
106
|
+
host == 'localhost' ? Rush::Connection::Local.new : Rush::Connection::Remote.new(host)
|
107
|
+
end
|
108
|
+
|
109
|
+
def ==(other) # :nodoc:
|
110
|
+
host == other.host
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# The commands module contains operations against Rush::File entries, and is
|
2
|
+
# mixed in to Rush::Entry and Array. This means you can run these commands against a single
|
3
|
+
# file, a dir full of files, or an arbitrary list of files.
|
4
|
+
#
|
5
|
+
# Examples:
|
6
|
+
#
|
7
|
+
# box['/etc/hosts'].search /localhost/ # single file
|
8
|
+
# box['/etc/'].search /localhost/ # entire directory
|
9
|
+
# box['/etc/**/*.conf'].search /localhost/ # arbitrary list
|
10
|
+
module Rush::Commands
|
11
|
+
# The entries command must return an array of Rush::Entry items. This
|
12
|
+
# varies by class that it is mixed in to.
|
13
|
+
def entries
|
14
|
+
raise "must define me in class mixed in to for command use"
|
15
|
+
end
|
16
|
+
|
17
|
+
# Search file contents for a regular expression. A Rush::SearchResults
|
18
|
+
# object is returned.
|
19
|
+
def search(pattern)
|
20
|
+
results = Rush::SearchResults.new(pattern)
|
21
|
+
entries.each do |entry|
|
22
|
+
if !entry.dir? and matches = entry.search(pattern)
|
23
|
+
results.add(entry, matches)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
results
|
27
|
+
end
|
28
|
+
|
29
|
+
# Search and replace file contents.
|
30
|
+
def replace_contents!(pattern, with_text)
|
31
|
+
entries.each do |entry|
|
32
|
+
entry.replace_contents!(pattern, with_text) unless entry.dir?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Count the number of lines in the contained files.
|
37
|
+
def line_count
|
38
|
+
entries.inject(0) do |count, entry|
|
39
|
+
count += entry.lines.size if !entry.dir?
|
40
|
+
count
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Invoke vi on one or more files - only works locally.
|
45
|
+
def vi(*args)
|
46
|
+
names = entries.map { |f| f.quoted_path }.join(' ')
|
47
|
+
system "vim #{names} #{args.join(' ')}"
|
48
|
+
end
|
49
|
+
|
50
|
+
# Invoke TextMate on one or more files - only works locally.
|
51
|
+
def mate(*args)
|
52
|
+
names = entries.map { |f| f.quoted_path }.join(' ')
|
53
|
+
system "mate #{names} #{args.join(' ')}"
|
54
|
+
end
|
55
|
+
end
|
data/lib/rush/config.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
# The config class accesses files in ~/.rush to load and save user preferences.
|
2
|
+
class Rush::Config
|
3
|
+
DefaultPort = 7770
|
4
|
+
|
5
|
+
attr_reader :dir
|
6
|
+
|
7
|
+
# By default, reads from the dir ~/.rush, but an optional parameter allows
|
8
|
+
# using another location.
|
9
|
+
def initialize(location=nil)
|
10
|
+
@dir = Rush::Dir.new(location || "#{ENV['HOME']}/.rush")
|
11
|
+
@dir.create
|
12
|
+
end
|
13
|
+
|
14
|
+
# History is a flat file of past commands in the interactive shell,
|
15
|
+
# equivalent to .bash_history.
|
16
|
+
def history_file
|
17
|
+
dir['history']
|
18
|
+
end
|
19
|
+
|
20
|
+
def save_history(array)
|
21
|
+
history_file.write(array.join("\n") + "\n")
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_history
|
25
|
+
history_file.contents_or_blank.split("\n")
|
26
|
+
end
|
27
|
+
|
28
|
+
# The environment file is executed when the interactive shell starts up.
|
29
|
+
# Put aliases and your own functions here; it is the equivalent of .bashrc
|
30
|
+
# or .profile.
|
31
|
+
#
|
32
|
+
# Example ~/.rush/env.rb:
|
33
|
+
#
|
34
|
+
# server = Rush::Box.new('www@my.server')
|
35
|
+
# myproj = home['projects/myproj/']
|
36
|
+
def env_file
|
37
|
+
dir['env.rb']
|
38
|
+
end
|
39
|
+
|
40
|
+
def load_env
|
41
|
+
env_file.contents_or_blank
|
42
|
+
end
|
43
|
+
|
44
|
+
# Commands are mixed in to Array and Rush::Entry, alongside the default
|
45
|
+
# commands from Rush::Commands. Any methods here should reference "entries"
|
46
|
+
# to get the list of entries to operate on.
|
47
|
+
#
|
48
|
+
# Example ~/.rush/commands.rb:
|
49
|
+
#
|
50
|
+
# def destroy_svn(*args)
|
51
|
+
# entries.select { |e| e.name == '.svn' }.destroy
|
52
|
+
# end
|
53
|
+
def commands_file
|
54
|
+
dir['commands.rb']
|
55
|
+
end
|
56
|
+
|
57
|
+
def load_commands
|
58
|
+
commands_file.contents_or_blank
|
59
|
+
end
|
60
|
+
|
61
|
+
# Passwords contains a list of username:password combinations used for
|
62
|
+
# remote access via rushd. You can fill this in manually, or let the remote
|
63
|
+
# connection publish it automatically.
|
64
|
+
def passwords_file
|
65
|
+
dir['passwords']
|
66
|
+
end
|
67
|
+
|
68
|
+
def passwords
|
69
|
+
hash = {}
|
70
|
+
passwords_file.lines_or_empty.each do |line|
|
71
|
+
user, password = line.split(":", 2)
|
72
|
+
hash[user] = password
|
73
|
+
end
|
74
|
+
hash
|
75
|
+
end
|
76
|
+
|
77
|
+
# Credentials is the client-side equivalent of passwords. It contains only
|
78
|
+
# one username:password combination that is transmitted to the server when
|
79
|
+
# connecting. This is also autogenerated if it does not exist.
|
80
|
+
def credentials_file
|
81
|
+
dir['credentials']
|
82
|
+
end
|
83
|
+
|
84
|
+
def credentials
|
85
|
+
credentials_file.lines.first.split(":", 2)
|
86
|
+
end
|
87
|
+
|
88
|
+
def save_credentials(user, password)
|
89
|
+
credentials_file.write("#{user}:#{password}\n")
|
90
|
+
end
|
91
|
+
|
92
|
+
def credentials_user
|
93
|
+
credentials[0]
|
94
|
+
end
|
95
|
+
|
96
|
+
def credentials_password
|
97
|
+
credentials[1]
|
98
|
+
end
|
99
|
+
|
100
|
+
def ensure_credentials_exist
|
101
|
+
generate_credentials if credentials_file.contents_or_blank == ""
|
102
|
+
end
|
103
|
+
|
104
|
+
def generate_credentials
|
105
|
+
save_credentials(generate_user, generate_password)
|
106
|
+
end
|
107
|
+
|
108
|
+
def generate_user
|
109
|
+
generate_secret(4, 8)
|
110
|
+
end
|
111
|
+
|
112
|
+
def generate_password
|
113
|
+
generate_secret(8, 15)
|
114
|
+
end
|
115
|
+
|
116
|
+
def generate_secret(min, max)
|
117
|
+
chars = self.secret_characters
|
118
|
+
len = rand(max - min + 1) + min
|
119
|
+
password = ""
|
120
|
+
len.times do |index|
|
121
|
+
password += chars[rand(chars.length)]
|
122
|
+
end
|
123
|
+
password
|
124
|
+
end
|
125
|
+
|
126
|
+
def secret_characters
|
127
|
+
[ ('a'..'z'), ('1'..'9') ].inject([]) do |chars, range|
|
128
|
+
chars += range.to_a
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# ~/.rush/tunnels contains a list of previously created ssh tunnels. The
|
133
|
+
# format is host:port, where port is the local port that the tunnel is
|
134
|
+
# listening on.
|
135
|
+
def tunnels_file
|
136
|
+
dir['tunnels']
|
137
|
+
end
|
138
|
+
|
139
|
+
def tunnels
|
140
|
+
tunnels_file.lines_or_empty.inject({}) do |hash, line|
|
141
|
+
host, port = line.split(':', 2)
|
142
|
+
hash[host] = port.to_i
|
143
|
+
hash
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def save_tunnels(hash)
|
148
|
+
string = ""
|
149
|
+
hash.each do |host, port|
|
150
|
+
string += "#{host}:#{port}\n"
|
151
|
+
end
|
152
|
+
tunnels_file.write string
|
153
|
+
end
|
154
|
+
end
|