forked from ankane/setup-mariadb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
131 lines (107 loc) · 3.97 KB
/
index.js
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
const execSync = require("child_process").execSync;
const fs = require('fs');
const os = require('os');
const path = require('path');
const process = require('process');
const spawnSync = require('child_process').spawnSync;
function run(command) {
console.log(command);
let env = Object.assign({}, process.env);
delete env.CI; // for Homebrew
execSync(command, {stdio: 'inherit', env: env});
}
function runSafe() {
const args = Array.from(arguments);
console.log(args.join(' '));
const command = args.shift();
// spawn is safer and more lightweight than exec
const ret = spawnSync(command, args, {stdio: 'inherit'});
if (ret.status !== 0) {
throw ret.error;
}
}
function addToPath(newPath) {
fs.appendFileSync(process.env.GITHUB_PATH, `${newPath}\n`);
}
function isMac() {
return process.platform == 'darwin';
}
function isWindows() {
return process.platform == 'win32';
}
function formulaPresent(formula) {
const tap = `/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core`;
return fs.existsSync(`${tap}/Formula/${formula}.rb`) || fs.existsSync(`${tap}/Aliases/${formula}`);
}
const defaultVersion = '10.10';
const mariadbVersion = process.env['INPUT_MARIADB-VERSION'] || defaultVersion;
if (!['10.11', '10.10', '10.9', '10.8', '10.7', '10.6', '10.5', '10.4', '10.3'].includes(mariadbVersion)) {
throw 'Invalid MariaDB version: ' + mariadbVersion;
}
const database = process.env['INPUT_DATABASE'];
let bin;
if (isMac()) {
const formula = `mariadb@${mariadbVersion}`;
if (!formulaPresent(formula)) {
run('brew update');
}
// install
run(`brew install ${formula}`);
// start
bin = `/usr/local/opt/${formula}/bin`;
run(`${bin}/mysql.server start`);
addToPath(bin);
// add permissions
if (mariadbVersion == '10.3') {
run(`${bin}/mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO ''@'localhost'"`);
run(`${bin}/mysql -u root -e "FLUSH PRIVILEGES"`);
}
} else if (isWindows()) {
// install
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mariadb-'));
process.chdir(tmpDir);
const versionMap = {
'10.11': '10.11.2',
'10.10': '10.10.2',
'10.9': '10.9.4',
'10.8': '10.8.6',
'10.7': '10.7.7',
'10.6': '10.6.11',
'10.5': '10.5.18',
'10.4': '10.4.27',
'10.3': '10.3.37'
};
const fullVersion = versionMap[mariadbVersion];
run(`curl -Ls -o mariadb.msi https://downloads.mariadb.com/MariaDB/mariadb-${fullVersion}/winx64-packages/mariadb-${fullVersion}-winx64.msi`);
run(`msiexec /i mariadb.msi SERVICENAME=MariaDB /qn`);
bin = `C:\\Program Files\\MariaDB ${mariadbVersion}\\bin`;
addToPath(bin);
// add user
run(`"${bin}\\mysql" -u root -e "CREATE USER 'runneradmin'@'localhost' IDENTIFIED BY ''"`);
run(`"${bin}\\mysql" -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'runneradmin'@'localhost'"`);
run(`"${bin}\\mysql" -u root -e "FLUSH PRIVILEGES"`);
} else {
const image = process.env['ImageOS'];
if (image == 'ubuntu20' || image == 'ubuntu22') {
// clear previous data
run(`sudo systemctl stop mysql.service`);
run(`sudo rm -rf /var/lib/mysql`);
}
// install
run(`sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8`);
run(`echo "deb https://downloads.mariadb.com/MariaDB/mariadb-${mariadbVersion}/repo/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) main" | sudo tee /etc/apt/sources.list.d/mariadb.list`);
run(`sudo apt-get update -o Dir::Etc::sourcelist="sources.list.d/mariadb.list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0"`);
run(`sudo apt-get install mariadb-server-${mariadbVersion}`);
// start
run(`sudo systemctl start mariadb`);
// remove root password
run(`sudo mysqladmin -proot password ''`);
// add user
run(`sudo mysql -e "CREATE USER '$USER'@'localhost' IDENTIFIED BY ''"`);
run(`sudo mysql -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'@'localhost'"`);
run(`sudo mysql -e "FLUSH PRIVILEGES"`);
bin = `/usr/bin`;
}
if (database) {
runSafe(path.join(bin, 'mysqladmin'), 'create', database);
}