forked from OTRS/module-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstallTestsystem.pl
executable file
·325 lines (261 loc) · 10.4 KB
/
InstallTestsystem.pl
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
#!/usr/bin/perl
# --
# module-tools/InstallTestsystem.pl
#
# Copyright (C) 2001-2012 OTRS AG, http://otrs.org/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# ---
=head1 NAME
InstallTestsystem.pl - script for installing a new test system
=head1 SYNOPSIS
InstallTestsystem.pl
=head1 DESCRIPTION
Please send any questions, suggestions & complaints to <[email protected]>
=cut
use strict;
use warnings;
use Cwd;
use DBI;
use File::Find;
use Getopt::Std;
use vars qw($VERSION);
$VERSION = qw($Revision: 1.2 $) [1];
# get options
my %Opts = ();
getopt( 'p', \%Opts );
my $InstallDir = $Opts{p};
if ( !$InstallDir || !-e $InstallDir ) {
Usage("ERROR: -p must be a valid directory!");
exit 2;
}
# remove possible slash at the end
$InstallDir =~ s{ / \z }{}xms;
# Configuration
my %Config = (
# the path to your workspace directory, w/ leading and trailing slashes
'EnvironmentRoot' => '/devel/',
# the path to your module tools directory, w/ leading and trailing slashes
'ModuleToolsRoot' => '/ws/module-tools/',
# user name for mysql (should be the same that you usually use to install a local OTRS instance)
'DatabaseUserName' => 'root',
# password for your mysql user
'DatabasePassword' => '',
'PermissionsOTRSUser' => '_www', # OTRS user
'PermissionsOTRSGroup' => '_www', # OTRS group
'PermissionsWebUser' => '_www', # otrs-web user
'PermissionsWebGroup' => '_www', # otrs-web group
# the apache config of the system you're going to install will be copied to this location
'ApacheCFGDir' => '/etc/apache2/other/',
# the command to restart apache (could be different on other systems)
'ApacheRestartCommand' => 'apachectl graceful',
);
my $SystemName = $InstallDir;
$SystemName =~ s{$Config{EnvironmentRoot}}{}xmsg;
$SystemName =~ s{/}{}xmsg;
# Determine a string that is used for database user name, database name and database password
my $DatabaseSystemName = $SystemName;
$DatabaseSystemName =~ s{-}{_}xmsg; # replace - by _ (hyphens not allowed in database name)
$DatabaseSystemName = substr( $DatabaseSystemName, 0, 16 ); # shorten the string (mysql requirement)
# edit Config.pm
print STDERR "--- Editing and copying Config.pm...\n";
if ( !-e $InstallDir . '/Kernel/Config.pm.dist' ) {
print STDERR "/Kernel/Config.pm.dist cannot be opened\n";
exit 2;
}
open FILE, $InstallDir . '/Kernel/Config.pm.dist' or die "Couldn't open $!";
my $ConfigStr = join( "", <FILE> );
close FILE;
$ConfigStr =~ s{/opt/otrs}{$InstallDir}xmsg;
$ConfigStr =~ s{('otrs'|'some-pass')}{'$DatabaseSystemName'}xmsg;
# inject some more data
my $ConfigInjectStr = <<"EOD";
\$Self->{'SecureMode'} = 1;
\$Self->{'SystemID'} = '54';
\$Self->{'SessionName'} = '$SystemName';
\$Self->{'ProductName'} = '$SystemName';
\$Self->{'ScriptAlias'} = '$SystemName/';
\$Self->{'Frontend::WebPath'} = '/$SystemName-web/';
\$Self->{'CheckEmailAddresses'} = 0;
\$Self->{'CheckMXRecord'} = 0;
\$Self->{'Organization'} = '';
\$Self->{'LogModule'} = 'Kernel::System::Log::File';
\$Self->{'LogModule::LogFile'} = '$Config{EnvironmentRoot}$SystemName/otrs.log';
\$Self->{'FQDN'} = 'localhost';
\$Self->{'DefaultLanguage'} = 'de';
\$Self->{'DefaultCharset'} = 'utf-8';
\$Self->{'AdminEmail'} = 'root\@localhost';
\$Self->{'Package::Timeout'} = '120';
\$Self->{'CheckEmailAddresses'} = 0;
\$Self->{'CheckMXRecord'} = 0;
# Fred
\$Self->{'Fred::BackgroundColor'} = '#006ea5';
\$Self->{'Fred::SystemName'} = '$SystemName';
\$Self->{'Fred::ConsoleOpacity'} = '0.7';
\$Self->{'Fred::ConsoleWidth'} = '30%';
# Misc
\$Self->{'Loader::Enabled::CSS'} = 0;
\$Self->{'Loader::Enabled::JS'} = 0;
EOD
$ConfigStr =~ s{\# \s* \$Self->\{CheckMXRecord\} \s* = \s* 0;}{$ConfigInjectStr}xms;
open( MYOUTFILE, '>' . $InstallDir . '/Kernel/Config.pm' );
print MYOUTFILE $ConfigStr;
close MYOUTFILE;
# check apache config
if ( !-e $InstallDir . '/scripts/apache2-httpd.include.conf' ) {
print STDERR "/scripts/apache2-httpd.include.conf cannot be opened\n";
exit 2;
}
# copy apache config file
my $ApacheConfigFile = "$Config{ApacheCFGDir}$SystemName.conf";
system(
"sudo cp $InstallDir/scripts/apache2-httpd.include.conf $ApacheConfigFile"
);
# copy apache mod perl file
my $ApacheModPerlFile = "$Config{ApacheCFGDir}$SystemName.apache2-perl-startup.pl";
system(
"sudo cp $InstallDir/scripts/apache2-perl-startup.pl $ApacheModPerlFile"
);
print STDERR "--- Editing Apache config...\n";
open FILE, $ApacheConfigFile or die "Couldn't open $!";
my $ApacheConfigStr = join( "", <FILE> );
close FILE;
$ApacheConfigStr =~ s{/opt/otrs}{$InstallDir}xmsg;
$ApacheConfigStr =~ s{/otrs/}{/$SystemName/}xmsg;
$ApacheConfigStr =~ s{/otrs-web/}{/$SystemName-web/}xmsg;
$ApacheConfigStr =~ s{<IfModule \s* mod_perl.c>}{<IfModule mod_perlOFF.c>}xmsg;
$ApacheConfigStr =~ s{Perlrequire \s+ /opt/otrs/scripts/apache2-perl-startup\.pl}{Perlrequire $ApacheModPerlFile}xms;
$ApacheConfigStr =~ s{<Location \s+ /otrs>}{<Location /$SystemName>}xms;
open( MYOUTFILE, '>' . $ApacheConfigFile );
print MYOUTFILE $ApacheConfigStr;
close MYOUTFILE;
print STDERR "--- Editing Apache mod perl config...\n";
open FILE, $ApacheModPerlFile or die "Couldn't open $!";
my $ApacheModPerlConfigStr = join( "", <FILE> );
close FILE;
# set correct path
$ApacheModPerlConfigStr =~ s{/opt/otrs}{$InstallDir}xmsg;
# enable lines for MySQL
$ApacheModPerlConfigStr =~ s{^#(use DBD::mysql \(\);)$}{$1}msg;
$ApacheModPerlConfigStr =~ s{^#(use Kernel::System::DB::mysql;)$}{$1}msg;
open( MYOUTFILE, '>' . $ApacheModPerlFile );
print MYOUTFILE $ApacheModPerlConfigStr;
close MYOUTFILE;
# restart apache
print STDERR "--- Restarting apache...\n";
system("sudo $Config{ApacheRestartCommand}");
# install database
print STDERR "--- Creating tables and inserting data...\n";
my $DSN = 'DBI:mysql:';
my $DBH = DBI->connect(
$DSN,
$Config{DatabaseUserName},
$Config{DatabasePassword},
);
$DBH->do("CREATE DATABASE $DatabaseSystemName charset utf8");
$DBH->do("use $DatabaseSystemName");
my @SQL = ParseSQLFile( $InstallDir . "/scripts/database/otrs-schema.mysql.sql" );
for (@SQL) {
$DBH->do($_);
}
@SQL = ParseSQLFile( $InstallDir . "/scripts/database/otrs-initial_insert.mysql.sql" );
for (@SQL) {
$DBH->do($_);
}
@SQL = ParseSQLFile( $InstallDir . "/scripts/database/otrs-schema-post.mysql.sql" );
for (@SQL) {
$DBH->do($_);
}
print STDERR "--- Creating database user and privileges...\n";
$DBH->do(
"GRANT ALL PRIVILEGES ON $DatabaseSystemName.* TO $DatabaseSystemName\@localhost IDENTIFIED BY '$DatabaseSystemName' WITH GRANT OPTION;"
);
$DBH->do('FLUSH PRIVILEGES');
# create logfile
print STDERR "--- Creating logfile...\n";
system("sudo touch $InstallDir/otrs.log");
# make sure we've got the correct rights set (e.g. in case you've downloaded the files as root)
system("sudo chown -R $Config{PermissionsOTRSUser}:$Config{PermissionsOTRSGroup} $InstallDir");
# link Fred
print STDERR "--- Linking Fred...\n";
print STDERR "############################################\n";
system(
"$Config{ModuleToolsRoot}/module-linker.pl install $Config{EnvironmentRoot}Fred $InstallDir"
);
print STDERR "############################################\n";
# Rebuild Config
print STDERR "--- Rebuilding config...\n";
print STDERR "############################################\n";
system("sudo perl $InstallDir/bin/otrs.RebuildConfig.pl");
print STDERR "############################################\n";
# setting permissions
print STDERR "--- Setting permissions...\n";
print STDERR "############################################\n";
system(
"sudo perl $InstallDir/bin/otrs.SetPermissions.pl --otrs-user=$Config{PermissionsOTRSUser} --web-user=$Config{PermissionsWebUser} --otrs-group=$Config{PermissionsOTRSGroup} --web-group=$Config{PermissionsWebGroup} --not-root $InstallDir"
);
print STDERR "############################################\n";
# inject test data
print STDERR "--- Injecting some test data...\n";
system("cp $Config{ModuleToolsRoot}FillTestsystem.pl $InstallDir/bin/FillTestsystem.pl");
print STDERR "############################################\n";
system("perl $InstallDir/bin/FillTestsystem.pl");
print STDERR "############################################\n";
system("rm $InstallDir/bin/FillTestsystem.pl");
# setting permissions
print STDERR "--- Setting permissions again (just to be sure)...\n";
print STDERR "############################################\n";
system(
"sudo perl $InstallDir/bin/otrs.SetPermissions.pl --otrs-user=$Config{PermissionsOTRSUser} --web-user=$Config{PermissionsWebUser} --otrs-group=$Config{PermissionsOTRSGroup} --web-group=$Config{PermissionsWebGroup} --not-root $InstallDir"
);
print STDERR "############################################\n";
print STDERR "Finished.\n";
sub ParseSQLFile {
my $File = shift;
my @SQL;
if ( open( my $In, '<', $File ) ) {
my $SQLEnd = 0;
my $SQLSingel = '';
while (<$In>) {
if ( $_ !~ /^(#|--)/ ) {
if ( $_ =~ /^(.*)(;|;\s)$/ || $_ =~ /^(\));/ ) {
$SQLSingel .= $1;
$SQLEnd = 1;
}
else {
$SQLSingel .= $_;
}
}
if ($SQLEnd) {
push @SQL, $SQLSingel;
$SQLEnd = 0;
$SQLSingel = '';
}
}
close $In;
}
return @SQL;
}
sub Usage {
my ($Message) = @_;
print STDERR <<"HELPSTR";
$Message
USAGE:
$0 -p /ws/otrs32-devel
HELPSTR
return;
}
1;