-
Notifications
You must be signed in to change notification settings - Fork 46
/
hanlon_init
executable file
·207 lines (159 loc) · 5.94 KB
/
hanlon_init
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
#!/usr/bin/env ruby
#
# Hanlon application initialization script
#
#
require 'pathname'
require 'colored'
require 'optparse'
require 'yaml'
require 'json'
$LOAD_PATH.unshift((Pathname(__FILE__).realpath.dirname + './core').cleanpath.to_s)
$LOAD_PATH.unshift((Pathname(__FILE__).realpath.dirname + './util').cleanpath.to_s)
$app_root = Pathname(__FILE__).realpath.dirname.to_s
$hanlon_root = Pathname(__FILE__).realpath.dirname.to_s
require 'config/server'
require 'config/client'
require 'properties'
require 'helper/swagger'
#require "highline/system_extensions" # gem install highline
module ProjectHanlon
class Init
def initialize
nil
end
def run(options)
default_params = {}
if options[:defaults_file]
default_params = begin
YAML.load(File.open(options[:defaults_file]))
rescue Exception => e
puts "Could not parse defaults YAML file (#{options[:defaults_file]}): #{e.message}"
exit 3
end
elsif options[:json_defaults]
default_params = begin
hash = JSON.parse(options[:json_defaults])
hash.each { |k, v| hash[k] = v.gsub(/^@/,'').to_sym if /^@/.match(v) }
rescue Exception => e
puts "Could not parse defaults JSON string '#{options[:json_defaults]}': #{e.message}"
exit 4
end
end
puts
puts "#{ProjectHanlon::Properties.app_name} (ver #{ProjectHanlon::Properties.app_build_version})".green
puts ProjectHanlon::Properties.app_copy_right.yellow
puts
puts "Setting initial environment for #{ProjectHanlon::Properties.app_name}".blue
puts
validate_hanlon_struct
create_hanlon_config_file(default_params)
puts
puts "Setting up of #{ProjectHanlon::Properties.app_name} environment complete".blue
return 0
end
def create_hanlon_config_file(override_params)
puts "Creating server configuration file...".cyan
web_config_path = File.join($app_root, "web/config/hanlon_server.conf")
default_server_config = ProjectHanlon::Config::Server.new
server_config = add_config_overrides(default_server_config, override_params, 'server', true)
case check_file server_config, web_config_path
when -1
puts " existing server config file at #{web_config_path} left intact"
when 0
puts " a new default server configuration file created at #{web_config_path}"
when 1
puts " a new default server configuration created at #{web_config_path}"
puts " existing file backed up with timestamp"
end
puts
puts "Creating client configuration file...".cyan
cli_config_path = File.join($app_root, "cli/config/hanlon_client.conf")
default_client_config = ProjectHanlon::Config::Client.new
client_config = add_config_overrides(default_client_config, override_params, 'client')
case check_file client_config, cli_config_path
when -1
puts " existing client config file at #{cli_config_path} left intact"
when 0
puts " a new default client configuration created at #{cli_config_path}"
when 1
puts " a new default client configuration created at #{cli_config_path}"
puts " existing file backed up with timestamp"
end
puts
puts "Please edit respective configuration files as needed before starting #{ProjectHanlon::Properties.app_name} server"
end
def check_file(obj, web_config_path)
file_action = 0 # 0 -create new 1 - overwrite existing 2 - skip
if File.exist? web_config_path then
puts " existing config file found at #{web_config_path}"
begin
printf " Do you want to replace it? [y/N] "
input = STDIN.gets.strip.upcase
if input == ''
input = 'N'
end
if ! %w(Y N).include? input
puts " sorry, invalid response!!!".red
end
end while ! %w(Y N).include? input
if %w(Y).include? input then
File.rename web_config_path, web_config_path + "_" + Time.now.strftime("%m%d%Y%I%M") + ".bak"
file_action = 1
else
return file_action = -1
end
end
obj.save_as_yaml(web_config_path)
return file_action
end
def validate_hanlon_struct
#ensure log directories
FileUtils.mkdir_p("cli/log")
FileUtils.mkdir_p("web/log")
"#{ProjectHanlon::Properties.app_name} log files initialized"
end
def add_config_overrides(default_config, override_params, config_type_str, warn_skipped_params = nil)
return default_server_config unless override_params
new_config = default_config.dup
override_params.each { |key, value|
begin
new_config[key] = value
rescue NoMethodError => e
print "WARNING: no parameter name '#{key}' found in Hanlon #{config_type_str} config; skipped\n" if warn_skipped_params
end
}
new_config
end
end
end
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name} [options]".green
opts.separator "Options".yellow
options[:defaults_file] = nil
opts.on( '-d', '--defaults-file FILE', 'FILE containing default config values (YAML)'.yellow ) do |file|
options[:defaults_file] = file
end
options[:json_defaults] = nil
opts.on( '-j', '--json-defaults STRING', 'STRING containing default config values (JSON)'.yellow ) do |json_string|
options[:json_defaults] = json_string
end
opts.on_tail( '-h', '--help', 'Display this screen'.yellow ) do
print opts
exit
end
end
begin
optparse.parse!
rescue Exception => e
print "ERROR: could not parse command (#{e.message})\n"
print optparse
exit 1
end
if options[:defaults_file] && options[:json_defaults]
print "ERROR: only one of '-d,--defaults-file FILE' or '-j,--json-defaults STRING' can be used\n"
print optparse
exit 2
end
exit ProjectHanlon::Init.new.run(options)