-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic_web.rb
More file actions
executable file
·334 lines (290 loc) · 10.4 KB
/
logic_web.rb
File metadata and controls
executable file
·334 lines (290 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env ruby
# Nitrogen Logic Automation Controller logic system status page
# (C)2016 Mike Bourgeous
#
# Useful references:
# http://titusd.co.uk/2010/04/07/a-beginners-sinatra-tutorial
require 'bundler/setup'
require 'rubygems'
require 'sinatra/async'
require 'socket'
require 'dnssd'
require 'json'
require 'nl/logic_client'
$:.unshift(File.expand_path('../lib', __FILE__))
require 'logic_web/version'
if __FILE__ == $0
exec('rackup -p 4567 -s thin ' + File.join(File.dirname(__FILE__), 'logic_web.ru'))
end
# Prints the given message, prefixed by the current time.
def n2log(msg)
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%6N %z')} - #{msg}"
end
# Logs an exception or error message.
def n2log_e(e, msg = nil)
if e.is_a? Exception
n2log "\e[1;31m#{msg ? (msg + ' - ') : ''}#{e.inspect}\e[0;31m\n\t#{e.backtrace.join("\n\t")}\e[0m"
else
n2log "\e[0;31m#{e}\e[0m"
end
end
# Implement logging by wrapping some async_sinatra internals. Logging can be
# disabled for an entire route by passing :no_log (with any value) in the route
# options, or by setting @no_log to a truthy value. @no_log may also be a
# lambda that returns whether to log a particular request.
module Sinatra::Async
alias :oldaroute :aroute
def aroute(verb, path, opts = {}, &block)
# Based on aroute from async_sinatra
run_method = :"RunA#{verb} #{path} #{opts.hash}"
define_method run_method, &block
no_log = opts.include?(:no_log)
opts.delete :no_log
log_method = :"LogA#{verb} #{path} #{opts.hash}"
define_method(log_method) { |*a|
@no_log = @no_log.call if @no_log.respond_to? :call
unless @no_log || no_log
msg = "#{request.ip} - #{status} #{verb} #{request.path}"
msg << " [#{path}]" if path != request.path
n2log msg
end
}
oldaroute verb, path, opts do |*a|
oldcb = request.env['async.callback']
request.env['async.callback'] = proc { |*args|
oldcb[*args]
async_runner(log_method, *a)
}
async_runner(run_method, *a)
end
end
end
module Sinatra::Async::Helpers
if Gem.respond_to? :searcher
# Rubygems 1.3.7
version = Gem.searcher.find('async_sinatra').version
else
# Rubygems 2.x
version = Gem::Specification.find_by_name('async_sinatra').version
end
if version <= Gem::Version.new('0.5.0')
# This is a workaround for a bug in async_sinatra 0.5.0
# Without this, a request that triggers an exception will never
# have its response sent.
alias :old_async_exception :async_handle_exception
def async_handle_exception(*args, &block)
raised = false
old_async_exception *args do |*a|
begin
yield *a
rescue ::Exception => e
n2log_e e, "Exception caught by async_handle_exception"
if settings.show_exceptions?
raised = true
raise
else
@title = 'Internal server error'
body erb box('An internal error occurred. Please contact your support representative.', @title)
end
end
end
if raised
env['async.callback'] [[response.status, response.headers, response.body]]
end
end
end
end
# For running a subset of commands via sudo. Only authorized commands set to
# NOPASSWD in /etc/sudoers will work. Will only run sudo if it exists in
# /usr/bin/sudo or /bin/sudo. Uses EventMachine::system().
#
# TODO: Share with KNC?
module Sudo
@@sudo_cmd = if File.executable?('/usr/bin/sudo')
'/usr/bin/sudo -S -n -- '
elsif File.executable?('/bin/sudo')
'/bin/sudo -S -n -- '
else
# TODO: Use a logging method that automatically adds timestamps
n2log_e "#{Time.now} - sudo command not found -- some tasks may not work"
''
end
# Passes the complete command_line to sudo, like this: "sudo -n -- [command_line]". The
# block will be passed to EM.system as well.
def self.sudo(command_line, &block)
n2log "SUDO: Calling sudo: #{@@sudo_cmd}#{command_line}"
EM.system('/bin/sh', '-c', "#{@@sudo_cmd}#{command_line}") do |*a|
block.call(*a)
end
end
end
class LogicStatusPage < Sinatra::Base
end
# FIXME: This class/require/class combo is needed so that Sinatra doesn't think
# that 'lib/' is our top-level directory. TODO: move all the netdevices
# methods into a different module and use qualified names.
require 'netdevices'
class LogicStatusPage < Sinatra::Base
register Sinatra::Async
def self.get_or_post(path, opts={}, &block)
get path, opts, &block
post path, opts, &block
end
def self.aget_or_post(path, opts={}, &block)
aget path, opts, &block
apost path, opts, &block
end
configure do
set :public_folder => File.dirname(__FILE__) + '/static'
$lsrv=ENV["LOGIC_HOST"] || 'localhost'
LogicStatusPage.start_network_browser
end
configure :development do
Thread.abort_on_exception = true
enable :raise_exceptions
end
configure :production do
enable :logging
end
helpers do
include Rack::Utils
alias :h :escape_html;
# Returns the current @title, or a default title if @title is nil.
def get_title
@title || "Nitrogen Logic Controller Status"
end
# Returns a string containing an error page
def error_page(header = 'Error', message = 'An error occurred.', http_status = nil)
content_type 'text/html', :charset => 'utf-8'
status(http_status || 500) if status == 200 or http_status
@title = "Error - #{get_title}"
erb :error, :locals => {:header => header || '', :message => message || ''}
end
# Sets an error_page-calling errback on a Logic System Command object
def check(cmd)
cmd.errback { |c|
body error_page 'Error processing command', c ? c.message : ''
}
end
# Returns a string containing a backend connection error page
def connect_error
error_page 'Error connecting', 'An error occurred while connecting to the logic backend.'
end
# Wraps content in a light-colored box. If header is an array,
# the first element will be HTML escaped and placed in the
# primary header. The second element will be used as a
# secondary header without HTML escaping.
def box_light(content = '', header = nil, classes = nil, tag = 'section')
# TODO: If allowing any user content in here, prevent
# injection of unwanted text
@boxcontent = content || ''
if header.is_a? Array
@boxheader = h header[0]
@boxright = header[1]
else
@boxheader = h header
@boxright = nil
end
@boxclasses = (classes && classes.respond_to?(:join)) ? classes.join(' ') : classes
@boxtag = tag || 'section'
erb :_box, :layout => false
end
# Wraps content in a dark-colored box
def box(content = '', header = nil, classes = nil, tag = 'section')
# TODO: If allowing any user content in here, prevent
# injection of unwanted text
classes = classes || [];
classes = ['darkbox', 'invbox', *Array(classes)];
box_light content, header, classes, tag
end
# Does a rough conversion of raw text to html.
def t2h(text)
# TODO: Use markdown or some other template engine?
escape_html(text).gsub(/\r?\n(.)/, "<br>\n\\1").gsub("\t", ' ');
end
# Gets a connection to the logic server and executes the block with it.
# Returns an error page to the client in the event of an error.
def logic(&block)
NL::LC.get_connection($lsrv, proc { yield nil }) do |c|
yield c
end
rescue EventMachine::ConnectionError => e
n2log_e e, "ConnectionError while connecting to logic backend"
yield nil
end
# Gets exports from the given logic_connection (obtained via
# the logic() helper above), then sends them to the client as
# JSON.
def send_exports(logic_connection)
cmd = logic_connection.get_exports { |exports|
content_type 'application/json', :charset => 'utf-8'
response.headers['Cache-Control'] = 'no-cache'
response.headers['Pragma'] = 'no-cache'
body exports.map {|ex| ex.to_h}.to_json
}
cmd.errback { |c|
content_type 'text/plain', :charset => 'utf-8'
status 422
text = 'Error getting exports from the backend'
text << (c ? ": #{c.message}" : '.')
body text
}
end
end
aget '/' do
logic do |c|
if c.nil?
@exports = []
@design = {
'name' => 'No design is running.',
'revision' => [0, 0],
'numobjs' => 0,
'avg' => Float::INFINITY,
'period' => Float::INFINITY,
}
body erb(:index)
else
cmd = c.get_exports do |list|
cmd2 = c.get_info do |info|
@exports = list
@design = info
body erb(:index)
end
cmd2.errback { body error_page 'Error getting logic design info' }
end
cmd.errback { body error_page 'Error getting list of exports' }
end
end
# TODO: When rendering a view, allow partials to add a stylesheet to the CSS
# links in <head> by inserting the path name of the stylesheet relative to the
# application (e.g. <% @styles << '/css/exports.css' %>)
# TODO: Allow automatic dynamic replacement of an individual partial view via
# AJAX. Wrap the partial in an unstyled div or span (use a tag parameter to a
# partial() helper method to specify any container, similar to box() -- set
# class="partial", data-partial="[name_of_partial]" on the container), then use
# JavaScript to set the innerHTML of the tag to the new content of the partial.
# Allow specifying update interval (in some granularity to allow grouping of
# requests). Maybe serve the content of multiple partials in a single JSON
# return. Also make it possible to have form "submission" replace a partial
# (use JavaScript to submit the form instead, or use an iframe).
end
not_found do
# FIXME: This doesn't work in production mode
status 404
n2log "#{request.ip} - #{status} #{request.request_method} #{request.url}"
@title = '404 - Not Found'
erb :notfound
end
end
# Calls require on each file in the given directory that ends with '.rb'. Does
# not recurse into subdirectories.
def require_dir(dir)
Dir.foreach(File.expand_path(dir)) do |file|
if file.end_with? '.rb'
path = File.join(dir, file)
puts "Loading #{path}"
require path
end
end
end
require_dir './routes/'