trinidad_init_services 1.1.6 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  == Trinidad
2
2
 
3
- Copyright (c) 2009 David Calavera
3
+ Copyright (c) 2012 Team Trinidad and contributors http://github.com/trinidad
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
@@ -23,4 +23,5 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
 
24
24
  == Additional Bundled Software
25
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.
26
+ Apache Commons Daemon is licensed under the Apache License, Version 2.0.
27
+ See http://www.apache.org/licenses/LICENSE-2.0 for details.
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # Trinidad Init Services
2
2
 
3
- Trinidad's init services based on Apache [commons daemon](http://commons.apache.org/daemon/)
4
- and [jruby-jsvc](https://github.com/nicobrevin/jruby-jsvc). Works on Unix and Windows systems.
3
+ Init services based on Apache [Commons Daemon](http://commons.apache.org/daemon/)
4
+ and [jruby-jsvc](https://github.com/nicobrevin/jruby-jsvc).
5
+ Allows you to run Trinidad as an OS daemon, works on Unix and Windows systems.
5
6
 
6
7
  ## Installation
7
8
 
@@ -12,29 +13,40 @@ When the gem is installed the user must launch the installation process:
12
13
  $ jruby -S trinidad_init_service
13
14
 
14
15
  This installer guides you through the configuration process and generates a
15
- init.d script if you are on a unix system or creates the service if you are
16
- on a windows box.
16
+ init.d script if you are on a Unix system or creates the service if you are
17
+ on a Windows box.
17
18
 
18
- You can optionally provide a configuration file to the trinidad_init_service
19
- command. An example configuration file might look like this:
19
+ You can optionally provide a YAML configuration file with defaults specified
20
+ for the `trinidad_init_service` command. An example configuration file :
20
21
 
21
- app_path: "/home/myuser/app"
22
- trinidad_options: "-e production"
22
+ app_path: "/home/trinidad/myapp/current"
23
+ ruby_compat_version: RUBY1_9
23
24
  jruby_home: "/opt/jruby"
24
- ruby_compat_version: RUBY1_8
25
- trinidad_name: Trinidad
26
- jsvc_path: "/usr/bin/jsvc"
27
25
  java_home: "/opt/java"
28
26
  output_path: "/etc/init.d"
29
- pid_file: "/tmp/trinidad.pid"
30
- log_file: "/tmp/trinidad.log"
27
+ pid_file: "/home/trinidad/myapp/shared/pids/trinidad.pid"
28
+ log_file: "/home/trinidad/myapp/shared/log/trinidad.log"
29
+ jsvc_path: "/usr/bin/jsvc"
30
+ trinidad_options: "-e production"
31
+ trinidad_name: Trinidad
32
+ trinidad_service_id: Trinidad # on Windows (defaults to :trinidad_name)
33
+ trinidad_service_desc: Trinidad Service Description # on Windows (optional)
31
34
 
32
35
  You can then run the installer like so:
33
36
 
34
- $ jruby -S trinidad_init_service trinidad_init_config.yml
37
+ $ trinidad_init_service --defaults trinidad_init_defaults.yml
35
38
 
36
39
  If any of the required options are not provided in the configuration file, then
37
- the installer will prompt you for them.
40
+ the installer will prompt you for them. If you're running this as part of an
41
+ environment initialization script than use the *--no-ask* option or provide
42
+ only the defaults file path on the command line (make sure all required options
43
+ are there) :
44
+
45
+ $ jruby -S trinidad_init_service trinidad_init_defaults.yml
46
+
47
+
48
+ **NOTE:** Do not confuse the *defaults.yml* "configuration" file with Trinidad's
49
+ own configuration (*config/trinidad.yml*) file used when setting up the server !
38
50
 
39
51
 
40
52
  ### Unix
@@ -64,11 +76,10 @@ as a different user. The preferred method for doing this is the `run_user:`
64
76
  attribute in the configuration YAML (or it's corresponding value at the prompt).
65
77
  For example:
66
78
 
67
- app_path: "/home/myuser/app"
68
- trinidad_options: "-e production"
69
- jruby_home: "/opt/jruby"
70
- run_user: myuser
71
- ...
79
+ app_path: "/home/trinidad/myapp/current"
80
+ # ...
81
+ run_user: trinidad
82
+ # ...
72
83
 
73
84
  This causes the the server to run with non-root privileges (it essentially executes
74
85
  as `sudo -u run_user jsvc ...`).
@@ -80,16 +91,29 @@ This can be configured with the `JSVC_ARGS_EXTRA` environment variable, like thi
80
91
 
81
92
  It not recommended that you mix the `-user` flag with the `run_user` option !
82
93
 
94
+ #### Uninstall
95
+
96
+ Manage as every other rc.d, assuming `update-rc.d -f /etc/init.d/trinidad defaults`
97
+ you can uninstall using :
98
+
99
+ $ [sudo] trinidad_init_service --uninstall /etc/init.d/trinidad
100
+
83
101
 
84
102
  ### Windows
85
103
 
86
104
  #### Execution
87
105
 
88
106
  Open the **Services** panel under **Administrative Tools** and look for a service
89
- called **Trinidad**.
107
+ called **Trinidad** (or whatever name you have chosen).
108
+
109
+ #### Uninstall
110
+
111
+ To remove the service you're going to need the service id (name), than run :
112
+
113
+ $ trinidad_init_service --uninstall Trinidad
90
114
 
91
115
 
92
116
  ## Copyright
93
117
 
94
- Copyright (c) 2011-2012 David Calavera<calavera@apache.org>.
95
- See LICENSE for details.
118
+ Copyright (c) 2012 [Team Trinidad](https://github.com/trinidad).
119
+ See LICENSE (http://en.wikipedia.org/wiki/MIT_License) for details.
@@ -1,11 +1,62 @@
1
1
  #!/usr/bin/env jruby
2
2
 
3
+ begin
4
+ require 'rubygems'
5
+ rescue LoadError
6
+ end
3
7
  require 'trinidad_init_services'
8
+ require 'optparse'
4
9
 
5
- if ARGV.size > 0
10
+ defaults = nil
11
+ configuration = Trinidad::InitServices::Configuration.new
12
+
13
+ opts = OptionParser.new do |opts|
14
+ opts.banner = "Usage: #{File.basename(__FILE__)} [options] [defaults]"
15
+
16
+ opts.on('-h', '--help', 'print this help') do
17
+ puts opts
18
+ exit
19
+ end
20
+
21
+ opts.on('-v', '--version', 'print version') do
22
+ begin
23
+ require 'trinidad'
24
+ puts("#{Trinidad::InitServices::VERSION} (trinidad-#{Trinidad::VERSION})")
25
+ rescue LoadError
26
+ puts("#{Trinidad::InitServices::VERSION} (trinidad not installed)")
27
+ end
28
+ exit(0)
29
+ end
30
+
31
+ opts.on('-d', '--defaults YAML_FILE', 'use passed file as defaults') do |config|
32
+ defaults = config
33
+ end
34
+
35
+ opts.on('-s', '--silent', 'do not print output on console') do
36
+ configuration.say = false
37
+ end
38
+
39
+ opts.on('--no-ask', 'disable interactive (console) mode') do
40
+ configuration.ask = false
41
+ end
42
+
43
+ opts.on('--uninstall NAME', 'remove installed init service') do |name|
44
+ configuration.uninstall(name)
45
+ exit(0)
46
+ end
47
+
48
+ end
49
+
50
+ opts.parse! ARGV
51
+
52
+ if ARGV.size > 0 # trinidad_init_service /home/trinidad/defaults.yml
53
+ defaults = ARGV[0]
54
+ configuration.ask = false
55
+ end
56
+
57
+ if defaults
6
58
  require 'yaml'
7
- config = YAML.load File.read(ARGV[0])
8
- Trinidad::InitServices::Configuration.new.configure(config)
9
- else
10
- Trinidad::InitServices::Configuration.new.configure
11
- end
59
+ defaults = YAML.load File.read(defaults)
60
+ end
61
+
62
+ defaults ? configuration.configure(defaults) : configuration.configure
@@ -32,6 +32,17 @@ TRINIDAD_OPTS="<%= @trinidad_options.join(" ") %>"
32
32
  PIDFILE=<%= @pid_file %>
33
33
  LOG_FILE=<%= @log_file %>
34
34
 
35
+ # Create pidfile directory if missing
36
+ # Todo: Remove PIDFILE_DIR creation once jsvc can be started
37
+ # as root using the -user option without any issues.
38
+ PIDFILE_DIR=$(dirname $PIDFILE)
39
+ if [ ! -d "$PIDFILE_DIR" ] ; then
40
+ mkdir -p $PIDFILE_DIR
41
+ if [ ! -z "$RUN_USER" ] ; then
42
+ chown -R $RUN_USER $PIDFILE_DIR
43
+ fi
44
+ fi
45
+
35
46
  # Implements the jsvc Daemon interface.
36
47
  MAIN_CLASS=com.msp.jsvc.JRubyDaemon
37
48
 
@@ -7,11 +7,18 @@ require 'shellwords'
7
7
 
8
8
  module Trinidad
9
9
  module InitServices
10
-
11
10
  class Configuration
11
+
12
+ def self.windows?
13
+ RbConfig::CONFIG['host_os'] =~ /mswin|mingw/i
14
+ end
15
+
16
+ def self.macosx?
17
+ RbConfig::CONFIG['host_os'] =~ /darwin/i
18
+ end
19
+
12
20
  def initialize(stdin = STDIN, stdout = STDOUT)
13
- @stdin = stdin
14
- @stdout = stdout
21
+ @stdin, @stdout = stdin, stdout
15
22
  end
16
23
 
17
24
  def initialize_paths(jruby_home = default_jruby_home)
@@ -22,17 +29,7 @@ module Trinidad
22
29
  @classpath << File.join(jruby_home, 'lib', 'jruby.jar')
23
30
  end
24
31
 
25
- def configure_jruby_opts
26
- opts = []
27
- opts << "-Djruby.home=#{@jruby_home}"
28
- opts << "-Djruby.lib=#{File.join(@jruby_home, 'lib')}"
29
- opts << "-Djruby.script=jruby"
30
- opts << "-Djruby.daemon.module.name=Trinidad"
31
- opts << "-Djruby.compat.version=#{@ruby_compat_version}"
32
- opts
33
- end
34
-
35
- def configure(defaults={})
32
+ def configure(defaults = {})
36
33
  @app_path = defaults["app_path"] || ask_path('Application path?')
37
34
  @trinidad_options = ["-d #{@app_path}"]
38
35
  options_ask = 'Trinidad options?'
@@ -46,10 +43,20 @@ module Trinidad
46
43
  @jruby_opts = configure_jruby_opts
47
44
  initialize_paths(@jruby_home)
48
45
 
49
- windows? ? configure_windows_service : configure_unix_daemon(defaults)
50
- puts 'Done.'
46
+ message = windows? ? configure_windows_service : configure_unix_daemon(defaults)
47
+ say message if message.is_a?(String)
51
48
  end
52
49
 
50
+ def configure_jruby_opts
51
+ opts = []
52
+ opts << "-Djruby.home=#{@jruby_home}"
53
+ opts << "-Djruby.lib=#{File.join(@jruby_home, 'lib')}"
54
+ opts << "-Djruby.script=jruby"
55
+ opts << "-Djruby.daemon.module.name=Trinidad"
56
+ opts << "-Djruby.compat.version=#{@ruby_compat_version}"
57
+ opts
58
+ end
59
+
53
60
  def configure_unix_daemon(defaults)
54
61
  @java_home = defaults["java_home"] || ask_path('Java home?', default_java_home)
55
62
  unless @jsvc = defaults["jsvc_path"] || detect_jsvc_path
@@ -57,7 +64,7 @@ module Trinidad
57
64
  if @jsvc.empty? # unpack and compile :
58
65
  jsvc_unpack_dir = defaults["jsvc_unpack_dir"] || ask_path("dir where jsvc dist should be unpacked?", '/usr/local/src')
59
66
  @jsvc = compile_jsvc(jsvc_unpack_dir, @java_home)
60
- puts "jsvc binary available at: #{@jsvc} " +
67
+ say "jsvc binary available at: #{@jsvc} " +
61
68
  "(consider adding it to $PATH if you plan to re-run trinidad_init_service)"
62
69
  end
63
70
  end
@@ -81,10 +88,12 @@ module Trinidad
81
88
  )
82
89
  ).result(binding)
83
90
 
84
- puts "Moving trinidad to #{@output_path}"
91
+ say "moving trinidad to #{@output_path}"
85
92
  trinidad_file = File.join(@output_path, "trinidad")
86
93
  File.open(trinidad_file, 'w') { |file| file.write(daemon) }
87
94
  FileUtils.chmod(@run_user == '' ? 0744 : 0755, trinidad_file)
95
+
96
+ "\nNOTE: you might want to: `[sudo] update-rc.d -f #{@output_path} defaults`"
88
97
  end
89
98
 
90
99
  def collect_windows_opts(options_ask, defaults)
@@ -92,24 +101,59 @@ module Trinidad
92
101
  name_ask = 'Service name? {Alphanumeric and spaces only}'
93
102
  name_default = 'Trinidad'
94
103
  @trinidad_name = defaults["trinidad_name"] || ask(name_ask, name_default)
104
+
105
+ id_ask = 'Service ID? {Alphanumeric and underscores only}'
106
+ id_default = @trinidad_name.gsub(/\s+/, '_').gsub(/\W/, '')
107
+ @trinidad_service_id = defaults["trinidad_service_id"] || ask(id_ask, id_default)
108
+
109
+ desc_ask = 'Service description? {Alphanumeric and spaces only}'
110
+ desc_default = 'Embedded Apache Tomcat running rack and rails applications'
111
+ @trinidad_service_desc = defaults["trinidad_service_desc"] || ask(desc_ask, desc_default)
95
112
  end
96
113
 
97
114
  def configure_windows_service
98
115
  srv_path = detect_prunsrv_path
99
- trinidad_service_id = @trinidad_name.gsub(/\W/, '')
100
116
 
101
- command = %Q{//IS//#{trinidad_service_id} --DisplayName="#{@trinidad_name}" \
117
+ command = %Q{//IS//#{@trinidad_service_id} --DisplayName="#{@trinidad_name}" \
118
+ --Description="#{@trinidad_service_desc}" \
102
119
  --Install=#{srv_path} --Jvm=auto --StartMode=jvm --StopMode=jvm \
103
120
  --StartClass=com.msp.procrun.JRubyService --StartMethod=start \
104
121
  --StartParams="#{escape_path(@trinidad_daemon_path)};#{format_options(@trinidad_options)}" \
105
122
  --StopClass=com.msp.procrun.JRubyService --StopMethod=stop --Classpath="#{format_options(@classpath)}" \
106
123
  --StdOutput=auto --StdError=auto \
107
- --LogPrefix="#{trinidad_service_id.downcase}" \
124
+ --LogPrefix="#{@trinidad_service_id.downcase}" \
108
125
  ++JvmOptions="#{format_options(@jruby_opts)}"
109
126
  }
110
127
  system "#{srv_path} #{command}"
128
+
129
+ "\nNOTE: you may use prunsrv to manage your service, try running:\n" +
130
+ "#{srv_path} help"
131
+ end
132
+
133
+ def uninstall(service)
134
+ windows? ? uninstall_windows_service(service) : uninstall_unix_daemon(service)
135
+ end
136
+
137
+ def uninstall_windows_service(service_name)
138
+ srv_path = detect_prunsrv_path
139
+ system "#{srv_path} stop #{service_name}"
140
+ system "#{srv_path} delete #{service_name}"
111
141
  end
112
142
 
143
+ def uninstall_unix_daemon(service)
144
+ name = File.basename(service) # e.g. /etc/init.d/trinidad
145
+ command = "update-rc.d -f #{name} remove"
146
+ system command
147
+ rescue => e
148
+ say "uninstall failed, try `sudo #{command}`"
149
+ raise e
150
+ ensure
151
+ unless File.exist?(service)
152
+ service = File.expand_path(service, '/etc/init.d')
153
+ end
154
+ FileUtils.rm(service) if File.exist?(service)
155
+ end
156
+
113
157
  private
114
158
 
115
159
  def escape_path(path)
@@ -131,15 +175,15 @@ module Trinidad
131
175
  def default_ruby_compat_version
132
176
  JRuby.runtime.is1_9 ? "RUBY1_9" : "RUBY1_8"
133
177
  end
134
-
178
+
135
179
  def windows?
136
- RbConfig::CONFIG['host_os'] =~ /mswin|mingw/i
180
+ self.class.windows?
137
181
  end
138
182
 
139
183
  def macosx?
140
- RbConfig::CONFIG['host_os'] =~ /darwin/i
184
+ self.class.macosx?
141
185
  end
142
-
186
+
143
187
  def bundled_jsvc_path # only called on *nix
144
188
  jsvc = 'jsvc_' + (macosx? ? 'darwin' : 'linux')
145
189
  jsvc_path = File.join(@jars_path, jsvc)
@@ -176,19 +220,19 @@ module Trinidad
176
220
  jdk_home = java_home # it's still worth trying
177
221
  end
178
222
  command = "cd #{jsvc_dir} && ./configure --with-java=#{jdk_home}"
179
- puts "configuring jsvc ..."
223
+ say "configuring jsvc ..."
180
224
  command_output = `#{command}`
181
225
  if $?.exitstatus != 0
182
- puts command_output
226
+ say command_output
183
227
  raise "`#{command}` failed with status: #{$?.exitstatus}"
184
228
  end
185
229
 
186
230
  # make
187
231
  command = "cd #{jsvc_dir} && make"
188
- puts "compiling jsvc ..."
232
+ say "compiling jsvc ..."
189
233
  command_output = `#{command}`
190
234
  if $?.exitstatus != 0
191
- puts command_output
235
+ say command_output
192
236
  raise "`#{command}` failed with status: #{$?.exitstatus}"
193
237
  end
194
238
 
@@ -234,7 +278,7 @@ module Trinidad
234
278
  FileUtils.mkdir_p dir, :mode => 0775
235
279
  rescue Errno::EACCES => e
236
280
  raise unless error
237
- puts "#{error} (#{e})"
281
+ say "#{error} (#{e})"
238
282
  end
239
283
  end
240
284
 
@@ -244,30 +288,38 @@ module Trinidad
244
288
  end
245
289
 
246
290
  def ask(question, default = nil)
247
- return nil if not @stdin.tty?
291
+ return default if ! @stdin.tty? || @ask == false
248
292
 
249
- question << " [#{default}]" if default && !default.empty?
293
+ question << " [#{default}]" if default && ! default.empty?
250
294
 
251
295
  result = nil
252
-
253
296
  while result.nil?
254
- @stdout.print(question + " ")
297
+ @stdout.print("#{question} ")
255
298
  @stdout.flush
256
299
 
257
300
  result = @stdin.gets
258
301
 
259
302
  if result
260
303
  result.chomp!
261
-
262
- result = case result
304
+ case result
263
305
  when /^$/
264
- default
265
- else
266
- result
306
+ result = default
267
307
  end
268
308
  end
269
309
  end
270
- return result
310
+ result
311
+ end
312
+
313
+ def ask=(flag)
314
+ @ask = !!flag
315
+ end
316
+
317
+ def say(msg)
318
+ puts msg unless @say == false
319
+ end
320
+
321
+ def say=(flag)
322
+ @say = !!flag
271
323
  end
272
324
 
273
325
  end
@@ -1,5 +1,5 @@
1
1
  module Trinidad
2
2
  module InitServices
3
- VERSION = '1.1.6'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -132,13 +132,17 @@ describe Trinidad::InitServices::Configuration do
132
132
  'app_path' => "C:/MyApp",
133
133
  'ruby_compat_version' => "RUBY1_9",
134
134
  'trinidad_name' => "Trinidad",
135
+ 'trinidad_service_id' => "TrinidadService",
136
+ 'trinidad_service_desc' => "Trinidad Service Description",
135
137
  'trinidad_options' => "-e production -p 4242 ",
136
138
  'java_home' => "C:/Program Files (x86)/jdk-1.7.0",
137
139
  'jruby_home' => "C:/Program Files/jruby",
138
140
  }
139
141
  subject.configure(config_options)
140
142
  subject.system_command.should_not be nil
143
+ subject.system_command.should =~ /\/\/IS\/\/TrinidadService/
141
144
  subject.system_command.should =~ /--DisplayName="Trinidad"/
145
+ subject.system_command.should =~ /--Description="Trinidad Service Description"/
142
146
  subject.system_command.should =~ /--StartParams=".*?\\daemon.rb;-d;C:\\MyApp;-e;production;-p;4242"/
143
147
  subject.system_command.should =~ /--Classpath=\".*?\\jruby-jsvc.jar;.*?\\commons-daemon.jar;.*?\\jruby.jar/
144
148
  subject.system_command.should =~ %r{
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: trinidad_init_services
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.6
5
+ version: 1.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - David Calavera
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-05-18 00:00:00 Z
13
+ date: 2012-09-03 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: trinidad