Browse files

Merge pull request #12 from kares/master

avoid jsvc -user and use sudo instead if $RUN_USER provided
  • Loading branch information...
2 parents 120d56e + ed94216 commit 72d1fd587fa3e100798133fa6b7669203711db87 @kares kares committed Jan 20, 2012
Showing with 114 additions and 44 deletions.
  1. +6 −2 init.d/trinidad.erb
  2. +23 −7 lib/trinidad_init_services/configuration.rb
  3. +85 −35 spec/trinidad_init_services/configuration_spec.rb
View
8 init.d/trinidad.erb
@@ -15,6 +15,7 @@ JAVA_HOME=<%= @java_home %>
JRUBY_HOME=<%= @jruby_home %>
APP_PATH=<%= @app_path %>
RUBY_SCRIPT=<%= @trinidad_daemon_path %>
+RUN_USER="<%= @run_user %>"
# Add here the options that Trinidad needs to run your application,
# but DO NOT delete the -d option, i.e -e production
@@ -50,16 +51,19 @@ JSVC_ARGS="-home $JAVA_HOME \
$JSVC_ARGS_EXTRA \
-wait 20 \
-pidfile $PIDFILE \
- -user $USER \
-procname jsvc-$PROC_NAME \
-jvm server
-outfile $LOG_FILE \
-errfile &1"
-
+
#
# Stop/Start
#
+if [[ -n $RUN_USER && $EUID -eq 0 ]]; then
+ JSVC="sudo -u $RUN_USER $JSVC"
+fi
+
STOP_COMMAND="$JSVC $JSVC_ARGS -stop $MAIN_CLASS"
START_COMMAND="$JSVC $JSVC_ARGS -cp $CLASSPATH $JAVA_PROPS $JAVA_OPTS $MAIN_CLASS $RUBY_SCRIPT $TRINIDAD_OPTS"
View
30 lib/trinidad_init_services/configuration.rb
@@ -59,20 +59,25 @@ def configure_unix_daemon(defaults)
@output_path = defaults["output_path"] || ask_path('init.d output path?', '/etc/init.d')
@pid_file = defaults["pid_file"] || ask_path('pid file?', '/var/run/trinidad/trinidad.pid')
@log_file = defaults["log_file"] || ask_path('log file?', '/var/log/trinidad/trinidad.log')
-
+ @run_user = defaults["run_user"] || ask('run daemon as user (enter a non-root username or leave blank)?', '')
+
+ if @run_user != '' && `id -u #{@run_user}` == ''
+ raise ArgumentError, "user '#{@run_user}' does not exist (leave blank if you're planning to `useradd' later)"
+ end
+
+ make_path_dir(@pid_file, "could not create dir for '#{@pid_file}', make sure dir exists before running daemon")
+ make_path_dir(@log_file, "could not create dir for '#{@log_file}', make sure dir exists before running daemon")
+
daemon = ERB.new(
File.read(
File.expand_path('../../init.d/trinidad.erb', File.dirname(__FILE__))
)
).result(binding)
puts "Moving trinidad to #{@output_path}"
- tmp_file = "#{@output_path}/trinidad"
- File.open(tmp_file, 'w') do |file|
- file.write(daemon)
- end
-
- FileUtils.chmod(0744, tmp_file)
+ trinidad_file = File.join(@output_path, "trinidad")
+ File.open(trinidad_file, 'w') { |file| file.write(daemon) }
+ FileUtils.chmod(@run_user == '' ? 0744 : 0755, trinidad_file)
end
def configure_windows_service
@@ -135,6 +140,17 @@ def prunsrv_path
File.join(@jars_path, prunsrv)
end
+ def make_path_dir(path, error = nil)
+ dir = File.dirname(path)
+ return if File.exist?(dir)
+ begin
+ FileUtils.mkdir_p dir, :mode => 0775
+ rescue Errno::EACCES => e
+ raise unless error
+ puts "#{error} (#{e})"
+ end
+ end
+
def ask_path(question, default = nil)
File.expand_path(ask(question, default))
end
View
120 spec/trinidad_init_services/configuration_spec.rb
@@ -1,37 +1,22 @@
require File.expand_path('spec_helper', File.join(File.dirname(__FILE__), '..'))
require 'yaml'
+require 'rbconfig'
describe Trinidad::InitServices::Configuration do
-
- before do
+
+ before :each do
Dir.mkdir(tmp_dir) unless File.exist?(tmp_dir)
- Dir.mkdir(init_dir)
-
- config = <<YAML
-app_path: "tmp/app"
-trinidad_options: "-e production"
-jruby_home: "tmp/jruby"
-ruby_compat_version: RUBY1_8
-trinidad_name: Trinidad
-jsvc_path: "tmp/jsvc"
-java_home: "tmp/java"
-output_path: "tmp/etc_init.d"
-pid_file: "tmp/trinidad.pid"
-log_file: "tmp/trinidad.log"
-YAML
-
- defaults = YAML::load(config)
-
- subject.configure(defaults)
+ Dir.mkdir(init_dir)
end
- after do
- File.delete(init_file)
- Dir.rmdir(init_dir)
- Dir.rmdir(tmp_dir)
+ after :each do
+ FileUtils.rm_r init_dir
+ Dir.rmdir(tmp_dir) if Dir.entries(tmp_dir) == [ '.', '..' ]
end
- it "is creates the init.d file" do
+ it "creates the init.d file" do
+ subject.configure(config_defaults)
+
File.exist?(init_file).should be_true
init_file_content = File.read(init_file)
@@ -41,21 +26,86 @@
init_file_content.match(/JRUBY_HOME=tmp\/jruby/).should be_true
init_file_content.match(/APP_PATH=tmp\/app/).should be_true
init_file_content.match(/TRINIDAD_OPTS="-d tmp\/app -e production"/).should be_true
+
+ init_file_content.match(/RUN_USER=""/).should be_true
end
- def init_file
- "#{init_dir}/trinidad"
- end
+ it "makes pid_file and log_file dirs" do
+ pids_dir = File.join(tmp_dir, "pids")
+ logs_dir = File.join(tmp_dir, "logs")
+ begin
+ subject.configure(
+ config_defaults.merge 'pid_file' => "tmp/pids/trinidad.pid", 'log_file' => "tmp/logs/trinidad.log"
+ )
+
+ File.exist?(pids_dir).should be_true
+ File.directory?(pids_dir).should be_true
+ Dir.entries(pids_dir).should == ['.', '..']
- def init_dir
- "#{tmp_dir}/etc_init.d/"
+ File.exist?(logs_dir).should be_true
+ File.directory?(logs_dir).should be_true
+ Dir.entries(logs_dir).should == ['.', '..']
+ ensure
+ Dir.rmdir(pids_dir) if File.exist?(pids_dir)
+ Dir.rmdir(logs_dir) if File.exist?(logs_dir)
+ end
end
+
+ if RbConfig::CONFIG['host_os'] !~ /mswin|mingw/i
- def tmp_dir
- "#{root_dir}/tmp"
- end
+ it "fails for non-existing run user" do
+ username = random_username
+ lambda {
+ subject.configure(config_defaults.merge 'run_user' => username)
+ }.should raise_error(ArgumentError)
+ end
+
+ it "sets valid run user" do
+ username = `whoami`.chomp
+ subject.configure(config_defaults.merge 'run_user' => username)
- def root_dir
- File.dirname(__FILE__) + "/../../"
+ init_file_content = File.read(init_file) rescue ''
+ init_file_content.match(/RUN_USER="#{username}"/).should be_true
+ end
+
end
+
+ private
+
+ def config_defaults
+ YAML::load %Q{
+app_path: "tmp/app"
+trinidad_options: "-e production"
+jruby_home: "tmp/jruby"
+ruby_compat_version: RUBY1_8
+trinidad_name: Trinidad
+jsvc_path: "tmp/jsvc"
+java_home: "tmp/java"
+output_path: "tmp/etc_init.d"
+pid_file: "tmp/trinidad.pid"
+log_file: "tmp/trinidad.log"
+run_user: ""
+}
+ end
+
+ def init_file
+ File.join init_dir, 'trinidad'
+ end
+
+ def init_dir
+ File.join tmp_dir, 'etc_init.d'
+ end
+
+ def tmp_dir
+ File.join root_dir, 'tmp'
+ end
+
+ def root_dir
+ File.join File.dirname(__FILE__), "/../../"
+ end
+
+ def random_username(len = 8)
+ (0...len).map{ ( 65 + rand(25) ).chr }.join.downcase
+ end
+
end

0 comments on commit 72d1fd5

Please sign in to comment.