Skip to content

Commit 6ee958a

Browse files
committed
Dockerfile refactoring changes
1 parent e404c0f commit 6ee958a

23 files changed

+173
-132
lines changed

classes/CCR/DB/MySQLHelper.php

+46-5
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,47 @@ public static function databaseExists(
342342
throw new Exception($msg);
343343
}
344344
}
345+
/*
346+
* Checks for existence of user
347+
*/
348+
/**
349+
* @throws Exception
350+
*/
351+
public static function userExists(
352+
$host,
353+
$port,
354+
$dbusername,
355+
$dbpassword,
356+
$userName,
357+
$xdmod_host
358+
) {
359+
$stmt = "SELECT User, Host FROM mysql.user WHERE User = '$userName' AND Host = '$xdmod_host'";
360+
$output = static::staticExecuteStatement(
361+
$host,
362+
$port,
363+
$dbusername,
364+
$dbpassword,
365+
null,
366+
$stmt
367+
);
368+
369+
$output = preg_replace('/\s+/', ' ', $output);
370+
if (!isset($output[0])) {
371+
return false;
372+
}
373+
$output = explode(' ', $output[0]);
374+
if (count($output) == 0 || (count($output) == 1 && $output[0] == '')) {
375+
return false;
376+
} elseif (count($output) == 2 && $output[0] == $userName && $output[1]== $xdmod_host) {
377+
return true;
378+
} else {
379+
$msg = 'Failed to check for existence of user: '
380+
. implode("\n", array_map(function($row) {
381+
return $row[0] . ' ' . $row[1];
382+
}, $output));
383+
throw new Exception($msg);
384+
}
385+
}
345386

346387
/**
347388
* Create a database.
@@ -415,7 +456,7 @@ public static function grantAllPrivileges(
415456
$port,
416457
$username,
417458
$password,
418-
$localHost,
459+
$xdmodHost,
419460
$dbUsername,
420461
$dbPassword
421462
) {
@@ -429,8 +470,8 @@ public static function grantAllPrivileges(
429470
. " CREATE ROUTINE, ALTER ROUTINE, EVENT, RELOAD, FILE,"
430471
. " CREATE TABLESPACE, PROCESS, REFERENCES,"
431472
. " LOCK TABLES"
432-
. " ON *.* TO '$dbUsername'@'$localHost'"
433-
. " IDENTIFIED BY '$dbPassword';FLUSH PRIVILEGES;";
473+
. " ON *.* TO '$dbUsername'@'$xdmodHost';"
474+
. " FLUSH PRIVILEGES;";
434475

435476
static::staticExecuteStatement(
436477
$host,
@@ -460,11 +501,11 @@ public static function grantAllPrivilegesOnDatabase(
460501
$username,
461502
$password,
462503
$dbName,
463-
$localHost,
504+
$xdmodHost,
464505
$dbUsername,
465506
$dbPassword
466507
) {
467-
$stmt = "GRANT ALL ON $dbName.* TO '$dbUsername'@'$localHost'"
508+
$stmt = "GRANT ALL ON $dbName.* TO '$dbUsername'@'$xdmodHost'"
468509
. " IDENTIFIED BY '$dbPassword'";
469510

470511
static::staticExecuteStatement(

classes/OpenXdmod/Setup/DatabaseSetup.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ public function handle()
5858
'DB Username:',
5959
$settings['database_user']
6060
);
61-
61+
$settings['xdmod_host'] = $this->console->prompt(
62+
'XDMoD Server name:',
63+
$settings['database_host']
64+
);
6265
$settings['db_pass'] = $this->console->silentPrompt(
6366
'DB Password:'
6467
);

classes/OpenXdmod/Shared/DatabaseHelper.php

+37-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OpenXdmod\Shared;
44

5+
use CCR\DB;
56
use CCR\DB\MySQLHelper;
67
use OpenXdmod\Setup\Console;
78

@@ -24,6 +25,7 @@ class DatabaseHelper
2425
* @param Console $console (Optional) The console to use to prompt the user.
2526
* If not provided, one will be obtained.
2627
*/
28+
2729
public static function createDatabases(
2830
$username,
2931
$password,
@@ -35,23 +37,50 @@ public static function createDatabases(
3537
$console = Console::factory();
3638
}
3739

40+
$rows = MySQLHelper::userExists(
41+
$settings['db_host'],
42+
$settings['db_port'],
43+
$username,
44+
$password,
45+
$settings['db_user'],
46+
$settings['xdmod_host']
47+
);
3848
$console->displayMessage(
39-
'Creating User ' . $settings['db_user']
49+
'rows retuned' . $rows
4050
);
41-
// TODO: If db_host is not localhost, need to set $localHost to
42-
// the correct hostname or IP address.
43-
$localHost = $settings['db_host'];
44-
51+
if ($rows == false) {
52+
$console->displayMessage(
53+
'Creating User ' . $settings['db_user']
54+
);
55+
$console->displayMessage(
56+
'Creating User with ' . $settings['db_user'] . ' on host ' . $settings['xdmod_host'] . ' with password ' . $settings['db_pass']
57+
);
58+
MySQLHelper::staticExecuteStatement(
59+
$settings['db_host'],
60+
$settings['db_port'],
61+
$username,
62+
$password,
63+
null,
64+
sprintf(
65+
"CREATE USER '%s'@'%s' IDENTIFIED BY '%s';",
66+
$settings['db_user'],
67+
$settings['xdmod_host'],
68+
$settings['db_pass'],
69+
)
70+
);
71+
$console->displayMessage(
72+
'Created User'
73+
);
74+
}
4575
MySQLHelper::grantAllPrivileges(
4676
$settings['db_host'],
4777
$settings['db_port'],
4878
$username,
4979
$password,
50-
$localHost,
80+
$settings['xdmod_host'],
5181
$settings['db_user'],
5282
$settings['db_pass']
5383
);
54-
5584
foreach ($databases as $database) {
5685
$console->displayBlankLine();
5786

@@ -97,10 +126,6 @@ public static function createDatabases(
97126
$database
98127
);
99128

100-
// TODO: If db_host is not localhost, need to set $localHost to
101-
// the correct hostname or IP address.
102-
$localHost = $settings['db_host'];
103-
104129
$console->displayMessage(
105130
"Granting privileges on database `$database`."
106131
);
@@ -110,7 +135,7 @@ public static function createDatabases(
110135
$username,
111136
$password,
112137
$database,
113-
$localHost,
138+
$settings['xdmod_host'],
114139
$settings['db_user'],
115140
$settings['db_pass']
116141
);

configuration/etl/etl_tables.d/cloud_common/domains.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"name": "domains",
55
"engine": "InnoDB",
66
"charset": "utf8",
7+
"collation": "utf8_unicode_ci",
78
"comment": "Which domains are currently being tracked by the Cloud realm",
89
"columns": [
910
{

configuration/etl/etl_tables.d/gateways/enduser.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"name": "enduser",
44
"comment": "Associate local gateway enduser names with the gateway on which they are registered",
55
"engine": "myisam",
6-
"charset": "utf8mb4",
7-
"collation": "utf8mb4_general_ci",
6+
"charset": "utf8",
7+
"collation": "utf8_unicode_ci",
88
"columns": [
99
{
1010
"type": "int(11)",

configuration/etl/etl_tables.d/gateways/gateway.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"name": "gateway",
44
"comment": "Listing of Science Gateways",
55
"engine": "myisam",
6-
"charset": "utf8mb4",
7-
"collation": "utf8mb4_general_ci",
6+
"charset": "utf8",
7+
"collation": "utf8_unicode_ci",
88
"columns": [
99
{
1010
"type": "int(11)",

configuration/etl/etl_tables.d/gateways/job_metadata.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"name": "job_metadata",
44
"comment": "Metadata associated with gateways-submitted jobs",
55
"engine": "myisam",
6-
"charset": "utf8mb4",
7-
"collation": "utf8mb4_general_ci",
6+
"charset": "utf8",
7+
"collation": "utf8_unicode_ci",
88
"columns": [
99
{
1010
"type": "int(11)",

configuration/etl/etl_tables.d/jobs/xdw/federation-instances.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
{
1313
"name": "prefix",
1414
"type": "varchar(191)",
15-
"comment": "generally fqdn with . replaced by - 191 limit due to utf8mb4",
15+
"comment": "generally fqdn with . replaced by - 191 limit due to utf8",
1616
"nullable": true
1717
},
1818
{
1919
"name": "timezone",
2020
"type": "varchar(191)",
21-
"comment": "Timezone of the instance - 191 limit due to utf8mb4",
21+
"comment": "Timezone of the instance - 191 limit due to utf8",
2222
"nullable": true
2323
},
2424
{

configuration/etl/etl_tables.d/xdb/report-template-charts.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"table_definition": {
33
"name": "ReportTemplateCharts",
44
"engine": "InnoDB",
5-
"charset": "utf8mb4",
6-
"collation": "utf8mb4_general_ci",
5+
"charset": "utf8",
6+
"collation": "utf8_unicode_ci",
77
"columns": [
88
{
99
"name": "template_id",

tests/artifacts/xdmod/etlv2/configuration/input/etl_tables_8.0.0.d/db_model_baseline_table.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "normalize_table_test",
55
"engine": "MyISAM",
66
"charset": "utf8",
7-
"collation": "utf8_general_ci",
7+
"collation": "utf8_unicode_ci",
88
"columns": [
99
{
1010
"name": "resource_id",
@@ -17,7 +17,7 @@
1717
"type": "varchar(40)",
1818
"nullable": true,
1919
"charset": "utf8",
20-
"collation": "utf8_general_ci",
20+
"collation": "utf8_unicode_ci",
2121
"comment": "This is a comment"
2222
},
2323
{

tests/artifacts/xdmod/etlv2/configuration/input/etl_tables_8.0.0.d/db_model_normalize_table_definitions.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"table_definition": {
44
"name": "normalize_table_test",
55
"engine": "myisam",
6-
"charset": "UTF8",
7-
"collation": "UTF8_GENERAL_CI",
6+
"charset": "utf8",
7+
"collation": "utf8_unicode_ci",
88
"columns": [
99
{
1010
"name": "resource_id",
@@ -16,8 +16,8 @@
1616
"name": "RESOURCE",
1717
"type": "VARCHAR(40)",
1818
"nullable": true,
19-
"charset": "UTF8",
20-
"collation": "utf8_general_ci",
19+
"charset": "utf8",
20+
"collation": "utf8_unicode_ci",
2121
"comment": "This is a comment"
2222
},
2323
{

tests/artifacts/xdmod/etlv2/configuration/output/create_table.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"comment": "",
33
"engine": "myisam",
4-
"charset": "latin1",
5-
"collation": "latin1_swedish_ci",
4+
"charset": "utf8",
5+
"collation": "utf8_unicode_ci",
66
"columns": [
77
{
88
"type": "varchar(40)",
9-
"charset": "latin1",
10-
"collation": "latin1_swedish_ci",
9+
"charset": "utf8",
10+
"collation": "utf8_unicode_ci",
1111
"nullable": true,
1212
"default": null,
1313
"extra": null,

tests/artifacts/xdmod/etlv2/configuration/output/modify_table.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"comment": "",
33
"engine": "myisam",
4-
"charset": "latin1",
5-
"collation": "latin1_swedish_ci",
4+
"charset": "utf8",
5+
"collation": "utf8_unicode_ci",
66
"columns": [
77
{
88
"type": "varchar(40)",
9-
"charset": "latin1",
10-
"collation": "latin1_swedish_ci",
9+
"charset": "utf8",
10+
"collation": "utf8_unicode_ci",
1111
"nullable": true,
1212
"default": null,
1313
"extra": null,
@@ -17,8 +17,8 @@
1717
},
1818
{
1919
"type": "varchar(40)",
20-
"charset": "latin1",
21-
"collation": "latin1_swedish_ci",
20+
"charset": "utf8",
21+
"collation": "utf8_unicode_ci",
2222
"nullable": true,
2323
"default": null,
2424
"extra": null,

tests/artifacts/xdmod/etlv2/configuration/output/normalized_table_definition.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"comment": "",
33
"engine": "myisam",
44
"charset": "utf8",
5-
"collation": "utf8_general_ci",
5+
"collation": "utf8_unicode_ci",
66
"columns": [
77
{
88
"type": "int(11) unsigned",
@@ -18,7 +18,7 @@
1818
{
1919
"type": "varchar(40)",
2020
"charset": "utf8",
21-
"collation": "utf8_general_ci",
21+
"collation": "utf8_unicode_ci",
2222
"nullable": true,
2323
"default": null,
2424
"extra": null,
@@ -29,7 +29,7 @@
2929
{
3030
"type": "varchar(16)",
3131
"charset": "utf8",
32-
"collation": "utf8_general_ci",
32+
"collation": "utf8_unicode_ci",
3333
"nullable": false,
3434
"default": "first name",
3535
"extra": null,
@@ -40,7 +40,7 @@
4040
{
4141
"type": "enum('Sample1','sample2')",
4242
"charset": "utf8",
43-
"collation": "utf8_general_ci",
43+
"collation": "utf8_unicode_ci",
4444
"nullable": true,
4545
"default": null,
4646
"extra": null,
@@ -75,7 +75,7 @@
7575
"event": "INSERT",
7676
"table": "normalize_table_test",
7777
"body": "BEGIN\nSET @resource = CONCAT(@resource, 'X');\nEND",
78-
"definer": "xdmod@localhost",
78+
"definer": "xdmod@xdmod.xdmod_default",
7979
"schema": "test",
8080
"name": "mytrigger"
8181
}

0 commit comments

Comments
 (0)