jerbil 1.2.2

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.
Files changed (86) hide show
  1. data/Bugs.rdoc +66 -0
  2. data/Gemfile +12 -0
  3. data/History.txt +359 -0
  4. data/Intro.txt +5 -0
  5. data/LICENCE.rdoc +159 -0
  6. data/README.md +335 -0
  7. data/README_SERVICES.md +410 -0
  8. data/README_TESTING.md +47 -0
  9. data/bin/jerbil +62 -0
  10. data/bin/jerbil-install +56 -0
  11. data/etc/conf.d/jerbild +15 -0
  12. data/etc/conf.d/jserviced +39 -0
  13. data/etc/init.d/jerbild +55 -0
  14. data/etc/init.d/jserviced +59 -0
  15. data/etc/jerbil/jerbil-client.rb +2 -0
  16. data/etc/jerbil/jerbil.rb +83 -0
  17. data/lib/jerbil.rb +636 -0
  18. data/lib/jerbil/config.md +49 -0
  19. data/lib/jerbil/config.rb +67 -0
  20. data/lib/jerbil/errors.rb +74 -0
  21. data/lib/jerbil/jerbil_service/base.rb +191 -0
  22. data/lib/jerbil/jerbil_service/client.rb +325 -0
  23. data/lib/jerbil/jerbil_service/config.md +119 -0
  24. data/lib/jerbil/jerbil_service/config.rb +72 -0
  25. data/lib/jerbil/jerbil_service/sclient.rb +343 -0
  26. data/lib/jerbil/jerbil_service/support.rb +58 -0
  27. data/lib/jerbil/jerbil_service/utils.rb +35 -0
  28. data/lib/jerbil/servers.rb +230 -0
  29. data/lib/jerbil/service.rb +216 -0
  30. data/lib/jerbil/support.rb +160 -0
  31. data/lib/jerbil/thor/server.rb +76 -0
  32. data/lib/jerbil/thor/service.rb +74 -0
  33. data/lib/jerbil/version.rb +13 -0
  34. data/sbin/jerbil-status +120 -0
  35. data/sbin/jerbil-stop +139 -0
  36. data/sbin/jerbild +186 -0
  37. data/sbin/jservice-status +107 -0
  38. data/sbin/jservice-stop +94 -0
  39. data/sbin/jserviced +111 -0
  40. data/spec/jerbil_2_jerbil_spec.rb +87 -0
  41. data/spec/jerbil_client1_spec.rb +80 -0
  42. data/spec/jerbil_client_spec.rb +114 -0
  43. data/spec/jerbil_client_stop_spec.rb +24 -0
  44. data/spec/jerbil_daemonised/jerbil_local_spec.rb +81 -0
  45. data/spec/jerbil_daemonised/jerbil_remote_spec.rb +116 -0
  46. data/spec/jerbil_load.rb +48 -0
  47. data/spec/jerbil_local_spec.rb +91 -0
  48. data/spec/jerbil_missing_spec.rb +98 -0
  49. data/spec/jerbil_remote_spec.rb +117 -0
  50. data/spec/jerbil_remote_spec_bup.rb +168 -0
  51. data/spec/jerbil_service_error_spec.rb +56 -0
  52. data/spec/jerbil_service_spec.rb +41 -0
  53. data/spec/jerbil_support_spec.rb +69 -0
  54. data/spec/jservice_utils_spec.rb +38 -0
  55. data/spec/server_spec.rb +69 -0
  56. data/spec/server_update_spec.rb +28 -0
  57. data/spec/service_spec.rb +72 -0
  58. data/spec/spec_helper.rb +12 -0
  59. data/spec/test_env_spec.rb +53 -0
  60. data/test/bad_test_service.rb +31 -0
  61. data/test/conf.d/jerbil +36 -0
  62. data/test/conf.d/jerbil.conf +39 -0
  63. data/test/conf.d/jerbil.rb +55 -0
  64. data/test/conf.d/jerbil_local.rb +33 -0
  65. data/test/conf.d/jerbil_no_local.conf +39 -0
  66. data/test/conf.d/jerbil_old.rb +47 -0
  67. data/test/conf.d/jerbil_test.rb +35 -0
  68. data/test/conf.d/malformed +1 -0
  69. data/test/conf.d/missing_services +39 -0
  70. data/test/conf.d/ruby_test.rb +8 -0
  71. data/test/init.d/jerbild +14 -0
  72. data/test/jerbil.rb +51 -0
  73. data/test/jerbil_config.rb +8 -0
  74. data/test/jstop.rb +36 -0
  75. data/test/key.asc +1 -0
  76. data/test/lib/ruby_test.rb +37 -0
  77. data/test/lib/ruby_test/config.rb +56 -0
  78. data/test/lib/ruby_test/version.rb +13 -0
  79. data/test/pids/jerbil-prod.asc +1 -0
  80. data/test/pids/jerbil-prod.pid +1 -0
  81. data/test/pids/jerbil.pid +1 -0
  82. data/test/private_key_file.asc +3 -0
  83. data/test/service-stop.rb +86 -0
  84. data/test/service_mock.rb +94 -0
  85. data/test/test_service_client.rb +25 -0
  86. metadata +265 -0
@@ -0,0 +1,160 @@
1
+ #
2
+ # Description
3
+ #
4
+ # Author:: Robert Sharp
5
+ # Copyright:: Copyright (c) 2010 Robert Sharp
6
+ # License:: Open Software Licence v3.0
7
+ #
8
+ # This software is licensed for use under the Open Software Licence v. 3.0
9
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
10
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
11
+ # must themselves be licensed under the Open Software Licence v. 3.0
12
+ #
13
+ #
14
+ require 'jerbil/errors'
15
+ require 'jerbil/config'
16
+ require 'digest/sha1'
17
+ require 'fileutils'
18
+ require 'socket'
19
+
20
+ module Jerbil
21
+
22
+ #
23
+ # Set of utilities to assist in managing Jerbil services. Used largely by JerbilService
24
+ # modules and classes
25
+ #
26
+ module Support
27
+
28
+ # create a pid file for the given service and env in the given pid_dir
29
+ #
30
+ # @param [Symbol] name of the service
31
+ # @param [Symbol] env the services is running in
32
+ # @param [String] pid_dir path to directory where pid file is to be written
33
+ # @return [String] the pid
34
+ # @raise [Jerbil::ServiceConfigError] if the pid file cannot be written to
35
+ def Support.write_pid_file(name, env, pid_dir)
36
+ pid = Process.pid.to_s
37
+ pid_file = "#{pid_dir}/#{name.to_s}-#{env}.pid"
38
+ FileUtils.rm_f(pid_file) if File.exists?(pid_file) # avoid permissions probs
39
+ File.open(pid_file, "w") do |pfile|
40
+ pfile.puts pid
41
+ end
42
+ return pid
43
+ rescue Errno::ENOENT
44
+ # failed to write pid to file
45
+ raise Jerbil::ServiceConfigError, "Cannot write pid file: #{pid_file}"
46
+ end
47
+
48
+ # retrieve the pid from a perviously created pid file
49
+ #
50
+ # @param (see Support.write_pid_file)
51
+ # @return [Integer] the pid from the pid file
52
+ def Support.get_pid_from_file(name, env, pid_dir)
53
+ pid_file = "#{pid_dir}/#{name.to_s}-#{env}.pid"
54
+ pid = File.read(pid_file).chomp
55
+ return pid.to_i
56
+ rescue
57
+ # something went wrong so return 0
58
+ return 0
59
+ end
60
+
61
+ # (see Support.get_pid_from_file)
62
+ # @note This deletes the pid file as well
63
+ def Support.get_pid_and_delete_file(name, env, pid_dir)
64
+ pid = get_pid_from_file(name, env, pid_dir)
65
+ if pid > 0 then
66
+ # there is a pid, so delete it
67
+ pid_file = "#{pid_dir}/#{name.to_s}-#{env}.pid"
68
+ FileUtils.rm_f(pid_file)
69
+ end
70
+ return pid
71
+ rescue
72
+ # hmm. something went wrong, but do I ignore it
73
+ return 0
74
+ end
75
+
76
+ # create a private key, save it to a key file in the given directory and return it
77
+ #
78
+ # Private keys should be created by the daemon start script and used to supervise
79
+ # the service (e.g. stop)
80
+ #
81
+ # @param [Symbol] name of the service
82
+ # @param [Symbol] env the services is running in
83
+ # @param [String] key_dir path to directory where key file is to be written
84
+ # @return [String] the private key
85
+ # @raise [Jerbil::ServiceConfigError] if the key file cannot be written to
86
+ def Support.create_private_key(name, env, key_dir)
87
+ key = Digest::SHA1.hexdigest(Time.now.to_s + rand(12341234).to_s)[1..20]
88
+ key_file = "#{key_dir}/#{name.to_s}-#{env}.asc"
89
+ FileUtils.rm_f(key_file) if File.exists?(key_file) # avoid permissions probs
90
+ File.open(key_file, "w") do |kfile|
91
+ kfile.puts key
92
+ end
93
+ return key
94
+ rescue Errno::ENOENT
95
+ # failed to write pid to file
96
+ raise Jerbil::ServiceConfigError, "Cannot write key file: #{key_file}"
97
+ end
98
+
99
+ # return a previously saved private key
100
+ #
101
+ # @param (see Support.create_private_key)
102
+ # @return (see Support.create_private_key)
103
+ def Support.get_private_key(name, env, key_dir)
104
+ key_file = "#{key_dir}/#{name.to_s}-#{env}.asc"
105
+ key = File.read(key_file).chomp
106
+ return key
107
+ rescue
108
+ return ''
109
+ end
110
+
111
+ # (see Support.get_private_key)
112
+ # @note This deletes the key file
113
+ def Support.get_key_and_delete_file(name, env, key_dir)
114
+ key = get_private_key(name, env, key_dir)
115
+ if key != '' then
116
+ # there is a key file, so delete it
117
+ key_file = "#{key_dir}/#{name.to_s}-#{env}.asc"
118
+ FileUtils.rm_f(key_file)
119
+ end
120
+ return key
121
+ rescue
122
+ # hmm. something went wrong, but do I ignore it
123
+ return ''
124
+
125
+ end
126
+
127
+ end
128
+
129
+ # General support methods for Jerbil itself
130
+
131
+ # get the Jerbil config options
132
+ #
133
+ # This will create a hash of the Jerbil Server config options from either
134
+ # the given config file or, if none is provided, the default file. The
135
+ # location of the default file is currently defined by the Jeckyl gem as the
136
+ # default location for all Jeckyl config files.
137
+ #
138
+ # @note This method is used to get the environment for the Jerbil Server
139
+ # and therefore find the server.
140
+ #
141
+ # @param [String] config_file path to jerbil config file
142
+ # @return [Hash] of config options
143
+ # @raise [Jerbil::JerbilConfigError] if there was any error reading the file
144
+ def Jerbil.get_config(config_file=nil)
145
+ # check that the config_file has been specified
146
+ if config_file.nil? then
147
+ # no, so set the default
148
+ config_file = File.join(Jeckyl.config_dir, "/jerbil.rb")
149
+ end
150
+
151
+ # read the config file
152
+ return Jerbil::Config.new(config_file)
153
+
154
+ rescue Jeckyl::JeckylError =>err
155
+ # something went wrong with the config file
156
+ raise Jerbil::JerbilConfigError, err.message
157
+ end
158
+
159
+
160
+ end
@@ -0,0 +1,76 @@
1
+ #
2
+ #
3
+ # = Title
4
+ #
5
+ # == SubTitle
6
+ #
7
+ # Author:: Robert Sharp
8
+ # Copyright:: Copyright (c) 2011 Robert Sharp
9
+ # License:: Open Software Licence v3.0
10
+ #
11
+ # This software is licensed for use under the Open Software Licence v. 3.0
12
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
13
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
14
+ # must themselves be licensed under the Open Software Licence v. 3.0
15
+ #
16
+ #
17
+ #
18
+
19
+ # define subcommands relating to Jerbil Servers
20
+ class Server < Thor
21
+
22
+ default_task :local
23
+ class_option :config, :aliases=>'-c', :desc=>'use the given config file', :default=>'/etc/jermine/jerbil-client.rb'
24
+ class_option :verify, :aliases=>'-v', :desc=>'check the server is running'
25
+
26
+ desc "local", "display information about the local jerbil server"
27
+ def local
28
+ config = Jerbil.get_config(options[:config])
29
+ local = Jerbil::Servers.get_local_server(config[:environment])
30
+ puts "Checking for local Jerbil server running in env: #{config[:environment]}"
31
+ begin
32
+ jerbs = local.connect
33
+ started = jerbs.started
34
+ puts " Jerbil server found, version: #{jerbs.version}".green
35
+ puts " Server has been up since #{started.strftime('%d %b %Y at %H:%M')}"
36
+ puts " and has had #{jerbs.registrations.to_s} registrations"
37
+ rescue Exception => err
38
+ puts " Server did not respond: #{err.message}".red.bold
39
+ end
40
+
41
+ end
42
+
43
+ desc "list", "list information about the network's Jerbil servers"
44
+ def list
45
+ config = Jerbil.get_config(options[:config])
46
+ local = Jerbil::Servers.get_local_server(config[:environment])
47
+ puts "Checking for Jerbil servers running in env: #{config[:environment]}"
48
+ begin
49
+ jerbs = local.connect
50
+ remotes = jerbs.remote_servers
51
+ remotes.each do |remote|
52
+ if options[:verify] then
53
+ begin
54
+ remote.connect.verify
55
+ puts " #{remote.ident}".green
56
+ rescue
57
+ puts " #{remote.ident}".red
58
+ end
59
+ else
60
+ puts " #{remote.ident}".cyan
61
+ end
62
+ end
63
+ rescue Exception => err
64
+ puts " Server did not respond: #{err.message}".red.bold
65
+ end
66
+
67
+ end
68
+
69
+ desc "secret", "generate a secret key for a new installation"
70
+ def secret
71
+ key = Digest::SHA1.hexdigest(Time.now.to_s + rand(12341234).to_s)
72
+ puts 'secret "' + key + '"'
73
+ end
74
+
75
+
76
+ end
@@ -0,0 +1,74 @@
1
+ #
2
+ #
3
+ # = Title
4
+ #
5
+ # == SubTitle
6
+ #
7
+ # Author:: Robert Sharp
8
+ # Copyright:: Copyright (c) 2011 Robert Sharp
9
+ # License:: Open Software Licence v3.0
10
+ #
11
+ # This software is licensed for use under the Open Software Licence v. 3.0
12
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
13
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
14
+ # must themselves be licensed under the Open Software Licence v. 3.0
15
+ #
16
+ #
17
+ #
18
+
19
+ class Service < Thor
20
+
21
+ default_task :list
22
+ class_option :verbose, :default=>false, :aliases=>'-V', :desc=>'print more information'
23
+ class_option :verify, :aliases=>'-v', :desc=>'check the service is running'
24
+ class_option :config, :aliases=>'-c', :desc=>'use the given config file', :default=>'/etc/jermine/jerbil-client.rb'
25
+
26
+ desc "list", "List services"
27
+ def list
28
+ config = Jerbil.get_config(options[:config])
29
+ local = Jerbil::Servers.get_local_server(config[:environment])
30
+ services = []
31
+ begin
32
+ jerbs = local.connect
33
+ services = jerbs.get_all(true) # ignore this access
34
+ rescue
35
+ puts "Failed to connect to the local Jerbil server".red.bold
36
+ return
37
+ end
38
+ puts "There are #{services.length} services registered with Jerbil:"
39
+ services.each do |s|
40
+ puts " #{s.name}[:#{s.env}]@#{s.host}:#{s.port}".cyan
41
+ puts " started at: #{s.registered_at.strftime('%d/%m/%y %H:%M')}" if options[:verbose]
42
+ puts " accessed #{s.access_count.to_s} times, last time at: #{s.accessed_at.strftime('%d/%m/%y %H:%M')}" if options[:verbose]
43
+ if options[:verify] then
44
+ if jerbs.service_missing?(s) then
45
+ puts " #{s.ident} has failed and should be removed".red
46
+ else
47
+ puts " #{s.ident} responded".green
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ desc "verify", "Verify that services are running"
54
+ def verify
55
+ config = Jerbil.get_config(options[:config])
56
+ local = Jerbil::Servers.get_local_server(options[:environment])
57
+ services = []
58
+ begin
59
+ jerbs = local.connect
60
+ services = jerbs.get_all(true) # ignore this access
61
+ rescue
62
+ puts "Failed to connect to the local Jerbil server".red.bold
63
+ end
64
+ puts "Checking #{services.length} services registered with Jerbil:"
65
+ services.each do |s|
66
+ if jerbs.service_missing?(s) then
67
+ puts " #{s.ident} has failed and should be removed".red
68
+ else
69
+ puts " #{s.ident} responded".green
70
+ end
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,13 @@
1
+ # Created by Jevoom
2
+ #
3
+ # 21-Nov-2012
4
+ # Add proper home for jerbil user in jerbil-install. Add guidance on logging to READMEs.
5
+
6
+ module Jerbil
7
+ # version set to 1.2.2
8
+ Version = '1.2.2'
9
+ # date set to 21-Nov-2012
10
+ Version_Date = '21-Nov-2012'
11
+ #ident string set to: jerbil-1.2.2 21-Nov-2012
12
+ Ident = 'jerbil-1.2.2 21-Nov-2012'
13
+ end
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env ruby18
2
+ #
3
+ #
4
+ # = Jerbil Service Stop
5
+ #
6
+ # == stops a jerbil service
7
+ #
8
+ # Author:: Robert Sharp
9
+ # Copyright:: Copyright (c) 2012 Robert Sharp
10
+ # License:: Open Software Licence v3.0
11
+ #
12
+ # This software is licensed for use under the Open Software Licence v. 3.0
13
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
14
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
15
+ # must themselves be licensed under the Open Software Licence v. 3.0
16
+ #
17
+ # This script should be used to stop a jerbil service, typically through the jerbil init script.
18
+ #
19
+ require 'rubygems'
20
+
21
+ require 'jerbil'
22
+ require 'jerbil/servers'
23
+ require 'jerbil/version'
24
+ require 'jerbil/config'
25
+ require 'jerbil/support'
26
+
27
+ require 'optparse'
28
+
29
+ # force quiet to suppress client output regardless of whether verbose is selected
30
+ options = Hash.new
31
+
32
+ verbose = false
33
+ config_file = nil
34
+ env = :null
35
+ quiet = nil
36
+ OptionParser.new do |opts|
37
+
38
+ opts.banner = "Usage: jerbil-status [opts]"
39
+ opts.separator ""
40
+ opts.separator " stop the given Jerbil Service"
41
+ opts.separator ""
42
+
43
+ opts.on("-n", "--no-daemon", "Do not daemonize") do
44
+ daemonize = false
45
+ end
46
+
47
+ opts.on("-S", "--no-syslog", "Do not log to syslog") do
48
+ log_to_syslog = false
49
+ end
50
+
51
+ opts.on("-c", "--config [file]", String, "use this config file to find Jerbil" ) do |cfile|
52
+ if FileTest.readable?(cfile) then
53
+ options = Jerbil.get_config(cfile)
54
+ else
55
+ puts "Config file cannot be read."
56
+ exit 1
57
+ end
58
+ end
59
+
60
+ opts.on("-e", "--environment [TYPE]", [:prod, :test, :dev], "Select env (prod, test, dev), default is prod") do |e|
61
+ env = e
62
+ end
63
+
64
+ opts.on("-V", "--verbose", "output more information about what is going on ") do
65
+ verbose = true
66
+ end
67
+
68
+ opts.on("-q", "--quiet", "output nothing") do
69
+ quiet = true
70
+ end
71
+
72
+ opts.on("-h", "--help", "Provide Help") do |h|
73
+ opts.separator ""
74
+ puts opts
75
+ exit 0
76
+ end
77
+
78
+ end.parse!
79
+
80
+ puts "Check status of the local Jerbil Server" if verbose
81
+ #puts "Version: jerbil-" + Jerbil::Version + " [#{Jerbil::Version_Date}]" if verbose
82
+
83
+ #options = Jerbil::Config.new(config_file)
84
+ #servers = options[:servers]
85
+
86
+ #server_env = nil
87
+ if env != :null then
88
+ server_env = env
89
+ else
90
+ server_env = options[:environment] || :prod
91
+ end
92
+
93
+ puts "Running with environment: #{server_env}" if verbose
94
+
95
+ local = Jerbil::Servers.get_local_server(server_env)
96
+
97
+ if local.nil? then
98
+ puts "Cannot find record for a local Jerbil Server" if verbose
99
+ exit 1
100
+ end
101
+
102
+ puts "Found local Jerbil Server record" if verbose
103
+
104
+
105
+ begin
106
+ jerbild = local.connect
107
+
108
+ # see if you can verify?
109
+ jerbild.verify
110
+ if verbose then
111
+ puts "Jerbil Server is running"
112
+ puts " started on #{jerbild.started.strftime('%d-%m-%y %H:%M')}"
113
+ end
114
+ exit 0
115
+ rescue DRb::DRbConnError, Jerbil::ServerConnectError
116
+ #fall back to pid killing
117
+ puts "Failed to connect to the Jerbil Server" if verbose
118
+ exit 1
119
+ end
120
+
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env ruby18
2
+ #
3
+ # Jerbil
4
+ #
5
+ # Author:: Robert Sharp
6
+ # Copyright:: Copyright (c) 2010 Robert Sharp
7
+ # License:: Open Software Licence v3.0
8
+ #
9
+ # This software is licensed for use under the Open Software Licence v. 3.0
10
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
11
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
12
+ # must themselves be licensed under the Open Software Licence v. 3.0
13
+ #
14
+ #
15
+ # stop the jerbil server on this host
16
+ #
17
+ require 'rubygems'
18
+
19
+ require 'jerbil'
20
+ require 'jerbil/servers'
21
+ require 'jerbil/version'
22
+ require 'jerbil/config'
23
+ require 'jerbil/support'
24
+
25
+ require 'drb'
26
+ require 'optparse'
27
+
28
+
29
+ verbose = false
30
+ env = :null
31
+ conf_file = nil
32
+
33
+ OptionParser.new do |opts|
34
+
35
+ opts.banner = "Usage: jerbild [opts]"
36
+ opts.separator ""
37
+ opts.separator " Stop the Jerbil Server"
38
+ opts.separator ""
39
+
40
+ opts.on("-n", "--no-daemon", "ignored") do
41
+ daemonize = false
42
+ end
43
+
44
+ opts.on("-S", "--no-syslog", "ignored") do
45
+ log_to_syslog = false
46
+ end
47
+
48
+
49
+ opts.on("-c", "--config [file]", String, "use this config file to find Jerbil" ) do |cfile|
50
+ if FileTest.readable?(cfile) then
51
+ conf_file = cfile
52
+ else
53
+ puts "Config file cannot be read."
54
+ exit 1
55
+ end
56
+ end
57
+
58
+ opts.on("-V", "--verbose", "output more information about what is going on ") do
59
+ verbose = true
60
+ end
61
+
62
+ opts.on("-e", "--environment [TYPE]", [:prod, :test, :dev], "Select env (prod, test, dev), default is prod") do |e|
63
+ env = e
64
+ end
65
+
66
+ opts.on("-h", "--help", "Provide Help") do |h|
67
+ opts.separator ""
68
+ puts opts
69
+ exit 0
70
+ end
71
+
72
+ end.parse!
73
+
74
+ puts "Stopping the local Jerbil Server" if verbose
75
+ puts "Version: jerbil-" + Jerbil::Version + " [#{Jerbil::Version_Date}]" if verbose
76
+
77
+ options = Jerbil.get_config(conf_file)
78
+ #servers = options[:servers]
79
+
80
+ if env != :null then
81
+ server_env = env
82
+ else
83
+ server_env = options[:environment] || :prod
84
+ end
85
+
86
+ puts "Running with environment: #{server_env}" if verbose
87
+
88
+ local = Jerbil::Servers.get_local_server(server_env)
89
+
90
+ if local.nil? then
91
+ puts "Cannot find record for a local Jerbil Server" if verbose
92
+ exit 1
93
+ end
94
+
95
+ puts "Found local Jerbil Server record" if verbose
96
+
97
+ pkey = Jerbil::Support.get_key_and_delete_file(:jerbil, server_env, options[:key_dir])
98
+ puts "Obtained a private key: #{pkey} from #{options[:key_dir]}" if verbose
99
+ pid = Jerbil::Support.get_pid_and_delete_file(:jerbil, server_env, options[:pid_dir])
100
+ puts "Obtained a pid: #{pid}" if verbose
101
+
102
+ puts "Stopping Jerbil Test Server" if verbose
103
+
104
+ begin
105
+ jerbild = local.connect
106
+
107
+ # see if you can verify?
108
+ jerbild.verify
109
+ rescue DRb::DRbConnError, Jerbil::ServerConnectError
110
+ #fall back to pid killing
111
+ if pid > 0 then
112
+ Process.kill("SIGKILL", pid.to_i)
113
+ puts "Manually killed the process" if verbose
114
+ else
115
+ puts "No pid, assume it is already dead" if verbose
116
+ end
117
+ exit 0
118
+ end
119
+
120
+ # did, so kill it properly
121
+ begin
122
+ jerbild.stop(pkey)
123
+ rescue Jerbil::InvalidPrivateKey
124
+ # process must exist but something went wrong with the key
125
+ puts "Invalid or missing private key: #{pkey}" if verbose
126
+ if pid > 0 then
127
+ puts "Manually killing the process by pid: #{pid}" if verbose
128
+ Process.kill("SIGKILL", pid.to_i)
129
+ else
130
+ # no key, no pid, but the process still exists.
131
+ puts "Jerbil Server appears to be running but there is no pid file or key file!" if verbose
132
+ exit 1
133
+ end
134
+ rescue DRb::DRbConnError
135
+ # always raised when remote end exits, so just ignore it
136
+ end
137
+
138
+ puts "Stopped Jerbil Server" if verbose
139
+ exit 0