-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
123 lines (102 loc) · 3.79 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
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 on macos-11.0
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`);
}
const image = process.env['ImageOS'];
const defaultVersion = '8.0';
const mysqlVersion = parseFloat(process.env['INPUT_MYSQL-VERSION'] || defaultVersion).toFixed(1);
// TODO make OS-specific
if (!['8.4', '8.0'].includes(mysqlVersion)) {
throw `MySQL version not supported: ${mysqlVersion}`;
}
const database = process.env['INPUT_DATABASE'];
let bin;
function useTmpDir() {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mysql-'));
process.chdir(tmpDir);
}
if (process.platform == 'darwin') {
// install
run(`brew install mysql@${mysqlVersion}`);
// start
const prefix = process.arch == 'arm64' ? '/opt/homebrew' : '/usr/local';
bin = `${prefix}/opt/mysql@${mysqlVersion}/bin`;
run(`${bin}/mysql.server start`);
// add user
run(`${bin}/mysql -e "CREATE USER '$USER'@'localhost' IDENTIFIED BY ''"`);
run(`${bin}/mysql -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'@'localhost'"`);
run(`${bin}/mysql -e "FLUSH PRIVILEGES"`);
// set path
addToPath(bin);
} else if (process.platform == 'win32') {
// install
const install = image == 'win19' ? true : mysqlVersion != '8.0';
if (install) {
// https://dev.mysql.com/downloads/mysql/
const versionMap = {
'8.4': '8.4.3',
'8.0': '8.0.40'
};
const fullVersion = versionMap[mysqlVersion];
useTmpDir();
run(`curl -Ls -o mysql.zip https://dev.mysql.com/get/Downloads/MySQL-${mysqlVersion}/mysql-${fullVersion}-winx64.zip`)
run(`unzip -q mysql.zip`);
fs.renameSync(`mysql-${fullVersion}-winx64`, `C:\\Program Files\\MySQL\\MySQL Server ${mysqlVersion}`);
}
// start
bin = `C:\\Program Files\\MySQL\\MySQL Server ${mysqlVersion}\\bin`;
run(`"${bin}\\mysqld" --initialize-insecure`);
run(`"${bin}\\mysqld" --install`);
run(`net start MySQL`);
addToPath(bin);
run(`"${bin}\\mysql" -u root -e "SELECT VERSION()"`);
// add user
run(`"${bin}\\mysql" -u root -e "CREATE USER 'ODBC'@'localhost' IDENTIFIED BY ''"`);
run(`"${bin}\\mysql" -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'ODBC'@'localhost'"`);
run(`"${bin}\\mysql" -u root -e "FLUSH PRIVILEGES"`);
} else {
if (mysqlVersion != '8.0' || process.arch == 'arm64') {
// install
useTmpDir();
// https://dev.mysql.com/downloads/repo/apt/
run(`wget -q -O mysql-apt-config.deb https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb`);
run(`echo mysql-apt-config mysql-apt-config/select-server select mysql-${mysqlVersion}-lts | sudo debconf-set-selections`);
run(`sudo dpkg -i mysql-apt-config.deb`);
// TODO only update single list
run(`sudo apt-get update`);
run(`sudo apt-get install mysql-server`);
}
// start
run('sudo systemctl start mysql');
// 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);
}