Class: Sunshine::Server

Inherits:
Daemon
  • Object
show all
Defined in:
lib/sunshine/daemons/server.rb

Overview

An abstract class to wrap simple server software setup and start/stop.

Child classes are expected to at least provide a start and stop bash script by either overloading the start_cmd and stop_cmd methods, or by setting may also be specified if restart requires more functionality than simply calling start_cmd && stop_cmd.

Direct Known Subclasses

Apache, Nginx, Unicorn

Instance Attribute Summary collapse

Attributes inherited from Daemon

#app, #bin, #config_file, #config_path, #config_template, #name, #pid, #processes, #restart_cmd, #server_apps, #start_cmd, #status_cmd, #sudo, #target, #timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Daemon

#config_file_path, #config_template_files, #each_server_app, #has_setup?, #log_file, #log_files, #restart, #start, #stop, underscore, #upload_config_files

Constructor Details

#initialize(app, options = {}) ⇒ Server

Server objects need only an App object to be instantiated. All Daemon init options are supported plus the following:

:port

port_num - the port to run the server on

defaults to 80
:server_name

myserver.com - host name used by server

defaults to nil

By default, servers also assign the option :role => :web.



34
35
36
37
38
39
40
41
42
43
# File 'lib/sunshine/daemons/server.rb', line 34

def initialize app, options={}
  options[:role] ||= :web

  super app, options

  @port          = options[:port] || 80
  @server_name   = options[:server_name]
  @sigkill       = 'QUIT'
  @supports_rack = false
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



19
20
21
# File 'lib/sunshine/daemons/server.rb', line 19

def port
  @port
end

#server_nameObject (readonly)

Returns the value of attribute server_name.



19
20
21
# File 'lib/sunshine/daemons/server.rb', line 19

def server_name
  @server_name
end

#sigkillObject

Returns the value of attribute sigkill.



20
21
22
# File 'lib/sunshine/daemons/server.rb', line 20

def sigkill
  @sigkill
end

Class Method Details

.binder_methodsObject



14
15
16
# File 'lib/sunshine/daemons/server.rb', line 14

def self.binder_methods
  [:server_name, :port].concat super
end

.passenger_root(shell) ⇒ Object

Gets the root of the installer passenger gem.



58
59
60
61
62
63
64
65
66
# File 'lib/sunshine/daemons/server.rb', line 58

def self.passenger_root shell
  str     = shell.call "gem list passenger -d"
  version = $1 if str =~ /passenger\s\((.*)\)$/
  gempath = $1 if str =~ /Installed\sat:\s(.*)$/

  return unless version && gempath

  File.join(gempath, "gems/passenger-#{version}")
end

Instance Method Details

#setupObject

Add passenger information to the binder at setup time.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sunshine/daemons/server.rb', line 72

def setup
  super do |server_app, binder|

    binder.forward :use_passenger?

    binder.set :passenger_root do
      Server.passenger_root server_app.shell
    end

    yield(server_app, binder) if block_given?
  end
end

#stop_cmdObject

Default server stop command.



89
90
91
92
# File 'lib/sunshine/daemons/server.rb', line 89

def stop_cmd
  "test -f #{@pid} && kill -#{@sigkill} $(cat #{@pid}) && sleep 1 && "+
    "rm -f #{@pid} || echo 'No #{@name} process to stop for #{@app.name}';"
end

#supports_rack?Boolean

Defines if this server supports interfacing with rack.

Returns:

  • (Boolean)


98
99
100
# File 'lib/sunshine/daemons/server.rb', line 98

def supports_rack?
  @supports_rack
end

#use_passenger?Boolean

Check if passenger is required to run the application. Returns true if the server’s target is a Sunshine::App

Returns:

  • (Boolean)


50
51
52
# File 'lib/sunshine/daemons/server.rb', line 50

def use_passenger?
  Sunshine::App === @target && !supports_rack?
end