Skip to content

Commit 8d000ba

Browse files
authored
Mysql Plugin v1 (#1221)
## Summary Adds a plugin for MySQL DB. Implementation is very similar to MariaDB plugin, except: 1. init_hook is changed to use `mysqld --initialize-insecure` 2. removed unnecessary symlink wrappers from flake.nix 3. renamed services ## How was it tested? `devbox shell` using the example in this PR, then: * `devbox services up` -- should show mysql + mysql_logs * `devbox run mysql` -- should give you a mysql console
1 parent bafc70d commit 8d000ba

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"packages": [
3+
"mysql80@latest"
4+
],
5+
"shell": {
6+
"init_hook": [
7+
"echo 'Welcome to devbox!' > /dev/null"
8+
],
9+
"scripts": {
10+
"test": [
11+
"echo \"Error: no test specified\" && exit 1"
12+
]
13+
}
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"lockfile_version": "1",
3+
"packages": {
4+
"mysql80@latest": {
5+
"last_modified": "2023-05-01T16:53:22Z",
6+
"plugin_version": "0.0.2",
7+
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#mysql80",
8+
"version": "8.0.33"
9+
}
10+
}
11+
}

plugins/mysql.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "mysql",
3+
"version": "0.0.1",
4+
"match": "^mysql?[0-9]*$",
5+
"readme": "* This plugin wraps mysqld and mysql_install_db to work in your local project\n* This plugin will create a new database for your project in MYSQL_DATADIR if one doesn't exist on shell init. This DB will be started in `insecure` mode, so be sure to add a root password after creation if needed.\n* Use mysqld to manually start the server, and `mysqladmin -u root shutdown` to manually stop it",
6+
"env": {
7+
"MYSQL_BASEDIR": "{{ .DevboxProfileDefault }}",
8+
"MYSQL_HOME": "{{ .Virtenv }}/run",
9+
"MYSQL_DATADIR": "{{ .Virtenv }}/data",
10+
"MYSQL_UNIX_PORT": "{{ .Virtenv }}/run/mysql.sock",
11+
"MYSQL_PID_FILE": "{{ .Virtenv }}/run/mysql.pid"
12+
},
13+
"create_files": {
14+
"{{ .Virtenv }}/run": "",
15+
"{{ .Virtenv }}/flake.nix": "mysql/flake.nix",
16+
"{{ .Virtenv }}/setup_db.sh": "mysql/setup_db.sh",
17+
"{{ .Virtenv }}/process-compose.yaml": "mysql/process-compose.yaml"
18+
},
19+
"packages": [
20+
"path:{{ .Virtenv }}"
21+
],
22+
"shell": {
23+
"init_hook": [
24+
"bash {{ .Virtenv }}/setup_db.sh"
25+
]
26+
}
27+
}

plugins/mysql/flake.nix

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
description = "A flake that outputs MySQL with custom configuration and aliases to work in Devbox";
3+
4+
inputs = {
5+
nixpkgs.url = "{{.URLForInput}}";
6+
};
7+
8+
outputs = {self, nixpkgs}:
9+
let
10+
mysql-bin = nixpkgs.legacyPackages.{{.System}}.symlinkJoin {
11+
12+
name = "mysql-wrapped";
13+
paths = [nixpkgs.legacyPackages.{{ .System }}.{{.PackageAttributePath}}];
14+
nativeBuildInputs = [ nixpkgs.legacyPackages.{{.System}}.makeWrapper];
15+
postBuild = ''
16+
17+
wrapProgram $out/bin/mysqld \
18+
--add-flags '--datadir=''$MYSQL_DATADIR --pid-file=''$MYSQL_PID_FILE --socket=''$MYSQL_UNIX_PORT';
19+
20+
wrapProgram $out/bin/mysqld_safe \
21+
--add-flags '--datadir=''$MYSQL_DATADIR --pid-file=''$MYSQL_PID_FILE --socket=''$MYSQL_UNIX_PORT';
22+
'';
23+
};
24+
in{
25+
packages.{{.System}} = {
26+
default = mysql-bin;
27+
};
28+
};
29+
}

plugins/mysql/process-compose.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: "0.5"
2+
3+
processes:
4+
mysql:
5+
command: "mysqld 2> $MYSQL_HOME/mysql.log & MYSQL_PID=$! && echo 'Starting mysqld... check mariadb_logs for details'"
6+
is_daemon: true
7+
shutdown:
8+
command: "mysqladmin -u root shutdown"
9+
availability:
10+
restart: "always"
11+
depends_on:
12+
mysql_logs:
13+
condition: "process_started"
14+
mysql_logs:
15+
command: "tail -f $MYSQL_HOME/mysql.log"
16+
availability:
17+
restart: "always"

plugins/mysql/setup_db.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#! bash
2+
3+
if [ ! -d "$MYSQL_DATADIR" ]; then
4+
# Install the Database
5+
mkdir $MYSQL_DATADIR
6+
mysqld --initialize-insecure
7+
fi

0 commit comments

Comments
 (0)