1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | $svnbase = "svn.genapp.rocks/svn/base"; |
---|
4 | |
---|
5 | $|=1; |
---|
6 | |
---|
7 | # $debug++; |
---|
8 | |
---|
9 | my $gb = $ENV{ "GENAPP" } || die "$0: environment variable GENAPP must be set\n"; |
---|
10 | |
---|
11 | print "perl version is $]\n" if $debug; |
---|
12 | print "command is: $0 @ARGV\n" if $debug; |
---|
13 | |
---|
14 | if ( $] < 5.018 ) { |
---|
15 | if ( -e "$gb/perl/bin/perl" ) { |
---|
16 | my $pv =`$gb/perl/bin/perl -e 'print \$];'`; |
---|
17 | if ( $pv >= 5.018 ) { |
---|
18 | print "will run new version\n" if $debug; |
---|
19 | unshift @ARGV, $0; |
---|
20 | exec( "$gb/perl/bin/perl", @ARGV ); |
---|
21 | } else { |
---|
22 | die "$gb/perl/bin/perl exists, but not a correct version of perl (needs a minimum of 5.18)\n"; |
---|
23 | } |
---|
24 | } else { |
---|
25 | die "you need to install a version of perl >= 5.18 in $gb/perl\n |
---|
26 | there is a script $gb/sbin/install-perl-stable to do this"; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | require "$gb/etc/perl/genapp_util.pl"; |
---|
31 | |
---|
32 | sub runcmd { |
---|
33 | my $cmd = $_[0]; |
---|
34 | my $out; |
---|
35 | print "running shell command:\n--------\n$cmd\n--------\n" if $debug; |
---|
36 | open my $fh, "$cmd |"; |
---|
37 | while ( <$fh> ) { |
---|
38 | $out .= $_; |
---|
39 | print; |
---|
40 | } |
---|
41 | close $fh; |
---|
42 | die "$0: command $cmd failed\n" if $?; |
---|
43 | $out; |
---|
44 | } |
---|
45 | |
---|
46 | sub runcmdsb { |
---|
47 | my $cmd = $_[0]; |
---|
48 | $cmd =~ s/"/\\\"/g; |
---|
49 | $cmd = "sudo bash -c \"$cmd\""; |
---|
50 | print "sd cmd is <$cmd>\n" if $debug; |
---|
51 | runcmd( $cmd ); |
---|
52 | } |
---|
53 | |
---|
54 | # -- read directives.json |
---|
55 | |
---|
56 | my $directivesfile = "directives.json"; |
---|
57 | |
---|
58 | { |
---|
59 | my $f = $directivesfile; |
---|
60 | if ( -e $f ) { |
---|
61 | print "reading $f\n" if $debug; |
---|
62 | open my $fh, $f || die "$0: can not open $f\n"; |
---|
63 | my @ol = <$fh>; |
---|
64 | close $fh; |
---|
65 | my @l = grep !/^\s*#/ , @ol; |
---|
66 | my $l = join '', @l; |
---|
67 | eval { |
---|
68 | $dirjson = decode_json( $l ); |
---|
69 | 1; |
---|
70 | } || do { |
---|
71 | my $e = $@; |
---|
72 | |
---|
73 | # figure out line # |
---|
74 | |
---|
75 | my ( $cp ) = $e =~ /at character offset (\d+) /; |
---|
76 | my $i; |
---|
77 | my $cpos = $cp; |
---|
78 | for ( $i = 0; $i < @ol; ++$i ) { |
---|
79 | next if $ol[ $i ] =~ /^\s*#/; |
---|
80 | $cpos -= length( $ol[ $i ] ); |
---|
81 | last if $cpos < 0; |
---|
82 | } |
---|
83 | |
---|
84 | my $sline = $i - 2; |
---|
85 | my $eline = $i + 2; |
---|
86 | $sline = 0 if $sline < 0; |
---|
87 | $eline = @ol - 1 if $eline >= @ol; |
---|
88 | |
---|
89 | print "JSON Error in file $f near these lines:\n"; |
---|
90 | for ( my $j = $sline; $j <= $eline; ++$j ) { |
---|
91 | my $uj = $j + 1; |
---|
92 | print "$uj: $ol[$j]"; |
---|
93 | print "$uj: " .'^'x(length($ol[$j])) . "\n" if $j == $i; |
---|
94 | } |
---|
95 | die; |
---|
96 | }; |
---|
97 | } else { |
---|
98 | die "$0: no $f found in current directory\n"; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | die "no languages key defined in directives.json\n" if !$$dirjson{ "languages" }; |
---|
104 | |
---|
105 | { |
---|
106 | my $ishtml; |
---|
107 | foreach my $k ( @{$$dirjson{ "languages" }} ) { |
---|
108 | if ( $k eq "html5" ) { |
---|
109 | $ishtml++; |
---|
110 | } |
---|
111 | } |
---|
112 | die "an html language is not specified in directives.json languages, permission resetting not needed\n" if !$ishtml; |
---|
113 | } |
---|
114 | |
---|
115 | #--- get config.json info |
---|
116 | |
---|
117 | my $cfgjson = {}; |
---|
118 | my $cfgjsonf = "$gb/etc/config.json"; |
---|
119 | my $cfgjsonnotes = '-'x80 . "\n |
---|
120 | $cfgjsonf contains global system information. |
---|
121 | this is used to setup individual applications values. |
---|
122 | to build a default config.json file |
---|
123 | $gb/sbin/setconfig.pl -pj |
---|
124 | and verify the information is correct. |
---|
125 | Particularly, if the machine is not publically exposed, you probably want to change the hostip and hostname, as it will likely report the public ip of your firewall. |
---|
126 | The full options are listed by |
---|
127 | $gb/sbin/setconfig.pl -h |
---|
128 | You can also manually edit $cfgjsonf |
---|
129 | |
---|
130 | Once you are satisified that the setting are correct |
---|
131 | you can rerun $0 |
---|
132 | " . '-'x80 . "\n" |
---|
133 | ; |
---|
134 | |
---|
135 | { |
---|
136 | my $f = $cfgjsonf; |
---|
137 | if ( -e $f ) { |
---|
138 | print "reading $f\n" if $debug; |
---|
139 | open my $fh, $f || die "$0: can not open $f\n"; |
---|
140 | my @ol = <$fh>; |
---|
141 | close $fh; |
---|
142 | my @l = grep !/^\s*#/ , @ol; |
---|
143 | my $l = join '', @l; |
---|
144 | eval { |
---|
145 | $cfgjson = decode_json( $l ); |
---|
146 | 1; |
---|
147 | } || do { |
---|
148 | my $e = $@; |
---|
149 | |
---|
150 | # figure out line # |
---|
151 | |
---|
152 | my ( $cp ) = $e =~ /at character offset (\d+) /; |
---|
153 | my $i; |
---|
154 | my $cpos = $cp; |
---|
155 | for ( $i = 0; $i < @ol; ++$i ) { |
---|
156 | next if $ol[ $i ] =~ /^\s*#/; |
---|
157 | $cpos -= length( $ol[ $i ] ); |
---|
158 | last if $cpos < 0; |
---|
159 | } |
---|
160 | |
---|
161 | my $sline = $i - 2; |
---|
162 | my $eline = $i + 2; |
---|
163 | $sline = 0 if $sline < 0; |
---|
164 | $eline = @ol - 1 if $eline >= @ol; |
---|
165 | |
---|
166 | print "JSON Error in file $f near these lines:\n"; |
---|
167 | for ( my $j = $sline; $j <= $eline; ++$j ) { |
---|
168 | my $uj = $j + 1; |
---|
169 | print "$uj: $ol[$j]"; |
---|
170 | print "$uj: " .'^'x(length($ol[$j])) . "\n" if $j == $i; |
---|
171 | } |
---|
172 | die; |
---|
173 | }; |
---|
174 | } else { |
---|
175 | my $res = `$gb/sbin/setconfig.pl -pj`; |
---|
176 | print "$0 : |
---|
177 | " . '-'x80 . " |
---|
178 | please verify these settings are correct |
---|
179 | " . '-'x80 . " |
---|
180 | $res |
---|
181 | $cfgjsonnotes |
---|
182 | "; |
---|
183 | exit; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | # fix perms |
---|
188 | { |
---|
189 | my $dir = `pwd`; |
---|
190 | chomp $dir; |
---|
191 | |
---|
192 | # $targetdir should be directives:application |
---|
193 | |
---|
194 | die "no application defined in $dir/directives.json\n" if !$$dirjson{'application'}; |
---|
195 | my $targetdir = $$dirjson{'application'}; |
---|
196 | |
---|
197 | # check if ajax results util are not correct symlinks before removing ... do it individually |
---|
198 | if ( $$cfgjson{ 'webroot' } ne $$dirjson{'docroot'}{'html5'} ) { |
---|
199 | die "Error: the config.json webroot [$$cfgjson{'webroot'}] does not match the directives.json docroot:html5 [$$dirjson{'docroot'}{'html5'}]. This must be manually corrected before proceeding.\n"; |
---|
200 | } |
---|
201 | |
---|
202 | my $cmds = |
---|
203 | "chgrp -R genapp . && chmod -R g+rw . && find . -type d | xargs chmod g+xs |
---|
204 | "; |
---|
205 | print "Resetting permissions: |
---|
206 | $cmds"; |
---|
207 | runcmdsb( $cmds ); |
---|
208 | } |
---|