h2o-configurator 0.4 → 0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d2957f2852560a37a815a8171aada782b3ea07cab3d9133d6a57c5717233de8
4
- data.tar.gz: e9666263537665189b952a890566ffbe02d8e0b25dd66192864b40cbfc67c0a2
3
+ metadata.gz: 9aabac121ceb4c0d23fb3c192e63ec9c0c6d880fd342fca83f920f384ee6ea3d
4
+ data.tar.gz: 4e48dcb4143a0c2ae0558f6f16bf3d3efa0ecedbb495b88ad80d5ec17e26bc15
5
5
  SHA512:
6
- metadata.gz: 17ec61f8ea25907e6ab70da9d8423c624fd2861d5af1289a47c218e26f58303f3ba86237752d088a82da44fc30ade271e5c114eb388f041849ac85a2228c9c4b
7
- data.tar.gz: 592624ac582fcee91acbe29b9beaf123e537a8a8054fe1e9da80b739d76f5252a133e429361da5baedc2e5b3fe4423938c2fd7d9aeb117c92d80293e1e71ec95
6
+ metadata.gz: c43dbe0b66c4d3fa6617c87be84a16a1e4a645e862f929f0322ffb2d6062a37eaed907a4c315c1c986bfca0fd73f6d1c7546749113151d9aacd886edfddcf899
7
+ data.tar.gz: ea3d7fbac40494bf30414f1f5f150dd8a654a25a386e8cb97638295900830ad50da007548c7b73bbc6946215bba8dadddbbec79931d1a91ae4063d6ccd4b1f60
@@ -3,5 +3,9 @@
3
3
  require 'h2o-configurator'
4
4
 
5
5
  configurator = H2OConfigurator::Builder.new
6
- configurator.write_config
7
- configurator.check_config
6
+ begin
7
+ configurator.write_config
8
+ rescue H2OConfigurator::Error => e
9
+ warn "Failed to write configuration: #{e}"
10
+ exit(1)
11
+ end
@@ -2,4 +2,28 @@ require 'path'
2
2
  require 'yaml'
3
3
 
4
4
  require 'h2o-configurator/builder'
5
- require 'h2o-configurator/version'
5
+ require 'h2o-configurator/site'
6
+ require 'h2o-configurator/version'
7
+
8
+ module H2OConfigurator
9
+
10
+ SitesDirGlob = '/Users/*/Sites/*'
11
+ H2OEtcDir = Path.new('/usr/local/etc/h2o')
12
+ H2OLogDir = Path.new('/usr/local/var/log/h2o')
13
+ H2OConfFile = H2OEtcDir / 'h2o.conf'
14
+ HandlersDir = Path.new(__FILE__).dirname / 'h2o-configurator' / 'handlers'
15
+ InstalledHandlersDir = H2OEtcDir / 'handlers'
16
+ Handlers = {
17
+ 'AutoExtensionHandler' => 'auto-extension.rb',
18
+ 'RedirectHandler' => 'redirect.rb',
19
+ }
20
+ ErrorLogFile = H2OLogDir / 'error.log'
21
+ CertBaseDir = Path.new('/etc/letsencrypt/live')
22
+ ServerCertificateFilename = 'fullchain.pem'
23
+ PrivateKeyFilename = 'privkey.pem'
24
+ DomainPrefixes = %w{www.}
25
+ DomainSuffixes = %w{.test}
26
+
27
+ class Error < Exception; end
28
+
29
+ end
@@ -1,26 +1,7 @@
1
1
  module H2OConfigurator
2
2
 
3
- SitesDirGlob = '/Users/*/Sites/*'
4
- H2OEtcDir = Path.new('/usr/local/etc/h2o')
5
- H2OLogDir = Path.new('/usr/local/var/log/h2o')
6
- H2OConfFile = H2OEtcDir / 'h2o.conf'
7
- AutoExtensionHandlerFile = Path.new(__FILE__).dirname / 'auto-extension-handler.rb'
8
- RedirectHandlerFile = Path.new(__FILE__).dirname / 'redirect-handler.rb'
9
- InstalledAutoExtensionHandlerFile = H2OEtcDir / AutoExtensionHandlerFile.basename
10
- InstalledRedirectHandlerFile = H2OEtcDir / RedirectHandlerFile.basename
11
- ErrorLogFile = H2OLogDir / 'error.log'
12
- CertBaseDir = Path.new('/etc/letsencrypt/live')
13
- ServerCertificateFilename = 'fullchain.pem'
14
- PrivateKeyFilename = 'privkey.pem'
15
- DomainPrefixes = %w{www.}
16
- DomainSuffixes = %w{.test}
17
-
18
3
  class Builder
19
4
 
20
- def initialize
21
- H2OLogDir.mkpath
22
- end
23
-
24
5
  def make_config
25
6
  config = {
26
7
  'compress' => 'ON',
@@ -30,158 +11,23 @@ module H2OConfigurator
30
11
  }
31
12
  Path.glob(SitesDirGlob).reject { |p| p.extname == '.old' || p.extname == '.new' }.each do |site_dir|
32
13
  site = Site.new(site_dir)
14
+ puts "%30s => %s" % [site.name, site.dir]
33
15
  config['hosts'].merge!(site.make_config)
34
16
  end
35
17
  config
36
18
  end
37
19
 
38
20
  def write_config
39
- RedirectHandlerFile.copy(InstalledRedirectHandlerFile)
40
- AutoExtensionHandlerFile.copy(InstalledAutoExtensionHandlerFile)
41
21
  H2OConfFile.write(YAML.dump(make_config))
42
- check_config
43
- end
44
-
45
- def check_config
46
- system('h2o', '-t', '-c', H2OConfFile.to_s)
47
- exit($?.to_i) unless $?.success?
48
- end
49
-
50
- end
51
-
52
- class Site
53
-
54
- attr_accessor :dir
55
- attr_accessor :name
56
-
57
- def initialize(dir)
58
- @dir = dir
59
- @name = dir.basename.to_s
60
- end
61
-
62
- def make_config
63
- config = {}
64
- if cert_dir.exist?
65
- https_redirect_host_config = make_https_redirect_host_config(80)
66
- host_config = make_host_config(443)
67
- else
68
- https_redirect_host_config = nil
69
- host_config = make_host_config(80)
70
- end
71
- domains.each do |domain|
72
- if https_redirect_host_config
73
- config["#{domain}:80"] = https_redirect_host_config
74
- config["#{domain}:443"] = host_config
75
- else
76
- config["#{domain}:80"] = host_config
77
- end
78
- end
79
- config
80
- end
81
-
82
- def domains
83
- ([''] + DomainPrefixes).map do |prefix|
84
- ([''] + DomainSuffixes).map do |suffix|
85
- "#{prefix}#{@name}#{suffix}"
86
- end
87
- end.flatten
88
- end
89
-
90
- def make_host_config(port=80)
91
- config = {
92
- 'listen' => {
93
- 'port' => port,
94
- },
95
- 'access-log' => access_log_file.to_s,
96
- 'setenv' => { 'HOST_DIR' => @dir.to_s },
97
- 'paths' => {
98
- '/' => make_handlers,
99
- },
100
- }
101
- if server_certificate_file.exist? && private_key_file.exist?
102
- config['listen']['ssl'] = {
103
- 'certificate-file' => server_certificate_file.to_s,
104
- 'key-file' => private_key_file.to_s,
105
- }
106
- end
107
- config
108
- end
109
-
110
- def make_https_redirect_host_config(port=80)
111
- {
112
- 'listen' => port,
113
- 'paths' => {
114
- '/' => {
115
- 'redirect' => "https://#{@name}",
116
- }
117
- }
118
- }
119
- end
120
-
121
- def make_handlers
122
- handlers = []
123
- if htpasswd_file.exist?
124
- handlers << RubyHandler.make(
125
- %Q{
126
- require 'htpasswd'
127
- Htpasswd.new('#{htpasswd_file}', '#{@name}')
128
- }
129
- )
130
- end
131
- handlers << RubyHandler.make(
132
- %Q{
133
- require '#{H2OConfigurator::InstalledRedirectHandlerFile}'
134
- H2OConfigurator::RedirectHandler.new
135
- },
136
- )
137
- handlers << RubyHandler.make(
138
- %Q{
139
- require '#{H2OConfigurator::InstalledAutoExtensionHandlerFile}'
140
- H2OConfigurator::AutoExtensionHandler.new
141
- }
142
- )
143
- handlers << FileDirHandler.make(@dir)
144
- handlers
145
- end
146
-
147
- def cert_dir
148
- H2OConfigurator::CertBaseDir / @name
149
- end
150
-
151
- def server_certificate_file
152
- cert_dir / H2OConfigurator::ServerCertificateFilename
153
- end
154
-
155
- def private_key_file
156
- cert_dir / H2OConfigurator::PrivateKeyFilename
157
- end
158
-
159
- def htpasswd_file
160
- @dir / '.htpasswd'
161
- end
162
-
163
- def access_log_file
164
- H2OConfigurator::H2OLogDir / "#{@name}.access.log"
165
- end
166
-
167
- end
168
-
169
- class RubyHandler
170
-
171
- def self.make(code)
172
- {
173
- 'mruby.handler' => code.gsub(/\n\s+/, "\n").strip,
174
- }
22
+ InstalledHandlersDir.rmtree if InstalledHandlersDir.exist?
23
+ HandlersDir.cp_r(InstalledHandlersDir)
24
+ H2OLogDir.mkpath
25
+ check_config(H2OConfFile)
175
26
  end
176
27
 
177
- end
178
-
179
- class FileDirHandler
180
-
181
- def self.make(dir)
182
- {
183
- 'file.dir' => dir.to_s,
184
- }
28
+ def check_config(file)
29
+ system('h2o', '--mode=test', '--conf', file.to_s)
30
+ raise Error, "h2o check failed: status #{$?.to_i}" unless $?.success?
185
31
  end
186
32
 
187
33
  end
@@ -0,0 +1,130 @@
1
+ module H2OConfigurator
2
+
3
+ class Site
4
+
5
+ attr_accessor :dir
6
+ attr_accessor :name
7
+
8
+ def initialize(dir)
9
+ @dir = dir
10
+ @name = dir.basename.to_s
11
+ end
12
+
13
+ def make_config
14
+ config = {}
15
+ if cert_dir.exist?
16
+ https_redirect_host_config = make_https_redirect_host_config(80)
17
+ host_config = make_host_config(443)
18
+ else
19
+ https_redirect_host_config = nil
20
+ host_config = make_host_config(80)
21
+ end
22
+ domains.each do |domain|
23
+ if https_redirect_host_config
24
+ config["#{domain}:80"] = https_redirect_host_config
25
+ config["#{domain}:443"] = host_config
26
+ else
27
+ config["#{domain}:80"] = host_config
28
+ end
29
+ end
30
+ config
31
+ end
32
+
33
+ def domains
34
+ ([''] + DomainPrefixes).map do |prefix|
35
+ ([''] + DomainSuffixes).map do |suffix|
36
+ "#{prefix}#{@name}#{suffix}"
37
+ end
38
+ end.flatten
39
+ end
40
+
41
+ def make_host_config(port)
42
+ config = {
43
+ 'listen' => {
44
+ 'port' => port,
45
+ },
46
+ 'access-log' => access_log_file.to_s,
47
+ 'setenv' => { 'HOST_DIR' => @dir.to_s },
48
+ 'paths' => {
49
+ '/' => make_handlers,
50
+ },
51
+ }
52
+ if server_certificate_file.exist? && private_key_file.exist?
53
+ config['listen']['ssl'] = {
54
+ 'certificate-file' => server_certificate_file.to_s,
55
+ 'key-file' => private_key_file.to_s,
56
+ }
57
+ end
58
+ config
59
+ end
60
+
61
+ def make_https_redirect_host_config(port)
62
+ {
63
+ 'listen' => port,
64
+ 'paths' => {
65
+ '/' => {
66
+ 'redirect' => "https://#{@name}",
67
+ }
68
+ }
69
+ }
70
+ end
71
+
72
+ def make_handlers
73
+ handlers = []
74
+ if htpasswd_file.exist?
75
+ handlers << make_ruby_handler(
76
+ %Q{
77
+ require 'htpasswd'
78
+ Htpasswd.new('#{htpasswd_file}', '#{@name}')
79
+ }
80
+ )
81
+ end
82
+ handlers << make_ruby_external_handler('RedirectHandler')
83
+ handlers << make_ruby_external_handler('AutoExtensionHandler')
84
+ handlers << make_file_dir_handler(@dir)
85
+ handlers
86
+ end
87
+
88
+ def make_ruby_handler(code)
89
+ {
90
+ 'mruby.handler' => code.gsub(/\n\s+/, "\n").strip,
91
+ }
92
+ end
93
+
94
+ def make_ruby_external_handler(klass)
95
+ file = InstalledHandlersDir / H2OConfigurator::Handlers[klass]
96
+ make_ruby_handler %Q{
97
+ require '#{file}'
98
+ H2OConfigurator::#{klass}.new
99
+ }
100
+ end
101
+
102
+ def make_file_dir_handler(dir)
103
+ {
104
+ 'file.dir' => dir.to_s,
105
+ }
106
+ end
107
+
108
+ def cert_dir
109
+ H2OConfigurator::CertBaseDir / @name
110
+ end
111
+
112
+ def server_certificate_file
113
+ cert_dir / H2OConfigurator::ServerCertificateFilename
114
+ end
115
+
116
+ def private_key_file
117
+ cert_dir / H2OConfigurator::PrivateKeyFilename
118
+ end
119
+
120
+ def htpasswd_file
121
+ @dir / '.htpasswd'
122
+ end
123
+
124
+ def access_log_file
125
+ H2OConfigurator::H2OLogDir / "#{@name}.access.log"
126
+ end
127
+
128
+ end
129
+
130
+ end
@@ -1,5 +1,5 @@
1
1
  module H2OConfigurator
2
2
 
3
- VERSION = '0.4'
3
+ VERSION = '0.5'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: h2o-configurator
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Labovitz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-08 00:00:00.000000000 Z
11
+ date: 2018-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: path
@@ -53,9 +53,10 @@ files:
53
53
  - bin/h2o-configurator
54
54
  - h2o-configurator.gemspec
55
55
  - lib/h2o-configurator.rb
56
- - lib/h2o-configurator/auto-extension-handler.rb
57
56
  - lib/h2o-configurator/builder.rb
58
- - lib/h2o-configurator/redirect-handler.rb
57
+ - lib/h2o-configurator/handlers/auto-extension.rb
58
+ - lib/h2o-configurator/handlers/redirect.rb
59
+ - lib/h2o-configurator/site.rb
59
60
  - lib/h2o-configurator/version.rb
60
61
  homepage: https://github.com/jslabovitz/h2o-configurator
61
62
  licenses: