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_cmd bash script by either overloading the start_cmd method, or by setting @start_cmd. A restart_cmd and stop_cmd method or attribute may also be specified if restart requires more functionality than simply calling start_cmd && stop_cmd.

Direct Known Subclasses

Apache, MongrelRails, Nginx, Thin, Unicorn

Constant Summary

Constants inherited from Daemon

Daemon::RESTART_FAILED_CODE, Daemon::START_FAILED_CODE, Daemon::STATUS_DOWN_CODE, Daemon::STOP_FAILED_CODE

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, #stop_cmd, #sudo, #timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Daemon

#_restart_cmd, #_start_cmd, #_status_cmd, #_stop_cmd, #chown_log_files, #config_file_path, #config_template_files, #configure_remote_dirs, #each_server_app, #exit_on_failure, #has_setup?, #log_file, #log_files, #pick_sudo, #restart, short_name, #start, #status, #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:

:connections

num - Number of connections allowed per server;

defaults to 1024.

:point_to

app|server - An app or server to point to,

defaults to the passed app. If a server object is given, will act as a proxy. (Only valid on front-end servers - Nginx, Apache)

:port

port_num - The port to run the server on defaults to 80.

:server_name

myserver.com - Host name used by server

defaults to the individual remote host.

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sunshine/daemons/server.rb', line 51

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

  super app, options

  @connections   = options[:connections]          || 1024
  @port          = options[:port]                 || 80
  @server_name   = options[:server_name]
  # Setting @sudo to nil will let the server apps' shells handle sudo
  @sudo          = options[:sudo] || @port < 1024 || nil
  @target        = options[:point_to]             || @app

  @supports_rack      = false
  @supports_passenger = false
end

Instance Attribute Details

#clusterObject

Returns the value of attribute cluster.



31
32
33
# File 'lib/sunshine/daemons/server.rb', line 31

def cluster
  @cluster
end

#connectionsObject (readonly)

Returns the value of attribute connections.



29
30
31
# File 'lib/sunshine/daemons/server.rb', line 29

def connections
  @connections
end

#portObject (readonly)

Returns the value of attribute port.



29
30
31
# File 'lib/sunshine/daemons/server.rb', line 29

def port
  @port
end

#server_nameObject (readonly)

Returns the value of attribute server_name.



29
30
31
# File 'lib/sunshine/daemons/server.rb', line 29

def server_name
  @server_name
end

#sigkillObject

Returns the value of attribute sigkill.



31
32
33
# File 'lib/sunshine/daemons/server.rb', line 31

def sigkill
  @sigkill
end

#targetObject (readonly)

Returns the value of attribute target.



29
30
31
# File 'lib/sunshine/daemons/server.rb', line 29

def target
  @target
end

Class Method Details

.binder_methodsObject



24
25
26
# File 'lib/sunshine/daemons/server.rb', line 24

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

.new_cluster(count, app, options = {}) ⇒ Object

Creates a server cluster object:

Mongrel.new_cluster 3, app, :port => 5000
#=> [<# mongrel_5000 >, <# mongrel_5001 >, <# mongrel_5002 >]


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

def self.new_cluster count, app, options={}
  ServerCluster.new self, count, app, options
end

.passenger_root(shell) ⇒ Object

Gets the root of the installer passenger gem.



81
82
83
# File 'lib/sunshine/daemons/server.rb', line 81

def self.passenger_root shell
  shell.call "passenger-config --root"
end

Instance Method Details

#setupObject

Adds passenger information to the binder at setup time.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sunshine/daemons/server.rb', line 89

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

#supports_passenger?Boolean

Defines if this server has passenger support.

Returns:

  • (Boolean)


106
107
108
# File 'lib/sunshine/daemons/server.rb', line 106

def supports_passenger?
  @supports_passenger
end

#supports_rack?Boolean

Defines if this server supports interfacing with rack.

Returns:

  • (Boolean)


114
115
116
# File 'lib/sunshine/daemons/server.rb', line 114

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 and if the server explicitely supports passenger.

Returns:

  • (Boolean)


73
74
75
# File 'lib/sunshine/daemons/server.rb', line 73

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