zfben_rails_rake 0.0.10 → 0.0.11

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.
@@ -0,0 +1,94 @@
1
+ desc 'do remote job'
2
+ task :remote, [:job] do |task, args|
3
+ args = args.to_hash
4
+ err 'No job found' unless args.has_key? :job
5
+ job = File.join Rails.root, args[:key] + '.rb'
6
+ err args[:job] + '.rb is not exists!' unless File.exists? release
7
+
8
+ require 'net/ssh'
9
+ @config = {}
10
+
11
+ def ssh user_host, *options
12
+ user_host = user_host.split '@'
13
+ if user_host.length != 2
14
+ err 'ssh error, you should set ssh like `username@host`'
15
+ else
16
+ @config[:ssh] = {
17
+ :user => user_host[0],
18
+ :host => user_host[1],
19
+ :options => options[0]
20
+ }
21
+ end
22
+ end
23
+
24
+ def remote_path path
25
+ @config[:remote_path] = path
26
+ end
27
+
28
+ def before_connect &block
29
+ if block
30
+ @cmd_list = []
31
+ block.call
32
+ if @cmd_list.length > 0
33
+ @config[:before_connect] = @cmd_list
34
+ end
35
+ end
36
+ end
37
+
38
+ def after_connect &block
39
+ if block
40
+ @cmd_list = []
41
+ block.call
42
+ if @cmd_list.length > 0
43
+ @config[:after_connect] = @cmd_list
44
+ end
45
+ end
46
+ end
47
+
48
+ def rake task
49
+ @cmd_list.push 'rake ' + task
50
+ end
51
+
52
+ def run cmd
53
+ @cmd_list.push 'run ' + cmd
54
+ end
55
+
56
+ load job
57
+
58
+ p @config
59
+
60
+ if @config.has_key?(:before_connect)
61
+ @config[:before_connect].each do |cmd|
62
+ sys cmd
63
+ end
64
+ end
65
+
66
+ Net::SSH.start(@config[:ssh][:host], @config[:ssh][:user]) do |net|
67
+ @net = net
68
+ def exec cmd
69
+ p "#{@config[:ssh][:user]}@#{@config[:ssh][:host]}: " + cmd
70
+ cmd = "cd #{@config[:remote_path]};#{cmd}"
71
+ channel = @net.open_channel do |ch|
72
+ ch.exec cmd do |ch, success|
73
+ err "could not execute command: #{cmd}" unless success
74
+
75
+ ch.on_data do |c, data|
76
+ data.split("\n").each{ |s| p s }
77
+ end
78
+
79
+ ch.on_extended_data do |c, type, data|
80
+ err data
81
+ end
82
+ end
83
+ end
84
+ channel.wait
85
+ end
86
+
87
+ p 'ssh connect success'
88
+ if @config.has_key?(:after_connect)
89
+ @config[:after_connect].each do |cmd|
90
+ exec cmd
91
+ end
92
+ end
93
+ end
94
+ end
@@ -17,13 +17,13 @@ namespace :unicorn do
17
17
  task :restart, [:env, :config] => [:stop, :start]
18
18
 
19
19
  desc 'copy unicorn.rb to root path'
20
- task :copy, [:processes, :port] do |task, args|
20
+ task :copy, [:processes] do |task, args|
21
21
  path = File.join(Rails.root, 'unicorn.rb')
22
22
  if File.exists? path
23
23
  err 'unicorn.rb is exists! Please remove it and run again.'
24
24
  else
25
- args = { :processes => 1, :port => 8080 }.merge args.to_hash
26
- p file = "# Added by zfben_rails_rake\nworker_processes #{args[:processes]}\nlisten #{args[:port]}, :tcp_nopush => true, :tcp_nodelay => true\npid 'tmp/unicorn.pid'\n# End zfben_rails_rake"
25
+ args = { :processes => 1 }.merge args.to_hash
26
+ p file = "# Added by zfben_rails_rake\nworker_processes #{args[:processes]}\nlisten File.realpath(File.dirname(__FILE__)) << '/tmp/unicorn.sock', :tcp_nopush => true, :tcp_nodelay => true\npid 'tmp/unicorn.pid'\npreload_app true\n# End zfben_rails_rake"
27
27
  File.open(path, 'w'){ |f| f.write file }
28
28
  end
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module ZfbenRailsRake
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
@@ -22,4 +22,5 @@ Gem::Specification.new do |s|
22
22
  s.add_dependency 'rainbow'
23
23
  s.add_dependency 'unicorn'
24
24
  s.add_dependency 'request-log-analyzer'
25
+ s.add_dependency 'net-ssh'
25
26
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: zfben_rails_rake
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.10
5
+ version: 0.0.11
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-09 00:00:00 Z
13
+ date: 2011-06-11 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rainbow
@@ -45,6 +45,17 @@ dependencies:
45
45
  version: "0"
46
46
  type: :runtime
47
47
  version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: net-ssh
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id004
48
59
  description: ""
49
60
  email:
50
61
  - ben@zfben.com
@@ -64,6 +75,7 @@ files:
64
75
  - lib/zfben_rails_rake/tasks/git.rb
65
76
  - lib/zfben_rails_rake/tasks/helper.rb
66
77
  - lib/zfben_rails_rake/tasks/log.rb
78
+ - lib/zfben_rails_rake/tasks/remote.rb
67
79
  - lib/zfben_rails_rake/tasks/unicorn.rb
68
80
  - lib/zfben_rails_rake/version.rb
69
81
  - zfben_rails_rake.gemspec