Skip to content

Commit

Permalink
do a service update when already installed (on windoze) + better serv…
Browse files Browse the repository at this point in the history
…ice removal
  • Loading branch information
kares committed Jul 2, 2014
1 parent 7d404a0 commit 40e6243
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 44 deletions.
71 changes: 46 additions & 25 deletions lib/trinidad_init_services/configuration.rb
Expand Up @@ -222,12 +222,12 @@ def configure_unix_daemon(defaults, java_home = default_java_home)
@out_file = File.join(@out_file, 'trinidad.out') if File.exist?(@out_file) && File.directory?(@out_file)
make_path_dir(@out_file, "could not create dir for '#{@out_file}', make sure dir exists before running daemon")

@run_user = defaults["run_user"] || ask('run daemon as user (enter a non-root username or leave blank)', '')
@run_user = defaults['run_user'] || ask('run daemon as user (enter a non-root username or leave blank)', '')
if ! @run_user.empty? && `id -u #{@run_user}` == ''
raise ArgumentError, "user '#{@run_user}' does not exist (leave blank if you're planning to `useradd' later)"
end

@output_path = defaults["output_path"] || ask_path('init.d output path', '/etc/init.d')
@output_path = defaults['output_path'] || ask_path('init.d output path', '/etc/init.d')

require('erb'); daemon = ERB.new(
File.read(
Expand Down Expand Up @@ -277,7 +277,7 @@ def configure_windows_service(defaults, java_home = default_java_home)

log_path = defaults['log_path'] || "%SystemRoot%\\System32\\LogFiles\\#{@service_id}"
@out_file = defaults['out_file'] || defaults['log_file'] ||
ask_path('out file (where system out/err gets redirected), leave blank for prunsrv default')
ask_path('out file (where system out/err gets redirected), leave blank for prunsrv default', '')
@pid_file = defaults['pid_file'] || "#{@service_id}.pid"

#stop_timeout = defaults['stop_timeout'] || 5
Expand All @@ -292,7 +292,13 @@ def configure_windows_service(defaults, java_home = default_java_home)
# //DS Delete service Stops the service first if it is currently running
# //PP[//seconds] Pause Default is 60 seconds

command = %Q{//IS//#{@service_id} --DisplayName="#{@service_name}"}
if service_listed_windows?(@service_id)
say "service '#{@service_id}' already installed, will update instead of install"
command = %Q{//US//#{@service_id} --DisplayName="#{@service_name}"}
else
command = %Q{//IS//#{@service_id} --DisplayName="#{@service_name}"}
end

command << " --Description=\"#{@service_desc}\""
command << " --Install=#{srv_path} --Jvm=auto"
command << " --JavaHome=\"#{escape_windows_path(@java_home)}\""
Expand Down Expand Up @@ -327,10 +333,17 @@ def uninstall(service = nil)
windows? ? uninstall_windows_service(service) : uninstall_unix_daemon(service)
end

def uninstall_windows_service(service_name)
def uninstall_windows_service(service)
srv_path = detect_prunsrv_path
exec_system "#{srv_path} stop #{service_name}", :allow_failure
exec_system "#{srv_path} delete #{service_name}"
exec_system "#{srv_path} //DS//#{service}" # does stop first if needed
end

def service_listed_windows?(service)
# *sc* will allow SERVICE_NAME only (DISPLAY_NAME won't work)
out = `sc queryex type= service state= all | find "#{service}"`
return false if out.chomp.empty?
# "SERVICE_NAME: Trinidad\nDISPLAY_NAME: Trinidad\n"
!! out =~ /SERVICE_NAME: #{service}$/i
end

def uninstall_unix_daemon(service)
Expand Down Expand Up @@ -368,8 +381,7 @@ def service_listed_unix?(service)
private

def exec_system(command, allow_failure = nil)
say command
log && (log.puts "#{command}\n\n"; log.flush)
log_command command
ok = system command
unless allow_failure
raise "could not execute `#{command}`" if ok.nil?
Expand All @@ -378,11 +390,18 @@ def exec_system(command, allow_failure = nil)
ok
end

def log_command(command)
say command
log && (log.puts "#{command}\n\n"; log.flush)
end

def log
return @_log if defined? @_log
@_log = File.open('trinidad_init_service.log', 'w') rescue nil
return @_log || nil if defined?(@_log) && ! @_log.nil?
( @_log = File.open('trinidad_init_service.log', 'w') rescue false ) || nil
end

def log?; defined?(@_log) ? !! @_log : nil end

def escape_windows_path(path)
path.gsub(%r{/}, '\\')
end
Expand Down Expand Up @@ -588,17 +607,18 @@ def make_path_dir(path, error = nil)
end
end

public

def ask_path(question, default = nil)
path = ask(question, default)
unless path # nil, false
block_given? ? yield : raise("#{question.inspect} not provided!") if path == false
return path # nil
unless path = ask(question, default) # nil, false
return path if path.nil?
block_given? ? yield : raise("#{question.inspect} not provided!")
end
path.empty? ? path : File.expand_path(path)
end

def ask(question, default = nil)
return default if ! @stdin.tty? || @ask == false
return default if ! @stdin.tty? || ! ask?

question = "#{question}?" if ! question.index('?') || ! question.index(':')
question += " [#{default}]" if default &&
Expand All @@ -613,27 +633,28 @@ def ask(question, default = nil)

if result
result.chomp!
case result
when /^$/
result = default
end
result = default if result.size == 0
end
end
result
end

def ask=(flag)
@ask = !!flag
def ask?
@ask = true unless defined? @ask; return @ask
end

def ask=(flag); @ask = !!flag end
public :ask=

def say(msg)
puts msg unless @say == false
puts msg if say?
end

def say=(flag)
@say = !!flag
def say?
@say = true unless defined? @say; return @say
end

def say=(flag); @say = !!flag end
public :say=

end
Expand Down
60 changes: 41 additions & 19 deletions spec/trinidad_init_services/configuration_spec.rb
Expand Up @@ -23,21 +23,21 @@
it "creates the init.d file" do
subject.configure config_defaults.merge 'java_home' => 'tmp/java', 'jruby_home' => 'tmp/jruby'

File.exist?(init_file).should be_true
expect( File.exist?(init_file) ).to be true

init_file_content = File.read(init_file)

init_file_content.match(/JSVC=tmp\/jsvc\/bin\/jsvc/).should be_true
init_file_content.match(/JAVA_HOME="tmp\/java"/).should be_true
init_file_content.match(/JRUBY_HOME="tmp\/jruby"/).should be_true
init_file_content.match(/BASE_PATH="tmp\/app"/).should be_true
expect( init_file_content ).to match(/JSVC=tmp\/jsvc\/bin\/jsvc/)
expect( init_file_content ).to match(/JAVA_HOME="tmp\/java"/)
expect( init_file_content ).to match(/JRUBY_HOME="tmp\/jruby"/)
expect( init_file_content ).to match(/BASE_PATH="tmp\/app"/)

init_file_content.match(/PID_FILE="tmp\/trinidad.pid"/).should be_true
init_file_content.match(/OUT_FILE="tmp\/trinidad.out"/).should be_true
expect( init_file_content ).to match(/PID_FILE="tmp\/trinidad.pid"/)
expect( init_file_content ).to match(/OUT_FILE="tmp\/trinidad.out"/)

init_file_content.match(/TRINIDAD_OPTS="--dir tmp\/app -e production"/).should be_true
expect( init_file_content ).to match(/TRINIDAD_OPTS="--dir tmp\/app -e production"/)

init_file_content.match(/RUN_USER=""/).should be_true
expect( init_file_content ).to match(/RUN_USER=""/)
end

it "configures memory requirements using JAVA_OPTS (Java 6)" do
Expand Down Expand Up @@ -124,13 +124,13 @@
config.merge! 'pid_file' => "tmp/pids/trinidad.pid", 'log_file' => "tmp/logs/trinidad.out"
subject.configure(config)

File.exist?(pids_dir).should be_true
File.directory?(pids_dir).should be_true
Dir.entries(pids_dir).should == ['.', '..']
expect( File.exist?(pids_dir) ).to be true
expect( File.directory?(pids_dir) ).to be true
expect( Dir.entries(pids_dir) ).to eql ['.', '..']

File.exist?(logs_dir).should be_true
File.directory?(logs_dir).should be_true
Dir.entries(logs_dir).should == ['.', '..']
expect( File.exist?(logs_dir) ).to be true
expect( File.directory?(logs_dir) ).to be true
expect( Dir.entries(logs_dir) ).to eql ['.', '..']
ensure
Dir.rmdir(pids_dir) if File.exist?(pids_dir)
Dir.rmdir(logs_dir) if File.exist?(logs_dir)
Expand Down Expand Up @@ -208,10 +208,12 @@ def ask_path(path, default = nil)
let(:windows_configuration) do
subject = Trinidad::InitServices::Configuration.new
subject.instance_eval do
def windows?; true; end
def macosx?; false; end
def system(command); @system_command = command; end
def system_command; @system_command; end
def windows?; true end
def macosx?; false end
def service_listed_windows?(service); false end
def system(command); @system_command = command end
def system_command; @system_command end
def log_command(command); end
end
subject
end
Expand Down Expand Up @@ -263,6 +265,26 @@ def system_command; @system_command; end
system_command.should =~ /--PidFile=Trinidad.pid/
end

it "updates windows service when installed" do
subject = windows_configuration
subject.instance_eval do
def service_listed_windows?(service); service == 'Trinidad' end
end
config_options = {
'app_path' => "C:/MyApp",
}
subject.configure(config_options)
( system_command = subject.system_command ).should_not be nil
expect( system_command ).to match /\/\/US\/\/Trinidad/
end

it "uninstalls windows service" do
subject = windows_configuration
subject.uninstall
( system_command = subject.system_command ).should_not be nil
expect( system_command ).to match /\/\/DS\/\/Trinidad/
end

it "ask_path works when non tty and default nil" do
subject.ask = false
stdin = mock('stdin')
Expand Down

0 comments on commit 40e6243

Please sign in to comment.