appserver 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/README.md +30 -9
- data/Rakefile +31 -0
- data/bin/appserver +9 -6
- data/lib/appserver.rb +10 -6
- data/lib/appserver/app.rb +106 -65
- data/lib/appserver/appserver.conf.rb +126 -0
- data/lib/appserver/command.rb +38 -10
- data/lib/appserver/configurator.rb +38 -0
- data/lib/appserver/logrotate.rb +46 -0
- data/lib/appserver/monit.rb +45 -0
- data/lib/appserver/nginx.rb +71 -0
- data/lib/appserver/repository.rb +80 -21
- data/lib/appserver/server_dir.rb +100 -0
- data/lib/appserver/unicorn.conf.rb +23 -27
- data/lib/appserver/utils.rb +24 -15
- data/test/apps/rack-simple/config.ru +7 -0
- data/test/apps/sinatra/Gemfile +4 -0
- data/test/apps/sinatra/config.ru +2 -0
- data/test/apps/sinatra/hello.rb +9 -0
- data/test/apps/sinatra/views/index.erb +1 -0
- data/test/helper.rb +49 -0
- data/test/unit/test_app.rb +72 -0
- data/test/unit/test_appserver.rb +26 -0
- data/test/unit/test_command.rb +64 -0
- data/test/unit/test_configurator.rb +116 -0
- data/test/unit/test_logrotate.rb +14 -0
- data/test/unit/test_monit.rb +13 -0
- data/test/unit/test_nginx.rb +13 -0
- data/test/unit/test_repository.rb +50 -0
- data/test/unit/test_server_dir.rb +121 -0
- data/test/unit/test_unicorn_conf.rb +58 -0
- data/test/unit/test_utils.rb +36 -0
- metadata +84 -11
- data/lib/appserver/appserver.yml +0 -85
- data/lib/appserver/server.rb +0 -136
data/lib/appserver/appserver.yml
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
# This is an appserver directory configuration of the "appserver" gem. Use the
|
2
|
-
# "appserver" command or visit http://github.com/zargony/appserver for details
|
3
|
-
|
4
|
-
#
|
5
|
-
# SERVER SETTINGS
|
6
|
-
# Non application specific. Paths are relative to the appserver directory. The
|
7
|
-
# appserver directory is the directory, that contains this configuration file.
|
8
|
-
#
|
9
|
-
|
10
|
-
# Path to the default directory of git repositories that contain the
|
11
|
-
# applications to deploy. Defaults to the home directory of the user 'git'
|
12
|
-
#repo_dir: /var/git
|
13
|
-
|
14
|
-
# Path/name of the Monit configuration snippet that should be written
|
15
|
-
#monit_conf: monitrc
|
16
|
-
|
17
|
-
# Command to execute to tell Monit to reload the configuration. Used within
|
18
|
-
# the Monit snippet, so this command will be called as root
|
19
|
-
#monit_reload: /usr/sbin/monit reload
|
20
|
-
|
21
|
-
# Path/name of the Nginx configuration snippet that should be written
|
22
|
-
#nginx_conf: nginx.conf
|
23
|
-
|
24
|
-
# Command to execute to tell Nginx to reload the configuration. Used within
|
25
|
-
# the Monit snippet, so this command will be called as root
|
26
|
-
#nginx_reload: /usr/sbin/nginx -s reload
|
27
|
-
|
28
|
-
|
29
|
-
#
|
30
|
-
# APPLICATION SETTINGS
|
31
|
-
# Can be either specified globally for all applications or application-
|
32
|
-
# specific under the "apps" subtree (see examples at the bottom of this file).
|
33
|
-
# Paths are relative to the respective application directory. Every deployed
|
34
|
-
# application has it's own directory under the appserver directory.
|
35
|
-
#
|
36
|
-
|
37
|
-
# Name/path of unicorn
|
38
|
-
#unicorn: /usr/local/bin/unicorn
|
39
|
-
|
40
|
-
# Environment to run the application in. Defaults to 'production'
|
41
|
-
#environment: production
|
42
|
-
|
43
|
-
# Default number of application instances (unicorn workers)
|
44
|
-
#instances: 3
|
45
|
-
|
46
|
-
# Let Monit watch the CPU usage of instances and restart them if their
|
47
|
-
# CPU usage exceeds this value
|
48
|
-
#max_cpu_usage:
|
49
|
-
|
50
|
-
# Let Monit watch the memory usage of instances and restart them if their
|
51
|
-
# memory usage exceeds this value
|
52
|
-
#max_memory_usage:
|
53
|
-
|
54
|
-
# When doing CPU/memory usage checks, only restart an instance if it exceeds
|
55
|
-
# a resource for at least this number of Monit cycles
|
56
|
-
#usage_check_cycles: 5
|
57
|
-
|
58
|
-
# Let Monit check periodically, if instances provide an answer to HTTP
|
59
|
-
# requests within the given timeout, or restart them if they don't. Set
|
60
|
-
# to 0 to disable
|
61
|
-
#http_check_timeout: 30
|
62
|
-
|
63
|
-
# The hostname, Nginx should accept requests for. You most porbably want to
|
64
|
-
# specify the hostname for every application below. If an application has no
|
65
|
-
# hostname set, a subdomain of this default hostname will be used. Defaults
|
66
|
-
# to the system's domainname.
|
67
|
-
#hostname: example.com
|
68
|
-
|
69
|
-
# Path where public static files should be served from. Defaults to the public
|
70
|
-
# directory in the application
|
71
|
-
#public_dir: public
|
72
|
-
|
73
|
-
|
74
|
-
#
|
75
|
-
# APPLICATIONS
|
76
|
-
# All application default settings from above can be overridden for every
|
77
|
-
# application. You most probably want to set "hostname" to your liking here.
|
78
|
-
# Most other settings should do well with their defaults in most cases.
|
79
|
-
#
|
80
|
-
|
81
|
-
#apps:
|
82
|
-
# # A simple blog application named "myblog"
|
83
|
-
# myblog:
|
84
|
-
# hostname: blog.example.com
|
85
|
-
# instances: 1
|
data/lib/appserver/server.rb
DELETED
@@ -1,136 +0,0 @@
|
|
1
|
-
require 'etc'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
module Appserver
|
5
|
-
class Server < Struct.new(:dir, :repo_dir, :monit_conf, :monit_reload, :nginx_conf, :nginx_reload)
|
6
|
-
class AlreadyInitializedError < RuntimeError; end
|
7
|
-
class DirectoryNotEmptyError < RuntimeError; end
|
8
|
-
class NotInitializedError < RuntimeError; end
|
9
|
-
|
10
|
-
include Utils
|
11
|
-
|
12
|
-
DEFAULTS = {
|
13
|
-
:repo_dir => (Etc.getpwnam('git') rescue {})[:dir],
|
14
|
-
:monit_conf => 'monitrc',
|
15
|
-
:monit_reload => '/usr/sbin/monit reload',
|
16
|
-
:nginx_conf => 'nginx.conf',
|
17
|
-
:nginx_reload => '/usr/sbin/nginx -s reload',
|
18
|
-
}
|
19
|
-
|
20
|
-
def self.config_file_template
|
21
|
-
File.expand_path('../appserver.yml', __FILE__)
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.search_dir (path = Dir.pwd)
|
25
|
-
if File.exist?(File.join(path, 'appserver.yml'))
|
26
|
-
path
|
27
|
-
elsif path =~ %r(/)
|
28
|
-
search_dir(path.sub(%r(/[^/]*$), ''))
|
29
|
-
else
|
30
|
-
nil
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.initialize_dir (options = {})
|
35
|
-
raise AlreadyInitializedError if search_dir && !options[:force]
|
36
|
-
raise DirectoryNotEmptyError if Dir.glob('*') != [] && !options[:force]
|
37
|
-
safe_replace_file('appserver.yml') do |f|
|
38
|
-
f.puts File.read(config_file_template)
|
39
|
-
end
|
40
|
-
['tmp', 'log'].each do |dir|
|
41
|
-
Dir.mkdir(dir) if !File.directory?(dir)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def initialize (options = {})
|
46
|
-
super()
|
47
|
-
# Search upwards for the appserver dir
|
48
|
-
self.dir = self.class.search_dir
|
49
|
-
raise NotInitializedError unless dir
|
50
|
-
# Load configuration settings
|
51
|
-
@config = load_config(config_file)
|
52
|
-
DEFAULTS.each do |key, default_value|
|
53
|
-
self[key] = @config[key] || default_value
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def config_file
|
58
|
-
File.join(dir, 'appserver.yml')
|
59
|
-
end
|
60
|
-
|
61
|
-
def tmp_dir
|
62
|
-
File.join(dir, 'tmp')
|
63
|
-
end
|
64
|
-
|
65
|
-
def log_dir
|
66
|
-
File.join(dir, 'log')
|
67
|
-
end
|
68
|
-
|
69
|
-
def app (name)
|
70
|
-
@apps ||= {}
|
71
|
-
@apps[name] ||= App.new(self, name, @config)
|
72
|
-
end
|
73
|
-
|
74
|
-
def apps
|
75
|
-
Dir.glob(File.join(dir, '*')).select { |f| File.directory?(f) }.map { |f| File.basename(f) }.map { |name| app(name) }
|
76
|
-
end
|
77
|
-
|
78
|
-
def repository (name_or_path)
|
79
|
-
path = name_or_path =~ %r(/) ? expand_path(name_or_path) : File.expand_path("#{name_or_path}.git", repo_dir)
|
80
|
-
@repositories ||= {}
|
81
|
-
@repositories[path] ||= Repository.new(self, path, @config)
|
82
|
-
end
|
83
|
-
|
84
|
-
def repositories
|
85
|
-
Dir.glob(File.join(repo_dir, '*.git')).select { |f| File.directory?(f) }.map { |path| repository(path) }
|
86
|
-
end
|
87
|
-
|
88
|
-
def write_configs
|
89
|
-
# Write Monit configuration file
|
90
|
-
safe_replace_file(monit_conf) do |f|
|
91
|
-
f.puts %Q(# Monit configuration automagically generated by the "appserver" gem using)
|
92
|
-
f.puts %Q(# the appserver directory config #{expand_path(config_file)})
|
93
|
-
f.puts %Q(# Include this file into your system's monitrc (using an include statement))
|
94
|
-
f.puts %Q(# to use it. See http://github.com/zargony/appserver for details.)
|
95
|
-
# Let Monit reload itself if this configuration changes
|
96
|
-
f.puts %Q(check file monit_conf with path #{expand_path(monit_conf)})
|
97
|
-
f.puts %Q( if changed checksum then exec "#{monit_reload}")
|
98
|
-
# Reload Nginx if its configuration changes
|
99
|
-
f.puts %Q(check file nginx_conf with path #{expand_path(nginx_conf)})
|
100
|
-
f.puts %Q( if changed checksum then exec "#{nginx_reload}")
|
101
|
-
# Add application-specific Monit configuration
|
102
|
-
apps.each do |app|
|
103
|
-
app.write_monit_config(f)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
# Write Nginx configuration file
|
107
|
-
safe_replace_file(nginx_conf) do |f|
|
108
|
-
f.puts %Q(# Nginx configuration automagically generated by the "appserver" gem using)
|
109
|
-
f.puts %Q(# the appserver directory config #{expand_path(config_file)})
|
110
|
-
f.puts %Q(# Include this file into your system's nginx.conf \(using an include statement)
|
111
|
-
f.puts %Q(# inside a http statement\) to use it. See http://github.com/zargony/appserver)
|
112
|
-
f.puts %Q(# for details.)
|
113
|
-
# The default server always responds with 403 Forbidden
|
114
|
-
f.puts %Q(server {)
|
115
|
-
f.puts %Q( listen 80 default;)
|
116
|
-
f.puts %Q( server_name _;)
|
117
|
-
f.puts %Q( deny all;)
|
118
|
-
f.puts %Q(})
|
119
|
-
# Add application-specific Nginx configuration
|
120
|
-
apps.each do |app|
|
121
|
-
app.write_nginx_config(f)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
protected
|
127
|
-
|
128
|
-
def expand_path (path)
|
129
|
-
File.expand_path(path, dir)
|
130
|
-
end
|
131
|
-
|
132
|
-
def load_config (filename)
|
133
|
-
symbolize_keys(YAML.load_file(filename) || {})
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|