redirus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23204aa2cdc879f6d5155a2568ff4d527509a74d
4
+ data.tar.gz: cc69405a2880bbf10a5fa310c25a69be55649290
5
+ SHA512:
6
+ metadata.gz: 1acda32916df2f9b7c8e701e0e1814b9d9da535808ec117f43b187653b9970fe71dbdd6b9b09efdb9a3ffca95fb1451ecea21b1af02e471c1db3eebf6359ccb9
7
+ data.tar.gz: 5bde7b49d4a977fac684a83565d2b8ae209f0fa443e2941abf403c23f5098ad921abbdd837746d1b0c6fc5b934269e3850bff7df91b006fcbb2ce8aae42ae888
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
17
+ config.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=progress
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p353
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/CHANGELOG ADDED
@@ -0,0 +1,5 @@
1
+ v 0.1.0
2
+ - Add/Remove nginx redirection
3
+ - redirus executable
4
+ - redirus-client executable
5
+ - Possibility to add redirection properties
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redirus-worker.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,104 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ redirus (0.1.0)
5
+ sidekiq
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.0.2)
11
+ i18n (~> 0.6, >= 0.6.4)
12
+ minitest (~> 4.2)
13
+ multi_json (~> 1.3)
14
+ thread_safe (~> 0.1)
15
+ tzinfo (~> 0.3.37)
16
+ atomic (1.1.14)
17
+ celluloid (0.15.2)
18
+ timers (~> 1.1.0)
19
+ coderay (1.1.0)
20
+ colorize (0.5.8)
21
+ connection_pool (2.0.0)
22
+ coveralls (0.6.7)
23
+ colorize
24
+ multi_json (~> 1.3)
25
+ rest-client
26
+ simplecov (>= 0.7)
27
+ thor
28
+ diff-lcs (1.2.5)
29
+ ffi (1.9.3)
30
+ formatador (0.2.4)
31
+ guard (2.3.0)
32
+ formatador (>= 0.2.4)
33
+ listen (~> 2.1)
34
+ lumberjack (~> 1.0)
35
+ pry (>= 0.9.12)
36
+ thor (>= 0.18.1)
37
+ guard-rspec (3.0.3)
38
+ guard (>= 1.8)
39
+ rspec (~> 2.13)
40
+ i18n (0.6.9)
41
+ json (1.8.1)
42
+ listen (2.4.0)
43
+ celluloid (>= 0.15.2)
44
+ rb-fsevent (>= 0.9.3)
45
+ rb-inotify (>= 0.9)
46
+ lumberjack (1.0.4)
47
+ 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)
54
+ slop (~> 3.4)
55
+ rake (10.1.1)
56
+ rb-fsevent (0.9.4)
57
+ rb-inotify (0.9.3)
58
+ ffi (>= 0.5.0)
59
+ redis (3.1.0)
60
+ redis-namespace (1.5.1)
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)
74
+ sidekiq (>= 2.4.0)
75
+ shoulda-matchers (2.4.0)
76
+ activesupport (>= 3.0.0)
77
+ sidekiq (3.2.2)
78
+ celluloid (>= 0.15.2)
79
+ connection_pool (>= 2.0.0)
80
+ json
81
+ redis (>= 3.0.6)
82
+ redis-namespace (>= 1.3.1)
83
+ simplecov (0.7.1)
84
+ 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
+ timers (1.1.0)
92
+ tzinfo (0.3.38)
93
+
94
+ PLATFORMS
95
+ ruby
96
+
97
+ DEPENDENCIES
98
+ bundler
99
+ coveralls
100
+ guard-rspec
101
+ rake
102
+ redirus!
103
+ rspec-sidekiq
104
+ shoulda-matchers
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marek Kasztelnik
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,317 @@
1
+ # Redirus [![build status](https://secure.travis-ci.org/dice-cyfronet/redirus.png)](https://travis-ci.org/dice-cyfronet/redirus) [![Code Climate](https://codeclimate.com/github/dice-cyfronet/redirus.png)](https://codeclimate.com/github/dice-cyfronet/redirus) [![Dependency Status](https://gemnasium.com/dice-cyfronet/redirus.png)](https://gemnasium.com/dice-cyfronet/redirus) [![Coverage Status](https://coveralls.io/repos/dice-cyfronet/redirus/badge.png?branch=master)](https://coveralls.io/r/dice-cyfronet/redirus)
2
+
3
+ The redirus is responsible for creating/deleting subdomain redirections.
4
+ It is done by generating the appropriate nginx configurations and
5
+ reloading nginx server.
6
+
7
+ ## Requirements
8
+
9
+ **This project is designed for Linux operating systems.**
10
+
11
+ - Linux (tested on Ubuntu)
12
+ - Nginx
13
+ - Ruby 2.0+
14
+ - Redis (can be installed on separate server)
15
+
16
+ ## Packages / Dependencies
17
+
18
+ Install the required packages (needed to compile Ruby and nginx):
19
+
20
+ ```
21
+ sudo apt-get update
22
+
23
+ apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6 libreadline6-dev zlib1g zlib1g-dev g++ make libpcre3 libpcre3-dev libssl-dev
24
+ ```
25
+
26
+ ## Ruby
27
+
28
+ You can use ruby installed by ruby version managers such as [RVM](http://rvm.io/)
29
+ or [rbenv](https://github.com/sstephenson/rbenv), or install it globally from
30
+ sources. The following manual presents global installation.
31
+
32
+ Remove the old Ruby 1.8 if present
33
+
34
+ ```
35
+ sudo apt-get remove ruby1.8
36
+ ```
37
+
38
+ Download Ruby and compile it:
39
+
40
+ ```
41
+ mkdir /tmp/ruby && cd /tmp/ruby
42
+ curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz | tar xz
43
+ cd ruby-2.1.2
44
+ ./configure --disable-install-rdoc
45
+ make
46
+ sudo make install
47
+ ```
48
+
49
+ ## Nginx installation
50
+
51
+ Download and compile nginx:
52
+
53
+ ```
54
+ mkdir -p /tmp/nginx && cd /tmp/nginx
55
+ curl http://nginx.org/download/nginx-1.7.4.tar.gz | tar xz
56
+ cd nginx-1.7.4
57
+ ./configure --with-http_ssl_module --prefix=/nginx/installation/path
58
+ make
59
+ make install
60
+ ```
61
+
62
+ Update nginx configuration:
63
+
64
+ ```
65
+ edit /nginx/installation/path/conf/nginx.conf
66
+ ```
67
+
68
+ the simplest configuration may look as follows:
69
+
70
+ ```
71
+ worker_processes 1;
72
+
73
+ pid /nginx/installation/path/nginx.pid;
74
+
75
+ events {
76
+ worker_connections 1024;
77
+ }
78
+
79
+
80
+ http {
81
+ include mime.types;
82
+ default_type application/octet-stream;
83
+
84
+ sendfile on;
85
+
86
+ keepalive_timeout 65;
87
+
88
+ types_hash_max_size 2048;
89
+ server_names_hash_bucket_size 128;
90
+
91
+ include /path/to/generated/nginx/configurations/*;
92
+ }
93
+ ```
94
+ Two elements from presented configuration need to be customized:
95
+ + `/nginx/installation/path/nginx.pid` file where nginx pid will be written.
96
+ + `/path/to/generated/nginx/configurations/*` path to the place where redirus
97
+ worker will be generating configurations specific to registered redirections. `*`
98
+ is necessary at the end in order to load all configurations from this directory.
99
+
100
+ If nginx is to bind to a low-numbered port, e.g. port 80,
101
+ the following command needs to be executed as root:
102
+
103
+ ```
104
+ # package required to invoke setcap
105
+ apt-get install libcap2-bin
106
+
107
+ # allow nginx to bind into low-numbered port
108
+ setcap 'cap_net_bind_service=+ep' /path/to/nginx/sbin/nginx
109
+ ```
110
+
111
+ ## Self signed certificate
112
+
113
+ In production environment valid certificate (with `*` in CN section) signed by trusted organization should be used. If you don't have such certificate than
114
+ you can generate self signed certificate:
115
+
116
+ ```
117
+ mkdir /usr/share/ssl/certs/my-domain.pl
118
+ cd /usr/share/ssl/certs/my-domain.pl
119
+ (umask 077 && touch host.key host.cert host.info host.pem)
120
+ openssl genrsa 2048 > host.key
121
+ openssl req -new -x509 -nodes -sha1 -days 3650 -key host.key > host.cert
122
+ ...[enter *.my-domain.pl for the Common Name]...
123
+ openssl x509 -noout -fingerprint -text < host.cert > host.info
124
+ cat host.cert host.key > host.pem
125
+ chmod 400 host.key host.pem
126
+ ```
127
+
128
+ ## Redirus worker installation
129
+
130
+ ```bash
131
+ # install redirus
132
+ gem install redirus
133
+
134
+ # Download sample redirus configuration
135
+ curl -L --progress https://raw.githubusercontent.com/dice-cyfronet/redirus/master/config.yml.example > config.yml
136
+
137
+ # Customise redis configuration and nginx config files locations
138
+ edit config.yml
139
+ ```
140
+
141
+ ## Example config.yml
142
+
143
+ ```yaml
144
+ queues:
145
+ - site_prefix
146
+ - second_site_prefix
147
+
148
+ redis_url: redis://localhost:6379
149
+ namespace: redirus
150
+
151
+ nginx:
152
+ configs_path: /path/to/generated/nginx/configurations/
153
+ 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
+ config_template: |
161
+ #{upstream}
162
+ server {
163
+ #{listen}
164
+ server_name #{name}.my-domain.pl;
165
+ server_tokens off;
166
+ proxy_set_header X-Server-Address $scheme://#{name}.my-domain.pl;
167
+ proxy_set_header Host $http_host;
168
+ location / {
169
+ proxy_pass http://#{upstream_name};
170
+ #{properties}
171
+ }
172
+ }
173
+ allowed_properties:
174
+ - proxy_sent_timeout \d
175
+ - proxy_read_timeout \d
176
+ ```
177
+
178
+ Some elements from presented configuration need to be customized:
179
+ + `/path/to/generated/nginx/configurations/` is the location where configuration
180
+ specific for concrete redirection will be created. Value of this path **need** to
181
+ be the same as in nginx configuration file.
182
+ + `/nginx/installation/path/nginx.pid` is a file containing nginx pid. To this pid
183
+ `SIGHUP` signal will be sent, which will triggers nginx configuration reload.
184
+ + `/path/to/cert/dir/server.crt` path to ssl certificate used in https redirections.
185
+ This certificate need to have `*` in CN field.
186
+ + `/path/to/cert/dir/server.key` path to ssl certificate key file.
187
+
188
+ By using `http_template`, `https_template`, `config_template` and
189
+ `allowed_properties` you can customize how nginx configuration looks like for each
190
+ subdomain.
191
+
192
+ + `http_template` is used when an http redirection is created
193
+ + `https_template` is used when an https redirection is created
194
+ + `config_template` is used for http and https redirections.
195
+ Inside this template, the `listen` variable section is specific to http or https
196
+ redirections.
197
+ + `allowed_properties` is used to define allowed parameters which can be
198
+ passed in the generated configuration. Regular expressions can be used here.
199
+
200
+ For example - when a redirection with the following parameters is requested:
201
+
202
+ ```ruby
203
+ Sidekiq::Client.push(
204
+ 'queue' => 'cyfronet',
205
+ 'class' => Redirus::Worker::AddProxy,
206
+ 'args' => ['subdomain', ['127.0.0.1:80'], :http, ["proxy_send_timeout 6000"]])
207
+ ```
208
+
209
+ ...then the following `/nginx/sites-enabled/subdomain_http` subdomain config file
210
+ will be created:
211
+
212
+ ```
213
+ upstream subdomain_http {
214
+ server 127.0.0.1:80;
215
+ }
216
+ server {
217
+ listen *:8000;
218
+ server_name subdomain.localhost;
219
+ server_tokens off;
220
+ location / {
221
+ proxy_pass http://subdomain_http;
222
+ proxy_send_timeout 6000;
223
+ }
224
+ }
225
+ ```
226
+
227
+ ## Run redirus
228
+
229
+ ```bash
230
+ redirus -c redirus_configuration_path
231
+ ```
232
+
233
+ When no configuration path is given than redirus tries to load configuration from `config.yml` located in current dir.
234
+
235
+ See `redirus -h` for details.
236
+
237
+ ## Run redirus client
238
+
239
+ ```
240
+ redirus-client -c config.yml -a add -t http my_redirection 10.100.0.1:80,10.100.0.2:80
241
+ redirus-client -c config.yml -a rm -t http my_redirection
242
+ ```
243
+
244
+ See `redirus-client -h` for details.
245
+
246
+ ## Starting using upstart
247
+
248
+ **NOTE**: presented configuration was tested on Ubuntu 14.04
249
+
250
+ Copy upstart configuration files:
251
+
252
+ ```
253
+ cd redirus-worker-directory
254
+ mkdir -p ${HOME}/.config/upstart
255
+ cd ${HOME}/.config/upstart
256
+
257
+ curl -L --progress https://raw.githubusercontent.com/dice-cyfronet/redirus/master/support/upstart/redirus.conf > redirus.conf
258
+ curl -L --progress https://raw.githubusercontent.com/dice-cyfronet/redirus/master/support/upstart/redirus-sidekiq.conf > redirus-sidekiq.conf
259
+ curl -L --progress https://raw.githubusercontent.com/dice-cyfronet/redirus/master/support/upstart/redirus-nginx.conf > redirus-nginx.conf
260
+
261
+ # Specify user name, path under which the redirus worker is installed and
262
+ # the location of the nginx configuration directory to be created:
263
+ editor ${HOME}/.config/upstart/redirus.conf
264
+
265
+ # Similar as above, plus if you are using ruby version manager - uncomment and
266
+ # customize the appropriate section for rbenv or rvm
267
+ editor ${HOME}/.config/upstart/redirus-sidekiq-1.conf
268
+
269
+ # Update path to nginx
270
+ editor ${HOME}/.config/upstart/redirus-nginx.conf
271
+ ```
272
+ Now you should be able to start/stop/restart redirus using the
273
+ following commands:
274
+
275
+ ```
276
+ initctl start redirus
277
+ initctl stop redirus
278
+ initctl restart redirus
279
+ ```
280
+
281
+ ## Generating Add/Remove redirection requests from ruby code
282
+
283
+ ```ruby
284
+ require 'rubygems'
285
+ require 'redirus/worker'
286
+ require 'redirus/worker/add_proxy'
287
+ require 'redirus/worker/rm_proxy'
288
+
289
+ # configure sidekiq client
290
+ Sidekiq.configure_client do |c|
291
+ c.redis = {
292
+ namespace: Redirus::Worker.config.namespace,
293
+ url: Redirus::Worker.config.redis_url,
294
+ queue: Redirus::Worker.config.queues.first
295
+ }
296
+ end
297
+
298
+ # add new redirection
299
+ Sidekiq::Client.push(
300
+ 'queue' => Redirus::Worker.config.queues.first,
301
+ 'class' => Redirus::Worker::AddProxy,
302
+ 'args' => ['subdomain', ['127.0.0.1'], :http, ["proxy_send_timeout 6000"]])
303
+
304
+ # remove redirection
305
+ Sidekiq::Client.push(
306
+ 'queue' => Redirus::Worker.config.queues.first,
307
+ 'class' => Redirus::Worker::RmProxy,
308
+ 'args' => ['subdomain', :http])
309
+ ```
310
+
311
+ ## Contributing
312
+
313
+ 1. Fork the project
314
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
315
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
316
+ 4. Push to the branch (`git push origin my-new-feature`)
317
+ 5. Create new pull request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new('rspec')
4
+
5
+ task :default => :rspec
data/bin/redirus ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'redirus'
4
+
5
+ cli = Redirus::ServerCLI.instance
6
+ cli.parse
7
+ cli.run
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'redirus'
4
+
5
+ cli = Redirus::CLI.instance
6
+ cli.parse
7
+ cli.run
@@ -0,0 +1,32 @@
1
+ queues:
2
+ - site_prefix
3
+ - second_site_prefix
4
+
5
+ redis_url: redis://localhost:6379
6
+ namespace: redirus
7
+
8
+ nginx:
9
+ configs_path: /path/to/dir/with/nginx/configs/
10
+ pid: /path/to/nginx.pid
11
+ http_template: |
12
+ listen *:80;
13
+ https_template: |
14
+ listen *:443 ssl;
15
+ ssl_certificate /path/to/cert/dir/server.crt;
16
+ ssl_certificate_key /path/to/cert/dir/server.key;
17
+ config_template: |
18
+ #{upstream}
19
+ server {
20
+ #{listen}
21
+ server_name #{name}.my-domain.pl;
22
+ server_tokens off;
23
+ proxy_set_header X-Server-Address $scheme://#{name}.my-domain.pl;
24
+ proxy_set_header Host $http_host;
25
+ location / {
26
+ proxy_pass http://#{upstream_name};
27
+ #{properties}
28
+ }
29
+ }
30
+ allowed_properties:
31
+ - proxy_sent_timeout \d
32
+ - proxy_read_timeout \d
@@ -0,0 +1,136 @@
1
+ require 'optparse'
2
+ require 'singleton'
3
+
4
+ module Redirus
5
+ class CLI
6
+ include Singleton
7
+
8
+ attr_reader :options
9
+
10
+ def parse(args = ARGV)
11
+ init_options(args)
12
+ validate!
13
+ end
14
+
15
+ def run
16
+ add? ? add : rm
17
+ end
18
+
19
+ private
20
+
21
+ def init_options(args)
22
+ opts = parse_options(args)
23
+ opts[:config_path] ||= 'config.yml'
24
+
25
+ Redirus.config_path = opts[:config_path]
26
+
27
+ @options = defaults.merge(opts)
28
+ end
29
+
30
+ def parse_options(args)
31
+ opts = {}
32
+
33
+ parser = OptionParser.new do |o|
34
+ o.on('-c', '--configuration PATH', 'Configuration path') do |arg|
35
+ opts[:config_path] = arg
36
+ end
37
+
38
+ o.on('-a',
39
+ '--action TYPE', [:add, :rm],
40
+ 'Action type (add, rm)') do |arg|
41
+ opts[:action] = arg
42
+ end
43
+
44
+ o.on('-q', '--queue NAME', 'Queue name') do |arg|
45
+ opts[:queue] = arg
46
+ end
47
+
48
+ o.on(
49
+ '-t',
50
+ '--type TYPE',
51
+ [:http, :https],
52
+ 'Rediraction type') do |arg|
53
+ opts[:type] = arg
54
+ end
55
+
56
+ o.on_tail("-h", "--help", "Show this message") do
57
+ puts o
58
+ exit
59
+ end
60
+
61
+ o.on_tail('-v', '--version', 'Show version') do
62
+ puts "Redirus #{Redirus.VERSION}"
63
+ exit
64
+ end
65
+ end
66
+
67
+ parser.banner = 'redirus-client [options] name [upstream1,upstream2,...]'
68
+ parser.parse!(args)
69
+
70
+ opts[:name] = args.shift
71
+
72
+ upstreams_str = args.shift
73
+ opts[:upstreams] = upstreams_str.split(',') if upstreams_str
74
+
75
+ opts
76
+ end
77
+
78
+ def defaults
79
+ {
80
+ config_path: 'config.yml',
81
+ type: :http,
82
+ queue: Redirus.config.queues.first
83
+ }
84
+ end
85
+
86
+ def validate!
87
+ unless File.exist?(options[:config_path])
88
+ puts "ERROR: Configuration file #{options[:config_path]} does not exist"
89
+ exit(1)
90
+ end
91
+
92
+ unless options[:name]
93
+ puts "ERROR: Redirection name is not set"
94
+ exit(1)
95
+ end
96
+
97
+ if add?
98
+ unless options[:upstreams]
99
+ puts "ERROR: Upstream locations are not set"
100
+ exit(1)
101
+ else
102
+ options[:upstreams].each do |u|
103
+ unless u.match(/\A\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}:\d+\z/)
104
+ puts "ERROR: #{u} is not valid upstream definition, use IP:PORT schema"
105
+ exit(1)
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ Redirus.config_path = options[:config_path]
112
+ end
113
+
114
+ def add?
115
+ options[:action] == :add
116
+ end
117
+
118
+ def add
119
+ puts "Adding new redirection #{options[:name]} with following upstreams #{options[:upstreams]}"
120
+
121
+ Sidekiq::Client.push(
122
+ 'queue' => options[:queue],
123
+ 'class' => Redirus::Worker::AddProxy,
124
+ 'args' => [options[:name], options[:upstreams], options[:type]])
125
+ end
126
+
127
+ def rm
128
+ puts "Removing redirection #{options[:name]}"
129
+
130
+ Sidekiq::Client.push(
131
+ 'queue' => options[:queue],
132
+ 'class' => Redirus::Worker::RmProxy,
133
+ 'args' => [options[:name], options[:type]])
134
+ end
135
+ end
136
+ end