ts-admin 0.2.3 → 0.2.4

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: 528db841c6ef8e98947079034726aa5a4ce3214b
4
- data.tar.gz: 5fe471e105e13a03729202b24991bd301c357825
3
+ metadata.gz: 8cfd3ae8690d1a8d7947db457375f7ef7b1cb93b
4
+ data.tar.gz: d873461743c02ff698fd70dccf7c595399ec7977
5
5
  SHA512:
6
- metadata.gz: 68fd21ece886a89189657474545b41a7098164e946bae268d82a6a848f8d32884186b368e6118bf1481979dbce20257bdfef2ae96d958454a4dd32a89ff2b037
7
- data.tar.gz: c1ad67697964dc72e52e1c0d558f203fef5d92c001e9b6a69e8218034c1a57dfcad6dbf136f0004768b352dfaeb9cfd50a9859ec810ad085b3ae76cfba473564
6
+ metadata.gz: 25cc66b1569c8319b3e5a2340db91438c5aea729050216124cc5ddaf84ecd4000fa0c83d946725a7c192740b8cc557507f71842af139b4defae30ee1754f62db
7
+ data.tar.gz: e2b9b88282ee405f46ca8137ab511511661cf11f89b993ffdb2b75204c7cc656993820171dbac30f9259c3f10e451220e1a626fcc92392e4a215cd05082e3350
data/app.rb CHANGED
@@ -17,5 +17,6 @@ APP_CONFIG = {
17
17
  # Make sure that Ramaze knows where you are
18
18
  Ramaze.options.roots = [__DIR__]
19
19
 
20
- require __DIR__('lib/ts-admin/traffic_server')
20
+ $: << __DIR__('lib')
21
+ require 'ts-admin'
21
22
  require __DIR__('controller/init')
data/controller/remap.rb CHANGED
@@ -14,13 +14,14 @@ class RemapController < Controller
14
14
  if request.post?
15
15
  @from = request[:from]
16
16
  @to = request[:to]
17
- if traffic_server.add_remap(@type, @from, @to)
17
+ if (entry = traffic_server.new_remap_entry(@type, @from, @to)).valid?
18
+ traffic_server.remap_entries << entry
18
19
  traffic_server.save
19
20
  restart_traffic_server
20
- flash[:info] = "Remap entry added"
21
+ flash[:info] = "#{entry.type.to_s.capitalize} definition added"
21
22
  call(r('/'))
22
23
  else
23
- flash[:error] = "Invalid Remap entry"
24
+ set_errors(entry)
24
25
  render_view :form
25
26
  end
26
27
  else
@@ -37,21 +38,31 @@ class RemapController < Controller
37
38
  if request.post?
38
39
  @from = request[:from]
39
40
  @to = request[:to]
40
- if traffic_server.edit_remap(@id, @from, @to)
41
- traffic_server.save
42
- restart_traffic_server
43
- flash[:info] = "Remap entry updated"
44
- call(r('/'))
41
+ if entry = traffic_server.remap_entries.get_id(@id).first
42
+ entry.from = @from
43
+ entry.to = @to
44
+ if entry.valid?
45
+ traffic_server.save
46
+ restart_traffic_server
47
+ flash[:info] = "#{entry.type.to_s.capitalize} definition updated"
48
+ call(r('/'))
49
+ else
50
+ set_errors(entry)
51
+ render_view :form
52
+ end
45
53
  else
46
- flash[:error] = "Invalid Remap entry"
54
+ flash[:error] = "Remap definition not found"
47
55
  render_view :form
48
56
  end
49
57
  else
50
- @entry = traffic_server.find_remap_by_id(@id)
51
- @from = @entry[:from]
52
- @to = @entry[:to]
53
- @type = @entry[:type]
54
- render_view :form
58
+ if @entry = traffic_server.remap_entries.get_id(@id).first
59
+ @type = @entry.type
60
+ @from = @entry.from
61
+ @to = @entry.to
62
+ render_view :form
63
+ else
64
+ call(r('/'))
65
+ end
55
66
  end
56
67
  end
57
68
 
@@ -59,11 +70,14 @@ class RemapController < Controller
59
70
  return unless login_required
60
71
  set_env
61
72
 
62
- traffic_server.delete_remap(request[:id])
63
- traffic_server.save
64
- restart_traffic_server
73
+ @id = request[:id]
65
74
 
66
- flash[:info] = "Remap entry removed"
75
+ if entry = traffic_server.remap_entries.get_id(@id).first
76
+ traffic_server.remap_entries.delete(entry)
77
+ traffic_server.save
78
+ restart_traffic_server
79
+ flash[:info] = "#{entry.type.to_s.capitalize} definition removed"
80
+ end
67
81
 
68
82
  call(r('/'))
69
83
  end
@@ -83,4 +97,17 @@ class RemapController < Controller
83
97
  @type = request[:type]
84
98
  end
85
99
 
100
+ def set_errors(entry)
101
+ case entry.errors.first
102
+ when :type_invalid
103
+ flash[:error] = "Invalid remap type: #{entry.type.to_s}"
104
+ when :from_invalid
105
+ flash[:error] = "Invalid from: #{entry.from.to_s}"
106
+ when :to_invalid
107
+ flash[:error] = "Invalid to: #{entry.to.to_s}"
108
+ when :duplicate_entry
109
+ flash[:error] = "Duplicate remap definition"
110
+ end
111
+ end
112
+
86
113
  end
data/layout/default.xhtml CHANGED
@@ -39,7 +39,7 @@
39
39
  <footer id="footer">
40
40
  <p>
41
41
  &copy; 2012-2013 <a href="http://www.sic-software.com/">SIC! Software GmbH</a><br />
42
- <a href="https://github.com/SICSoftwareGmbH/TSAdmin">TSAdmin</a> is free software and is licensed under the MIT license.
42
+ <a href="https://github.com/SICSoftwareGmbH/TSAdmin">TSAdmin</a> (v#{TSAdmin::VERSION}) is free software and is licensed under the MIT license.
43
43
  </p>
44
44
  </footer>
45
45
  </div>
@@ -5,89 +5,87 @@ module TSAdmin
5
5
  class TrafficServer
6
6
  MARKER_TEXT = "CONFIGURATION FROM TSADMIN"
7
7
 
8
- def initialize(options={})
9
- @options = {
10
- 'config_path' => '/etc/trafficserver',
11
- 'restart_cmd' => '/etc/init.d/trafficserver restart'
12
- }.merge(options)
13
- @config_path = @options['config_path']
14
- end
15
-
16
- def redirects
17
- load unless defined?(@redirects)
18
- @redirects.dup.freeze
19
- end
20
-
21
- def maps
22
- load unless defined?(@maps)
23
- @maps.dup.freeze
24
- end
8
+ class RemapError < StandardError; end
25
9
 
26
- def find_remap_by_id(id)
27
- (maps + redirects).select{|m| m[:id] == id}.first
10
+ class ValidatingArray < Array
11
+ class InvalidObject < StandardError; end
12
+ def add(o)
13
+ raise InvalidObject unless o.respond_to?(:valid?) && o.valid?
14
+ super
15
+ end
16
+ def method_missing(m, *args, &block)
17
+ if m.to_s =~ /^get_(\w+)$/ && args.count == 1
18
+ select{|i| i.respond_to?($1) && i.send($1) == args[0]}
19
+ else
20
+ super
21
+ end
22
+ end
28
23
  end
29
24
 
30
- def add_remap(type, from, to, options={})
31
- load
25
+ class RemapEntry
26
+ attr_accessor :type, :id, :from, :to
32
27
 
33
- return unless list = case type.to_sym
34
- when :map
35
- @maps
36
- when :redirect
37
- @redirects
38
- else
39
- nil
40
- end
28
+ def initialize(ts, type, from, to)
29
+ @ts = ts
30
+ @type = type.to_sym
31
+ @from = from.downcase
32
+ @to = to.downcase
33
+ @id = Digest::SHA1.hexdigest(@from)
34
+ end
41
35
 
42
- id = Digest::SHA1.hexdigest("#{from}_#{to}")
36
+ def valid?
37
+ errors.count == 0
38
+ end
43
39
 
44
- return false if find_remap_by_id(id)
45
- return false unless validate_url(from) && validate_url(to)
40
+ def errors
41
+ e = []
42
+ e << :type_invalid unless [:map, :redirect].include?(@type)
43
+ e << :from_invalid unless valid_url?(@from)
44
+ e << :to_invalid unless valid_url?(@to)
45
+ e << :duplicate_entry unless @ts.remap_entries.get_from(@from).select{|e| e != self}.count == 0
46
+ e.compact.uniq
47
+ end
46
48
 
47
- list << {:id => id, :type => type.to_sym, :from => from, :to => to, :options => options}
49
+ private
48
50
 
49
- true
51
+ def valid_url?(url)
52
+ !!(url =~ URI::regexp) rescue false
53
+ end
50
54
  end
51
55
 
52
- def edit_remap(id, from, to, options=nil)
53
- return false unless validate_url(from) && validate_url(to)
54
- return false unless entry = find_remap_by_id(id)
55
- entry[:from] = from
56
- entry[:to] = to
57
- entry[:options] = options unless options.nil?
58
- entry[:id] = Digest::SHA1.hexdigest("#{from}_#{to}")
56
+ def initialize(options={})
57
+ @options = {
58
+ 'config_path' => '/etc/trafficserver',
59
+ 'restart_cmd' => '/etc/init.d/trafficserver restart'
60
+ }.merge(options)
61
+ @config_path = @options['config_path']
59
62
  end
60
63
 
61
- def delete_remap(id)
62
- load
63
-
64
- @maps.delete_if{|m| m[:id] == id}
65
- @redirects.delete_if{|m| m[:id] == id}
64
+ def remap_entries
65
+ load unless defined?(@remap_entries)
66
+ @remap_entries
67
+ end
66
68
 
67
- true
69
+ def new_remap_entry(type, from, to)
70
+ RemapEntry.new(self, type, from, to)
68
71
  end
69
72
 
70
73
  def save
71
74
  own_config = ''
72
- own_config << "#{marker_begin}\n"
73
- redirects.each do |redirect|
74
- options = redirect[:options] ? '' : ''
75
- own_config << "redirect #{redirect[:from]} #{redirect[:to]} #{options}\n"
76
- end
77
- maps.each do |map|
78
- options = map[:options] ? '' : ''
79
- own_config << "map #{map[:from]} #{map[:to]} #{options}\n"
75
+ own_config << "#{self.class.marker_begin}\n"
76
+ remap_entries.each do |remap_entry|
77
+ own_config << "#{remap_entry.type.to_s} #{remap_entry.from} #{remap_entry.to}\n"
80
78
  end
81
- own_config << "#{marker_end}\n"
79
+ own_config << "#{self.class.marker_end}\n"
82
80
 
83
81
  file_content = ''
84
82
  in_config_block = false
85
83
  own_config_written = false
86
84
  File.read(remap_path).each_line do |line|
87
- if line.strip == marker_begin
85
+ if line.strip == self.class.marker_begin
88
86
  in_config_block = true
89
87
  next
90
- elsif line.strip == marker_end
88
+ elsif line.strip == self.class.marker_end
91
89
  in_config_block = false
92
90
  next
93
91
  end
@@ -112,28 +110,19 @@ module TSAdmin
112
110
 
113
111
  private
114
112
 
115
- def marker_begin
116
- "# BEGIN #{MARKER_TEXT}"
117
- end
118
-
119
- def marker_end
120
- "# END #{MARKER_TEXT}"
121
- end
122
-
123
113
  def remap_path
124
114
  File.join(@config_path, 'remap.config')
125
115
  end
126
116
 
127
117
  def load
128
- @redirects = []
129
- @maps = []
118
+ @remap_entries = ValidatingArray.new
130
119
 
131
120
  in_config_block = false
132
121
  File.read(remap_path).each_line do |line|
133
- if line.strip == marker_begin
122
+ if line.strip == self.class.marker_begin
134
123
  in_config_block = true
135
124
  next
136
- elsif line.strip == marker_end
125
+ elsif line.strip == self.class.marker_end
137
126
  in_config_block = false
138
127
  next
139
128
  end
@@ -141,19 +130,17 @@ module TSAdmin
141
130
  next unless in_config_block
142
131
 
143
132
  type, from, to, *options = line.split(/\s+/)
144
- id = Digest::SHA1.hexdigest("#{from}_#{to}")
145
- case type.to_sym
146
- when :redirect
147
- @redirects << {:id => id, :type => type.to_sym, :from => from, :to => to, :options => {}}
148
- when :map
149
- @maps << {:id => id, :type => type.to_sym, :from => from, :to => to, :options => {}}
150
- end
133
+ @remap_entries << RemapEntry.new(self, type, from, to)
134
+ @remap_entries.delete_if{|e| !e.valid?}
151
135
  end
152
136
  end
153
137
 
154
- def validate_url(url)
155
- !!(url =~ URI::regexp) rescue false
138
+ def self.marker_begin
139
+ "# BEGIN #{MARKER_TEXT}"
156
140
  end
157
141
 
142
+ def self.marker_end
143
+ "# END #{MARKER_TEXT}"
144
+ end
158
145
  end
159
146
  end
@@ -1,3 +1,3 @@
1
1
  module TSAdmin
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  <div class="grid_12 content">
2
2
  <header>
3
- <h2>Remap entry</h2>
3
+ <h2>#{@type.capitalize} entry</h2>
4
4
  </header>
5
5
  <form method="post" action="/remap/#{defined?(@id) ? 'edit' : 'new'}">
6
6
  <p>
@@ -7,14 +7,14 @@
7
7
  </header>
8
8
  <p>
9
9
  <ul class="remap">
10
- <?r @traffic_server.send("#{@type}s").each_with_index do |map, i| ?>
10
+ <?r @traffic_server.remap_entries.get_type(@type.to_sym).each do |entry| ?>
11
11
  <li>
12
- <span class="url from"><a href="#{map[:from]}">#{remap_url_highlight_scheme(map[:from])}</a></span>
12
+ <span class="url from"><a href="#{entry.from}">#{remap_url_highlight_scheme(entry.from)}</a></span>
13
13
  <span class="arrow icon-chevron-right"></span>
14
- <span class="url to"><a href="#{map[:to]}">#{remap_url_highlight_scheme(map[:to])}</a></span>
14
+ <span class="url to"><a href="#{entry.to}">#{remap_url_highlight_scheme(entry.to)}</a></span>
15
15
  <span class="buttons">
16
- <a class="icon-pencil" href="/remap/edit?id=#{map[:id]}" title="Edit"></a>
17
- <a class="icon-trash" href="/remap/delete?id=#{map[:id]}" title="Delete"></a>
16
+ <a class="icon-pencil" href="/remap/edit?id=#{entry.id}" title="Edit"></a>
17
+ <a class="icon-trash" href="/remap/delete?id=#{entry.id}" title="Delete"></a>
18
18
  </span>
19
19
  </li>
20
20
  <?r end ?>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ts-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Schwab
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-01 00:00:00.000000000 Z
11
+ date: 2013-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler