From 69779da97d94c04a84a0cee5b123c10bfb0b11ef Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 16 May 2016 00:24:38 -0400 Subject: [PATCH] Remove remaining direct dokku calls --- functions | 54 ++++++++++++++++++++++++++++++++++++++++++++++ subcommands/clone | 4 ++-- subcommands/create | 26 +--------------------- subcommands/export | 10 +-------- subcommands/import | 9 +------- 5 files changed, 59 insertions(+), 44 deletions(-) diff --git a/functions b/functions index becaf39..ba16d03 100755 --- a/functions +++ b/functions @@ -38,6 +38,19 @@ service_alias() { echo "$SERVICE_NAME" | tr ._ - } +service_export() { + local SERVICE="$1" + local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + local SERVICE_NAME="$(get_service_name "$SERVICE")" + local PASSWORD=$(cat "$SERVICE_ROOT/PASSWORD") + + [[ -n $SSH_TTY ]] && stty -opost + docker exec "$SERVICE_NAME" mysqldump --user=mysql --password="$PASSWORD" "$SERVICE" + status=$? + [[ -n $SSH_TTY ]] && stty opost + exit $status +} + service_info() { local SERVICE="$1" local SERVICE_URL=$(service_url "$SERVICE") @@ -45,6 +58,18 @@ service_info() { echo " DSN: $SERVICE_URL" } +service_import() { + local SERVICE="$1" + SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + SERVICE_NAME="$(get_service_name "$SERVICE")" + PASSWORD=$(cat "$SERVICE_ROOT/PASSWORD") + + if [[ -t 0 ]]; then + dokku_log_fail "No data provided on stdin." + fi + docker exec -i "$SERVICE_NAME" mysql --user=mysql --password="$PASSWORD" "$SERVICE" +} + service_list() { local SERVICES=$(ls "$PLUGIN_DATA_ROOT" 2> /dev/null) if [[ -z $SERVICES ]]; then @@ -253,6 +278,35 @@ service_start() { fi } +service_create() { + local SERVICE="$1" + [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" + [[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] || dokku_log_fail "$PLUGIN_SERVICE service $SERVICE already exists" + SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"; LINKS_FILE="$SERVICE_ROOT/LINKS" + + if ! docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " ; then + docker pull "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" || dokku_log_fail "$PLUGIN_SERVICE image $PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION pull failed" + fi + + mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory" + mkdir -p "$SERVICE_ROOT/data" || dokku_log_fail "Unable to create service data directory" + mkdir -p "$SERVICE_ROOT/config" || dokku_log_fail "Unable to create service config directory" + echo -e "[mysqld]\nperformance_schema = 0" > "$SERVICE_ROOT/config/disable_performance_schema.cnf" + rootpassword=$(openssl rand -hex 8) + password=$(openssl rand -hex 8) + echo "$rootpassword" > "$SERVICE_ROOT/ROOTPASSWORD" + echo "$password" > "$SERVICE_ROOT/PASSWORD" + chmod 640 "$SERVICE_ROOT/ROOTPASSWORD" "$SERVICE_ROOT/PASSWORD" + touch "$LINKS_FILE" + + if [[ -n $MYSQL_CUSTOM_ENV ]]; then + echo "$MYSQL_CUSTOM_ENV" | tr ';' "\n" > "$SERVICE_ROOT/ENV" + else + echo "" > "$SERVICE_ROOT/ENV" + fi + service_create_container "$SERVICE" +} + service_create_container() { local SERVICE="$1" local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" diff --git a/subcommands/clone b/subcommands/clone index ee5bee1..8c1ad15 100755 --- a/subcommands/clone +++ b/subcommands/clone @@ -13,9 +13,9 @@ mysql-clone-cmd() { [[ -z "$NEW_SERVICE" ]] && dokku_log_fail "Please specify a name for the new service" verify_service_name "$SERVICE" - dokku "$PLUGIN_COMMAND_PREFIX:create" "$NEW_SERVICE" + service_create "$NEW_SERVICE" dokku_log_info1 "Copying data from $SERVICE to $NEW_SERVICE" - dokku "$PLUGIN_COMMAND_PREFIX:export" "$SERVICE" | dokku "$PLUGIN_COMMAND_PREFIX:import" "$NEW_SERVICE" > /dev/null 2>&1 || true + service_export "$SERVICE" | service_import "$NEW_SERVICE" > /dev/null 2>&1 || true dokku_log_info1 "Done" } diff --git a/subcommands/create b/subcommands/create index e896fc9..1006edc 100755 --- a/subcommands/create +++ b/subcommands/create @@ -9,31 +9,7 @@ mysql-create-cmd() { local cmd="$PLUGIN_COMMAND_PREFIX:create" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" - [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" - [[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] || dokku_log_fail "$PLUGIN_SERVICE service $SERVICE already exists" - SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"; LINKS_FILE="$SERVICE_ROOT/LINKS" - - if ! docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " ; then - docker pull "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" || dokku_log_fail "$PLUGIN_SERVICE image $PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION pull failed" - fi - - mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory" - mkdir -p "$SERVICE_ROOT/data" || dokku_log_fail "Unable to create service data directory" - mkdir -p "$SERVICE_ROOT/config" || dokku_log_fail "Unable to create service config directory" - echo -e "[mysqld]\nperformance_schema = 0" > "$SERVICE_ROOT/config/disable_performance_schema.cnf" - rootpassword=$(openssl rand -hex 8) - password=$(openssl rand -hex 8) - echo "$rootpassword" > "$SERVICE_ROOT/ROOTPASSWORD" - echo "$password" > "$SERVICE_ROOT/PASSWORD" - chmod 640 "$SERVICE_ROOT/ROOTPASSWORD" "$SERVICE_ROOT/PASSWORD" - touch "$LINKS_FILE" - - if [[ -n $MYSQL_CUSTOM_ENV ]]; then - echo "$MYSQL_CUSTOM_ENV" | tr ';' "\n" > "$SERVICE_ROOT/ENV" - else - echo "" > "$SERVICE_ROOT/ENV" - fi - service_create_container "$SERVICE" + service_create "$SERVICE" } mysql-create-cmd "$@" diff --git a/subcommands/export b/subcommands/export index 102f506..b85268a 100755 --- a/subcommands/export +++ b/subcommands/export @@ -11,15 +11,7 @@ mysql-export-cmd() { [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$SERVICE" - SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" - SERVICE_NAME="$(get_service_name "$SERVICE")" - PASSWORD=$(cat "$SERVICE_ROOT/PASSWORD") - - [[ -n $SSH_TTY ]] && stty -opost - docker exec "$SERVICE_NAME" mysqldump --user=mysql --password="$PASSWORD" "$SERVICE" - status=$? - [[ -n $SSH_TTY ]] && stty opost - exit $status + service_export "$SERVICE" } mysql-export-cmd "$@" diff --git a/subcommands/import b/subcommands/import index 7bb79b9..2cf3563 100755 --- a/subcommands/import +++ b/subcommands/import @@ -11,14 +11,7 @@ mysql-import-cmd() { [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$SERVICE" - SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" - SERVICE_NAME="$(get_service_name "$SERVICE")" - PASSWORD=$(cat "$SERVICE_ROOT/PASSWORD") - - if [[ -t 0 ]]; then - dokku_log_fail "No data provided on stdin." - fi - docker exec -i "$SERVICE_NAME" mysql --user=mysql --password="$PASSWORD" "$SERVICE" + service_import "$SERVICE" } mysql-import-cmd "$@"