trinidad_init_services 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ == 1.1.3 (2012-02-20)
2
+
3
+ * Do not ask for a path with $RUN_USER.
4
+
1
5
  == 1.1.2 (2012-01-17)
2
6
 
3
7
  * Revert previous fix.
@@ -10,11 +10,22 @@
10
10
  # Things you can set:
11
11
  # PROG_OPTS - Arguments to send to the program. A few defaults are appended to this.
12
12
 
13
+ # Path to the jsvc binary, if you're having issues with the pre-compiled binary
14
+ # try installing it (e.g. apt-get install jsvc) or compiling it yourself :
15
+ # https://github.com/trinidad/trinidad_init_services/wiki/Installing-JSVC
13
16
  JSVC=<%= @jsvc %>
14
17
  JAVA_HOME=<%= @java_home %>
15
18
  JRUBY_HOME=<%= @jruby_home %>
16
19
  APP_PATH=<%= @app_path %>
17
20
  RUBY_SCRIPT=<%= @trinidad_daemon_path %>
21
+ # The user rights for the running daemon, if you don't bind directly to 80 than
22
+ # it's always a good idea to run trinidad with non-root user rights.
23
+ # Make sure this user has rights on PIDFILE and LOG_FILE paths.
24
+ # Leaving this empty will cause the daemon to always run as whichever user calls
25
+ # the script (which during init is equivalent to running it as root).
26
+ RUN_USER="<%= @run_user %>"
27
+ # Set this to "1" to echo commands before running them (for troubleshooting) :
28
+ ECHO_COMMAND=""
18
29
 
19
30
  # Add here the options that Trinidad needs to run your application,
20
31
  # but DO NOT delete the -d option, i.e -e production
@@ -50,16 +61,19 @@ JSVC_ARGS="-home $JAVA_HOME \
50
61
  $JSVC_ARGS_EXTRA \
51
62
  -wait 20 \
52
63
  -pidfile $PIDFILE \
53
- -user $USER \
54
64
  -procname jsvc-$PROC_NAME \
55
65
  -jvm server
56
66
  -outfile $LOG_FILE \
57
67
  -errfile &1"
58
-
68
+
59
69
  #
60
70
  # Stop/Start
61
71
  #
62
72
 
73
+ if [[ -n "$RUN_USER" && $EUID -eq 0 ]]; then
74
+ JSVC="sudo -u $RUN_USER $JSVC"
75
+ fi
76
+
63
77
  STOP_COMMAND="$JSVC $JSVC_ARGS -stop $MAIN_CLASS"
64
78
  START_COMMAND="$JSVC $JSVC_ARGS -cp $CLASSPATH $JAVA_PROPS $JAVA_OPTS $MAIN_CLASS $RUBY_SCRIPT $TRINIDAD_OPTS"
65
79
 
@@ -72,6 +86,7 @@ case "$1" in
72
86
  exit 1
73
87
  else
74
88
  echo "Starting $PROC_NAME daemon..."
89
+ if [ -n "$ECHO_COMMAND" ]; then echo $START_COMMAND; fi
75
90
  $START_COMMAND
76
91
  EXIT_CODE=$?
77
92
  if [ "$EXIT_CODE" != 0 ]; then
@@ -82,6 +97,7 @@ case "$1" in
82
97
  stop)
83
98
  if [ -e "$PIDFILE" ]; then
84
99
  echo "Stopping $PROC_NAME daemon..."
100
+ if [ -n "$ECHO_COMMAND" ]; then echo $STOP_COMMAND; fi
85
101
  $STOP_COMMAND
86
102
  else
87
103
  echo "No pid file, not stopping."
@@ -91,12 +107,14 @@ case "$1" in
91
107
  restart)
92
108
  if [ -e "$PIDFILE" ]; then
93
109
  echo "Stopping $PROC_NAME daemon..."
110
+ if [ -n "$ECHO_COMMAND" ]; then echo $STOP_COMMAND; fi
94
111
  $STOP_COMMAND
95
112
  fi
96
113
  if [ -e "$PIDFILE" ]; then
97
114
  echo "Pidfile still present, $PROC_NAME hasn't stopped"
98
115
  exit 1
99
116
  else
117
+ if [ -n "$ECHO_COMMAND" ]; then echo $START_COMMAND; fi
100
118
  $START_COMMAND
101
119
  EXIT_CODE=$?
102
120
  if [ "$EXIT_CODE" != 0 ]; then
@@ -3,7 +3,7 @@ require 'trinidad'
3
3
 
4
4
  module Trinidad
5
5
  module Daemon
6
- VERSION = '1.1.2'
6
+ VERSION = '1.1.3'
7
7
 
8
8
  def init
9
9
  end
@@ -12,10 +12,10 @@ module Trinidad
12
12
  true
13
13
  end
14
14
 
15
- def start
16
- opts = Trinidad::CommandLineParser.parse(ARGV)
17
- opts[:trap] = false
18
- @server = Trinidad::Server.new(opts)
15
+ def start(args = ARGV)
16
+ Trinidad::CommandLineParser.parse(args)
17
+ Trinidad.configuration.trap = false
18
+ @server = Trinidad::Server.new
19
19
  @server.start
20
20
  end
21
21
 
@@ -59,7 +59,15 @@ module Trinidad
59
59
  @output_path = defaults["output_path"] || ask_path('init.d output path?', '/etc/init.d')
60
60
  @pid_file = defaults["pid_file"] || ask_path('pid file?', '/var/run/trinidad/trinidad.pid')
61
61
  @log_file = defaults["log_file"] || ask_path('log file?', '/var/log/trinidad/trinidad.log')
62
-
62
+ @run_user = defaults["run_user"] || ask('run daemon as user (enter a non-root username or leave blank)?', '')
63
+
64
+ if @run_user != '' && `id -u #{@run_user}` == ''
65
+ raise ArgumentError, "user '#{@run_user}' does not exist (leave blank if you're planning to `useradd' later)"
66
+ end
67
+
68
+ make_path_dir(@pid_file, "could not create dir for '#{@pid_file}', make sure dir exists before running daemon")
69
+ make_path_dir(@log_file, "could not create dir for '#{@log_file}', make sure dir exists before running daemon")
70
+
63
71
  daemon = ERB.new(
64
72
  File.read(
65
73
  File.expand_path('../../init.d/trinidad.erb', File.dirname(__FILE__))
@@ -67,12 +75,9 @@ module Trinidad
67
75
  ).result(binding)
68
76
 
69
77
  puts "Moving trinidad to #{@output_path}"
70
- tmp_file = "#{@output_path}/trinidad"
71
- File.open(tmp_file, 'w') do |file|
72
- file.write(daemon)
73
- end
74
-
75
- FileUtils.chmod(0744, tmp_file)
78
+ trinidad_file = File.join(@output_path, "trinidad")
79
+ File.open(trinidad_file, 'w') { |file| file.write(daemon) }
80
+ FileUtils.chmod(@run_user == '' ? 0744 : 0755, trinidad_file)
76
81
  end
77
82
 
78
83
  def configure_windows_service
@@ -135,6 +140,17 @@ module Trinidad
135
140
  File.join(@jars_path, prunsrv)
136
141
  end
137
142
 
143
+ def make_path_dir(path, error = nil)
144
+ dir = File.dirname(path)
145
+ return if File.exist?(dir)
146
+ begin
147
+ FileUtils.mkdir_p dir, :mode => 0775
148
+ rescue Errno::EACCES => e
149
+ raise unless error
150
+ puts "#{error} (#{e})"
151
+ end
152
+ end
153
+
138
154
  def ask_path(question, default = nil)
139
155
  File.expand_path(ask(question, default))
140
156
  end
@@ -0,0 +1,6 @@
1
+ Trinidad.configure do |config|
2
+ config.port = 3002
3
+ config.address = '127.0.0.1'
4
+ config.jruby_min_runtimes = 1
5
+ config.jruby_max_runtimes = 2
6
+ end
@@ -0,0 +1,5 @@
1
+ ---
2
+ port: 3001
3
+ context_path: "/foo"
4
+ jruby_min_runtimes: 1
5
+ jruby_max_runtimes: 1
@@ -0,0 +1,69 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+ require 'trinidad_init_services'
3
+
4
+ Trinidad::Daemon.module_eval do
5
+ def server; @server; end
6
+ end
7
+
8
+ describe Trinidad::Daemon do
9
+
10
+ after :each do
11
+ Trinidad.configuration = nil
12
+ end
13
+
14
+ it "starts a configured server" do
15
+ Trinidad::Server.any_instance.expects(:start)
16
+ Trinidad::Daemon.start([])
17
+ Trinidad::Daemon.server.should be_a(Trinidad::Server)
18
+ Trinidad::Daemon.server.config.should be_a(Trinidad::Configuration)
19
+ end
20
+
21
+ context "started with a yaml config" do
22
+
23
+ before :each do
24
+ Trinidad::Server.any_instance.stubs(:start)
25
+ config = File.expand_path('stubs/trinidad.yml', File.dirname(__FILE__))
26
+ Trinidad::Daemon.start([ '--config', config ])
27
+ @config = Trinidad::Daemon.server.config
28
+ end
29
+
30
+ it "is configured without signal trap" do
31
+ @config[:trap].should == false
32
+ end
33
+
34
+ it "is configured according to .yml" do
35
+ @config[:port].should == 3001
36
+ @config[:context_path].should == '/foo'
37
+ @config[:jruby_min_runtimes].should == 1
38
+ @config[:jruby_max_runtimes].should == 1
39
+
40
+ @config[:address].should == 'localhost'
41
+ end
42
+
43
+ end
44
+
45
+ context "started with a ruby config" do
46
+
47
+ before :each do
48
+ Trinidad::Server.any_instance.stubs(:start)
49
+ config = File.expand_path('stubs/trinidad.rb', File.dirname(__FILE__))
50
+ Trinidad::Daemon.start([ '--config', config ])
51
+ @config = Trinidad::Daemon.server.config
52
+ end
53
+
54
+ it "is configured without signal trap" do
55
+ @config[:trap].should == false
56
+ end
57
+
58
+ it "is configured according to .rb" do
59
+ @config[:port].should == 3002
60
+ @config[:address].should == '127.0.0.1'
61
+ @config[:jruby_min_runtimes].should == 1
62
+ @config[:jruby_max_runtimes].should == 2
63
+
64
+ @config[:context_path].should == '/'
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -1,37 +1,22 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require File.expand_path('spec_helper', File.join(File.dirname(__FILE__), '..'))
2
2
  require 'yaml'
3
+ require 'rbconfig'
3
4
 
4
5
  describe Trinidad::InitServices::Configuration do
5
-
6
- before do
6
+
7
+ before :each do
7
8
  Dir.mkdir(tmp_dir) unless File.exist?(tmp_dir)
8
- Dir.mkdir(init_dir)
9
-
10
- config = <<YAML
11
- app_path: "tmp/app"
12
- trinidad_options: "-e production"
13
- jruby_home: "tmp/jruby"
14
- ruby_compat_version: RUBY1_8
15
- trinidad_name: Trinidad
16
- jsvc_path: "tmp/jsvc"
17
- java_home: "tmp/java"
18
- output_path: "tmp/etc_init.d"
19
- pid_file: "tmp/trinidad.pid"
20
- log_file: "tmp/trinidad.log"
21
- YAML
22
-
23
- defaults = YAML::load(config)
24
-
25
- subject.configure(defaults)
9
+ Dir.mkdir(init_dir)
26
10
  end
27
11
 
28
- after do
29
- File.delete(init_file)
30
- Dir.rmdir(init_dir)
31
- Dir.rmdir(tmp_dir)
12
+ after :each do
13
+ FileUtils.rm_r init_dir
14
+ Dir.rmdir(tmp_dir) if Dir.entries(tmp_dir) == [ '.', '..' ]
32
15
  end
33
16
 
34
- it "is creates the init.d file" do
17
+ it "creates the init.d file" do
18
+ subject.configure(config_defaults)
19
+
35
20
  File.exist?(init_file).should be_true
36
21
 
37
22
  init_file_content = File.read(init_file)
@@ -41,21 +26,86 @@ YAML
41
26
  init_file_content.match(/JRUBY_HOME=tmp\/jruby/).should be_true
42
27
  init_file_content.match(/APP_PATH=tmp\/app/).should be_true
43
28
  init_file_content.match(/TRINIDAD_OPTS="-d tmp\/app -e production"/).should be_true
29
+
30
+ init_file_content.match(/RUN_USER=""/).should be_true
44
31
  end
45
32
 
46
- def init_file
47
- "#{init_dir}/trinidad"
48
- end
33
+ it "makes pid_file and log_file dirs" do
34
+ pids_dir = File.join(tmp_dir, "pids")
35
+ logs_dir = File.join(tmp_dir, "logs")
36
+ begin
37
+ subject.configure(
38
+ config_defaults.merge 'pid_file' => "tmp/pids/trinidad.pid", 'log_file' => "tmp/logs/trinidad.log"
39
+ )
40
+
41
+ File.exist?(pids_dir).should be_true
42
+ File.directory?(pids_dir).should be_true
43
+ Dir.entries(pids_dir).should == ['.', '..']
49
44
 
50
- def init_dir
51
- "#{tmp_dir}/etc_init.d/"
45
+ File.exist?(logs_dir).should be_true
46
+ File.directory?(logs_dir).should be_true
47
+ Dir.entries(logs_dir).should == ['.', '..']
48
+ ensure
49
+ Dir.rmdir(pids_dir) if File.exist?(pids_dir)
50
+ Dir.rmdir(logs_dir) if File.exist?(logs_dir)
51
+ end
52
52
  end
53
+
54
+ if RbConfig::CONFIG['host_os'] !~ /mswin|mingw/i
53
55
 
54
- def tmp_dir
55
- "#{root_dir}/tmp"
56
- end
56
+ it "fails for non-existing run user" do
57
+ username = random_username
58
+ lambda {
59
+ subject.configure(config_defaults.merge 'run_user' => username)
60
+ }.should raise_error(ArgumentError)
61
+ end
62
+
63
+ it "sets valid run user" do
64
+ username = `whoami`.chomp
65
+ subject.configure(config_defaults.merge 'run_user' => username)
57
66
 
58
- def root_dir
59
- File.dirname(__FILE__) + "/../../"
67
+ init_file_content = File.read(init_file) rescue ''
68
+ init_file_content.match(/RUN_USER="#{username}"/).should be_true
69
+ end
70
+
60
71
  end
72
+
73
+ private
74
+
75
+ def config_defaults
76
+ YAML::load %Q{
77
+ app_path: "tmp/app"
78
+ trinidad_options: "-e production"
79
+ jruby_home: "tmp/jruby"
80
+ ruby_compat_version: RUBY1_8
81
+ trinidad_name: Trinidad
82
+ jsvc_path: "tmp/jsvc"
83
+ java_home: "tmp/java"
84
+ output_path: "tmp/etc_init.d"
85
+ pid_file: "tmp/trinidad.pid"
86
+ log_file: "tmp/trinidad.log"
87
+ run_user: ""
88
+ }
89
+ end
90
+
91
+ def init_file
92
+ File.join init_dir, 'trinidad'
93
+ end
94
+
95
+ def init_dir
96
+ File.join tmp_dir, 'etc_init.d'
97
+ end
98
+
99
+ def tmp_dir
100
+ File.join root_dir, 'tmp'
101
+ end
102
+
103
+ def root_dir
104
+ File.join File.dirname(__FILE__), "/../../"
105
+ end
106
+
107
+ def random_username(len = 8)
108
+ (0...len).map{ ( 65 + rand(25) ).chr }.join.downcase
109
+ end
110
+
61
111
  end
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'trinidad_init_services'
16
- s.version = '1.1.2'
17
- s.date = '2012-01-17'
16
+ s.version = '1.1.3'
17
+ s.date = '2012-02-20'
18
18
  s.rubyforge_project = 'trinidad_init_services'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -44,8 +44,11 @@ Gem::Specification.new do |s|
44
44
 
45
45
  ## List your runtime dependencies here. Runtime dependencies are those
46
46
  ## that are needed for an end user to actually USE your code.
47
- s.add_dependency('trinidad', '>=1.2.2')
47
+ s.add_dependency('trinidad', '>= 1.3.2')
48
48
 
49
+ s.add_development_dependency('rspec', '>= 2.7.1')
50
+ s.add_development_dependency('mocha', '>= 0.10')
51
+
49
52
  ## Leave this section as-is. It will be automatically generated from the
50
53
  ## contents of your Git repository via the gemspec task. DO NOT REMOVE
51
54
  ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
@@ -60,6 +63,9 @@ Gem::Specification.new do |s|
60
63
  lib/trinidad_init_services.rb
61
64
  lib/trinidad_init_services/configuration.rb
62
65
  spec/spec_helper.rb
66
+ spec/stubs/trinidad.rb
67
+ spec/stubs/trinidad.yml
68
+ spec/trinidad_daemon_spec.rb
63
69
  spec/trinidad_init_services/configuration_spec.rb
64
70
  trinidad-libs/commons-daemon.jar
65
71
  trinidad-libs/jruby-jsvc.jar
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trinidad_init_services
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,41 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-17 00:00:00.000000000 Z
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: trinidad
16
- requirement: &2153350240 !ruby/object:Gem::Requirement
16
+ requirement: &2162638580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.2.2
21
+ version: 1.3.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153350240
24
+ version_requirements: *2162638580
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2162638100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.7.1
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2162638100
36
+ - !ruby/object:Gem::Dependency
37
+ name: mocha
38
+ requirement: &2162637580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0.10'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2162637580
25
47
  description: Trinidad init service scripts on Apache Commons Daemon and JRuby-jsvc,
26
48
  compatible with Unix and Windows services
27
49
  email: calavera@apache.org
@@ -41,6 +63,9 @@ files:
41
63
  - lib/trinidad_init_services.rb
42
64
  - lib/trinidad_init_services/configuration.rb
43
65
  - spec/spec_helper.rb
66
+ - spec/stubs/trinidad.rb
67
+ - spec/stubs/trinidad.yml
68
+ - spec/trinidad_daemon_spec.rb
44
69
  - spec/trinidad_init_services/configuration_spec.rb
45
70
  - trinidad-libs/commons-daemon.jar
46
71
  - trinidad-libs/jruby-jsvc.jar