silk 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,15 +10,17 @@ This gem is just the runner, that need to be installed on all client servers.
10
10
 
11
11
  (Requires Gemcutter)
12
12
 
13
- gem install silk
13
+ gem install silk
14
14
 
15
- Create /etc/silk and drop in a Rakefile - you can also add *.rake files in /etc/silk (or $HOME/.silk/) and they will automatically be read in.
15
+ Create /etc/silk and drop *.rake files in /etc/silk (or $HOME/.silk/) and they will automatically be read in.
16
16
 
17
17
  == Usage
18
18
 
19
- The gem should install an executable called silk
19
+ The gem should install an executable called silk run
20
20
 
21
- run silk -h for full argument descriptions.
21
+ silk -h
22
+
23
+ for full argument descriptions.
22
24
 
23
25
  The system automatically converts the rest URL (only GETs at the moment) into a rake task name - if the name doesn't exist, you'll get a 404. Any query params get passed into the task
24
26
 
@@ -26,11 +28,19 @@ So http://localhost:8888/users/get?login=myles
26
28
 
27
29
  would run the users:get task. do something like this in the rake task:
28
30
 
29
- namespace :users do
30
- task :get do |t, args|
31
- # args[:login] will be populated with myles
31
+ require 'json'
32
+
33
+ namespace :users do
34
+ task :get do |t, args|
35
+ # args[:login] will be populated with myles
36
+ if args[:login]
37
+ puts "Success".to_json
38
+ else
39
+ $stderr.puts "Fail".to_json
40
+ exit(-1)
41
+ end
42
+ end
32
43
  end
33
- end
34
44
 
35
45
  NOTE: The idea is you write your own rake task to do stuff, and then interface them how you want. The project doesn't care what data you transfer and represent, although it would be possible to share rakefiles and interfaces for common tasks.
36
46
 
@@ -38,9 +48,17 @@ By default it runs on port 8888 and will probably need to be run as root to make
38
48
 
39
49
  Eventually it'll support SSL certificates etc, but until then, I suggest you either use SSH tunnels, and/or (at the very least) lock down via a firewall. You could also build a rack.up file, and run it behind a web server that gives you more control (Rack.up file is on the todo list).
40
50
 
51
+ == Testing
52
+
53
+ There is a test suite. It uses shoulda + mocha + test::rack
54
+
55
+ If you are going to submit patches, please try to make sure the tests pass, and that you have created a test covering the changes you have made
56
+
57
+ (Please note: I haven't got complete coverage yet, so I'm not really practicing what I preach. I'm working on it - feel free to help out!)
58
+
41
59
  == Status
42
60
 
43
- This is a really early release, that still needs formal testing (This is so far more proof of concept at this point), and may blow up at any given moment. Feel free to fork and help out.
61
+ This is a really early release, that still needs more testing (This is so far more proof of concept at this point), and may blow up at any given moment. Feel free to fork and help out.
44
62
 
45
63
  == Note on Patches/Pull Requests
46
64
 
data/Rakefile CHANGED
@@ -13,9 +13,14 @@ begin
13
13
  gem.authors = ["Myles Eftos"]
14
14
 
15
15
  gem.add_dependency 'daemons'
16
+ gem.add_dependency 'json'
17
+ gem.add_dependency 'sinatra'
16
18
  gem.add_dependency 'SyslogLogger'
17
19
 
18
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
20
+ gem.add_development_dependency "thoughtbot-shoulda"
21
+ gem.add_development_dependency "rack-test"
22
+ gem.add_development_dependency "redgreen"
23
+ gem.add_development_dependency "mocha"
19
24
  end
20
25
  rescue LoadError
21
26
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -40,9 +40,9 @@ module Silk
40
40
  headers('X_PROCESS_EXIT_STATUS' => $?.exitstatus.to_s)
41
41
 
42
42
  if $?.exitstatus != 0
43
- error(500, results[:stderr])
43
+ error(500, results[:stderr].strip)
44
44
  else
45
- results[:stdout]
45
+ results[:stdout].strip
46
46
  end
47
47
  end
48
48
  end
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+
3
+ desc "Level 1"
4
+ task :level_1 do
5
+ puts "Level 1".to_json
6
+ end
7
+
8
+ namespace :level_1 do
9
+ desc "Level 2"
10
+ task :level_2 do
11
+ puts "Level 2".to_json
12
+ end
13
+
14
+ desc "Level 2 with args"
15
+ task :level_2_with_args do |t, args|
16
+ puts(("Level 2: " + args.inspect).to_json)
17
+ end
18
+
19
+ namespace :level_2 do
20
+ task :level_3 do
21
+ puts "Level 3".to_json
22
+ end
23
+ end
24
+ end
25
+
26
+ namespace :errors do
27
+ desc "Returns an error"
28
+ task :return do
29
+ $stderr.puts "Error".to_json
30
+ exit(-1)
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ $:.unshift File.join(File.dirname(File.expand_path(__FILE__)), '..', 'lib')
2
+ require 'rubygems'
3
+ require 'silk'
4
+
5
+ require 'redgreen'
6
+ require 'test/unit'
7
+ require 'mocha'
8
+ require 'shoulda'
9
+ require 'rack/test'
10
+
11
+ module TestHelper
12
+
13
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ class TestSilk < Test::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ Silk::Server
8
+ end
9
+
10
+ context '' do
11
+ setup do
12
+ Silk.options = { :filter_paths => File.join(File.dirname(File.expand_path(__FILE__)), 'rakefiles') }
13
+ end
14
+
15
+ should "return a 404 if the rake task is not found" do
16
+ get '/'
17
+ assert "application/json", last_response.headers['Content-Type']
18
+ assert last_response.not_found?
19
+ end
20
+
21
+ should "return a success if the rake task exists and is successfully run" do
22
+ get '/level_1'
23
+ assert last_response.ok?
24
+ assert "application/json", last_response.headers['Content-Type']
25
+ assert_equal "0", last_response.headers['X_PROCESS_EXIT_STATUS']
26
+ assert_equal "Level 1".to_json, last_response.body.to_s
27
+ end
28
+
29
+ should "return a success if the rake task exists at a second level and is successfully run" do
30
+ get '/level_1/level_2'
31
+ assert last_response.ok?
32
+ assert "application/json", last_response.headers['Content-Type']
33
+ assert_equal "0", last_response.headers['X_PROCESS_EXIT_STATUS']
34
+ assert_equal "Level 2".to_json, last_response.body
35
+ end
36
+
37
+ should "return a success if the rake task exists an process arguments if present" do
38
+ get '/level_1/level_2_with_args?argument_1=1&argument_2=2'
39
+ assert last_response.ok?
40
+ assert "application/json", last_response.headers['Content-Type']
41
+ assert_equal "0", last_response.headers['X_PROCESS_EXIT_STATUS']
42
+ assert_equal ("Level 2: " + { "argument_1" => "1", "argument_2" => "2" }.inspect).to_json, last_response.body
43
+ end
44
+
45
+ should "return read stderr, and set the exit status, and return 500 if the rake task exists, but returns a non-zero exit code" do
46
+ get '/errors/return'
47
+ assert_equal 500, last_response.status
48
+ assert "application/json", last_response.headers['Content-Type']
49
+ assert_equal "255", last_response.headers['X_PROCESS_EXIT_STATUS']
50
+ assert_equal "Error".to_json, last_response.body
51
+ end
52
+ end
53
+ end
@@ -1,7 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestSilk < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
4
+ context '' do
5
+
6
6
  end
7
7
  end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ class TestSilk < Test::Unit::TestCase
4
+ context '' do
5
+ setup do
6
+ Silk.options = { :filter_paths => File.join(File.dirname(File.expand_path(__FILE__)), 'rakefiles') }
7
+ @task = Silk::Tasks.new
8
+ end
9
+
10
+ should "return a list of all the available rake tasks" do
11
+ assert @task.list.include?("level_1")
12
+ assert @task.list.include?("level_1:level_2")
13
+ assert @task.list.include?("level_1:level_2_with_args")
14
+ assert @task.list.include?("level_1:level_2:level_3")
15
+ assert @task.list.include?("errors:return")
16
+ end
17
+
18
+ should "run a task if it exists" do
19
+ stdout_read, stdout_write = IO.pipe
20
+ stderr_read, stderr_write = IO.pipe
21
+ pid = Process.fork do
22
+ $stdout.reopen stdout_write
23
+ $stderr.reopen stderr_write
24
+ stdout_read.close
25
+ stderr_read.close
26
+ @task.run("level_1")
27
+ end
28
+
29
+ stdout_write.close
30
+ stderr_write.close
31
+
32
+ stdout = ''
33
+ stderr = ''
34
+ stdout_read.each do |line|
35
+ stdout += line
36
+ end
37
+ stderr_read.each do |line|
38
+ stderr += line
39
+ end
40
+ Process.waitpid(pid)
41
+
42
+ assert_equal "Level_1".to_json, stdout.strip
43
+ assert_equal "".to_json, stderr.strip
44
+ end
45
+
46
+ should "run a task and pass in arrguments if it exists" do
47
+ # I'm guessing this test fails because STDOUT and STDERR are already being captured
48
+ stdout_read, stdout_write = IO.pipe
49
+ stderr_read, stderr_write = IO.pipe
50
+ pid = Process.fork do
51
+ $stdout.reopen stdout_write
52
+ $stderr.reopen stderr_write
53
+ stdout_read.close
54
+ stderr_read.close
55
+ @task.run("level_2_with_args", { 'param_1' => '1' })
56
+ end
57
+
58
+ stdout_write.close
59
+ stderr_write.close
60
+
61
+ stdout = ''
62
+ stderr = ''
63
+ stdout_read.each do |line|
64
+ stdout += line
65
+ end
66
+ stderr_read.each do |line|
67
+ stderr += line
68
+ end
69
+ Process.waitpid(pid)
70
+
71
+ assert_equal(("Level 2: " + { 'param_1' => '1' }.inspect).to_json, stdout.strip)
72
+ assert_equal "".to_json, stderr.strip
73
+ end
74
+
75
+ should "run the task only once if a new Application is built"
76
+ end
77
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: silk
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
  - Myles Eftos
@@ -22,6 +22,26 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: sinatra
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
25
45
  - !ruby/object:Gem::Dependency
26
46
  name: SyslogLogger
27
47
  type: :runtime
@@ -42,6 +62,36 @@ dependencies:
42
62
  - !ruby/object:Gem::Version
43
63
  version: "0"
44
64
  version:
65
+ - !ruby/object:Gem::Dependency
66
+ name: rack-test
67
+ type: :development
68
+ version_requirement:
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ - !ruby/object:Gem::Dependency
76
+ name: redgreen
77
+ type: :development
78
+ version_requirement:
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ - !ruby/object:Gem::Dependency
86
+ name: mocha
87
+ type: :development
88
+ version_requirement:
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
45
95
  description: It allows you to write rake tasks to do common tasks, such as creating email addresses, adding users etc. Silk provides a HTTP wrapper the the rake tasks, and allows communication via JSON objects, which makes it dead easy for them to be called from a web app.
46
96
  email: myles@madpilot.com.au
47
97
  executables:
@@ -60,12 +110,15 @@ files:
60
110
  - VERSION
61
111
  - bin/silk
62
112
  - lib/silk.rb
63
- - lib/silk/http.rb
64
113
  - lib/silk/options.rb
65
114
  - lib/silk/server.rb
66
115
  - lib/silk/tasks.rb
67
116
  - test/helper.rb
117
+ - test/rakefiles/test.rake
118
+ - test/test_helper.rb
119
+ - test/test_server.rb
68
120
  - test/test_silk.rb
121
+ - test/test_tasks.rb
69
122
  has_rdoc: true
70
123
  homepage: http://github.com/madpilot/silk
71
124
  licenses: []
@@ -95,5 +148,8 @@ signing_key:
95
148
  specification_version: 3
96
149
  summary: A framework for creating a hosting console
97
150
  test_files:
151
+ - test/test_server.rb
98
152
  - test/test_silk.rb
153
+ - test/test_tasks.rb
154
+ - test/test_helper.rb
99
155
  - test/helper.rb
@@ -1,2 +0,0 @@
1
- require 'sinatra'
2
- require 'json'