forked from gderosa/onboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonboard.rb
122 lines (97 loc) · 2.96 KB
/
onboard.rb
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
# encoding: utf-8
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib'
require 'rubygems'
require 'find'
require 'json'
require 'yaml'
require 'logger'
require 'pp'
require 'onboard/extensions/object'
require 'onboard/menu/node'
require 'onboard/platform/debian'
class OnBoard
LONGNAME = 'Ruby OnBoard'
ROOTDIR = File.dirname File.expand_path(__FILE__)
CONFDIR = ROOTDIR + '/etc/config'
PLATFORM = Platform::Debian # TODO? make in configurable?
LOGGER = Logger.new(ROOTDIR + '/' + 'onboard.log')
MENU_ROOT = Menu::MenuNode.new('ROOT', {
:href => '/',
:name => 'Home',
:desc => 'Home page',
:n => 0
})
#LOGGER.datetime_format = "%Y-%m-%d %H:%M:%S"
LOGGER.formatter = proc { |severity, datetime, progname, msg|
"#{datetime} #{severity}: #{msg}\n"
}
LOGGER.info "Ruby OnBoard started."
def self.find_n_load(dir)
# sort to resamble /etc/rc*.d/* or run-parts behavior
Find.find(dir).sort.each do |file|
if file =~ /\.rb$/
print "loading: #{file}... "
STDOUT.flush
if load file
print "OK\n"
STDOUT.flush
end
end
end
end
def self.prepare
# menu
unless ARGV.include? '--restore-only'
# modular menu
find_n_load ROOTDIR + '/etc/menu/'
end
# modules
Dir.foreach(ROOTDIR + '/modules') do |dir|
dir_fullpath = ROOTDIR + '/modules/' + dir
if File.directory? dir_fullpath and not dir =~ /^\./
file = dir_fullpath + '/load.rb'
if File.readable? file
load dir_fullpath + '/load.rb'
else
STDERR.puts "Warning: Couldn't load modules/#{dir}/load.rb: Skipped!"
end
end
end
# restore scripts, sorted like /etc/rc?.d/ SysVInit/Unix/Linux scripts
unless ARGV.include? '--no-restore'
restore_scripts =
Dir.glob(ROOTDIR + '/etc/restore/[0-9][0-9]*.rb') +
Dir.glob(ROOTDIR + '/modules/*/etc/restore/[0-9][0-9]*.rb')
restore_scripts.sort!{|x,y| File.basename(x) <=> File.basename(y)}
restore_scripts.each do |script|
print "loading: #{script}... "
STDOUT.flush
begin
load script and puts "OK"
rescue
exception = $!
puts exception.inspect
LOGGER.error "loading #{script}: #{exception.inspect}"
backtrace_str = "Exception backtrace follows:"
exception.backtrace.each{|line| backtrace_str << "\n" << line}
LOGGER.error backtrace_str
end
end
end
end
def self.save!
LOGGER.info 'Saving configuration...'
find_n_load ROOTDIR + '/etc/save/'
# modules
Dir.glob(ROOTDIR + '/modules/*/etc/save/*.rb').each do |script|
print "loading: #{script}... " and STDOUT.flush
load script and puts ' OK'
end
end
end
OnBoard.prepare
exit if ARGV.include? '--restore-only'
require OnBoard::ROOTDIR + '/controller.rb'
if $0 == __FILE__
OnBoard::Controller.run!(:host => '::')
end