Class: Trinidad::InitServices::Configuration
- Inherits:
-
Object
- Object
- Trinidad::InitServices::Configuration
- Defined in:
- lib/trinidad_init_services/configuration.rb
Class Method Summary collapse
Instance Method Summary collapse
- #ask=(flag) ⇒ Object
- #collect_windows_opts(options_ask, defaults) ⇒ Object
- #configure(defaults = {}) ⇒ Object
- #configure_jruby_opts ⇒ Object
- #configure_unix_daemon(defaults) ⇒ Object
- #configure_windows_service ⇒ Object
-
#initialize(stdin = STDIN, stdout = STDOUT) ⇒ Configuration
constructor
A new instance of Configuration.
- #initialize_paths(jruby_home = default_jruby_home) ⇒ Object
- #say=(flag) ⇒ Object
- #uninstall(service) ⇒ Object
- #uninstall_unix_daemon(service) ⇒ Object
- #uninstall_windows_service(service_name) ⇒ Object
Constructor Details
#initialize(stdin = STDIN, stdout = STDOUT) ⇒ Configuration
Returns a new instance of Configuration.
20 21 22 |
# File 'lib/trinidad_init_services/configuration.rb', line 20 def initialize(stdin = STDIN, stdout = STDOUT) @stdin, @stdout = stdin, stdout end |
Class Method Details
.macosx? ⇒ Boolean
16 17 18 |
# File 'lib/trinidad_init_services/configuration.rb', line 16 def self.macosx? RbConfig::CONFIG['host_os'] =~ /darwin/i end |
.windows? ⇒ Boolean
12 13 14 |
# File 'lib/trinidad_init_services/configuration.rb', line 12 def self.windows? RbConfig::CONFIG['host_os'] =~ /mswin|mingw/i end |
Instance Method Details
#ask=(flag) ⇒ Object
313 314 315 |
# File 'lib/trinidad_init_services/configuration.rb', line 313 def ask=(flag) @ask = !!flag end |
#collect_windows_opts(options_ask, defaults) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/trinidad_init_services/configuration.rb', line 99 def collect_windows_opts(, defaults) << '(separated by `;`)' name_ask = 'Service name? {Alphanumeric and spaces only}' name_default = 'Trinidad' @trinidad_name = defaults["trinidad_name"] || ask(name_ask, name_default) id_ask = 'Service ID? {Alphanumeric and underscores only}' id_default = @trinidad_name.gsub(/\s+/, '_').gsub(/\W/, '') @trinidad_service_id = defaults["trinidad_service_id"] || ask(id_ask, id_default) desc_ask = 'Service description? {Alphanumeric and spaces only}' desc_default = 'Embedded Apache Tomcat running rack and rails applications' @trinidad_service_desc = defaults["trinidad_service_desc"] || ask(desc_ask, desc_default) end |
#configure(defaults = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/trinidad_init_services/configuration.rb', line 32 def configure(defaults = {}) @app_path = defaults["app_path"] || ask_path('Application path?') @trinidad_options = ["-d #{@app_path}"] = 'Trinidad options?' = '-e production' collect_windows_opts(, defaults) if windows? @trinidad_options << (defaults["trinidad_options"] || ask(, )) @trinidad_options.map! { |opt| Shellwords.shellsplit(opt) }.flatten! @jruby_home = defaults["jruby_home"] || ask_path('JRuby home?', default_jruby_home) @ruby_compat_version = defaults["ruby_compat_version"] || ask('Ruby 1.8.x or 1.9.x compatibility?', default_ruby_compat_version) @jruby_opts = configure_jruby_opts initialize_paths(@jruby_home) = windows? ? configure_windows_service : configure_unix_daemon(defaults) say if .is_a?(String) end |
#configure_jruby_opts ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/trinidad_init_services/configuration.rb', line 50 def configure_jruby_opts opts = [] opts << "-Djruby.home=#{@jruby_home}" opts << "-Djruby.lib=#{File.join(@jruby_home, 'lib')}" opts << "-Djruby.script=jruby" opts << "-Djruby.daemon.module.name=Trinidad" opts << "-Djruby.compat.version=#{@ruby_compat_version}" opts end |
#configure_unix_daemon(defaults) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/trinidad_init_services/configuration.rb', line 60 def configure_unix_daemon(defaults) @java_home = defaults["java_home"] || ask_path('Java home?', default_java_home) unless @jsvc = defaults["jsvc_path"] || detect_jsvc_path @jsvc = ask_path("path to jsvc binary (leave blank and we'll try to compile)?", '') if @jsvc.empty? # unpack and compile : jsvc_unpack_dir = defaults["jsvc_unpack_dir"] || ask_path("dir where jsvc dist should be unpacked?", '/usr/local/src') @jsvc = compile_jsvc(jsvc_unpack_dir, @java_home) say "jsvc binary available at: #{@jsvc} " + "(consider adding it to $PATH if you plan to re-run trinidad_init_service)" end end @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 @pid_file = File.join(@pid_file, 'trinidad.pid') if File.exist?(@pid_file) && File.directory?(@pid_file) make_path_dir(@pid_file, "could not create dir for '#{@pid_file}', make sure dir exists before running daemon") @log_file = File.join(@log_file, 'trinidad.log') if File.exist?(@log_file) && File.directory?(@log_file) 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.('../../init.d/trinidad.erb', File.dirname(__FILE__)) ) ).result(binding) say "moving trinidad to #{@output_path}" trinidad_file = File.join(@output_path, "trinidad") File.open(trinidad_file, 'w') { |file| file.write(daemon) } FileUtils.chmod(@run_user == '' ? 0744 : 0755, trinidad_file) "\nNOTE: you might want to: `[sudo] update-rc.d -f #{@output_path} defaults`" end |
#configure_windows_service ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/trinidad_init_services/configuration.rb', line 114 def configure_windows_service srv_path = detect_prunsrv_path command = %Q{//IS//#{@trinidad_service_id} --DisplayName="#{@trinidad_name}" \ --Description="#{@trinidad_service_desc}" \ --Install=#{srv_path} --Jvm=auto --StartMode=jvm --StopMode=jvm \ --StartClass=com.msp.procrun.JRubyService --StartMethod=start \ --StartParams="#{escape_path(@trinidad_daemon_path)};#{(@trinidad_options)}" \ --StopClass=com.msp.procrun.JRubyService --StopMethod=stop --Classpath="#{(@classpath)}" \ --StdOutput=auto --StdError=auto \ --LogPrefix="#{@trinidad_service_id.downcase}" \ ++JvmOptions="#{(@jruby_opts)}" } system "#{srv_path} #{command}" "\nNOTE: you may use prunsrv to manage your service, try running:\n" + "#{srv_path} help" end |
#initialize_paths(jruby_home = default_jruby_home) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/trinidad_init_services/configuration.rb', line 24 def initialize_paths(jruby_home = default_jruby_home) @trinidad_daemon_path = File.('../../trinidad/daemon.rb', __FILE__) @jars_path = File.('../../../trinidad-libs', __FILE__) @classpath = ['jruby-jsvc.jar', 'commons-daemon.jar'].map { |jar| File.join(@jars_path, jar) } @classpath << File.join(jruby_home, 'lib', 'jruby.jar') end |
#say=(flag) ⇒ Object
322 323 324 |
# File 'lib/trinidad_init_services/configuration.rb', line 322 def say=(flag) @say = !!flag end |
#uninstall(service) ⇒ Object
133 134 135 |
# File 'lib/trinidad_init_services/configuration.rb', line 133 def uninstall(service) windows? ? uninstall_windows_service(service) : uninstall_unix_daemon(service) end |
#uninstall_unix_daemon(service) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/trinidad_init_services/configuration.rb', line 143 def uninstall_unix_daemon(service) name = File.basename(service) # e.g. /etc/init.d/trinidad command = "update-rc.d -f #{name} remove" system command rescue => e say "uninstall failed, try `sudo #{command}`" raise e ensure unless File.exist?(service) service = File.(service, '/etc/init.d') end FileUtils.rm(service) if File.exist?(service) end |
#uninstall_windows_service(service_name) ⇒ Object
137 138 139 140 141 |
# File 'lib/trinidad_init_services/configuration.rb', line 137 def uninstall_windows_service(service_name) srv_path = detect_prunsrv_path system "#{srv_path} stop #{service_name}" system "#{srv_path} delete #{service_name}" end |