joekhoobyar-capistrano-extensions 0.0.3 → 0.0.5

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.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 3
2
+ :patch: 5
3
3
  :major: 0
4
4
  :minor: 0
@@ -1,38 +1,40 @@
1
- require File.join(File.dirname(__FILE__), %w(files local.rb))
2
- require File.join(File.dirname(__FILE__), %w(files remote.rb))
1
+ module CapistranoExtensions
2
+ module Files
3
3
 
4
- module CapistranoExtensions::Files
4
+ COMMANDS = [ %w(mkdir mkdir_p rmdir cp cp_r rm rm_r rm_rf
5
+ chmod chmod_R chown chown_R touch),
6
+ %w(ln ln_s ln_sf mv install) ]
5
7
 
6
- COMMANDS = [ %w(mkdir mkdir_p rmdir cp cp_r rm rm_r rm_rf
7
- chmod chmod_R chown chown_R touch),
8
- %w(ln ln_s ln_sf mv install) ]
8
+ FILE_TESTS = [
9
+ %w(blockdev? -b),
10
+ %w(chardev? -c),
11
+ %w(directory? -d),
12
+ %w(exists? -e),
13
+ %w(file? -f),
14
+ %w(grpowned? -G),
15
+ %w(owned? -O),
16
+ %w(pipe? -p),
17
+ %w(readable? -r),
18
+ %w(setgid? -g),
19
+ %w(setuid? -u),
20
+ %w(size? -s),
21
+ %w(socket? -S),
22
+ %w(sticky? -k),
23
+ %w(symlink? -h),
24
+ %w(writable? -w),
25
+ %w(executable? -x)
26
+ ]
9
27
 
10
- FILE_TESTS = [
11
- %w(blockdev? -b),
12
- %w(chardev? -c),
13
- %w(directory? -d),
14
- %w(exists? -e),
15
- %w(file? -f),
16
- %w(grpowned? -G),
17
- %w(owned? -O),
18
- %w(pipe? -p),
19
- %w(readable? -r),
20
- %w(setgid? -g),
21
- %w(setuid? -u),
22
- %w(size? -s),
23
- %w(socket? -S),
24
- %w(sticky? -k),
25
- %w(symlink? -h),
26
- %w(writable? -w),
27
- %w(executable? -x)
28
- ]
28
+ require File.join(File.dirname(__FILE__), %w(files local.rb))
29
+ require File.join(File.dirname(__FILE__), %w(files remote.rb))
29
30
 
30
- class_eval(Local.public_instance_methods(false).map do |m|
31
- "def #{m}(*f) send(_via.to_s + '_files').#{m}(*f) end"
32
- end.join("\n"))
31
+ class_eval(Local.public_instance_methods(false).map do |m|
32
+ "def #{m}(*args, &block)\n send(_via.to_s + '_files').#{m}(*args, &block)\nend"
33
+ end.join("\n"), __FILE__, __LINE__)
33
34
 
34
- def _via
35
- @config.fetch(:files_via, :remote).to_sym != :local ? :remote : :local
35
+ def _via
36
+ @config.fetch(:files_via, :remote).to_sym != :local ? :remote : :local
37
+ end
36
38
  end
37
39
  end
38
40
 
@@ -6,11 +6,11 @@ module CapistranoExtensions
6
6
 
7
7
  include FileUtils::Verbose
8
8
 
9
- public *COMMANDS
9
+ public *COMMANDS.flatten
10
10
  public :pwd
11
11
 
12
12
  FILE_TESTS.each do |m,t|
13
- class_eval <<-EODEF
13
+ class_eval <<-EODEF, __FILE__, __LINE__
14
14
  def #{m}(a, options={})
15
15
  logger.trace "test #{t} \#{a.gsub ' ', '\\ '}" if logger
16
16
  File.#{m} a
@@ -9,11 +9,11 @@ module CapistranoExtensions
9
9
  %w(ln ln_s ln_sf mv install) ]
10
10
 
11
11
  COMMANDS.each_with_index do |l,n|
12
- l.each do |k|
13
- k, f = k.split('_')
12
+ l.each do |m|
13
+ k, f = m.split('_')
14
14
  f = ' -' + f if f
15
- class_eval <<-EODEF
16
- def #{k}(a, options={})
15
+ class_eval <<-EODEF, __FILE__, __LINE__
16
+ def #{m}(*args)
17
17
  options = args.pop if Hash === args.last
18
18
  _r '#{k}#{f}', args#{', ' + (n+1).to_s if n > 0}
19
19
  end
@@ -23,7 +23,7 @@ module CapistranoExtensions
23
23
 
24
24
  def tail_f(file, n=10)
25
25
  cmd = "tail -n #{n} -f #{_q file}"
26
- _via == :system ? system(cmd) : stream(cmd, :via => _via)
26
+ _via == :system ? local_run(cmd) : stream(cmd, :via => _via)
27
27
  rescue Interrupt
28
28
  logger.trace "interrupted (Ctrl-C)" if logger
29
29
  end
@@ -62,7 +62,7 @@ module CapistranoExtensions
62
62
  def _t(cmd, args=nil, min=nil)
63
63
  cmd = _a cmd, args, min
64
64
  if _via == :system then
65
- system(cmd)
65
+ local_run(cmd)
66
66
  else
67
67
  capture("#{cmd}; echo $?", :via => _via).strip == '0'
68
68
  end
@@ -71,10 +71,9 @@ module CapistranoExtensions
71
71
  def _r(cmd, args=nil, min=nil)
72
72
  cmd = _a cmd, args, min
73
73
  if _via != :system then
74
- invoke_command(cmd, :via => _via)
74
+ invoke_command cmd, :via => _via
75
75
  else
76
- $stdout.puts cmd
77
- system cmd
76
+ local_run cmd
78
77
  end
79
78
  end
80
79
 
@@ -1,5 +1,13 @@
1
- module CapistranoExtension
1
+ module CapistranoExtensions
2
+
2
3
  module Invocation
4
+ def local_run(*args, &block)
5
+ args.pop if Hash===args.last
6
+ args = args.first
7
+ logger.debug "executing locally: #{args}"
8
+ system args
9
+ end
10
+
3
11
  def sudo_as(*args, &block)
4
12
  options = Hash===args.last ? args.pop.dup : {}
5
13
  options[:as] = fetch(:runner, nil)
@@ -17,10 +25,9 @@ module CapistranoExtension
17
25
  options[:shell] = false
18
26
  cmd = args[0].gsub(/[$\\`"]/) { |m| "\\#{m}" }
19
27
  args[0] = "echo \"#{cmd}\" | #{sudo} su - #{fetch(:runner, nil)}"
20
- #args[0] = "echo \"#{.gsub('"', '\\"')}\" | #{sudo} su - #{fetch(:runner, nil)}"
21
28
  run *args.push(options), &block
22
29
  end
23
30
  end
31
+
24
32
  Capistrano::Configuration.send :include, Invocation
25
33
  end
26
-
@@ -1,109 +1,21 @@
1
1
  module CapistranoExtensions
2
- module Service
3
-
4
- LSB_DEFAULT_ACTIONS = %w(status start stop restart)
5
-
6
- CRM_DEFAULT_ACTIONS = [ [ :status, '-W' ],
7
- [ :summary, "-x | awk '/^raw xml:/ { exit }; { print }'" ],
8
- [ :start, "--meta -d 'target_role'" ],
9
- [ :stop, "--meta -p 'target_role' -v 'stopped'" ] ]
10
2
 
11
- CRM_OCF_DEFAULT_ACTIONS = [ [ :validate, '-c -C' ],
12
- [ :monitor, '-c -m' ] ]
3
+ module Service
13
4
 
14
5
  SVC_ACTION_CAPTIONS = Hash.new do |h,k|
15
6
  h[k] = "#{k.to_s.capitalize} Service"
16
- end
17
- SVC_ACTION_CAPTIONS.update :status => 'Check Status', :check => 'Check Config', :summary => 'Status Summary'
7
+ end.update :status => 'Check Status',
8
+ :check => 'Check Config',
9
+ :summary => 'Status Summary'
18
10
 
19
- def crm(id,*args)
20
- svc_desc = next_description(:reset)
21
- svc_cmd = "/usr/sbin/crm_resource -r #{id.to_s.split(':').last}"
22
- svc_actions = CRM_DEFAULT_ACTIONS
23
-
24
- if Hash === args.last
25
- options = args.pop
26
- svc_desc = id.to_s.capitalize unless svc_desc or options.delete(:hide)
27
- svc_actions += args.shift if Array === args.first
28
- else
29
- options = {}
30
- end
31
-
32
- namespace id do
33
- desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:status]}" if svc_desc
34
- task :default, options do
35
- sudo "#{svc_cmd} -W"
36
- end
37
-
38
- svc_actions.each do |svc_action,svc_args|
39
- svc_action = svc_action.intern unless Symbol===svc_action
40
- desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[svc_action]}" if svc_desc
41
- task svc_action, options do
42
- sudo "#{svc_cmd} #{svc_args}"
43
- end
44
- end
45
-
46
- instance_eval { yield } if block_given?
47
- end
11
+ %w(lsb crm windows command).each do |k|
12
+ require File.join(File.dirname(__FILE__), 'service', k+'.rb')
48
13
  end
49
14
 
50
- def crm_ocf(id,*args)
51
- svc_desc = next_description(:reset)
52
- svc_cmd = "ocf_resource -g #{id.to_s.split(':').last}"
53
- svc_actions = CRM_OCF_DEFAULT_ACTIONS
54
-
55
- if Hash === args.last
56
- options = args.pop
57
- svc_desc = id.to_s.capitalize unless svc_desc or options.delete(:hide)
58
- svc_actions += args.shift if Array === args.first
59
- else
60
- options = {}
61
- end
15
+ include LSB, CRM, Windows, Command
62
16
 
63
- namespace id do
64
- svc_actions.each do |svc_action,svc_args|
65
- svc_action = svc_action.intern unless Symbol===svc_action
66
- desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[svc_action]}" if svc_desc
67
- task svc_action, options do
68
- sudo "#{svc_cmd} #{svc_args}"
69
- end
70
- end
71
-
72
- instance_eval { yield } if block_given?
73
- end
74
- end
75
-
76
- def lsb(id,*args)
77
- svc_desc = next_description(:reset)
78
- svc_cmd = "/etc/init.d/#{id.to_s.split(':').last}"
79
- svc_actions = LSB_DEFAULT_ACTIONS
80
-
81
- if Hash === args.last
82
- options = args.pop
83
- svc_desc = id.to_s.capitalize unless svc_desc or options.delete(:hide)
84
- svc_actions += args.shift if Array === args.first
85
- else
86
- options = {}
87
- end
88
-
89
- namespace id do
90
- desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:status]}" if svc_desc
91
- task :default, options do
92
- sudo "#{svc_cmd} status"
93
- end
94
-
95
- svc_actions.each do |svc_action|
96
- svc_action = svc_action.intern
97
- desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[svc_action]}" if svc_desc
98
- task svc_action, options do
99
- sudo "#{svc_cmd} #{svc_action}"
100
- end
101
- end
102
-
103
- instance_eval { yield } if block_given?
104
- end
105
- end
106
17
  end
18
+
107
19
  end
108
20
 
109
21
  Capistrano.plugin :service, CapistranoExtensions::Service
@@ -0,0 +1,44 @@
1
+ module CapistranoExtensions
2
+ module Service
3
+ module Command
4
+
5
+ # Check for the existance of a generic Windows NT service.
6
+ def command?(start, stop)
7
+ files.executable? start and files.executable? stop
8
+ end
9
+
10
+ # Defines a recipe to control a generic Windows NT service.
11
+ #
12
+ def command(id,start,stop,*args)
13
+ options = Hash===args.last ? args.pop : {}
14
+
15
+ svc_name = id.to_s
16
+ svc_desc = next_description(:reset) || (svc_name.capitalize unless options.delete(:hide))
17
+ extras = args.pop if Array === args.last
18
+ via = options.delete(:via)
19
+
20
+ namespace id do
21
+
22
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:start]}" if svc_desc
23
+ task :start, options do
24
+ send(via || fetch(:run_method, :local_run), start)
25
+ end
26
+
27
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:stop]}" if svc_desc
28
+ task :stop, options do
29
+ send(via || fetch(:run_method, :local_run), stop)
30
+ end
31
+
32
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:restart]}" if svc_desc
33
+ task :restart, options do
34
+ stop
35
+ start
36
+ end
37
+
38
+ instance_eval { yield } if block_given?
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,84 @@
1
+ module CapistranoExtensions
2
+ module Service
3
+ module CRM
4
+
5
+ DEFAULT_ACTIONS = [ [ :status, '-W' ],
6
+ [ :summary, "-x | awk '/^raw xml:/ { exit }; { print }'" ],
7
+ [ :start, "--meta -d 'target_role'" ],
8
+ [ :stop, "--meta -p 'target_role' -v 'stopped'" ] ]
9
+
10
+ OCF_DEFAULT_ACTIONS = [ [ :validate, '-c -C' ],
11
+ [ :monitor, '-c -m' ] ]
12
+
13
+ # Defines a recipe to control a cluster-managed service, using heartbeat or pacemaker.
14
+ #
15
+ def crm(id,*args)
16
+ options = Hash===args.last ? args.pop : {}
17
+
18
+ svc_name = id.to_s
19
+ svc_desc = next_description(:reset) || (svc_name.capitalize unless options.delete(:hide))
20
+ svc_actions = DEFAULT_ACTIONS
21
+ svc_actions += args.pop if Array === args.last
22
+
23
+ namespace id do
24
+
25
+ case args.first
26
+ when String; id = args.shift.intern
27
+ when Symbol; id = args.shift
28
+ end
29
+ svc_cmd = "/usr/sbin/crm_resource -r #{id.to_s.split(':').last}"
30
+
31
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:status]}" if svc_desc
32
+ task :default, options do
33
+ sudo "#{svc_cmd} -W"
34
+ end
35
+
36
+ svc_actions.each do |svc_action,svc_args|
37
+ svc_action = svc_action.intern unless Symbol===svc_action
38
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[svc_action]}" if svc_desc
39
+ task svc_action, options do
40
+ sudo "#{svc_cmd} #{svc_args}"
41
+ end
42
+ end
43
+
44
+ instance_eval { yield } if block_given?
45
+ end
46
+ end
47
+
48
+ # Defines a recipe providing additional controls for a cluster-managed service,
49
+ # using the ocf_resource tool which interoperates with heartbeat or pacemaker.
50
+ #
51
+ # For more information about ocf_resource and other add-ons for heartbeat/pacemaker,
52
+ # see http://github.com/joekhoobyar/ha-tools
53
+ #
54
+ def crm_ocf(id,*args)
55
+ options = Hash===args.last ? args.pop : {}
56
+
57
+ svc_name = id.to_s
58
+ svc_desc = next_description(:reset) || (svc_name.capitalize unless options.delete(:hide))
59
+ svc_actions = DEFAULT_ACTIONS
60
+ svc_actions += args.pop if Array === args.last
61
+
62
+ namespace id do
63
+
64
+ case args.first
65
+ when String; id = args.shift.intern
66
+ when Symbol; id = args.shift
67
+ end
68
+ svc_cmd = "ocf_resource -g #{id.to_s.split(':').last}"
69
+
70
+ svc_actions.each do |svc_action,svc_args|
71
+ svc_action = svc_action.intern if String === svc_action
72
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[svc_action]}" if svc_desc
73
+ task svc_action, options do
74
+ sudo "#{svc_cmd} #{svc_args}"
75
+ end
76
+ end
77
+
78
+ instance_eval { yield } if block_given?
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
@@ -0,0 +1,47 @@
1
+ module CapistranoExtensions
2
+ module Service
3
+ module LSB
4
+
5
+ DEFAULT_ACTIONS = %w(status start stop restart)
6
+
7
+ # Check for the existance of a generic Windows NT service.
8
+ def lsb?(id)
9
+ files.exists? "/etc/init.d/#{id.to_s.split(':').last}"
10
+ end
11
+
12
+ # Defines a recipe to control a generic LSB service.
13
+ #
14
+ def lsb(id,*args)
15
+ options = Hash===args.last ? args.pop : {}
16
+
17
+ svc_name = id.to_s
18
+ svc_desc = next_description(:reset) || (svc_name.capitalize unless options.delete(:hide))
19
+ svc_actions = DEFAULT_ACTIONS
20
+ svc_actions += args.pop if Array === args.last
21
+
22
+ namespace id do
23
+ case args.first
24
+ when String; id = args.shift.intern
25
+ when Symbol; id = args.shift
26
+ end
27
+ svc_cmd = "/etc/init.d/#{id.to_s.split(':').last}"
28
+
29
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:status]}" if svc_desc
30
+ task :default, options do
31
+ sudo "#{svc_cmd} status"
32
+ end
33
+
34
+ svc_actions.each do |svc_action|
35
+ svc_action = svc_action.intern if String === svc_action
36
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[svc_action]}" if svc_desc
37
+ task svc_action, options do
38
+ sudo "#{svc_cmd} #{svc_action}"
39
+ end
40
+ end
41
+
42
+ instance_eval { yield } if block_given?
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,64 @@
1
+ module CapistranoExtensions
2
+ module Service
3
+ module Windows
4
+
5
+ DEFAULT_ACTIONS = [:start, :stop]
6
+
7
+ STATUS_REGEX = /STATE +: +([0-9])+ +([^ ]+)/
8
+
9
+ # Check for the existance of a generic Windows NT service.
10
+ def windows?(id)
11
+ `sc query "#{id}"` !~ / FAILED /
12
+ end
13
+
14
+ # Defines a recipe to control a generic Windows NT service.
15
+ #
16
+ def windows(id,*args)
17
+ options = Hash===args.last ? args.pop : {}
18
+
19
+ svc_name = id.to_s
20
+ svc_desc = next_description(:reset) || (svc_name.capitalize unless options.delete(:hide))
21
+ svc_actions = DEFAULT_ACTIONS
22
+ svc_actions += args.pop if Array === args.last
23
+
24
+ namespace id do
25
+ case args.first
26
+ when String; id = args.shift.intern
27
+ when Symbol; id = args.shift
28
+ end
29
+
30
+ [:default, :status].each do |k|
31
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:status]}" if svc_desc
32
+ task k, options do
33
+ output = `sc query "#{id}"`
34
+ if output =~ STATUS_REGEX
35
+ logger.trace "Service status: #{svc_name}: #{$2} (#{$1})" if logger
36
+ else
37
+ logger.error output if logger
38
+ abort "Failed to get service status for #{svc_name}"
39
+ end
40
+ end
41
+ end
42
+
43
+ DEFAULT_ACTIONS.each do |svc_action|
44
+ svc_action = svc_action.intern if String === svc_action
45
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[svc_action]}" if svc_desc
46
+ task svc_action, options do
47
+ system "net #{svc_action} \"#{id}\""
48
+ end
49
+ end
50
+
51
+ desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:restart]}" if svc_desc
52
+ task :restart, options do
53
+ `sc query "#{id}"` =~ STATUS_REGEX
54
+ $1 == '4' or stop
55
+ start
56
+ end
57
+
58
+ instance_eval { yield } if block_given?
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
@@ -0,0 +1,11 @@
1
+ module CapistranoExtensions
2
+ module LocalSys
3
+
4
+ def windows?
5
+ @windows ||= !! (RUBY_PLATFORM =~ /cygwin|mingw|win32|mswin|windows/)
6
+ end
7
+
8
+ end
9
+
10
+ Capistrano.plugin :local_sys, LocalSys
11
+ end
@@ -3,6 +3,7 @@ unless Capistrano::Configuration.respond_to?(:instance)
3
3
  end
4
4
  require 'capistrano'
5
5
 
6
- require "#{File.dirname(__FILE__)}/capistrano_extensions/invocation.rb"
7
- require "#{File.dirname(__FILE__)}/capistrano_extensions/files.rb"
8
- require "#{File.dirname(__FILE__)}/capistrano_extensions/service.rb"
6
+ require File.join(File.dirname(__FILE__), %w(capistrano_extensions sys))
7
+ require File.join(File.dirname(__FILE__), %w(capistrano_extensions invocation))
8
+ require File.join(File.dirname(__FILE__), %w(capistrano_extensions files))
9
+ require File.join(File.dirname(__FILE__), %w(capistrano_extensions service))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joekhoobyar-capistrano-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Khoobyar
@@ -38,6 +38,11 @@ files:
38
38
  - lib/capistrano_extensions/files/remote.rb
39
39
  - lib/capistrano_extensions/invocation.rb
40
40
  - lib/capistrano_extensions/service.rb
41
+ - lib/capistrano_extensions/service/command.rb
42
+ - lib/capistrano_extensions/service/crm.rb
43
+ - lib/capistrano_extensions/service/lsb.rb
44
+ - lib/capistrano_extensions/service/windows.rb
45
+ - lib/capistrano_extensions/sys.rb
41
46
  - lib/jk_capistrano_extensions.rb
42
47
  - README
43
48
  has_rdoc: true