Skip to content

Commit

Permalink
feat(mariadb): create database if MARIADB_* env are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Aug 18, 2024
1 parent dc2ac07 commit cf66195
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ jobs:
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NIX_CONFIG: extra-access-tokens = github.com=${{ secrets.GH_ACCESS_TOKEN }}
MARIADB_USER: demo
MARIADB_PASSWORD: demo
MARIADB_DATABASE: demo
40 changes: 40 additions & 0 deletions mariadb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@ pub mod helpers;
#[plugin_fn]
pub fn start(_args: String) -> FnResult<String> {
helpers::setup()?;
let user = dag().get_env("MARIADB_USER")?;
let password = dag().get_env("MARIADB_PASSWORD")?;
let database = dag().get_env("MARIADB_DATABASE")?;
let port = dag().get_env("MYSQL_PORT")?;

if user.is_empty() {
dag().set_envs(vec![("MARIADB_USER".into(), "fluentci".into())])?;
}

if password.is_empty() {
dag().set_envs(vec![("MARIADB_PASSWORD".into(), "fluentci".into())])?;
}

if database.is_empty() {
dag().set_envs(vec![("MARIADB_DATABASE".into(), "demo".into())])?;
}

let stdout = dag()
.flox()?
.with_workdir(".fluentci/mariadb")?
Expand All @@ -21,6 +37,30 @@ pub fn start(_args: String) -> FnResult<String> {
.with_exec(vec!["sleep 2 && tail $MYSQL_HOME/mysql.log"])?
.wait_on(port.parse()?, None)?
.with_exec(vec!["cat", "$MYSQL_HOME/mysql.log"])?
.with_exec(vec![
"mysql",
"-u",
"`whoami`",
"--socket=$MYSQL_HOME/mysql.socket -e \"CREATE DATABASE IF NOT EXISTS $MARIADB_DATABASE;\"",
])?
.with_exec(vec![
"mysql",
"-u",
"`whoami`",
"--socket=$MYSQL_HOME/mysql.socket -e \"CREATE USER IF NOT EXISTS '$MARIADB_USER'@'localhost' IDENTIFIED BY '$MARIADB_PASSWORD';\"",
])?
.with_exec(vec![
"mysql",
"-u",
"`whoami`",
"--socket=$MYSQL_HOME/mysql.socket -e \"GRANT ALL PRIVILEGES ON $MARIADB_DATABASE.* TO '$MARIADB_USER'@'localhost';\"",
])?
.with_exec(vec![
"mysql",
"-u",
"`whoami`",
"--socket=$MYSQL_HOME/mysql.socket -e \"FLUSH PRIVILEGES;\"",
])?
.with_exec(vec!["overmind", "status"])?
.stdout()?;
Ok(stdout)
Expand Down

0 comments on commit cf66195

Please sign in to comment.