auser-poolparty 1.2.10 → 1.2.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/VERSION.yml +1 -1
  2. data/bin/cloud +1 -1
  3. data/bin/cloud-list +19 -7
  4. data/bin/cloud-provision +3 -0
  5. data/bin/cloud-run +37 -0
  6. data/bin/server-list-active +1 -1
  7. data/config/jeweler.rb +2 -2
  8. data/examples/basic.rb +4 -3
  9. data/examples/fairchild.rb +2 -2
  10. data/examples/metavirt_cloud.rb +18 -8
  11. data/examples/simple.rb +4 -3
  12. data/examples/vmrun_cloud.rb +2 -5
  13. data/lib/poolparty.rb +1 -1
  14. data/lib/poolparty/core/object.rb +6 -4
  15. data/lib/poolparty/core/string.rb +2 -1
  16. data/lib/poolparty/helpers/optioner.rb +1 -1
  17. data/lib/poolparty/modules/callbacks.rb +2 -0
  18. data/lib/poolparty/modules/cloud_resourcer.rb +4 -5
  19. data/lib/poolparty/modules/daemonizable.rb +4 -5
  20. data/lib/poolparty/modules/definable_resource.rb +1 -0
  21. data/lib/poolparty/modules/pinger.rb +5 -1
  22. data/lib/poolparty/monitors/monitor_daemon.rb +159 -0
  23. data/lib/poolparty/monitors/monitor_rack.rb +21 -11
  24. data/lib/poolparty/monitors/monitors/clock_monitor.rb +5 -0
  25. data/lib/poolparty/monitors/monitors/elections_monitor.rb +49 -0
  26. data/lib/poolparty/monitors/monitors/neighborhood_monitor.rb +7 -7
  27. data/lib/poolparty/monitors/monitors/stats_monitor.rb +15 -3
  28. data/lib/poolparty/net/init.rb +1 -1
  29. data/lib/poolparty/net/remote_instance.rb +43 -39
  30. data/lib/poolparty/net/remoter/connections.rb +6 -3
  31. data/lib/poolparty/net/remoter/interactive.rb +18 -11
  32. data/lib/poolparty/net/remoter_base.rb +39 -12
  33. data/lib/poolparty/net/remoter_bases/ec2/ec2.rb +23 -19
  34. data/lib/poolparty/net/remoter_bases/ec2/ec2_remote_instance.rb +15 -33
  35. data/lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb +28 -20
  36. data/lib/poolparty/net/remoter_bases/libvirt/libvirt.rb +73 -0
  37. data/lib/poolparty/net/remoter_bases/libvirt/libvirt_instance.rb +64 -0
  38. data/lib/poolparty/net/remoter_bases/metavirt/metavirt.rb +27 -17
  39. data/lib/poolparty/net/remoter_bases/metavirt/metavirt_instance.rb +20 -35
  40. data/lib/poolparty/net/remoter_bases/vmrun/vmrun.rb +35 -17
  41. data/lib/poolparty/net/remoter_bases/vmrun/vmrun_instance.rb +32 -24
  42. data/lib/poolparty/plugins/apache2/apache.rb +1 -1
  43. data/lib/poolparty/plugins/chef/chef.rb +12 -26
  44. data/lib/poolparty/plugins/chef/chef_deploy.rb +5 -23
  45. data/lib/poolparty/plugins/chef/chef_deploy_definition.rb +32 -0
  46. data/lib/poolparty/plugins/chef/chef_library.rb +7 -0
  47. data/lib/poolparty/plugins/chef/chef_recipe.rb +7 -0
  48. data/lib/poolparty/plugins/chef/include_chef_recipe.rb +14 -0
  49. data/lib/poolparty/{resources → plugins}/host.rb +5 -4
  50. data/lib/poolparty/plugins/line_in_file.rb +1 -1
  51. data/lib/poolparty/{resources → plugins}/sshkey.rb +10 -8
  52. data/lib/poolparty/poolparty/cloud.rb +7 -0
  53. data/lib/poolparty/poolparty/default.rb +0 -2
  54. data/lib/poolparty/poolparty/pool.rb +13 -3
  55. data/lib/poolparty/provision/boot_strapper.rb +8 -8
  56. data/lib/poolparty/resources/exec.rb +7 -0
  57. data/lib/poolparty/resources/group.rb +3 -3
  58. data/lib/poolparty/resources/user.rb +6 -1
  59. data/lib/poolparty/templates/monitor.ru +12 -0
  60. data/spec/poolparty/net/remoter_bases/ec2_remote_instance_spec.rb +0 -2
  61. data/spec/poolparty/net/remoter_bases/ec2_spec.rb +7 -4
  62. data/spec/poolparty/poolparty/example_spec.rb +21 -21
  63. data/spec/poolparty/poolparty/pool_spec.rb +2 -1
  64. data/spec/poolparty/resources/sshkey_spec.rb +39 -40
  65. data/tasks/poolparty.rake +27 -1
  66. data/test/fixtures/fake_clouds.rb +11 -0
  67. data/test/poolparty/dependency_resolver/chef_resolver_test.rb +82 -0
  68. data/test/poolparty/monitors/test_base_monitor.rb +2 -2
  69. data/test/poolparty/monitors/test_monitor_rack.rb +11 -24
  70. data/test/poolparty/net/remoter_base_test.rb +4 -5
  71. data/test/poolparty/net/remoter_bases/libvirt/libvirt_test.rb +70 -0
  72. data/test/poolparty/net/remoter_bases/metavirt/metavirt_test.rb +32 -3
  73. data/test/poolparty/plugins/chef_plugin_test.rb +23 -0
  74. data/test/poolparty/poolparty/pool_test.rb +22 -0
  75. data/vendor/gems/dslify/lib/dslify.rb +3 -0
  76. data/vendor/gems/dslify/test/dslify_test.rb +4 -15
  77. data/vendor/gems/git-style-binaries/README.markdown +6 -0
  78. data/vendor/gems/git-style-binaries/Rakefile +1 -1
  79. data/vendor/gems/git-style-binaries/VERSION.yml +1 -1
  80. data/vendor/gems/git-style-binaries/doc/gsb-screencast.png +0 -0
  81. data/vendor/gems/git-style-binaries/doc/poolparty-binaries.screenplay +11 -13
  82. data/vendor/gems/git-style-binaries/git-style-binaries.gemspec +12 -3
  83. data/vendor/gems/git-style-binaries/lib/git-style-binary.rb +15 -1
  84. data/vendor/gems/git-style-binaries/test/running_binaries_test.rb +2 -2
  85. data/vendor/gems/suitcase/VERSION.yml +1 -1
  86. data/vendor/gems/suitcase/lib/suitcase/zipper.rb +73 -25
  87. data/vendor/gems/suitcase/suitcase.gemspec +5 -5
  88. data/vendor/gems/suitcase/test/suitcase_test.rb +12 -6
  89. data/vendor/gems/suitcase/test/test_dir/gems/famoseagle-carrot-0.6.0.gem +0 -0
  90. metadata +24 -52
  91. data/spec/poolparty/resources/host_spec.rb +0 -35
  92. data/vendor/gems/rest-client/README.rdoc +0 -151
  93. data/vendor/gems/rest-client/Rakefile +0 -85
  94. data/vendor/gems/rest-client/bin/restclient +0 -87
  95. data/vendor/gems/rest-client/lib/rest_client.rb +0 -2
  96. data/vendor/gems/rest-client/lib/restclient.rb +0 -93
  97. data/vendor/gems/rest-client/lib/restclient/exceptions.rb +0 -84
  98. data/vendor/gems/rest-client/lib/restclient/mixin/response.rb +0 -43
  99. data/vendor/gems/rest-client/lib/restclient/raw_response.rb +0 -30
  100. data/vendor/gems/rest-client/lib/restclient/request.rb +0 -232
  101. data/vendor/gems/rest-client/lib/restclient/resource.rb +0 -146
  102. data/vendor/gems/rest-client/lib/restclient/response.rb +0 -20
  103. data/vendor/gems/rest-client/rest-client.gemspec +0 -21
  104. data/vendor/gems/rest-client/spec/base.rb +0 -4
  105. data/vendor/gems/rest-client/spec/exceptions_spec.rb +0 -54
  106. data/vendor/gems/rest-client/spec/mixin/response_spec.rb +0 -46
  107. data/vendor/gems/rest-client/spec/raw_response_spec.rb +0 -17
  108. data/vendor/gems/rest-client/spec/request_spec.rb +0 -442
  109. data/vendor/gems/rest-client/spec/resource_spec.rb +0 -75
  110. data/vendor/gems/rest-client/spec/response_spec.rb +0 -16
  111. data/vendor/gems/rest-client/spec/restclient_spec.rb +0 -53
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 2
3
- :patch: 10
3
+ :patch: 11
4
4
  :major: 1
data/bin/cloud CHANGED
@@ -31,7 +31,7 @@ EOS
31
31
  @loaded_clouds = command[:cloud_name] ? [clouds[command[:cloud_name].to_sym]] : clouds.map {|k,c| c }
32
32
  @loaded_clouds.each do |cld|
33
33
  cld.verbose command[:verbose]
34
- cld.debug command[:debug]
34
+ cld.debugging = command[:debug]
35
35
  end
36
36
  end
37
37
 
data/bin/cloud-list CHANGED
@@ -14,6 +14,7 @@ Usage: #{$0} #{all_options_string}
14
14
  EOS
15
15
 
16
16
  opt :instance_id, "Describe a specific instance", :type => String, :default => nil
17
+ opt :short, "show a short description rather than a long one", :default => false
17
18
 
18
19
  short_desc "show a list of the current instances on the clouds"
19
20
 
@@ -23,13 +24,24 @@ EOS
23
24
  require 'pp'
24
25
  pp result = cld.nodes(:instance_id=>command[:instance_id])
25
26
  else
26
- puts "Listing cloud #{cld.name}"
27
- puts "Active instances"
28
- puts cld.nodes(:status => "running").map{|a| "#{a[:instance_id] || a[:id]}\t#{a[:ip] ||a[:public_ip] }" }.join("\n")
29
-
30
- if cld.nodes(:status => "pending").size > 0
31
- puts "Pending instances"
32
- puts cld.nodes(:status => "pending").map{|a| "#{a[:instance_id] || a[:id]}\t#{a[:ip] ||a[:public_ip] }" }.join("\n")
27
+ if command[:short]
28
+ %w{running pending}.each do |status|
29
+ nodes = cld.nodes(:status => status)
30
+ if nodes.size > 0
31
+ puts nodes.map{|a|
32
+ "%-15s [%s]: #{a[:instance_id] || a[:id]}\t#{a[:ip] ||a[:public_ip] }" % [cld.name, status]
33
+ }.join("\n")
34
+ end
35
+ end
36
+ else
37
+ puts "Listing cloud #{cld.name}"
38
+ puts "Active instances"
39
+ puts cld.nodes(:status => "running").map{|a| "#{a[:instance_id] || a[:id]}\t#{a[:ip] ||a[:public_ip] }" }.join("\n")
40
+
41
+ if cld.nodes(:status => "pending").size > 0
42
+ puts "Pending instances"
43
+ puts cld.nodes(:status => "pending").map{|a| "#{a[:instance_id] || a[:id]}\t#{a[:ip] ||a[:public_ip] }" }.join("\n")
44
+ end
33
45
  end
34
46
  end
35
47
  end
data/bin/cloud-provision CHANGED
@@ -21,6 +21,9 @@ EOS
21
21
  if cld.nodes.empty?
22
22
  puts "No nodes to provision"
23
23
  else
24
+ # Call before_provision callback on cloud
25
+ cld.call_before_provision_callbacks
26
+
24
27
  if command[:inst_num]
25
28
  vputs "\nConfiguring: #{nodes[o.inst_num]}\n--------------------"
26
29
  address = cld.nodes[ command[:inst_num] ]
data/bin/cloud-run ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
3
+ require "poolparty"
4
+ # require "poolpartycl"
5
+ require 'git-style-binary/command'
6
+
7
+ GitStyleBinary.command do
8
+ version "PoolParty #{$0} command"
9
+ banner <<-EOS
10
+ Usage: #{$0} #{all_options_string} cmd
11
+
12
+ run a cmd in the cloud
13
+ EOS
14
+
15
+ short_desc "run a cmd in the cloud"
16
+ opt :inst_num, "The number of the instance to ssh to, starts at 0.", :type => :int
17
+
18
+ run do |command|
19
+ @loaded_clouds.each do |cloud|
20
+ @cloud = cloud
21
+ nodes = @cloud.nodes(:status => "running")
22
+
23
+ if !nodes.empty?
24
+ nodes.each_with_index do |node, i|
25
+ next unless i == command[:inst_num].to_i if command[:inst_num]
26
+ ip = nodes[i].ip || nodes[i].public_ip
27
+ cmd = ARGV.join(" ")
28
+ puts "%s %s: %s" % [cloud.cloud_name, ip, cmd]
29
+ @cloud.ssh_into(ip, :cmd => cmd) if @cloud
30
+ end
31
+ else
32
+ puts "No running instances can be found"
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -11,7 +11,7 @@ raise "ERROR: no json_file given" if !json_file
11
11
 
12
12
  cld = Cloud.load_from_json(::File.read(json_file))
13
13
  conditions= {:status=>'running', :keypair=>cld.keypair_name}
14
- inst_array = cld.remote_base.describe_instances.select_with_hash(conditions)
14
+ inst_array = cld.nodes(conditions)
15
15
  puts inst_array.collect{|n| n[list_type] || n[list_type.to_sym]}.join("\t")
16
16
 
17
17
 
data/config/jeweler.rb CHANGED
@@ -36,14 +36,14 @@ end
36
36
  "--main", "README"
37
37
  ]
38
38
 
39
- s.add_dependency 'activesupport'
39
+ # s.add_dependency 'activesupport'
40
40
  s.add_dependency 'logging'
41
41
  # s.add_dependency 'auser-dslify'
42
42
  # s.add_dependency 'auser-parenting'
43
43
  # s.add_dependency 'auser-suitcase'
44
44
  # s.add_dependency 'ruby2ruby' #TODO: remove this after verifying removing doesnt break anything
45
45
  s.add_dependency 'grempe-amazon-ec2'
46
- s.add_dependency 'rubigen', ">=1.5.2"
46
+ # s.add_dependency 'rubigen', ">=1.5.2"
47
47
  # Certainly there should be more here
48
48
 
49
49
  end
data/examples/basic.rb CHANGED
@@ -3,14 +3,15 @@
3
3
  pool :application do
4
4
  instances 1..5
5
5
 
6
- cloud :basic_app do
6
+ cloud :frontend do
7
7
  minimum_instances 3
8
- keypair 'front'
8
+ keypair 'application_frontend'
9
9
  image_id "ami-abc123"
10
10
  has_file :name => "/etc/motd", :content => "Welcome to your PoolParty instance"
11
11
  end
12
12
 
13
- cloud :basic_db do
13
+ cloud :database do
14
+ keypair 'application_database'
14
15
  using :vmrun do
15
16
  vmx_hash "/path/to/vmx_file" => "192.168.248.122"
16
17
  end
@@ -9,8 +9,8 @@ pool :party do
9
9
  image_id 'ami-7cfd1a15'
10
10
  debugging true
11
11
 
12
- access_key "XXXXXX"
13
- secret_access_key "XXXXXXX"
12
+ # access_key "XXXXXX"
13
+ # secret_access_key "XXXXXXX"
14
14
 
15
15
  cloud :app do
16
16
  keypair 'app'
@@ -4,18 +4,28 @@ require "poolparty"
4
4
 
5
5
  pool :multiverse do
6
6
 
7
- cloud :vv do
8
- instances 2
7
+ cloud :front do
8
+ instances 1
9
+ keypair 'multiverse_front'
9
10
  has_file "/etc/motd", :content => "Welcome to your poolparty instance!"
10
11
  using :metavirt do
11
- server_config({:host=>"192.168.248.1", :port=>3000})
12
+ server_config({:host=>"192.168.1.97", :port=>3000})
12
13
  authorized_keys 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCTppECfx7Tb0zoviRfqFaePyAek6+ZktKkHiTHu/jkhG1s4q1oHEe89no21xLxuReyJrDlNe8rLxxZzoYCaAWRdhcqMR3BNqb2w2jpF4pH+bFj0557KrwWP6HSNpRRkyYhxLqZbuH/2t3TzkPevZbcfSYa09jIzqnmTruh9l1s+n5E3cNr/RDdDn7tv3Ok7mKN7GEjkK7F83Pt9xviHevg22xqzm99nS+hg6Kl/fQUTO6pOmC5x+9V47RJz1+9WdhGJ7M83zijX9rMnAwrR5LFoL6aZyyU0G71SpoIL5e8XD/jt1WNKFJOfG8YMLb3i03UABm/Q5Q30+R7UoRxSWRX'
13
- using :vmrun do
14
- # vmx_files Dir[::File.expand_path("~/Documents/Virtual Machines.localized/metavirts/*/*.vmx")]
15
- vmx_files [
16
- "/Users/alerner/Documents/vm/Ubuntu32bitVM.vmwarevm/Ubuntu32bitVM.vmx"
17
- ]
14
+
15
+ # using :vmrun do
16
+ # # vmx_files Dir[::File.expand_path("~/Documents/Virtual Machines.localized/metavirts/*/*.vmx")]
17
+ # vmx_files [
18
+ # "/Users/alerner/Documents/vm/Ubuntu32bitVM.vmwarevm/Ubuntu32bitVM.vmx"
19
+ # ]
20
+ # end
21
+
22
+ using :libvirt do
23
+ image_id 'mvi_ef77fdf0'
24
+ # memory 256000
25
+ # cpus 1
26
+ # network 'vnet0'
18
27
  end
28
+
19
29
  end
20
30
  end
21
31
 
data/examples/simple.rb CHANGED
@@ -2,11 +2,12 @@
2
2
 
3
3
  pool :poolparty do
4
4
 
5
- instances 2..5
5
+ instances 1
6
6
 
7
7
  cloud :app do
8
- keypair "auser"
9
- ami "ami-4bb05422"
8
+ keypair "cloudteam_test"
9
+
10
+ has_file "/etc/motd", :content => "Simple"
10
11
  end
11
12
 
12
13
  end
@@ -6,13 +6,10 @@ pool :multiverse do
6
6
 
7
7
  cloud :vv do
8
8
  keypair "front"
9
- instances 2
9
+ instances 1
10
10
  has_file "/etc/motd", :content => "Welcome to your poolparty instance!"
11
11
  using :vmrun do
12
- vmx_files [
13
- "/Users/mfairchild/Documents/Virtual\ Machines.localized/Ubuntu-jaunty.vmwarevm/Ubuntu-jaunty.vmx",
14
- "/Users/mfairchild/Documents/Virtual\ Machines.localized/metavirts/Ubuntu32bitVM copy.vmwarevm/Ubuntu32bitVM.vmx"
15
- ]
12
+ vmx_hash "/Users/stimble/Documents/Virtual Machines.localized/Jaunty.vmwarevm/Jaunty.vmx" => '172.16.68.143'
16
13
  end
17
14
  end
18
15
 
data/lib/poolparty.rb CHANGED
@@ -75,7 +75,7 @@ $_poolparty_load_directories = [
75
75
  "dependency_resolver",
76
76
  "aska.rb",
77
77
  "config",
78
- "monitors/monitor_rack.rb",
78
+ "monitors/monitor_rack.rb",
79
79
  "capistrano.rb",
80
80
  'provisioners/provisioner_base.rb',
81
81
  'provisioners/capistrano/capistrano.rb',
@@ -94,15 +94,17 @@ class Object
94
94
  o.respond_to?(:verbose) ? o.verbose : (debugging? || $TESTING ||= false)
95
95
  end
96
96
  def debugging?(o=self)
97
- o.respond_to?(:debug) ? o.debug : ($DEBUGGING ||= false)
97
+ debugging
98
98
  end
99
- def debugging(bool=nil)
100
- bool.nil? ? $DEBUGGING : $DEBUGGING = bool
99
+ def debugging
100
+ $DEBUGGING ||= false
101
+ end
102
+ def debugging=(bool)
103
+ $DEBUGGING = bool
101
104
  end
102
105
  def testing(bool=$TESTING)
103
106
  bool.nil? ? $TESTING : $TESTING = bool
104
107
  end
105
- alias :debug :debugging
106
108
  def unix_hide_string
107
109
  "2>&1 > /dev/null"
108
110
  end
@@ -29,8 +29,9 @@ class String
29
29
  self.strip.split(/\n/).join(" && ")
30
30
  end
31
31
  def top_level_class
32
- self.split("::")[-1].underscore.downcase rescue self.class.to_s
32
+ self.split("::")[-1].snake_case rescue self.class.to_s
33
33
  end
34
+
34
35
  def sanitize
35
36
  self.gsub(/[ \.\/\-]*/, '')
36
37
  end
@@ -94,7 +94,7 @@ module PoolParty
94
94
  @opts.separator "Options:"
95
95
 
96
96
  @opts.on('-v', '--verbose', 'Be verbose') { self.verbose true }
97
- @opts.on('-d', "--debug", "Debug setting") { self.debugging true }
97
+ @opts.on('-d', "--debug", "Debug setting") { self.debugging = true }
98
98
  @opts.on('-s [file]', '--spec-file [file]', 'Set the spec file') { |file| self.spec file.chomp }
99
99
  @opts.on('-t', '--test', 'Testing mode') { self.testing true }
100
100
 
@@ -11,6 +11,8 @@ module PoolParty
11
11
  [
12
12
  :before_bootstrap,
13
13
  :after_bootstrap,
14
+ :before_provision,
15
+ :after_provision,
14
16
  :before_configure,
15
17
  :after_configure,
16
18
  :after_create,
@@ -61,11 +61,10 @@ module PoolParty
61
61
  # instance of the remote base
62
62
  def using(t, o={}, &block)
63
63
  return self.send(t) if self.respond_to?(t)
64
- if ::PoolParty::Remote::RemoterBase.available_bases.include?(t.to_sym)
65
- klass_string = "#{t}".classify
66
- remote_base_klass = "::PoolParty::Remote::#{klass_string}".constantize
64
+ klass_string = "::PoolParty::Remote::#{t.to_s.camelcase}"
65
+ remote_base_klass = klass_string.constantize
66
+ if ::PoolParty::Remote.available.include?(remote_base_klass)
67
67
  set_default_options(remote_base_klass.default_options)
68
-
69
68
  @remote_base = remote_base_klass.send(:new, o.merge(:cloud=>self), &block)
70
69
  self.remoter_base t.to_sym
71
70
  instance_eval "def #{t};@remote_base;end"
@@ -78,7 +77,7 @@ module PoolParty
78
77
  raise "Unknown remote base: #{t}"
79
78
  end
80
79
  end
81
-
80
+
82
81
  def dependency_resolver(name=nil)
83
82
  if !name.nil?
84
83
  ext = name=~/Resolver$/ ? nil : 'Resolver'
@@ -1,6 +1,5 @@
1
- # TODO: Diet
2
- # module PoolParty
3
- #
1
+ # # TODO: Diet
2
+ # module PoolParty
4
3
  # module Daemonizeable
5
4
  #
6
5
  # attr_accessor :pid_file
@@ -14,7 +13,7 @@
14
13
  # end
15
14
  #
16
15
  # def pid_file
17
- # @pid_file ||= PoolParty::Default.agent_pid_file
16
+ # @pid_file ||= "/tmp/poolparty_monitor.pid"
18
17
  # end
19
18
  #
20
19
  # # Returns +true+ the process identied by +pid+ is running.
@@ -135,7 +134,7 @@
135
134
  # end
136
135
  # end
137
136
  # end
138
- # class Daemonize
137
+ # class Daemon
139
138
  # include Daemonizeable
140
139
  # end
141
140
  # end
@@ -16,6 +16,7 @@ module PoolParty
16
16
  #
17
17
  # For example usage, see lib/poolparty/plugins/line.rb
18
18
  def define_resource(name, &block)
19
+ $stderr.puts "define_resource (#{name}) is depricated (#{caller[0]})"
19
20
  name.to_s.new_resource_class &block
20
21
  end
21
22
 
@@ -1,3 +1,4 @@
1
+ require "timeout"
1
2
  module PoolParty
2
3
  module Pinger
3
4
  module ClassMethods
@@ -5,9 +6,12 @@ module PoolParty
5
6
  # for response.
6
7
  def ping_port(host, port=22, retry_times=400)
7
8
  connected = false
9
+ dputs "pinging #{host}:#{port} for #{retry_times}"
8
10
  retry_times.times do |i|
9
11
  begin
10
- break if connected = TCPSocket.new(host, port).is_a?(TCPSocket)
12
+ timeout 5 do
13
+ break if connected = TCPSocket.new(host, port).is_a?(TCPSocket)
14
+ end
11
15
  rescue Exception => e
12
16
  sleep(2)
13
17
  end
@@ -0,0 +1,159 @@
1
+ require "open-uri"
2
+
3
+ module PoolParty
4
+ class MonitorDaemon
5
+
6
+ attr_reader :should_daemonize, :pid_file, :log_file_path, :sleep_time
7
+
8
+ def self.run(o={})
9
+ new(o).run
10
+ end
11
+
12
+ def initialize(o={})
13
+ @should_daemonize = o.delete(:daemonize)
14
+ @pid_file = o.delete(:daemonize) || "/tmp/poolparty_monitor.pid"
15
+ temp_log_file_path = o.delete(:log_file_path) || "poolparty_monitor.log"
16
+ @log_file_path = temp_log_file_path
17
+
18
+ unless ::File.file?(temp_log_file_path)
19
+ ::FileUtils.mkdir_p ::File.dirname(temp_log_file_path) unless ::File.directory?(::File.dirname(temp_log_file_path))
20
+ ::File.open(temp_log_file_path, 'a+')
21
+ temp_log_file_path
22
+ end
23
+ @sleep_time = o.delete(:sleep_time) || 5
24
+ end
25
+
26
+ def pass_the_baton
27
+ %w(Memory neighborhood elections).each do |monitor|
28
+ out = open("http://localhost:8642/#{monitor}").read
29
+ log "#{monitor} / #{out.inspect}"
30
+ end
31
+ sleep sleep_time
32
+ end
33
+
34
+ def run
35
+ if should_daemonize
36
+ @should_daemonize = false
37
+ daemonize
38
+ else
39
+ log "Starting MonitorDaemon"
40
+ loop {pass_the_baton}
41
+ end
42
+ end
43
+
44
+ def daemonize(o={})
45
+ raise unless pid_file
46
+
47
+ pwd = Dir.pwd # Current directory is changed during daemonization, so store it
48
+
49
+ remove_stale_pid_file
50
+ pid = fork do
51
+ Signal.trap('HUP') do
52
+ restart
53
+ end
54
+ Signal.trap('INT') do
55
+ stop!
56
+ end
57
+ Signal.trap("CHLD") do
58
+ Process.waitpid(pid, Process::WNOHANG)
59
+ end
60
+ File.open("/dev/null", "r+") do |devnull|
61
+ $stdout.reopen(devnull)
62
+ $stderr.reopen(devnull)
63
+ $stdin.reopen(devnull) unless @use_stdin
64
+ end
65
+ run
66
+ end
67
+
68
+ Dir.chdir(pwd)
69
+
70
+ write_pid_file(pid)
71
+
72
+ Process.detach(pid)
73
+ end
74
+
75
+ def log(msg)
76
+ log_file.flush
77
+ log_file << "[INFO] - #{Time.now} -- #{msg}\n"
78
+ end
79
+
80
+ private
81
+
82
+ def log_file
83
+ if @logfile
84
+ @logfile
85
+ else
86
+ begin
87
+ ::FileUtils.mkdir_p ::File.dirname(log_file_path) unless ::File.directory?(::File.dirname(log_file_path))
88
+ @logfile ||= ::File.open(log_file_path, 'a+')
89
+ rescue Exception => e
90
+ puts "ERROR: #{e.inspect}"
91
+ @logfile = $stdout
92
+ end
93
+
94
+ end
95
+ end
96
+
97
+ def pid
98
+ @pid ||= File.file?(pid_file) ? open(pid_file).read.to_i : nil
99
+ end
100
+
101
+ def stop!
102
+ log "Stopping daemon"
103
+ log_file.close
104
+ send_signal("INT")
105
+ remove_stale_pid_file
106
+ exit 0
107
+ end
108
+
109
+ def restart
110
+ log "TODO: implement restart for HUP signal on #{__FILE__}"
111
+ end
112
+
113
+ def running?
114
+ return false unless pid
115
+ Process.getpgid(pid) != -1
116
+ rescue Errno::ESRCH
117
+ false
118
+ end
119
+
120
+ def send_signal(signal)
121
+ if File.exist?(pid_file) && pid = open(pid_file).read
122
+ pid = pid.to_i
123
+ print "Sending #{signal} signal to process #{pid} ... "
124
+ Process.kill(signal, pid)
125
+ puts
126
+ pid
127
+ else
128
+ puts "Can't stop process, no PID found in #{pid_file}"
129
+ nil
130
+ end
131
+ rescue Errno::ESRCH # No such process
132
+ puts "process not found!"
133
+ nil
134
+ end
135
+
136
+ protected
137
+ def remove_pid_file
138
+ File.delete(pid_file) if pid_file && File.exists?(pid_file)
139
+ end
140
+
141
+ def write_pid_file(pid)
142
+ FileUtils.mkdir_p File.dirname(pid_file)
143
+ open(pid_file,"w") { |f| f.write(pid) }
144
+ File.chmod(0644, pid_file)
145
+ end
146
+
147
+ def remove_stale_pid_file
148
+ if File.exist?(@pid_file)
149
+ if pid && running?
150
+ puts "Pid file exists" and raise
151
+ else
152
+ remove_pid_file
153
+ end
154
+ end
155
+ end
156
+
157
+
158
+ end
159
+ end