hoof 0.0.2 → 0.0.3

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{hoof}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["pyromaniac"]
12
- s.date = %q{2011-05-15}
12
+ s.date = %q{2011-05-16}
13
13
  s.default_executable = %q{hoof}
14
14
  s.description = %q{Hoof is linux variant of pow. It's based on nss, eventmachine and unicorn}
15
15
  s.email = %q{kinwizard@gmail.com}
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/hoof/application.rb",
36
36
  "lib/hoof/application_pool.rb",
37
37
  "lib/hoof/cli.rb",
38
+ "lib/hoof/control_server.rb",
38
39
  "lib/hoof/http_server.rb",
39
40
  "lib/hoof/unicorn_config.rb",
40
41
  "test/helper.rb",
@@ -3,17 +3,22 @@ require 'unicorn/launcher'
3
3
  require 'http/parser'
4
4
 
5
5
  require 'hoof/http_server'
6
+ require 'hoof/control_server'
6
7
  require 'hoof/application'
7
8
  require 'hoof/application_pool'
8
9
 
9
10
  module Hoof
10
11
 
11
12
  def self.pool
12
- @pool ||= Hoof::ApplicationPool.new
13
+ @pool ||= begin
14
+ app_pool = Hoof::ApplicationPool.new
15
+ app_pool.reload
16
+ app_pool
17
+ end
13
18
  end
14
19
 
15
- def self.application name
16
- pool.add name
20
+ def self.find name
21
+ pool[name]
17
22
  end
18
23
 
19
24
  def self.start
@@ -23,12 +28,17 @@ module Hoof
23
28
  trap("INT") { stop }
24
29
 
25
30
  EventMachine::start_server "127.0.0.1", 3001, Hoof::HttpServer
31
+ EventMachine::start_server sock, Hoof::ControlServer
26
32
  end
27
33
  end
28
34
 
29
35
  def self.stop
30
- EventMachine.stop
31
36
  pool.stop
37
+ EventMachine.stop
38
+ end
39
+
40
+ def self.sock
41
+ '/tmp/hoof.sock'
32
42
  end
33
43
 
34
44
  end
@@ -5,18 +5,13 @@ module Hoof
5
5
  def initialize name
6
6
  @name = name
7
7
  @root = File.readlink(File.expand_path(File.join("~/.hoof/", name)))
8
-
9
- load_rvm
10
- load_bundler
11
- start
12
8
  end
13
9
 
14
10
  def start
15
- system "cd #{root} && bundle exec unicorn_rails -c #{File.join(File.dirname(__FILE__), 'unicorn_config.rb')} -l #{sock} -D"
16
- end
17
-
18
- def stop
19
- Process.kill "TERM", pid if File.exists? pid_file
11
+ unless running?
12
+ load_rvm
13
+ system "cd #{root} && bundle exec unicorn_rails -c #{File.join(File.dirname(__FILE__), 'unicorn_config.rb')} -l #{sock} -D"
14
+ end
20
15
  end
21
16
 
22
17
  def load_rvm
@@ -30,12 +25,15 @@ module Hoof
30
25
  end
31
26
  end
32
27
 
33
- def load_bundler
34
- #ENV['BUNDLE_GEMFILE'] = File.join(root, 'Gemfile')
35
- #require 'bundler/setup'
28
+ def running?
29
+ Daemons::Pid.running? pid
30
+ end
31
+
32
+ def stop
33
+ Process.kill 'TERM', pid if running?
36
34
  end
37
35
 
38
- def static? path
36
+ def static_file? path
39
37
  File.file? File.join(root, 'public', path)
40
38
  end
41
39
 
@@ -51,7 +49,7 @@ module Hoof
51
49
  end
52
50
 
53
51
  def sock
54
- @sock ||= "/tmp/hoof_#{name}.sock"
52
+ @sock ||= File.join(root, 'tmp/sockets/unicorn.sock')
55
53
  end
56
54
 
57
55
  def pid_file
@@ -59,7 +57,7 @@ module Hoof
59
57
  end
60
58
 
61
59
  def pid
62
- File.read(pid_file).to_i
60
+ File.read(pid_file).to_i if File.exists? pid_file
63
61
  end
64
62
 
65
63
  end
@@ -1,12 +1,11 @@
1
1
  module Hoof
2
2
  class ApplicationPool < Hash
3
3
 
4
- def add name
5
- self[name] ||= Hoof::Application.new name
6
- end
7
-
8
- def list
9
- keys
4
+ def reload
5
+ Dir[File.expand_path('~/.hoof/*')].each do |dir|
6
+ name = File.basename dir
7
+ self[name] = Hoof::Application.new name if File.symlink? dir
8
+ end
10
9
  end
11
10
 
12
11
  def stop
@@ -8,17 +8,22 @@ module Hoof
8
8
 
9
9
  default_task :start
10
10
 
11
- desc 'hoof start', 'Starts hoof daemon'
11
+ desc 'start', 'Starts hoof daemon'
12
12
  def start
13
13
  daemon 'start'
14
14
  end
15
15
 
16
- desc 'hoof stop', 'Stops hoof daemon'
16
+ desc 'stop', 'Stops hoof daemon'
17
17
  def stop
18
18
  daemon 'stop'
19
19
  end
20
20
 
21
- desc 'hoof init [NAME]', 'Initializes hoof for app in current directory'
21
+ desc 'restart', 'Restarts hoof daemon'
22
+ def restart
23
+ daemon 'restart'
24
+ end
25
+
26
+ desc 'init [NAME]', 'Initializes hoof for app in current directory'
22
27
  long_desc <<-D
23
28
  Initializes hoof for current directory application.
24
29
  This task creates symlink in ~/.hoof and adds unicorn to
@@ -31,9 +36,10 @@ module Hoof
31
36
  append_to_file 'Gemfile', "\ngem 'unicorn'"
32
37
  end
33
38
 
34
- #desc "list", "Lists started applications"
35
- #def list
36
- #end
39
+ desc "status", "Lists hoof applications"
40
+ def status
41
+ control 'status'
42
+ end
37
43
 
38
44
  private
39
45
 
@@ -43,5 +49,16 @@ module Hoof
43
49
  end
44
50
  end
45
51
 
52
+ def control comand
53
+ begin
54
+ UNIXSocket.open(Hoof.sock) do |s|
55
+ s.write comand
56
+ puts s.read
57
+ end
58
+ rescue Errno::ECONNREFUSED
59
+ puts 'Hoof is not running'
60
+ end
61
+ end
62
+
46
63
  end
47
64
  end
@@ -0,0 +1,19 @@
1
+ module Hoof
2
+ class ControlServer < EventMachine::Connection
3
+
4
+ def receive_data data
5
+ p "comand #{data}"
6
+ result = send data
7
+ send_data result
8
+ close_connection_after_writing
9
+ end
10
+
11
+ def status
12
+ Hoof.pool.map do |(name, app)|
13
+ status = app.running? ? "[\033[1;32mrunning\033[0m]" : "[\033[1;31mstopped\033[0m]"
14
+ " #{app.pid if app.running?}\t#{name}\t\t\t#{status}"
15
+ end.join("\n")
16
+ end
17
+
18
+ end
19
+ end
@@ -1,34 +1,39 @@
1
1
  module Hoof
2
2
  class HttpServer < EventMachine::Connection
3
- attr_accessor :applications
4
-
5
- def initialize *args
6
- super
7
- @applications = {}
8
- end
9
3
 
10
4
  def receive_data data
11
5
  parser = Http::Parser.new
12
6
  parser.parse data
13
-
14
7
  host = parser.headers["HOST"].gsub(/:\d+$/, '')
15
- if host =~ /.dev$/
16
- name = host.gsub(/.dev$/, '')
17
- application = Hoof.application name
18
8
 
19
- if application && application.static?(parser.path.split('?', 2)[0])
20
- p "serve static #{host}#{parser.path}"
21
- send_data application.serve_static parser.path.split('?', 2)[0]
9
+ close_connection and return unless host =~ /.dev$/
10
+
11
+ name = host.gsub(/.dev$/, '')
12
+ path = parser.path.split('?', 2)[0]
13
+
14
+ application = Hoof.find name
15
+
16
+ if application
17
+ if application.static_file? path
18
+ puts "Serve static #{host}#{parser.path}"
19
+ send_data application.serve_static(path)
22
20
  close_connection_after_writing
23
21
  else
24
- p "serve #{host}#{parser.path}"
25
- EventMachine.defer(proc {
26
- application.serve(data)
27
- }, proc { |result|
28
- send_data result
29
- close_connection_after_writing
30
- })
22
+ begin
23
+ application.start
24
+ puts "Serve #{host}#{parser.path}"
25
+ EventMachine.defer(proc {
26
+ application.serve data
27
+ }, proc { |result|
28
+ send_data result
29
+ close_connection_after_writing
30
+ })
31
+ rescue
32
+ puts "Failed to serve #{name}"
33
+ end
31
34
  end
35
+ else
36
+ close_connection
32
37
  end
33
38
  end
34
39
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoof
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - pyromaniac
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-15 00:00:00 +04:00
18
+ date: 2011-05-16 00:00:00 +04:00
19
19
  default_executable: hoof
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -174,6 +174,7 @@ files:
174
174
  - lib/hoof/application.rb
175
175
  - lib/hoof/application_pool.rb
176
176
  - lib/hoof/cli.rb
177
+ - lib/hoof/control_server.rb
177
178
  - lib/hoof/http_server.rb
178
179
  - lib/hoof/unicorn_config.rb
179
180
  - test/helper.rb