trinidad_daemon 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ == Trinidad
2
+
3
+ Copyright (c) 2009 David Calavera
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ == Additional Bundled Software
25
+
26
+ Apache Commons Daemon is licensed according to the terms of Apache License, Version 2.0 (current). See http://www.apache.org/licenses/LICENSE-2.0 for details.
data/README ADDED
@@ -0,0 +1,45 @@
1
+ Trinidad daemon
2
+ ===============
3
+
4
+ Trinidad's daemon library based on Apache Commons Daemon and JRuby-jsvc.
5
+
6
+ It should replace the daemon_extension in a near future due to the problem we
7
+ are finding with the current forking process.
8
+
9
+ Dependencies
10
+ ============
11
+
12
+ The Jsvc binary file is required, but it's already available for all the
13
+ platforms supported as a bundled package, i.e:
14
+
15
+ Debian/Ubuntu:
16
+ $ sudo apt-get install jsvc
17
+ Mac OS X:
18
+ $ brew install jsvc
19
+
20
+ Those binaries are also available in the Apache distribution directory:
21
+
22
+ http://www.apache.org/dist/commons/daemon/binaries/1.0.3/
23
+
24
+ Installation
25
+ ============
26
+
27
+ When the gem is installed the user must launch the installation process:
28
+
29
+ $ jruby -S trinidad_daemon_install
30
+
31
+ This installer guides you through the configuration process and generates a
32
+ init.d script for your platform.
33
+
34
+ Execution
35
+ =========
36
+
37
+ When the installation process finishes you can use the script trinidad-daemon.sh
38
+ generated to launch the server as a daemon with the options start|stop|restart,
39
+ i.e:
40
+
41
+ $ /etc/init.d/trinidad-daemon.sh restart
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2010 David Calavera<calavera@apache.org>. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,146 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rake/rdoctask'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ task :release => :build do
91
+ unless `git branch` =~ /^\* master$/
92
+ puts "You must be on the master branch to release!"
93
+ exit!
94
+ end
95
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
96
+ sh "git tag v#{version}"
97
+ sh "git push origin master"
98
+ sh "git push origin v#{version}"
99
+ sh "gem push pkg/#{name}-#{version}.gem"
100
+ end
101
+
102
+ task :build => :gemspec do
103
+ sh "mkdir -p pkg"
104
+ sh "gem build #{gemspec_file}"
105
+ sh "mv #{gem_file} pkg"
106
+ end
107
+
108
+ task :gemspec => :validate do
109
+ # read spec file and split out manifest section
110
+ spec = File.read(gemspec_file)
111
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
112
+
113
+ # replace name version and date
114
+ replace_header(head, :name)
115
+ replace_header(head, :version)
116
+ replace_header(head, :date)
117
+ #comment this out if your rubyforge_project has a different name
118
+ replace_header(head, :rubyforge_project)
119
+
120
+ # determine file list from git ls-files
121
+ files = `git ls-files`.
122
+ split("\n").
123
+ sort.
124
+ reject { |file| file =~ /^\./ }.
125
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
126
+ map { |file| " #{file}" }.
127
+ join("\n")
128
+
129
+ # piece file back together and write
130
+ manifest = " s.files = %w[\n#{files}\n ]\n"
131
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
132
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
133
+ puts "Updated #{gemspec_file}"
134
+ end
135
+
136
+ task :validate do
137
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
138
+ unless libfiles.empty?
139
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
140
+ exit!
141
+ end
142
+ unless Dir['VERSION*'].empty?
143
+ puts "A `VERSION` file at root level violates Gem best practices."
144
+ exit!
145
+ end
146
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ __LIB_DIR__ = File.expand_path('../lib', File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(__LIB_DIR__) unless $LOAD_PATH.include? __LIB_DIR__
5
+
6
+ require 'trinidad_daemon/configuration'
7
+
8
+ Trinidad::Daemon::Configuration.new.configure
@@ -0,0 +1,126 @@
1
+ #! /bin/sh
2
+ # Generic script for running ruby scripts as daemons using
3
+ # jsvc and a java class to control the daemon.
4
+ #
5
+ # Contains common parameters and start/stop
6
+
7
+ # Things you'll need to set on a per script/daemon basis:
8
+ # SCRIPT_NAME - Path to the ruby script which creates a Daemon
9
+ # object for jsvc to control
10
+ # APP_NAME - Name of your application
11
+ #
12
+ # Things you can set:
13
+ # PROG_OPTS - Arguments to send to the program. A few defaults are appended to this.
14
+
15
+ JSVC=<%= @jsvc %>
16
+ JAVA_HOME=<%= @java_home %>
17
+ JRUBY_HOME=<%= @jruby_home %>
18
+ APP_PATH=<%= @app_path %>
19
+ RUBY_SCRIPT=<%= @trinidad_daemon_path %>
20
+ JRUBY_JSVC_JAR=<%= "#{@jars_path}/jruby-jsvc.jar" %>
21
+ DAEMON_JAR=<%= "#{@jars_path}/commons-daemon.jar" %>
22
+ BSF_JAR=<%= "#{@jars_path}/bsf.jar" %>
23
+
24
+ # Add here the options that Trinidad needs to run your application,
25
+ # but DO NOT delete the -d option, i.e -e production
26
+ TRINIDAD_OPTS="-d $APP_PATH"
27
+
28
+ PIDFILE=<%= @pid_file %>
29
+ LOG_FILE=<%= @log_file %>
30
+
31
+ # Implements the jsvc Daemon interface.
32
+ MAIN_CLASS=com.msp.jsvc.JRubyDaemon
33
+
34
+ CLASSPATH=$JRUBY_HOME/lib/jruby.jar:$JRUBY_HOME/lib/profile.jar:$DAEMON_JAR:$JRUBY_JSVC_JAR
35
+
36
+ echo "CLASSPATH : $CLASSPATH"
37
+
38
+ # TODO: Allow configuration or detect from the OS
39
+ JAVA_NATIVE_PROPS="-Djna.boot.library.path=$JRUBY_HOME/lib/native/linux-i386:$JRUBY_HOME/lib/native/linux-amd64 \
40
+ -Djffi.boot.library.path=$JRUBY_HOME/lib/native/i386-Linux:$JRUBY_HOME/lib/native/s390x-Linux:$JRUBY_HOME/lib/native/x86_64-Linux"
41
+
42
+ JAVA_PROPS="$JAVA_PROPS -Djruby.memory.max=500m \
43
+ -Djruby.stack.max=1024k \
44
+ $JAVA_NATIVE_PROPS \
45
+ -Djruby.home=$JRUBY_HOME \
46
+ -Djruby.lib=$JRUBY_HOME/lib \
47
+ -Djruby.script=jruby \
48
+ -Djruby.shell=/bin/sh
49
+ -Djruby.daemon.module.name=Trinidad"
50
+
51
+ JAVA_OPTS="-Xmx500m -Xss1024k -Xbootclasspath/a:$JRUBY_HOME/lib/jruby.jar"
52
+
53
+ JSVC_ARGS="-home $JAVA_HOME \
54
+ $JSVC_ARGS_EXTRA \
55
+ -wait 20 \
56
+ -pidfile $PIDFILE \
57
+ -user $USER \
58
+ -procname jsvc-$SCRIPT_NAME \
59
+ -jvm server
60
+ -outfile $LOG_FILE \
61
+ -errfile &1"
62
+
63
+ #
64
+ # Stop/Start
65
+ #
66
+
67
+ STOP_COMMAND="$JSVC $JSVC_ARGS -stop $MAIN_CLASS"
68
+ START_COMMAND="$JSVC $JSVC_ARGS -cp $CLASSPATH $JAVA_PROPS $JAVA_OPTS $MAIN_CLASS $RUBY_SCRIPT $TRINIDAD_OPTS"
69
+
70
+ case "$1" in
71
+ start)
72
+ if [ -e "$PIDFILE" ]; then
73
+ echo "Pidfile already exists, not starting."
74
+ exit 1
75
+ else
76
+ echo "Starting $SCRIPT_NAME daemon..."
77
+ $START_COMMAND
78
+ EXIT_CODE=$?
79
+ if [ "$EXIT_CODE" != 0 ]; then
80
+ echo "Daemon exited with status: $EXIT_CODE. Check pidfile and log"
81
+ fi
82
+ fi
83
+ ;;
84
+ stop)
85
+ if [ -e "$PIDFILE" ]; then
86
+ echo "Stopping $SCRIPT_NAME daemon..."
87
+ $STOP_COMMAND
88
+ else
89
+ echo "No pid file, not stopping."
90
+ exit 1
91
+ fi
92
+ ;;
93
+ restart)
94
+ if [ -e "$PIDFILE" ]; then
95
+ echo "Stopping $SCRIPT_NAME daemon..."
96
+ $STOP_COMMAND
97
+ fi
98
+ if [ -e "$PIDFILE" ]; then
99
+ echo "Pidfile still present, $SCRIPT_NAME hasn't stopped"
100
+ exit 1
101
+ else
102
+ $START_COMMAND
103
+ EXIT_CODE=$?
104
+ if [ "$EXIT_CODE" != 0 ]; then
105
+ echo "Daemon exited with status: $EXIT_CODE. Check pidfile and log"
106
+ fi
107
+ fi
108
+ ;;
109
+ status)
110
+ if [ "$PIDFILE" ]; then
111
+ PID=`cat $PIDFILE`
112
+ OUTPUT=`ps $PID | egrep "^$PID "`
113
+ if [ ${#OUTPUT} -gt 0 ]; then
114
+ echo "Service running with pid: $PID"
115
+ else
116
+ echo "Pidfile present, but process not running"
117
+ fi
118
+ else
119
+ echo "No pidfile present"
120
+ fi
121
+ ;;
122
+ *)
123
+ echo "Unrecognised command. Usage trinidad-daemon [ start | stop | restart ]"
124
+ ;;
125
+ esac
126
+
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'trinidad'
3
+
4
+ module Trinidad
5
+ module Daemon
6
+ VERSION = '0.1.0'
7
+
8
+ def init
9
+ end
10
+
11
+ def setup?
12
+ true
13
+ end
14
+
15
+ def start
16
+ opts = Trinidad::CommandLineParser.parse(ARGV)
17
+ @server = Trinidad::Server.new(opts)
18
+ @server.start
19
+ end
20
+
21
+ def stop
22
+ @server.tomcat.destroy
23
+ end
24
+
25
+ extend self
26
+ end
27
+ end
28
+
29
+ Trinidad::Daemon.init
@@ -0,0 +1,77 @@
1
+ module Trinidad
2
+ module Daemon
3
+ require 'erb'
4
+ require 'java'
5
+
6
+ class Configuration
7
+ def initialize(stdin = STDIN, stdout = STDOUT)
8
+ @stdin = stdin
9
+ @stdout = stdout
10
+ end
11
+
12
+ def configure
13
+ @app_path = File.expand_path(ask('Application path?'))
14
+ @jsvc = ask('Jsvc path?', `which jsvc`.chomp)
15
+ @java_home = ask('Java home?', default_java_home)
16
+ @jruby_home = ask('JRuby home?', default_jruby_home)
17
+ @output_path = ask('init.d output path?', '/etc/init.d')
18
+ @pid_file = ask('pid file?', '/var/run/trinidad/trinidad.pid')
19
+ @log_file = ask('log file?', '/var/log/trinidad/trinidad.log')
20
+
21
+ @trinidad_daemon_path = File.expand_path('../../trinidad_daemon.rb', __FILE__)
22
+ @jars_path = File.expand_path('../../../trinidad-libs', __FILE__)
23
+
24
+ daemon = ERB.new(
25
+ File.read(
26
+ File.expand_path('../../init.d/trinidad_daemon.sh.erb', File.dirname(__FILE__))
27
+ )
28
+ ).result(binding)
29
+
30
+ tmp_file = "#{ENV['TMP_DIR']}/trinidad-daemon.sh"
31
+ File.open(tmp_file, 'w') do |file|
32
+ file.write(daemon)
33
+ end
34
+
35
+ puts "Moving trinidad-daemon.sh to #{@output_path}"
36
+ `cp #{tmp_file} #{@output_path}`
37
+ puts 'Done.'
38
+ end
39
+
40
+ private
41
+ def default_jruby_home
42
+ Java::JavaLang::System.get_property("jruby.home")
43
+ end
44
+
45
+ def default_java_home
46
+ Java::JavaLang::System.get_property("java.home")
47
+ end
48
+
49
+ def ask(question, default = nil)
50
+ return nil if not @stdin.tty?
51
+
52
+ question << " [#{default}]" if default && !default.empty?
53
+
54
+ result = nil
55
+
56
+ while result.nil?
57
+ @stdout.print(question + " ")
58
+ @stdout.flush
59
+
60
+ result = @stdin.gets
61
+
62
+ if result
63
+ result.chomp!
64
+
65
+ result = case result
66
+ when /^$/
67
+ default
68
+ else
69
+ result
70
+ end
71
+ end
72
+ end
73
+ return result
74
+ end
75
+ end
76
+ end
77
+ end
Binary file
Binary file
@@ -0,0 +1,70 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'trinidad_daemon'
16
+ s.version = '0.1.0'
17
+ s.date = '2010-09-12'
18
+ s.rubyforge_project = 'trinidad_daemon'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "Trinidad daemon based on Apache Commons Daemon"
23
+ s.description = "Trinidad daemon based on Apache Commons Daemon and JRuby-jsvc"
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["David Calavera"]
29
+ s.email = 'calavera@apache.org'
30
+ s.homepage = 'http://github.com/calavera/trinidad_daemon'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ ## If your gem includes any executables, list them here.
37
+ s.executables = ["trinidad_daemon_install"]
38
+ s.default_executable = 'trinidad_daemon_install'
39
+
40
+ ## Specify any RDoc options here. You'll want to add your README and
41
+ ## LICENSE files to the extra_rdoc_files list.
42
+ s.rdoc_options = ["--charset=UTF-8"]
43
+ s.extra_rdoc_files = %w[README LICENSE]
44
+
45
+ ## List your runtime dependencies here. Runtime dependencies are those
46
+ ## that are needed for an end user to actually USE your code.
47
+ s.add_dependency('trinidad', '>=0.9.6')
48
+
49
+ ## Leave this section as-is. It will be automatically generated from the
50
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
51
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
52
+ # = MANIFEST =
53
+ s.files = %w[
54
+ LICENSE
55
+ README
56
+ Rakefile
57
+ bin/trinidad_daemon_install
58
+ init.d/trinidad-daemon.sh.erb
59
+ lib/trinidad_daemon.rb
60
+ lib/trinidad_daemon/configuration.rb
61
+ trinidad-libs/commons-daemon.jar
62
+ trinidad-libs/jruby-jsvc.jar
63
+ trinidad_daemon.gemspec
64
+ ]
65
+ # = MANIFEST =
66
+
67
+ ## Test files will be grabbed from the file list. Make sure the path glob
68
+ ## matches what you actually use.
69
+ ## s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
70
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trinidad_daemon
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - David Calavera
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-12 00:00:00 +02:00
18
+ default_executable: trinidad_daemon_install
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: trinidad
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 9
31
+ - 6
32
+ version: 0.9.6
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Trinidad daemon based on Apache Commons Daemon and JRuby-jsvc
36
+ email: calavera@apache.org
37
+ executables:
38
+ - trinidad_daemon_install
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ - LICENSE
44
+ files:
45
+ - LICENSE
46
+ - README
47
+ - Rakefile
48
+ - bin/trinidad_daemon_install
49
+ - init.d/trinidad-daemon.sh.erb
50
+ - lib/trinidad_daemon.rb
51
+ - lib/trinidad_daemon/configuration.rb
52
+ - trinidad-libs/commons-daemon.jar
53
+ - trinidad-libs/jruby-jsvc.jar
54
+ - trinidad_daemon.gemspec
55
+ has_rdoc: true
56
+ homepage: http://github.com/calavera/trinidad_daemon
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project: trinidad_daemon
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: Trinidad daemon based on Apache Commons Daemon
87
+ test_files: []
88
+