shrub 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,9 @@
1
+ To install the Shrub Service, use:
2
+
3
+ shrub_service install
4
+
5
+ and to uninstall it:
6
+
7
+ shrub_service uninstall
8
+
9
+ There's no documentation for now. You can get to the meat of it by looking up the ShrubClient.rb file. Check the methods out. More docs to come :-)
data/bin/shrub_service ADDED
@@ -0,0 +1,6 @@
1
+ # This project runs on Windows at the moment.
2
+
3
+ require 'rubygems'
4
+ require 'ShrubServiceInstaller'
5
+
6
+ Shrub::ServiceInstaller.run(ARGV)
@@ -0,0 +1,72 @@
1
+ require 'socket'
2
+ require 'drb'
3
+
4
+ class ShrubClient
5
+ def initialize(url, username, password)
6
+ @url = url
7
+ @username = username
8
+ @password = password
9
+ end
10
+
11
+ def run(code)
12
+ DRb.start_service
13
+ runner = DRbObject.new(nil, @url)
14
+ puts runner.run_code(@username, @password, code)
15
+
16
+ return
17
+
18
+ code = code.strip
19
+ return "" if code.length == 0
20
+ command = "#{code.length}#{code}"
21
+ command = "#{code.length}\n#{code}"
22
+ data = ""
23
+
24
+ TCPSocket.open(@host, @port) {|s|
25
+ s.write(command)
26
+ size = s.readline
27
+ data = s.read(size.to_i)
28
+ }
29
+
30
+ return data
31
+ end
32
+
33
+ def runfile(filename)
34
+ size = File.size(filename)
35
+ program = File.open(filename) {|f| f.read(size) }
36
+
37
+ return run(program)
38
+ end
39
+
40
+ def convert_slashes(data)
41
+ data.gsub(/\\/, "\\\\\\\\")
42
+ end
43
+
44
+ def dir(remote_path)
45
+ remote_path = convert_slashes(remote_path)
46
+ code = "system(\"dir \\\"#{remote_path}\\\"\")"
47
+ return run(code)
48
+ end
49
+
50
+ def copy(local_path, remote_path)
51
+ sz = File.size(local_path)
52
+ data = nil
53
+ File.open(local_path, "rb") {|f| data = f.read sz }
54
+
55
+ base64_encoded_data = [data].pack('m')
56
+
57
+ code = "data = \"#{base64_encoded_data}\".unpack('m')[0]; File.open(\"#{convert_slashes(remote_path)}\", \"wb+\") {|f| f.write(data) } "
58
+ return run(code)
59
+ end
60
+
61
+ def execute(remote_command)
62
+ remote_command = convert_slashes(remote_command)
63
+ code = "system(\"#{remote_command}\")"
64
+ return run(code)
65
+ end
66
+
67
+ def purge(remote_file_path)
68
+ remote_file_path = convert_slashes(remote_file_path)
69
+ code = "system(\"del /F /Q #{remote_file_path}\")"
70
+ return run(code)
71
+ end
72
+ end
@@ -0,0 +1,38 @@
1
+ require 'drb'
2
+
3
+ require 'rubygems'
4
+ require 'win32/service'
5
+ require 'win32/daemon'
6
+
7
+ include Win32
8
+
9
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'drb_shrub_server.rb')
10
+
11
+ class Daemon
12
+ def service_main
13
+ sleep 3
14
+
15
+ DRb.start_service('druby://0.0.0.0:12000', Shrub::CodeRunner.new(ARGV[0]))
16
+ DRb.thread.join
17
+ end
18
+
19
+ def service_stop
20
+ DRb.stop_service
21
+ end
22
+
23
+ def service_init
24
+ end
25
+
26
+ def service_pause
27
+ end
28
+
29
+ def service_resume
30
+ end
31
+
32
+ def service_shutdown
33
+ DRb.stop_service
34
+ end
35
+ end
36
+
37
+ svc = Daemon.new
38
+ svc.mainloop
@@ -0,0 +1,42 @@
1
+ require 'drb'
2
+
3
+ require 'rubygems'
4
+ require 'win32/service'
5
+ require 'win32/daemon'
6
+
7
+ module Shrub
8
+ class ServiceInstaller
9
+ def self.run(argv)
10
+ service_name = 'Shrub Server'
11
+ ruby_bin = Config::CONFIG['bindir']
12
+ file_dir = File.dirname(File.expand_path(__FILE__))
13
+ @@ruby_exe = File.join("#{ruby_bin}", "ruby.exe")
14
+
15
+ file_path = File.join(File.dirname(File.expand_path(__FILE__)), 'ShrubService.rb')
16
+ cmd = "#{@@ruby_exe} \"#{file_path}\" #{@@ruby_exe}"
17
+
18
+ if argv[0] == 'install'
19
+ puts "Creating service for #{cmd}"
20
+
21
+ Win32::Service.new(service_name, nil,
22
+ :display_name => service_name,
23
+ :desired_access => Win32::Service::ALL_ACCESS,
24
+ :service_type => Win32::Service::WIN32_OWN_PROCESS,
25
+ :start_type => Win32::Service::DEMAND_START,
26
+ :error_control => Win32::Service::ERROR_IGNORE,
27
+ :binary_path_name => cmd,
28
+ :load_order_group => 'Network',
29
+ :dependencies => nil,
30
+ :description => 'Executes ruby code sent from another machine'
31
+ )
32
+ exit
33
+ elsif argv[0] == 'uninstall'
34
+ Win32::Service.delete(service_name)
35
+ exit
36
+ elseif argv.length > 0
37
+ puts 'I can\'t understand the command.'
38
+ exit
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,25 @@
1
+ require 'drb'
2
+
3
+ module Shrub
4
+ class CodeRunner
5
+ def initialize(ruby_exe)
6
+ @ruby_exe = ruby_exe
7
+ @users = {
8
+ 'root' => 'qwedsa'
9
+ }
10
+ end
11
+
12
+ def run_code(username, pwd, code)
13
+ raise 'Invalid username and pwd' if @users[username] == nil || @users[username] != pwd
14
+
15
+ io = IO.popen("#{@ruby_exe} 2>&1", "w+")
16
+ io.write code
17
+ io.close_write
18
+
19
+ result = io.readlines.join
20
+ io.close_read
21
+
22
+ return result
23
+ end
24
+ end
25
+ end
data/lib/shrub.rb ADDED
@@ -0,0 +1 @@
1
+ require 'ShrubClient.rb'
data/license.txt ADDED
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2008, Joel Rosario
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ * Neither the name of the Joel Rosario nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrub
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Joel Rosario
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-31 00:00:00 +05:30
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: win32-service
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.6.1
24
+ version:
25
+ description:
26
+ email: joel.rosaio@gmail.com
27
+ executables:
28
+ - shrub_service
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - license.txt
34
+ files:
35
+ - lib/drb_shrub_server.rb
36
+ - lib/shrub.rb
37
+ - lib/ShrubClient.rb
38
+ - lib/ShrubService.rb
39
+ - lib/ShrubServiceInstaller.rb
40
+ - README
41
+ - license.txt
42
+ has_rdoc: true
43
+ homepage: http://rubyforge.org/projects/shrub/
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project: shrub
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: A simple console for running operations on a remote machine using ruby.
68
+ test_files: []
69
+