redirus 0.1.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a52df1a66b61631d4906babcba2bf20aed4e0ad
4
- data.tar.gz: 30cdec5ac63efaa135a8ce7c53e55e05899b63c3
3
+ metadata.gz: 956920b5a2b187d43ffc475d478a7baa00139206
4
+ data.tar.gz: fd00a3f1d54e8601f6b22a9cffbc785002bac863
5
5
  SHA512:
6
- metadata.gz: c71b3d6dde486e69153d3250180f4a3ffe3dcbdebe5d696c4c22ce876ed35357f4b28ae6429d4b14b3beb79764634d210d116419ed1d219716a00d4acbbb7a6f
7
- data.tar.gz: 053f86bdaf783adec2a26d28d0cf3b1a5d6cca0676f742c9927df6b671ca50a07de8f4baf4cd2670a7986c2bb73ca60316032ad775261eca8c29c77233cebb8a
6
+ metadata.gz: 057063d0c94cd37502b27949dc3f8ace2e5bac69920078a9999bdeb7aa21556a73e4e9315ceef10515a125a00e570d9fcf9e4e1cd1b9847187b3e02d9ba86d64
7
+ data.tar.gz: da1615d53261279f36fc8e2a84f7a688ab55191adea4f1ea9b9059ab1493bf281fd89f5c7e7fa607c489e8f2a4828e22ffd348f8354433d7cf85f02ccc4c9a09
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.0.0-p353
1
+ 2.1.4
data/CHANGELOG CHANGED
@@ -1,3 +1,14 @@
1
+ v 0.2.1
2
+ - set minimal dependency versions in gemspec
3
+
4
+ v 0.2.0
5
+ - erb used to create nginx http and https config templates
6
+ - redirus-init command added to bootstrap redirus configuration
7
+ - possiblity to pass location parameters and options using redirus-client
8
+ - better error messages
9
+ - fix problem with showing redirus version using redirus executables
10
+ - help for redirus executable
11
+
1
12
  v 0.1.2
2
13
  - ssl_protocols set to TLSv1 TLSv1.1 TLSv1.2 in default nginx configurations
3
14
 
data/Gemfile.lock CHANGED
@@ -1,104 +1,113 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- redirus (0.1.2)
5
- sidekiq
4
+ redirus (0.2.0)
5
+ sidekiq (~> 3.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (4.0.2)
11
- i18n (~> 0.6, >= 0.6.4)
12
- minitest (~> 4.2)
13
- multi_json (~> 1.3)
10
+ activesupport (4.1.6)
11
+ i18n (~> 0.6, >= 0.6.9)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
14
  thread_safe (~> 0.1)
15
- tzinfo (~> 0.3.37)
16
- atomic (1.1.14)
15
+ tzinfo (~> 1.1)
17
16
  celluloid (0.15.2)
18
17
  timers (~> 1.1.0)
19
18
  coderay (1.1.0)
20
- colorize (0.5.8)
21
19
  connection_pool (2.0.0)
22
- coveralls (0.6.7)
23
- colorize
20
+ coveralls (0.7.1)
24
21
  multi_json (~> 1.3)
25
22
  rest-client
26
23
  simplecov (>= 0.7)
24
+ term-ansicolor
27
25
  thor
28
26
  diff-lcs (1.2.5)
29
- ffi (1.9.3)
30
- formatador (0.2.4)
31
- guard (2.3.0)
27
+ docile (1.1.5)
28
+ ffi (1.9.6)
29
+ formatador (0.2.5)
30
+ guard (2.6.1)
32
31
  formatador (>= 0.2.4)
33
- listen (~> 2.1)
32
+ listen (~> 2.7)
34
33
  lumberjack (~> 1.0)
35
34
  pry (>= 0.9.12)
36
35
  thor (>= 0.18.1)
37
- guard-rspec (3.0.3)
38
- guard (>= 1.8)
39
- rspec (~> 2.13)
40
- i18n (0.6.9)
36
+ guard-rspec (4.3.1)
37
+ guard (~> 2.1)
38
+ rspec (>= 2.14, < 4.0)
39
+ i18n (0.6.11)
41
40
  json (1.8.1)
42
- listen (2.4.0)
41
+ listen (2.7.11)
43
42
  celluloid (>= 0.15.2)
44
43
  rb-fsevent (>= 0.9.3)
45
44
  rb-inotify (>= 0.9)
46
- lumberjack (1.0.4)
45
+ lumberjack (1.0.9)
47
46
  method_source (0.8.2)
48
- mime-types (1.25.1)
49
- minitest (4.7.5)
50
- multi_json (1.8.2)
51
- pry (0.9.12.4)
52
- coderay (~> 1.0)
53
- method_source (~> 0.8)
47
+ mime-types (2.4.2)
48
+ minitest (5.4.2)
49
+ multi_json (1.10.1)
50
+ netrc (0.7.8)
51
+ pry (0.10.1)
52
+ coderay (~> 1.1.0)
53
+ method_source (~> 0.8.1)
54
54
  slop (~> 3.4)
55
- rake (10.1.1)
55
+ rake (10.4.2)
56
56
  rb-fsevent (0.9.4)
57
- rb-inotify (0.9.3)
57
+ rb-inotify (0.9.5)
58
58
  ffi (>= 0.5.0)
59
59
  redis (3.1.0)
60
60
  redis-namespace (1.5.1)
61
61
  redis (~> 3.0, >= 3.0.4)
62
- rest-client (1.6.7)
63
- mime-types (>= 1.16)
64
- rspec (2.14.1)
65
- rspec-core (~> 2.14.0)
66
- rspec-expectations (~> 2.14.0)
67
- rspec-mocks (~> 2.14.0)
68
- rspec-core (2.14.7)
69
- rspec-expectations (2.14.4)
70
- diff-lcs (>= 1.1.3, < 2.0)
71
- rspec-mocks (2.14.4)
72
- rspec-sidekiq (0.5.1)
73
- rspec (~> 2.0)
62
+ rest-client (1.7.2)
63
+ mime-types (>= 1.16, < 3.0)
64
+ netrc (~> 0.7)
65
+ rspec (3.1.0)
66
+ rspec-core (~> 3.1.0)
67
+ rspec-expectations (~> 3.1.0)
68
+ rspec-mocks (~> 3.1.0)
69
+ rspec-core (3.1.7)
70
+ rspec-support (~> 3.1.0)
71
+ rspec-expectations (3.1.2)
72
+ diff-lcs (>= 1.2.0, < 2.0)
73
+ rspec-support (~> 3.1.0)
74
+ rspec-mocks (3.1.3)
75
+ rspec-support (~> 3.1.0)
76
+ rspec-sidekiq (2.0.0)
77
+ rspec (~> 3.0, >= 3.0.0)
74
78
  sidekiq (>= 2.4.0)
75
- shoulda-matchers (2.4.0)
79
+ rspec-support (3.1.2)
80
+ shoulda-matchers (2.7.0)
76
81
  activesupport (>= 3.0.0)
77
- sidekiq (3.2.2)
78
- celluloid (>= 0.15.2)
82
+ sidekiq (3.2.5)
83
+ celluloid (= 0.15.2)
79
84
  connection_pool (>= 2.0.0)
80
85
  json
81
86
  redis (>= 3.0.6)
82
87
  redis-namespace (>= 1.3.1)
83
- simplecov (0.7.1)
88
+ simplecov (0.9.1)
89
+ docile (~> 1.1.0)
84
90
  multi_json (~> 1.0)
85
- simplecov-html (~> 0.7.1)
86
- simplecov-html (0.7.1)
87
- slop (3.4.7)
88
- thor (0.18.1)
89
- thread_safe (0.1.3)
90
- atomic
91
+ simplecov-html (~> 0.8.0)
92
+ simplecov-html (0.8.0)
93
+ slop (3.6.0)
94
+ term-ansicolor (1.3.0)
95
+ tins (~> 1.0)
96
+ thor (0.19.1)
97
+ thread_safe (0.3.4)
91
98
  timers (1.1.0)
92
- tzinfo (0.3.38)
99
+ tins (1.3.3)
100
+ tzinfo (1.2.2)
101
+ thread_safe (~> 0.1)
93
102
 
94
103
  PLATFORMS
95
104
  ruby
96
105
 
97
106
  DEPENDENCIES
98
- bundler
99
- coveralls
100
- guard-rspec
101
- rake
107
+ bundler (~> 1.7)
108
+ coveralls (~> 0)
109
+ guard-rspec (~> 4.3)
110
+ rake (~> 10)
102
111
  redirus!
103
- rspec-sidekiq
104
- shoulda-matchers
112
+ rspec-sidekiq (~> 2.0)
113
+ shoulda-matchers (~> 2.7)
data/Guardfile CHANGED
@@ -1,4 +1,4 @@
1
- guard :rspec do
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
2
  watch(%r{^spec/.+_spec\.rb$})
3
3
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
4
  watch('spec/spec_helper.rb') { "spec" }
data/README.md CHANGED
@@ -131,11 +131,13 @@ chmod 400 host.key host.pem
131
131
  # install redirus
132
132
  gem install redirus
133
133
 
134
- # Download sample redirus configuration
135
- curl -L --progress https://raw.githubusercontent.com/dice-cyfronet/redirus/master/config.yml.example > config.yml
134
+ # Generate inital redirus and nginx configuration
135
+ redirus-init
136
136
 
137
137
  # Customise redis configuration and nginx config files locations
138
138
  edit config.yml
139
+ edit http.erb.conf
140
+ edit https.erb.conf
139
141
  ```
140
142
 
141
143
  ## Example config.yml
@@ -151,26 +153,8 @@ namespace: redirus
151
153
  nginx:
152
154
  configs_path: /path/to/generated/nginx/configurations/
153
155
  pid: /nginx/installation/path/nginx.pid
154
- http_template: |
155
- listen *:80;
156
- https_template: |
157
- listen *:443 ssl;
158
- ssl_certificate /path/to/cert/dir/server.crt;
159
- ssl_certificate_key /path/to/cert/dir/server.key;
160
- ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
161
- config_template: |
162
- #{upstream}
163
- server {
164
- #{listen}
165
- server_name #{name}.my-domain.pl;
166
- server_tokens off;
167
- proxy_set_header X-Server-Address $scheme://#{name}.my-domain.pl;
168
- proxy_set_header Host $http_host;
169
- location / {
170
- proxy_pass http://#{upstream_name};
171
- #{properties}
172
- }
173
- }
156
+ http_template: /path/to/http/nginx/config/template
157
+ https_template: /path/to/https/nginx/config/template
174
158
  allowed_properties:
175
159
  - proxy_sent_timeout \d
176
160
  - proxy_read_timeout \d
@@ -182,29 +166,84 @@ specific for concrete redirection will be created. Value of this path **need** t
182
166
  be the same as in nginx configuration file.
183
167
  + `/nginx/installation/path/nginx.pid` is a file containing nginx pid. To this pid
184
168
  `SIGHUP` signal will be sent, which will triggers nginx configuration reload.
185
- + `/path/to/cert/dir/server.crt` path to ssl certificate used in https redirections.
186
- This certificate need to have `*` in CN field.
187
- + `/path/to/cert/dir/server.key` path to ssl certificate key file.
188
-
189
- By using `http_template`, `https_template`, `config_template` and
190
- `allowed_properties` you can customize how nginx configuration looks like for each
191
- subdomain.
192
-
193
- + `http_template` is used when an http redirection is created
194
- + `https_template` is used when an https redirection is created
195
- + `config_template` is used for http and https redirections.
196
- Inside this template, the `listen` variable section is specific to http or https
197
- redirections.
198
- + `allowed_properties` is used to define allowed parameters which can be
169
+ + `allowed_properties` is used to define allowed location parameters which can be
199
170
  passed in the generated configuration. Regular expressions can be used here.
200
171
 
172
+ ## Example `http.erb.conf` and `https.erb.conf`
173
+
174
+ `http.erb.conf` used to create `http` redirections:
175
+
176
+ ```
177
+ upstream <%= @name %>_http {
178
+ <% if @options[:load_balancing] == :ip_hash -%>
179
+ ip_hash;
180
+ <% end -%>
181
+ <% for worker in @workers -%>
182
+ server <%= worker %>;
183
+ <% end -%>
184
+ }
185
+
186
+ server {
187
+ listen 127.0.0.1:80;
188
+ server_name <%= @name %>.localhost;
189
+ location / {
190
+ proxy_pass http://<%= @name %>_http;
191
+ <% for property in @location_properties -%>
192
+ <%= property %>;
193
+ <% end -%>
194
+ }
195
+ }
196
+ ```
197
+
198
+ `https.erb.conf` used to create `https` redirections:
199
+
200
+ ```
201
+ upstream <%= @name %>_https {
202
+ <% for worker in @workers -%>
203
+ server <%= worker %>;
204
+ <% end -%>
205
+ }
206
+
207
+ server {
208
+ listen 127.0.0.1:443 ssl;
209
+ server_name <%= @name %>.localhost;
210
+
211
+ ssl_certificate /path/to/cert/dir/server.crt;
212
+ ssl_certificate_key /path/to/cert/dir/server.key;
213
+ ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
214
+
215
+ location / {
216
+ proxy_pass http://<%= @name %>_https;
217
+ <% for property in @location_properties -%>
218
+ <%= property %>;
219
+ <% end -%>
220
+ }
221
+ }
222
+ ```
223
+
224
+ In presented templates following dynamic elements are used:
225
+
226
+ - `@name` - redirection name used as a url prefix
227
+ - `@workers` - list of upstream addresses in following format `ip:port`
228
+ - `@location_properties` - list of location properties
229
+ (possible properties can be limitted using `allowed_properties` section
230
+ in `config.yml` file)
231
+ - `@options` - other properties passed by the redirus client,
232
+ can be used e.g. to allow sticky session configuration like presented in
233
+ `http.erb.conf`
234
+
201
235
  For example - when a redirection with the following parameters is requested:
202
236
 
203
237
  ```ruby
204
238
  Sidekiq::Client.push(
205
239
  'queue' => 'cyfronet',
206
240
  'class' => Redirus::Worker::AddProxy,
207
- 'args' => ['subdomain', ['127.0.0.1:80'], :http, ["proxy_send_timeout 6000"]])
241
+ 'args' => [
242
+ 'subdomain', ['127.0.0.1:80'], :http,
243
+ ["proxy_send_timeout 6000"],
244
+ { load_balancing: :ip_hash }
245
+ ]
246
+ )
208
247
  ```
209
248
 
210
249
  ...then the following `/nginx/sites-enabled/subdomain_http` subdomain config file
@@ -212,6 +251,7 @@ will be created:
212
251
 
213
252
  ```
214
253
  upstream subdomain_http {
254
+ ip_hash;
215
255
  server 127.0.0.1:80;
216
256
  }
217
257
  server {
@@ -225,6 +265,25 @@ server {
225
265
  }
226
266
  ```
227
267
 
268
+ **NOTE:** It is redirus administrator role to prepare `http` and `https`
269
+ templates in such a way that generated configuration will be valid.
270
+ If at least one nginx configuration will be invalid then nginx will fail to
271
+ reboot.
272
+
273
+ ## Redirus generator
274
+
275
+ By running following command:
276
+
277
+ ```
278
+ redirus-init
279
+ ```
280
+
281
+ set of `redirus` and `nginx` configuration files will be created.
282
+ You can customize generated files by providing additional `redirus-init`
283
+ parameters.
284
+
285
+ See `redirus-init -h` for details.
286
+
228
287
  ## Run redirus
229
288
 
230
289
  ```bash
@@ -300,7 +359,12 @@ end
300
359
  Sidekiq::Client.push(
301
360
  'queue' => Redirus::Worker.config.queues.first,
302
361
  'class' => Redirus::Worker::AddProxy,
303
- 'args' => ['subdomain', ['127.0.0.1'], :http, ["proxy_send_timeout 6000"]])
362
+ 'args' => [
363
+ 'subdomain', ['127.0.0.1'], :http,
364
+ ["proxy_send_timeout 6000"],
365
+ { load_balancing: :ip_hash }
366
+ ]
367
+ )
304
368
 
305
369
  # remove redirection
306
370
  Sidekiq::Client.push(
data/bin/redirus CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  require 'redirus'
4
4
 
5
- cli = Redirus::ServerCLI.instance
5
+ cli = Redirus::Cli::Server.instance
6
6
  cli.parse
7
- cli.run
7
+ cli.run
data/bin/redirus-client CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  require 'redirus'
4
4
 
5
- cli = Redirus::CLI.instance
5
+ cli = Redirus::Cli::Client.instance
6
6
  cli.parse
7
- cli.run
7
+ cli.run
data/bin/redirus-init ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'redirus'
4
+
5
+ cli = Redirus::Cli::Init.instance
6
+ cli.parse
7
+ cli.run
@@ -0,0 +1,183 @@
1
+ require 'optparse'
2
+ require 'singleton'
3
+
4
+ module Redirus
5
+ module Cli
6
+ class Client
7
+ include Singleton
8
+
9
+ attr_reader :options
10
+
11
+ def parse(args = ARGV)
12
+ init_options(args)
13
+ validate!
14
+ end
15
+
16
+ def run
17
+ Redirus.config_path = options[:config_path]
18
+ init_sidekiq
19
+
20
+ add? ? add : rm
21
+ end
22
+
23
+ private
24
+
25
+ def init_sidekiq
26
+ Sidekiq.configure_client do |c|
27
+ c.redis = {
28
+ namespace: Redirus.config.namespace,
29
+ url: Redirus.config.redis_url
30
+ }
31
+ end
32
+ end
33
+
34
+ def init_options(args)
35
+ opts = parse_options(args)
36
+ opts[:config_path] ||= 'config.yml'
37
+
38
+ Redirus.config_path = opts[:config_path]
39
+
40
+ @options = defaults.merge(opts)
41
+ end
42
+
43
+ def parse_options(args)
44
+ opts = {}
45
+
46
+ parser = OptionParser.new do |o|
47
+ o.on('-c', '--configuration PATH', 'Configuration path') do |arg|
48
+ opts[:config_path] = arg
49
+ end
50
+
51
+ o.on('-a',
52
+ '--action TYPE', [:add, :rm],
53
+ 'Action type (add, rm)') do |arg|
54
+ opts[:action] = arg
55
+ end
56
+
57
+ o.on('-q', '--queue NAME', 'Queue name') do |arg|
58
+ opts[:queue] = arg
59
+ end
60
+
61
+ o.on('-t',
62
+ '--type TYPE',
63
+ [:http, :https],
64
+ 'Rediraction type') do |arg|
65
+ opts[:type] = arg
66
+ end
67
+
68
+ o.on('-l',
69
+ '--location-property LIST',
70
+ Array,
71
+ 'List of location properties (e.g. prop1,prop2)') do |arg|
72
+ opts[:location_properties] = arg
73
+ end
74
+
75
+ o.on('-o',
76
+ '--options MAP',
77
+ Array,
78
+ 'Option map (e.g. key1:value1,key2:value2)') do |arg|
79
+ opts[:options] = to_hsh(arg)
80
+ end
81
+
82
+ o.on_tail('-h', '--help', 'Show this message') do
83
+ puts o
84
+ exit
85
+ end
86
+
87
+ o.on_tail('-v', '--version', 'Show version') do
88
+ puts "Redirus #{Redirus::VERSION}"
89
+ exit
90
+ end
91
+ end
92
+
93
+ parser.banner = 'redirus-client [options] name '\
94
+ '[upstream1,upstream2,...]'
95
+
96
+ parser.parse!(args)
97
+
98
+ opts[:name] = args.shift
99
+
100
+ upstreams_str = args.shift
101
+ opts[:upstreams] = upstreams_str.split(',') if upstreams_str
102
+
103
+ opts
104
+ end
105
+
106
+ def to_hsh(array)
107
+ array.inject({}) do |hsh, item|
108
+ kv = item.split(':')
109
+ if kv.size >= 2
110
+ hsh[kv[0]] = kv[1]
111
+ end
112
+ hsh
113
+ end
114
+ end
115
+
116
+ def defaults
117
+ {
118
+ config_path: 'config.yml',
119
+ type: :http,
120
+ action: :add,
121
+ location_properties: [],
122
+ options: {},
123
+ queue: Redirus.config.queues.first
124
+ }
125
+ end
126
+
127
+ def validate!
128
+ unless File.exist?(options[:config_path])
129
+ puts "ERROR: Configuration file #{options[:config_path]} "\
130
+ 'does not exist'
131
+ exit(1)
132
+ end
133
+
134
+ unless options[:name]
135
+ puts 'ERROR: Redirection name is not set'
136
+ exit(1)
137
+ end
138
+
139
+ if add?
140
+ if options[:upstreams]
141
+ options[:upstreams].each do |u|
142
+ next if u.match(/\A\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}:\d+\z/)
143
+ puts "ERROR: #{u} is not valid upstream definition, "\
144
+ 'use IP:PORT schema'
145
+ exit(1)
146
+ end
147
+ else
148
+ puts 'ERROR: Upstream locations are not set'
149
+ exit(1)
150
+ end
151
+ end
152
+
153
+ Redirus.config_path = options[:config_path]
154
+ end
155
+
156
+ def add?
157
+ options[:action] == :add
158
+ end
159
+
160
+ def add
161
+ puts "Adding new redirection #{options[:name]} with following "\
162
+ "upstreams #{options[:upstreams]}"
163
+
164
+ Sidekiq::Client.push(
165
+ 'queue' => options[:queue],
166
+ 'class' => Redirus::Worker::AddProxy,
167
+ 'args' => [options[:name],
168
+ options[:upstreams],
169
+ options[:type], options[:location_properties],
170
+ options[:options]])
171
+ end
172
+
173
+ def rm
174
+ puts "Removing redirection #{options[:name]}"
175
+
176
+ Sidekiq::Client.push(
177
+ 'queue' => options[:queue],
178
+ 'class' => Redirus::Worker::RmProxy,
179
+ 'args' => [options[:name], options[:type]])
180
+ end
181
+ end
182
+ end
183
+ end