This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_dlb.php
492 lines (414 loc) · 20.5 KB
/
build_dlb.php
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
$projectDir = dirname(__FILE__);
require_once "$projectDir/../ginco/lib/share.php";
// ------------------------------------------------------------------------------
// Synopsis: build ogam services for GINCO DLB instance defined in config file.
// ------------------------------------------------------------------------------
function usage($mess = NULL) {
echo "------------------------------------------------------------------------\n";
echo "\nBuild dlb services and website\n";
echo "> php build_dlb.php -f configFile [{-D<propertiesName>=<Value>}] [--mode=<dev|prod>] [--task1 --task2...]\n\n";
echo "o configFile: a java style properties file for the instance on which you work\n";
echo "o -D : inline options to complete or override the config file.\n";
echo "o --mode : accepted values are prod and dev. 'prod' will create a build/ directory and put all builded services in it.
'dev' will put builded services in the project, and output post-installation instructions.\n";
echo "o The available tasks are:\n";
echo " o --java : build java integration and report services\n";
echo " o --website : build the 'server' part of the project (symfony)\n";
echo " o --ext : build the 'client' part of the project (ext)\n";
echo " o --mapfile : build the mapfile for mapserver\n";
echo " o --apacheconf : build the apache configuration file\n";
echo " o --supervisorconf : build the supervisor configuration file\n\n";
echo "------------------------------------------------------------------------\n";
if (!is_null($mess)) {
echo ("$mess\n\n");
exit(1);
}
exit();
}
// Build des services
// ======================
// on les range dans ./build/services
function buildJavaServices($config, $buildMode) {
global $projectDir, $buildDir, $postBuildInstructions;
chdir($projectDir);
echo ("Building java services...\n");
echo ("-------------------------\n");
$gincoDir = realpath($config['ginco.path']);
if ($buildMode == 'dev') {
$servicesBuildDir = "$buildDir/services/build";
} else {
$servicesBuildDir = "$buildDir/services";
}
is_dir($servicesBuildDir) || mkdir($servicesBuildDir, 0755, true);
is_dir("$servicesBuildDir/webapps") || mkdir("$servicesBuildDir/webapps", 0755, true);
is_dir("$servicesBuildDir/conf") || mkdir("$servicesBuildDir/conf", 0755, true);
$ISFilename = "SINP" . $config['instance.name'] . "IntegrationService";
$dlbCustomPath = "$projectDir/services/service_integration/custom/fr/ifn/ogam/integration";
$ogamCustomPath = "$gincoDir/service_integration/custom/fr/ifn/ogam/integration";
$dlbCustomServicesNames = array(
'JddDlbService'
);
// build du service d'intégration
echo ("Building integration service...\n");
system("mv -f $gincoDir/service_integration/config/log4j.properties $gincoDir/service_integration/config/log4j.properties.save");
substituteInFile("$gincoDir/services_configs/service_integration/log4j_tpl.properties", "$gincoDir/service_integration/config/log4j.properties", $config);
// copie des services Java custom
foreach ($dlbCustomServicesNames as $serviceName) {
copy("$dlbCustomPath/business/$serviceName.java", "$ogamCustomPath/business/$serviceName.java");
}
chdir("$gincoDir");
system("./gradlew service_integration:war --daemon");
// le war se trouve dans ${$gincoDir}/service_integration/build/libs/service_integration-4.0.0.war
// Cleaning Ogam dir
system("mv -f $gincoDir/service_integration/config/log4j.properties.save $gincoDir/service_integration/config/log4j.properties ");
foreach ($dlbCustomServicesNames as $serviceName) {
system("rm -f $ogamCustomPath/business/$serviceName.java");
}
copy("$gincoDir/service_integration/build/libs/service_integration-4.0.0.war", "$servicesBuildDir/webapps/$ISFilename.war");
substituteInFile("$gincoDir/services_configs/service_integration/IntegrationService_tpl.xml", "$servicesBuildDir/conf/$ISFilename.xml", $config);
// Post installation command
if ($buildMode == 'dev') {
$postBuildInstructions[] = "Java services war files have been built: $servicesBuildDir/webapps/$ISFilename.war and $servicesBuildDir/webapps/$RGFilename.war\n";
$postBuildInstructions[] = "To install, do:\n\n";
$postBuildInstructions[] = "sudo service tomcat8 stop\n";
$postBuildInstructions[] = "sudo rm -rf /var/lib/tomcat8/webapps/$ISFilename\n";
$postBuildInstructions[] = "sudo cp -f $servicesBuildDir/webapps/$ISFilename.war /var/lib/tomcat8/webapps/\n";
$postBuildInstructions[] = "sudo cp -f $servicesBuildDir/conf/$ISFilename.xml /etc/tomcat8/Catalina/localhost/\n";
$postBuildInstructions[] = "sudo service tomcat8 start\n\n";
}
echo ("Done building java services.\n\n");
}
// on range tout dans ./build/website
function buildWebsite($config, $buildMode) {
global $projectDir, $buildDir, $postBuildInstructions;
chdir($projectDir);
echo ("building server: Ginco, Dlb, Configurator (symfony parts)...\n");
echo ("-------------------------------------------------------------\n");
$serverDirGinco = realpath($config['ginco.path'] . "/website/server");
$buildServerDir = $buildDir . "/website/server";
is_dir($buildServerDir) || mkdir($buildServerDir, 0755, true);
// Copy website files if in prod mode
if ($buildMode == 'prod') {
echo ("Copying dlb server directory project to $buildServerDir...\n");
system("cp -r $projectDir/website/server/* $buildServerDir/");
}
// Copy or symlink GincoBundle
if ($buildMode == 'prod') {
echo ("Copying GincoBundle to $buildServerDir/src/Ign/Bundle/...\n");
system("rm -rf $buildServerDir/src/Ign/Bundle/GincoBundle");
system("rm -rf $buildServerDir/src/Ign/Bundle/*ConfigurateurBundle");
system("cp -r $serverDirGinco/src/Ign/Bundle/GincoBundle $buildServerDir/src/Ign/Bundle/");
system("cp -r $serverDirGinco/src/Ign/Bundle/*ConfigurateurBundle $buildServerDir/src/Ign/Bundle/");
} else {
echo ("Creating a symlink to GincoBundle in $buildServerDir/src/Ign/Bundle/...\n");
system("rm -rf $buildServerDir/src/Ign/Bundle/GincoBundle");
system("rm -rf $buildServerDir/src/Ign/Bundle/*ConfigurateurBundle");
system("ln -s $serverDirGinco/src/Ign/Bundle/GincoBundle $buildServerDir/src/Ign/Bundle/GincoBundle");
system("ln -s $serverDirGinco/src/Ign/Bundle/OGAMConfigurateurBundle $buildServerDir/src/Ign/Bundle/OGAMConfigurateurBundle");
system("ln -s $serverDirGinco/src/Ign/Bundle/GincoConfigurateurBundle $buildServerDir/src/Ign/Bundle/GincoConfigurateurBundle");
}
// ajout de la version du build dans le template du site
if ($buildMode == 'prod') {
chdir($projectDir);
$currentBranch = system("git rev-parse --abbrev-ref HEAD");
$versionInfo = $currentBranch . ' ' . date('d/m/o G:i:s');
echo ("Adding version ($versionInfo) in site template...\n");
$layoutFile = "$buildServerDir/app/Resources/views/base.html.twig";
substituteInFile($layoutFile, $layoutFile, [
'build.version' => $versionInfo
]);
// Piwik
$piwikTrackingFile = "$buildServerDir/app/Resources/views/piwik.html.twig";
substituteInFile($piwikTrackingFile, $piwikTrackingFile, $config);
}
echo ("Filling parameters.yml with configuration parameters...\n");
substituteInFile("$buildServerDir/app/config/parameters.yml.dist", "$buildServerDir/app/config/parameters.yml", [
'host' => $config['db.host'],
'port' => $config['db.port'],
'db' => $config['db.name'],
'user' => $config['db.user'],
'pw' => $config['db.user.pw'],
'admin_user' => $config['db.adminuser'],
'admin_pw' => $config['db.adminuser.pw'],
'db_version' => $config['db.version'],
'mailer_transport' => $config['mailer.transport'],
'mailer_host' => $config['mailer.host'],
'instance_name' => $config['instance.name'],
'url_basepath' => $config['url.basepath'],
'basepath' => (empty($config['url.basepath'])) ? '' : $config['url.basepath']
], '__');
chdir("$buildServerDir");
echo "Installing project vendors...\n";
system("composer install --no-scripts");
// Installing assets and clear cache:
// --> Ok in dev mode
// --> Not done in prod mode because bin/console assets:install need a connection
// to the database, which is not accessible from local ign or jenkins.
// --> done by the installer in switch_version.sh, on the target server.
if ($buildMode == 'dev') {
echo ("Installing assets...\n");
system("php bin/console assets:install --symlink");
}
// Directories used in application:
// Production mode: they are created once, outside the project, and the installer create symlinks,
// execpt the website/logs directory
// Development mode: we show post-installation instructions
if ($buildMode == 'prod') {
$buildLogsDir = $buildDir . "/website/logs";
is_dir($buildLogsDir) || mkdir($buildLogsDir, 0755, true);
} else if ($buildMode == 'dev') {
$postBuildInstructions[] = "Create the following directories, where you want (best outside the project):\n";
$postBuildInstructions[] = "* tmp: directory where data will be uploaded\n";
$postBuildInstructions[] = "* upload: directory where data will be uploaded\n";
$postBuildInstructions[] = "* dee: directory where dee files and archives will be stored\n";
$postBuildInstructions[] = "* export: directory where export files will be stored\n";
$postBuildInstructions[] = "Then complete with the absolute paths the values of the following parameters in table website.application_parameters:\n";
$postBuildInstructions[] = "uploadDir (tmp), UploadDirectory (upload), deePublicDirectory and deePrivateDirectory (???), exportPuplicDirectory (export)\n\n";
$postBuildInstructions[] = "Set permissions on application directories, execute:\n";
$postBuildInstructions[] = " cd $buildServerDir\n";
$postBuildInstructions[] = " sudo setfacl -R -m u:www-data:rwX -m u:`whoami`:rwX var/cache var/logs var/sessions\n";
$postBuildInstructions[] = " sudo setfacl -dR -m u:www-data:rwX -m u:`whoami`:rwX var/cache var/logs var/sessions\n\n";
}
echo ("Done building server (symfony).\n\n");
}
// partie extjs
function buildExtJS($config, $buildMode) {
global $projectDir, $buildDir;
chdir($projectDir);
echo ("building client (extJs)...\n");
echo ("--------------------------\n");
$clientDir = "$projectDir/website/client";
$clientDirGinco = realpath($config['ginco.path'] . "/website/client");
$buildClientDir = $buildDir . "/website/client";
is_dir($buildClientDir) || mkdir($buildClientDir, 0755, true);
// Copy ext and ogam code to project
echo ("Copying ext and ogam code from ogam project...\n");
system("cp -r $clientDirGinco/ext $clientDir");
system("cp -r $clientDirGinco/packages $clientDir");
system("cp -r $clientDirGinco/ogamDesktop $clientDir");
system("cp -r $clientDirGinco/.sencha $clientDir");
system("cp $clientDirGinco/workspace.json $clientDir");
// Customize app.json and index.html
echo ("Customize app.json...\n");
substituteInFile("$clientDir/ogamDesktop/app_tpl.json", "$clientDir/ogamDesktop/app.json", $config);
echo ("Customize index.html...\n");
substituteInFile("$clientDir/ogamDesktop/index_tpl.html", "$clientDir/ogamDesktop/index.html", $config);
// Build with sencha command
echo ("Upgrade sencha command...\n");
chdir("$clientDir/ogamDesktop/");
system("sencha app upgrade");
if ($buildMode == 'dev') {
echo ("Build with sencha command in dev environment...\n\n");
system("sencha app build development");
}
// Always build (also) in prod environment
echo ("Build with sencha command in prod environment...\n\n");
system("sencha app build");
// Clean up
if ($buildMode == 'dev') {
echo ("Cleaning up...\n");
// Delete code : all but gincoDesktop
chdir($clientDir);
system("rm -rf .sencha ext ogamDesktop packages workspace.json");
} // Prod mode: mv build directory to $buildDir
else {
echo ("Moving build files to $buildClientDir...\n");
system("mv $clientDir/build $buildClientDir/");
}
echo ("Done building client (extJs).\n\n");
}
// Customize Mapfile
function buildMapfile($config, $buildMode) {
global $projectDir, $buildDir;
chdir($projectDir);
echo ("building mapfile...\n");
echo ("-------------------\n");
$buildMapserverDir = $buildDir . "/mapserver";
$gincoMapserverDir = realpath($config['ginco.path'] . "/mapserver");
// Copy data Ginco data folder into dlb
system("cp -r $gincoMapserverDir/data $projectDir/mapserver/");
// Same effect as if ($buildMode=='prod')
if (!is_dir($buildMapserverDir)) {
echo ("Creating $buildMapserverDir directory...\n");
mkdir($buildMapserverDir, 0755, true);
system("cp -r $gincoMapserverDir/data $buildMapserverDir");
}
echo ("Creating mapfile: $buildMapserverDir/ginco_{$config['instance.name']}.map...\n");
substituteInFile("$gincoMapserverDir/ginco_tpl.map", "$buildMapserverDir/ginco_{$config['instance.name']}.map", $config);
echo ("Done building mapfile.\n\n");
}
// on la range dans ./build/confapache
function buildApacheConf($config, $buildMode) {
global $projectDir, $buildDir, $postBuildInstructions;
chdir($projectDir);
echo ("building apache config...\n");
echo ("-------------------------\n");
$confApacheGincoDir = realpath($config['ginco.path'] . "/confapache");
$confapacheBuildDir = "$buildDir/confapache";
// Same effect as if ($buildMode=='prod')
if (!is_dir($confapacheBuildDir)) {
echo ("Creating $confapacheBuildDir directory...\n");
mkdir($confapacheBuildDir, 0755, true);
}
// No basepath: the Ginco application is alone on the domain and the virtual host
if (empty($config['url.basepath'])) {
$buildConfFile = "$confapacheBuildDir/ginco_{$config['instance.name']}.conf";
echo ("Creating apache configuration file: $buildConfFile...\n");
substituteInFile("$confApacheGincoDir/ginco_own_domain_tpl_$buildMode.conf", $buildConfFile, $config);
if ($buildMode == 'dev') {
$postBuildInstructions[] = "Apache configuration file has been built: $buildConfFile\n";
$postBuildInstructions[] = "To install, do:\n\n";
$postBuildInstructions[] = "sudo cp $buildConfFile /etc/apache2/sites-available/\n";
$postBuildInstructions[] = "sudo a2ensite " . pathinfo($buildConfFile, PATHINFO_BASENAME) . "\n";
$postBuildInstructions[] = "sudo service apache2 reload\n\n";
}
} // Basepath: the Ginco application can share the virtual host with other apps
else {
echo ("Creating apache configuration files: \n");
$buildConfFileVHost = "$confapacheBuildDir/ginco_{$config['url.domain']}.conf";
substituteInFile("$confApacheGincoDir/ginco_vhost_tpl.conf", $buildConfFileVHost, $config);
echo ("* $buildConfFileVHost\n");
$buildConfFileMapserver = "$confapacheBuildDir/include_mapserver_{$config['instance.name']}.conf";
substituteInFile("$confApacheGincoDir/include_mapserver_tpl.conf", $buildConfFileMapserver, $config);
echo ("* $buildConfFileMapserver\n");
$buildConfFileLogs = "$confapacheBuildDir/include_customlog_{$config['instance.name']}.conf";
substituteInFile("$confApacheGincoDir/include_customlog_tpl.conf", $buildConfFileLogs, $config);
echo ("* $buildConfFileLogs\n");
$buildConfFileGinco = "$confapacheBuildDir/include_ginco_{$config['instance.name']}.conf";
substituteInFile("$confApacheGincoDir/include_ginco_tpl.conf", $buildConfFileGinco, $config);
echo ("* $buildConfFileGinco\n");
$buildConfFileMaintenance = "$confapacheBuildDir/include_maintenance_{$config['instance.name']}.conf";
substituteInFile("$confApacheGincoDir/include_maintenance_tpl.conf", $buildConfFileMaintenance, $config);
echo ("* $buildConfFileMaintenance\n");
if ($buildMode == 'dev') {
$postBuildInstructions[] = "Apache configuration files has been built: $buildConfFileVHost, $buildConfFileGinco, $buildConfFileLogs, $buildConfFileMapserver\n";
$postBuildInstructions[] = "To install, do:\n\n";
$postBuildInstructions[] = "sudo cp -f $buildConfFileVHost /etc/apache2/sites-available/\n";
$postBuildInstructions[] = "sudo ln -fs $buildConfFileGinco /etc/apache2/sites-available/\n";
$postBuildInstructions[] = "sudo ln -fs $buildConfFileLogs /etc/apache2/sites-available/\n";
$postBuildInstructions[] = "sudo ln -fs $buildConfFileMapserver /etc/apache2/sites-available/\n";
$postBuildInstructions[] = "sudo ln -fs $buildConfFileMaintenance /etc/apache2/sites-available/\n";
$postBuildInstructions[] = "sudo a2ensite " . pathinfo($buildConfFileVHost, PATHINFO_BASENAME) . "\n";
$postBuildInstructions[] = "sudo service apache2 reload\n\n";
}
}
echo ("Done building apache config.\n\n");
}
// on la range dans ./build/confsupervisor
function buildSupervisorConf($config, $buildMode) {
global $projectDir, $buildDir, $postBuildInstructions;
chdir($projectDir);
echo ("building supervisor config...\n");
echo ("-----------------------------\n");
$confsupervisorBuildDir = "$buildDir/confsupervisor";
$confsupervisorGincoDir = realpath($config['ginco.path'] . "/confsupervisor");
// Same effect as if ($buildMode=='prod')
if (!is_dir($confsupervisorBuildDir)) {
echo ("Creating $confsupervisorBuildDir directory...\n");
mkdir($confsupervisorBuildDir, 0755, true);
}
$buildConfFile = "$confsupervisorBuildDir/ginco_{$config['instance.name']}.conf";
echo ("Creating supervisor configuration file: $buildConfFile...\n");
// Add parameter in config
$config['consumer.name'] = 'dlb_generic';
substituteInFile("$confsupervisorGincoDir/ginco_supervisor_tpl.conf", $buildConfFile, $config);
if ($buildMode == 'dev') {
$postBuildInstructions[] = "Supervisor configuration file has been built: $buildConfFile\n";
$postBuildInstructions[] = "To install, do:\n\n";
$postBuildInstructions[] = "sudo cp $buildConfFile /etc/supervisor/conf.d/\n";
$postBuildInstructions[] = "sudo service supervisor restart\n\n";
}
echo ("Done building supervisor config.\n\n");
}
// ------------------------------------------------------------------------------------------------------
if (count($argv) == 1)
usage();
// Get configuration and build options
$shortOpts = "f:"; // Name of config file
$shortOpts .= "D::"; // Overwrite config options
// Order of tasks listed here is important: they will be run that order
$tasksOptions = array(
"java", // Build java services
"website", // Build ginco website
"mapfile", // Build mapfile
"ext", // Build extJS client
"apacheconf", // Build apache configuration file
"supervisorconf"
); // Build supervisor configuration file
$otherOptions = array(
"mode::", // Development mode: symlinks instead of copy files
"builddir::"
) // Directory where build output will be put (only in prod mode)
;
$longOpts = array_merge($tasksOptions, $otherOptions);
$params = getopt($shortOpts, $longOpts);
// var_dump($params);
if (!isset($params['f']) || empty($params['f']))
usage();
// Get configuration parameters
$config = loadPropertiesFromArgs();
// var_dump($config);
// Get tasks to execute
$tasks = array();
foreach ($tasksOptions as $task) {
if (isset($params[$task])) {
$tasks[] = $task;
}
}
// If no task given, build all
if (count($tasks) == 0) {
$tasks = $tasksOptions;
}
// var_dump($tasks);
// Mode: development or prod (default is prod)
$buildMode = (isset($params['mode']) && $params['mode'] == 'dev') ? 'dev' : 'prod';
// build dir: where to put resulting builded files
// In prod mod, always erase build directory to have a coherent set of builded services
if ($buildMode == 'prod') {
$buildDir = (isset($params['builddir']) && !empty($params['builddir'])) ? $params['builddir'] : "$projectDir/build";
if (is_dir($buildDir) && $buildDir != $projectDir) {
system("rm -rf $buildDir");
}
mkdir($buildDir, 0755, true);
} else {
$buildDir = $projectDir;
}
// deploy dir: the path of the ginco application once deployed on target machine
$deployDir = ($buildMode == 'prod') ? "/var/www/" . $config['instance.name'] : $projectDir;
$config['deploy.dir'] = $deployDir;
// Post build instructions in dev mode
$postBuildInstructions = array();
// Execute tasks
if (in_array('java', $tasks)) {
buildJavaServices($config, $buildMode);
}
if (in_array('website', $tasks)) {
buildWebsite($config, $buildMode);
}
if (in_array('ext', $tasks)) {
buildExtJS($config, $buildMode);
}
if (in_array('mapfile', $tasks)) {
buildMapfile($config, $buildMode);
}
if (in_array('apacheconf', $tasks)) {
buildApacheConf($config, $buildMode);
}
if (in_array('supervisorconf', $tasks)) {
buildSupervisorConf($config, $buildMode);
}
// In prod mode, we keep the config parameters to a file which will be sent to the target machine
// It will be used by database creation/update scripts
if ($buildMode == 'prod') {
propertiesToFile($config, "$buildDir/config.properties");
}
echo ("Build finished build_dlb.php.\n\n");
// Show post-build instructions
if (count($postBuildInstructions) > 0) {
echo ("Post-build installation instructions:\n");
echo ("=====================================\n\n");
foreach ($postBuildInstructions as $instruction) {
echo ($instruction);
}
}