1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | my $gb = $ENV{ "GENAPP" }; |
---|
4 | if ( !$gb ) { |
---|
5 | use Cwd 'abs_path'; |
---|
6 | $gb = abs_path($0); |
---|
7 | $gb =~ s/\/sbin\/setconfig\.pl$//; |
---|
8 | } |
---|
9 | |
---|
10 | if ( $] < 5.018 ) { |
---|
11 | if ( -e "$gb/perl/bin/perl" ) { |
---|
12 | $pv =`$gb/perl/bin/perl -e 'print \$];'`; |
---|
13 | if ( $pv >= 5.018 ) { |
---|
14 | print "will run new version\n" if $debug; |
---|
15 | unshift @ARGV, $0; |
---|
16 | exec( "$gb/perl/bin/perl", @ARGV ); |
---|
17 | } else { |
---|
18 | die "$gb/perl/bin/perl exists, but not a correct version of perl (needs a minimum of 5.18)\n"; |
---|
19 | } |
---|
20 | } else { |
---|
21 | die "you need to install a version of perl >= 5.18 in $gb/perl\n |
---|
22 | there is a script $gb/sbin/install-perl-stable to do this"; |
---|
23 | } |
---|
24 | } |
---|
25 | |
---|
26 | my $rc = eval { |
---|
27 | require JSON; JSON->import( -support_by_pp ); |
---|
28 | }; |
---|
29 | |
---|
30 | my $notes = " |
---|
31 | usage: $0 |
---|
32 | |
---|
33 | sets up $GENAPP/config.json |
---|
34 | |
---|
35 | options |
---|
36 | -f force overwrite of identified values to those previously stored |
---|
37 | -h show help |
---|
38 | -hostip hostip use specified host ip address |
---|
39 | -hostname hostname use specified host name |
---|
40 | -if network-interface-id find and set hostip from that of specified interface (e.g. eth0, eth1, lo, etc) |
---|
41 | -pj print resulting config.json |
---|
42 | -https use https and wss |
---|
43 | -webroot webroot specifiy the webroot directory |
---|
44 | "; |
---|
45 | |
---|
46 | my $pj; |
---|
47 | my $fo; |
---|
48 | my $hostip; |
---|
49 | my $hostname; |
---|
50 | my $https = 0; |
---|
51 | my $webroot; |
---|
52 | |
---|
53 | while ( @ARGV ) { |
---|
54 | my $option = shift @ARGV; |
---|
55 | if ( $option =~ /^-f$/ ) { |
---|
56 | $fo = 1; |
---|
57 | next; |
---|
58 | } |
---|
59 | if ( $option =~ /^-if$/ ) { |
---|
60 | die "$0: option $option requries an argument\n" . $notes if !@ARGV; |
---|
61 | my $if = shift @ARGV; |
---|
62 | my $cmd = "/sbin/ip -4 addr show $if | grep inet | awk '{ print \$2 }' | sed 's/\\\/.*\$//'"; |
---|
63 | $hostip = `$cmd`; |
---|
64 | chomp $hostip; |
---|
65 | die "$0: could not get hostip of interface $if\n" if !$hostip; |
---|
66 | next; |
---|
67 | } |
---|
68 | if ( $option =~ /^-hostip$/ ) { |
---|
69 | die "$0: option $option requries an argument\n" . $notes if !@ARGV; |
---|
70 | $hostip = shift @ARGV;; |
---|
71 | next; |
---|
72 | } |
---|
73 | if ( $option =~ /^-hostname$/ ) { |
---|
74 | die "$0: option $option requries an argument\n" . $notes if !@ARGV; |
---|
75 | $hostname = shift @ARGV;; |
---|
76 | next; |
---|
77 | } |
---|
78 | if ( $option =~ /^-webroot$/ ) { |
---|
79 | die "$0: option $option requries an argument\n" . $notes if !@ARGV; |
---|
80 | $webroot = shift @ARGV;; |
---|
81 | next; |
---|
82 | } |
---|
83 | if ( $option =~ /^-h$/ ) { |
---|
84 | print $notes; |
---|
85 | exit; |
---|
86 | } |
---|
87 | if ( $option =~ /^-pj$/ ) { |
---|
88 | $pj++; |
---|
89 | next; |
---|
90 | } |
---|
91 | if ( $option =~ /^-https$/ ) { |
---|
92 | $https++; |
---|
93 | next; |
---|
94 | } |
---|
95 | |
---|
96 | die "Unknown command line option specified '$option'\n" . $notes; |
---|
97 | } |
---|
98 | |
---|
99 | my $f = "$gb/etc/config.json"; |
---|
100 | |
---|
101 | my $json = {}; |
---|
102 | |
---|
103 | if ( -e $f ) { |
---|
104 | print "reading $f\n"; |
---|
105 | open my $fh, $f || die "$0: can not open $f\n"; |
---|
106 | my @ol = <$fh>; |
---|
107 | close $fh; |
---|
108 | my @l = grep !/^\s*#/ , @ol; |
---|
109 | my $l = join '', @l; |
---|
110 | eval { |
---|
111 | $json = decode_json( $l ); |
---|
112 | 1; |
---|
113 | } || do { |
---|
114 | my $e = $@; |
---|
115 | |
---|
116 | # figure out line # |
---|
117 | |
---|
118 | my ( $cp ) = $e =~ /at character offset (\d+) /; |
---|
119 | my $i; |
---|
120 | my $cpos = $cp; |
---|
121 | for ( $i = 0; $i < @ol; ++$i ) { |
---|
122 | next if $ol[ $i ] =~ /^\s*#/; |
---|
123 | $cpos -= length( $ol[ $i ] ); |
---|
124 | last if $cpos < 0; |
---|
125 | } |
---|
126 | |
---|
127 | my $sline = $i - 2; |
---|
128 | my $eline = $i + 2; |
---|
129 | $sline = 0 if $sline < 0; |
---|
130 | $eline = @ol - 1 if $eline >= @ol; |
---|
131 | |
---|
132 | print "JSON Error in file $f near these lines:\n"; |
---|
133 | for ( my $j = $sline; $j <= $eline; ++$j ) { |
---|
134 | my $uj = $j + 1; |
---|
135 | print "$uj: $ol[$j]"; |
---|
136 | print "$uj: " .'^'x(length($ol[$j])) . "\n" if $j == $i; |
---|
137 | } |
---|
138 | die; |
---|
139 | }; |
---|
140 | } |
---|
141 | |
---|
142 | # determine os |
---|
143 | |
---|
144 | my $tos = `uname -v`; |
---|
145 | my $os; |
---|
146 | my $os_release; |
---|
147 | |
---|
148 | $os = 'ubuntu' if $tos =~ /Ubuntu/; |
---|
149 | |
---|
150 | if ( $os eq 'ubuntu' ) { |
---|
151 | $os_release = `lsb_release -r | awk '{ print \$2 }'`; |
---|
152 | chomp $os_release; |
---|
153 | } |
---|
154 | |
---|
155 | if ( !$os && -e "/etc/redhat-release" ) { |
---|
156 | # check for centos/redhat |
---|
157 | my $check = `cat /etc/redhat-release`; |
---|
158 | chomp $check; |
---|
159 | if ( $check =~ /^CentOS release/ ) { |
---|
160 | $os = "centos"; |
---|
161 | ( $os_release ) = $check =~ /^CentOS release (\S+)/; |
---|
162 | |
---|
163 | } |
---|
164 | if ( $check =~ /^Red Hat Enterprise Linux Server release/ ) { |
---|
165 | $os = "redhat"; |
---|
166 | ( $os_release ) = $check =~ /^Red Hat Enterprise Linux Server release (\S+)/; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | if ( !$os && -e "/etc/slackware-version" ) { |
---|
171 | my $check = `cat /etc/slackware-version`; |
---|
172 | $os = "slackware"; |
---|
173 | ( $os_release ) = $check =~ /^Slackware (\S+)/; |
---|
174 | } |
---|
175 | |
---|
176 | if ( !os ) { |
---|
177 | warn "$0: operating system not recognized\n"; |
---|
178 | } |
---|
179 | |
---|
180 | print "Operating system is identified as '$os' release '$os_release'\n"; |
---|
181 | |
---|
182 | my $notice; |
---|
183 | |
---|
184 | if ( $$json{'os'} ) { |
---|
185 | if ( $$json{'os'} ne $os ) { |
---|
186 | $notice .= "The identified operating system [$os] is different than that stored [$$json{'os'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
187 | } else { |
---|
188 | if ( $$json{'os_release'} ne $os_release ) { |
---|
189 | $notice .= "The identified $os release [$os_release] is different than that stored [$$json{'os_release'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
190 | } |
---|
191 | } |
---|
192 | if ( $fo ) { |
---|
193 | $$json{ 'os' } = $os; |
---|
194 | $$json{ 'os_release' } = $os_release; |
---|
195 | } |
---|
196 | } else { |
---|
197 | $$json{ 'os' } = $os; |
---|
198 | $$json{ 'os_release' } = $os_release; |
---|
199 | } |
---|
200 | |
---|
201 | if ( !$hostip ) { |
---|
202 | my $foundwgetorcurl; |
---|
203 | if ( `which wget 2> /dev/null` ) { |
---|
204 | $hostip = `wget http://ipinfo.io/ip -qO -`; |
---|
205 | chomp $hostip; |
---|
206 | $foundwgetorcurl++; |
---|
207 | } else { |
---|
208 | if ( `which curl 2> /dev/null` ) { |
---|
209 | $cmd = 'curl -q ipinfo.io 2> /dev/null | grep \'"ip":\' | awk \'{ print $2 }\' | sed s/,// | sed s/\"//g'; |
---|
210 | $hostip = `$cmd`; |
---|
211 | chomp $hostip; |
---|
212 | $foundwgetorcurl++; |
---|
213 | } |
---|
214 | } |
---|
215 | die "$0: please install wget or curl so that your public ip address can be determined" if !$foundwgetorcurl; |
---|
216 | } |
---|
217 | |
---|
218 | if ( $$json{'hostip'} ) { |
---|
219 | if ( $$json{'hostip'} ne $hostip ) { |
---|
220 | $notice .= "The identified hostip for this server [$hostip] is different than that stored [$$json{'hostip'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
221 | } |
---|
222 | if ( $fo ) { |
---|
223 | $$json{ 'hostip' } = $hostip; |
---|
224 | } |
---|
225 | } else { |
---|
226 | $$json{ 'hostip' } = $hostip; |
---|
227 | } |
---|
228 | |
---|
229 | $hostname = `host $hostip` if !$hostname; |
---|
230 | chomp $hostname; |
---|
231 | if ( $hostname =~ /(not found|no PTR record)/ ) { |
---|
232 | $hostname = $hostip; |
---|
233 | } else { |
---|
234 | ( $hostname ) = $hostname =~ /\s+(\S+)\.$/; |
---|
235 | } |
---|
236 | |
---|
237 | if ( $$json{'hostname'} ) { |
---|
238 | if ( $$json{'hostname'} ne $hostname ) { |
---|
239 | $notice .= "The identified hostname for this server [$hostname] is different than that stored [$$json{'hostname'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
240 | } |
---|
241 | if ( $fo ) { |
---|
242 | $$json{ 'hostname' } = $hostname; |
---|
243 | } |
---|
244 | } else { |
---|
245 | $$json{ 'hostname' } = $hostname; |
---|
246 | } |
---|
247 | |
---|
248 | my $wssport = $https ? 443 : 80; |
---|
249 | my $wsport = 37777; |
---|
250 | my $zmqport = 37778; |
---|
251 | my $zmqhostip = "127.0.0.1"; |
---|
252 | my $udpport = 37779; |
---|
253 | my $udphostip = "127.0.0.1"; |
---|
254 | my $tcpport = 37780; |
---|
255 | my $tcphostip = "127.0.0.1"; |
---|
256 | |
---|
257 | if ( $$json{ "messaging" }{ "wssport" } ) { |
---|
258 | if ( $$json{'messaging'}{'wssport'} != $wssport ) { |
---|
259 | $notice .= "The identified wssport for this server [$wssport] is different than that stored [$$json{'messaging'}{'wssport'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
260 | } |
---|
261 | if ( $fo ) { |
---|
262 | $$json{ 'messaging'}{'wssport' } = $wssport; |
---|
263 | } |
---|
264 | } else { |
---|
265 | $$json{ 'messaging'}{'wssport' } = $wssport; |
---|
266 | } |
---|
267 | |
---|
268 | if ( $$json{ "messaging" }{ "wsport" } ) { |
---|
269 | if ( $$json{'messaging'}{'wsport'} != $wsport ) { |
---|
270 | $notice .= "The identified wsport for this server [$wsport] is different than that stored [$$json{'messaging'}{'wsport'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
271 | } |
---|
272 | if ( $fo ) { |
---|
273 | $$json{ 'messaging'}{'wsport' } = $wsport; |
---|
274 | } |
---|
275 | } else { |
---|
276 | $$json{ 'messaging'}{'wsport' } = $wsport; |
---|
277 | } |
---|
278 | |
---|
279 | if ( $$json{ "messaging" }{ "zmqport" } ) { |
---|
280 | if ( $$json{'messaging'}{'zmqport'} != $zmqport ) { |
---|
281 | $notice .= "The identified zmqport for this server [$zmqport] is different than that stored [$$json{'messaging'}{'zmqport'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
282 | } |
---|
283 | if ( $fo ) { |
---|
284 | $$json{ 'messaging'}{'zmqport' } = $zmqport; |
---|
285 | } |
---|
286 | } else { |
---|
287 | $$json{ 'messaging'}{'zmqport' } = $zmqport; |
---|
288 | } |
---|
289 | |
---|
290 | if ( $$json{ "messaging" }{ "zmqhostip" } ) { |
---|
291 | if ( $$json{'messaging'}{'zmqhostip'} ne $zmqhostip ) { |
---|
292 | $notice .= "The identified zmqhostip for this server [$zmqhostip] is different than that stored [$$json{'messaging'}{'zmqhostip'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
293 | } |
---|
294 | if ( $fo ) { |
---|
295 | $$json{ 'messaging'}{'zmqhostip' } = $zmqhostip; |
---|
296 | } |
---|
297 | } else { |
---|
298 | $$json{ 'messaging'}{'zmqhostip' } = $zmqhostip; |
---|
299 | } |
---|
300 | |
---|
301 | if ( $$json{ "messaging" }{ "udpport" } ) { |
---|
302 | if ( $$json{'messaging'}{'udpport'} != $udpport ) { |
---|
303 | $notice .= "The identified udpport for this server [$udpport] is different than that stored [$$json{'messaging'}{'udpport'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
304 | } |
---|
305 | if ( $fo ) { |
---|
306 | $$json{ 'messaging'}{'udpport' } = $udpport; |
---|
307 | } |
---|
308 | } else { |
---|
309 | $$json{ 'messaging'}{'udpport' } = $udpport; |
---|
310 | } |
---|
311 | |
---|
312 | if ( $$json{ "messaging" }{ "udphostip" } ) { |
---|
313 | if ( $$json{'messaging'}{'udphostip'} ne $udphostip ) { |
---|
314 | $notice .= "The identified udphostip for this server [$udphostip] is different than that stored [$$json{'messaging'}{'udphostip'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
315 | } |
---|
316 | if ( $fo ) { |
---|
317 | $$json{ 'messaging'}{'udphostip' } = $udphostip; |
---|
318 | } |
---|
319 | } else { |
---|
320 | $$json{ 'messaging'}{'udphostip' } = $udphostip; |
---|
321 | } |
---|
322 | |
---|
323 | if ( $$json{ "messaging" }{ "tcpport" } ) { |
---|
324 | if ( $$json{'messaging'}{'tcpport'} != $tcpport ) { |
---|
325 | $notice .= "The identified tcpport for this server [$tcpport] is different than that stored [$$json{'messaging'}{'tcpport'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
326 | } |
---|
327 | if ( $fo ) { |
---|
328 | $$json{ 'messaging'}{'tcpport' } = $tcpport; |
---|
329 | } |
---|
330 | } else { |
---|
331 | $$json{ 'messaging'}{'tcpport' } = $tcpport; |
---|
332 | } |
---|
333 | |
---|
334 | if ( $$json{ "messaging" }{ "tcphostip" } ) { |
---|
335 | if ( $$json{'messaging'}{'tcphostip'} ne $tcphostip ) { |
---|
336 | $notice .= "The identified tcphostip for this server [$tcphostip] is different than that stored [$$json{'messaging'}{'tcphostip'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
337 | } |
---|
338 | if ( $fo ) { |
---|
339 | $$json{ 'messaging'}{'tcphostip' } = $tcphostip; |
---|
340 | } |
---|
341 | } else { |
---|
342 | $$json{ 'messaging'}{'tcphostip' } = $tcphostip; |
---|
343 | } |
---|
344 | |
---|
345 | if ( $$json{ "https" } ) { |
---|
346 | if ( $$json{'https'} != $https ) { |
---|
347 | $notice .= "The identified https flag for this server [$https] is different than that stored [$$json{'https'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
348 | } |
---|
349 | if ( $fo ) { |
---|
350 | $$json{'https'} = int($https); |
---|
351 | } |
---|
352 | } else { |
---|
353 | $$json{'https'} = int($https); |
---|
354 | } |
---|
355 | |
---|
356 | if ( !$webroot ) { |
---|
357 | $webroot = "/var/www/html" if $os =~ /^(ubuntu|centos|redhat)$/; |
---|
358 | $webroot = "/var/www/htdocs" if $os =~ /^slackware$/; |
---|
359 | } |
---|
360 | |
---|
361 | if ( $$json{ "webroot" } ) { |
---|
362 | if ( $$json{'webroot'} != $webroot ) { |
---|
363 | $notice .= "The identified webroot for this server [$webroot] is different than that stored [$$json{'webroot'}] in $f. " . ( $fo ? "Overwriting with identified value." : "Leaving stored value untouched (use -f to overwrite)." ) . "\n"; |
---|
364 | } |
---|
365 | if ( $fo ) { |
---|
366 | $$json{'webroot'} = $webroot; |
---|
367 | } |
---|
368 | } else { |
---|
369 | $$json{'webroot'} = $webroot; |
---|
370 | } |
---|
371 | |
---|
372 | open my $fh, ">$f" || die "$0: can not open $f for writing, check permissions\n"; |
---|
373 | print $fh to_json( $json, { utf8 => 1, pretty => 1 } ); |
---|
374 | close $fh; |
---|
375 | |
---|
376 | if ( $pj ) { |
---|
377 | print "-"x80 . "\n"; |
---|
378 | print "Final config.json:\n"; |
---|
379 | print "-"x80 . "\n"; |
---|
380 | print to_json( $json, { utf8 => 1, pretty => 1 } ); |
---|
381 | print "-"x80 . "\n"; |
---|
382 | } |
---|
383 | |
---|
384 | print "-"x80 . "\n" . "Notices:\n" . "-"x80 . "\n" . $notice . "-"x80 . "\n" if $notice; |
---|