Skip to content

Commit

Permalink
Improve cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
joglomedia committed Jan 22, 2025
1 parent 7f0afdd commit 5541053
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
5 changes: 3 additions & 2 deletions bin/lemper-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,16 @@ These are common ${PROG_NAME} commands used in various situations:
For help with each command run:
${PROG_NAME} <command> -h | --help
EOL

exit 0
}

##
# Show version.
##
function cmd_version() {
echo "${PROG_NAME} version ${PROG_VERSION}"
exit 0
}

##
Expand All @@ -193,11 +196,9 @@ function init_lemper_cli() {
case "${CMD}" in
help | -h | --help)
cmd_help
exit 0
;;
version | -v | --version)
cmd_version
exit 0
;;
*)
if [[ -x "${CLI_PLUGINS_DIR}/lemper-${CMD}" ]]; then
Expand Down
32 changes: 15 additions & 17 deletions lib/lemper-create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -906,15 +906,15 @@ function init_lemper_create() {
if [[ "${MAIN_ARGS}" -ge 1 ]]; then
# Additional Check - ensure that Nginx's configuration meets the requirements.
if [[ ! -d /etc/nginx/sites-available && ! -d /etc/nginx/vhost ]]; then
fail "It seems that your Nginx installation doesn't meet LEMPer requirements. Aborting..."
fail "Your Nginx installation does not meet the LEMPer stack requirements. The installation has been aborted."
fi

# Check domain parameter.
if [[ -z "${SERVERNAME}" ]]; then
fail -e "Domain name parameter shouldn't be empty.\n -d or --domain-name parameter is required!"
fail -e "The domain name parameter should not be empty. \n -d or --domain-name parameter is required!"
else
if [[ $(validate_fqdn "${SERVERNAME}") == false ]]; then
fail "Your Domain name is not valid 'Fully Qualified Domain Name (FQDN)' format!"
fail "Invalid input: '${SERVERNAME}' is not a valid FQDN. Expected format: example.com."
fi
fi

Expand All @@ -928,17 +928,17 @@ function init_lemper_create() {

# Check if vhost not exists.
if [[ ! -f "${VHOST_FILE}" ]]; then
echo "Add new domain name '${SERVERNAME}' to virtual host."
echo "Adding domain ${SERVERNAME} to the Nginx virtual host configuration."

# Check for username.
if [[ -z "${USERNAME}" ]]; then
info "Username parameter is empty. Attempt to use default '${LEMPER_USERNAME}' account."
info "Username parameter is empty. Using default account: '${LEMPER_USERNAME}'."
USERNAME=${LEMPER_USERNAME:-"lemper"}
fi

# Additional Check - are user account exist?
if [[ -z $(getent passwd "${USERNAME}") ]]; then
fail "User account '${USERNAME}' does not exist. Please add new account first! Aborting..."
fial "User account '${USERNAME}' does not exist. Create the account first. Aborting..."
fi

# Check PHP runtime version is exists.
Expand All @@ -949,8 +949,8 @@ function init_lemper_create() {

# Additional check - if FPM user's pool doesn't exist.
if [[ ! -f "/etc/php/${PHP_VERSION}/fpm/pool.d/${USERNAME}.conf" ]]; then
info "The PHP${PHP_VERSION} FPM pool configuration for user ${USERNAME} doesn't exist."
echo "Creating new PHP-FPM pool '${USERNAME}' configuration..."
info "PHP ${PHP_VERSION} FPM pool configuration for user '${USERNAME}' does not exist."
echo "Creating new PHP ${PHP_VERSION} FPM pool configuration for '${USERNAME}'..."

# Create PHP FPM pool conf.
create_fpm_pool_conf "${USERNAME}" "${PHP_VERSION}" > "/etc/php/${PHP_VERSION}/fpm/pool.d/${USERNAME}.conf"
Expand All @@ -969,25 +969,23 @@ function init_lemper_create() {
run chown -hR "${USERNAME}:${USERNAME}" "/home/${USERNAME}/.lemper" "/home/${USERNAME}/cgi-bin" "/home/${USERNAME}/logs"

# Restart PHP FPM.
echo "Restart php${PHP_VERSION}-fpm configuration..."

echo "Restarting php${PHP_VERSION}-fpm configuration..."
run systemctl restart "php${PHP_VERSION}-fpm"

success "New php${PHP_VERSION}-fpm pool [${USERNAME}] has been created."
success "PHP ${PHP_VERSION} FPM pool '[${USERNAME}]' has been created."
fi
else
fail "Oops, PHP ${PHP_VERSION} runtime not found. Please install it first! Aborting..."
fail "PHP ${PHP_VERSION} is not installed. Install it before proceeding. Aborting..."
fi

# Check web root parameter.
if [[ -z "${WEBROOT}" ]]; then
WEBROOT="/home/${USERNAME}/webapps/${SERVERNAME}"
info "Webroot parameter is empty. Set to default web root '${WEBROOT}'."
info "Web root path parameter is empty. Using default path: '${WEBROOT}'."
fi

# Creates document root.
if [[ ! -d "${WEBROOT}" ]]; then
echo "Creating web root directory '${WEBROOT}'..."
echo "Creating web root directory: '${WEBROOT}'."

run mkdir -p "${WEBROOT}" && \
run chown -hR "${USERNAME}:${USERNAME}" "${WEBROOT}" && \
Expand All @@ -1006,10 +1004,10 @@ function init_lemper_create() {
# Check framework parameter.
if [[ -z "${FRAMEWORK}" ]]; then
FRAMEWORK="default"
info "Framework parameter is empty. Set to default framework '${FRAMEWORK}'."
info "Framework parameter is empty. Using default: '${FRAMEWORK}'."
fi

echo "Selecting '${FRAMEWORK^}' framework..."
echo "Configuring '${FRAMEWORK^}' framework..."

# Ugly hacks for custom framework-specific configs + Skeleton auto installer.
case "${FRAMEWORK}" in
Expand Down
51 changes: 39 additions & 12 deletions lib/lemper-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ These are common ${CMD_PARENT} ${CMD_NAME} account subcommands used in various s
For help with each command run:
${CMD_PARENT} ${CMD_NAME} account <command> -h|--help
EOL

exit 0
}

function cmd_account_version() {
echo "${CMD_PARENT} ${CMD_NAME} account version ${PROG_VERSION}"
exit 0
}

# Grant access privileges.
Expand All @@ -105,10 +108,11 @@ function cmd_account_access() {
if "${MYSQLCLI}" -u root -p"${MYSQL_ROOT_PASSWORD}" -e "SHOW DATABASES;" | grep -qwE "${DBNAME}"; then
echo "Grants database '${DBNAME}' privileges to '${DBUSER}'@'${DBHOST}'"
run "${MYSQLCLI}" -u root -p"${MYSQL_ROOT_PASSWORD}" -e "GRANT ${DBPRIVILEGES} ON ${DBNAME}.* TO '${DBUSER}'@'${DBHOST}'; FLUSH PRIVILEGES;"
exit 0
else
fail "The specified database '${DBNAME}' does not exist."
fi

exit 0
}

# Creates a new account.
Expand All @@ -127,12 +131,17 @@ function cmd_account_create() {

if "${MYSQLCLI}" -u root -p"${MYSQL_ROOT_PASSWORD}" -e "SELECT User FROM mysql.user WHERE user='${DBUSER}';" | grep -qwE "${DBUSER}"; then
success "MySQL account ${DBUSER} has been created."
[[ ${VERBOSE} == true ]] && echo -e "Below the account details:\nUsername: ${DBUSER}\nPassword: ${DBPASS}\nHost: ${DBHOST}"

if [[ ${VERBOSE} == true ]]; then
echo -e "Below the account details:\nUsername: ${DBUSER}\nPassword: ${DBPASS}\nHost: ${DBHOST}"
fi
fi
fi
else
fail "Root user is already exist. Please use another one!"
fi

exit 0
}

# Deletes an existing account.
Expand All @@ -156,6 +165,8 @@ function cmd_account_delete() {
info "SQL query: \"${SQL_QUERY}\""
fi
fi

exit 0
}

# Update password.
Expand Down Expand Up @@ -183,6 +194,8 @@ function cmd_account_passwd() {
else
info "SQL query: \"${SQL_QUERY}\""
fi

exit 0
}

# Rename an existing account.
Expand Down Expand Up @@ -216,6 +229,8 @@ function cmd_account_rename() {
else
info "SQL query: \"${SQL_QUERY}\""
fi

exit 0
}

# List all database users
Expand All @@ -227,6 +242,8 @@ function cmd_account_users() {
echo "List all existing database users."

run "${MYSQLCLI}" -u "${DBUSER}" -p"${DBPASS}" -e "SELECT user,host FROM mysql.user;"

exit 0
}

# Aliases to create.
Expand Down Expand Up @@ -255,11 +272,9 @@ function init_cmd_account() {
case "${SUBCOMMAND}" in
help | -h | --help)
cmd_account_help
exit 0
;;
version | -v | --version)
cmd_account_version
exit 0
;;
*)
if declare -F "cmd_account_${SUBCOMMAND}" &>/dev/null; then
Expand Down Expand Up @@ -473,11 +488,12 @@ function db_operations() {
run "${MYSQLCLI}" -u root -p"${DBPASS}" -e "${SQL_QUERY}"

if "${MYSQLCLI}" -u root -p"${DBPASS}" -e "SHOW DATABASES LIKE '${DBNAME}';" | grep -qwE "${DBNAME}"; then
success "MySQL database '${DBNAME}' has been created."
exit 0
success "MySQL database '${DBNAME}' has been created and granted to '${DBUSER}'@'${DBHOST}'."
else
fail "Failed creating database '${DBNAME}'."
fi

exit 0
;;

# List all databases.
Expand All @@ -500,7 +516,7 @@ function db_operations() {
DBS=(${DATABASES})
IFS=${SAVEIFS} # Restore IFS

echo "There are ${#DBS[@]} databases granted to '${DBUSER}'."
success "There are ${#DBS[@]} databases granted to '${DBUSER}'."
echo "+-------------------------------------+"
echo "| 'database'@'host' |"
echo "+-------------------------------------+"
Expand All @@ -513,8 +529,10 @@ function db_operations() {

echo "+-------------------------------------+"
else
echo "No database found."
info "There is no database granted to '${DBUSER}'."
fi

exit 0
;;

# Drope / delete database.
Expand All @@ -535,11 +553,13 @@ function db_operations() {
if ! "${MYSQLCLI}" -u root -p"${DBPASS}" -e "SHOW DATABASES LIKE '${DBNAME}';" | grep -qwE "${DBNAME}"; then
success "Database '${DBNAME}' has been dropped."
else
fail "Failed deleting database '${DBNAME}'."
fail "Failed deropping database '${DBNAME}'."
fi
else
fail "The specified database '${DBNAME}' does not exist."
fi

exit 0
;;

# Export / dump database to file.
Expand Down Expand Up @@ -572,6 +592,8 @@ function db_operations() {
else
fial "Mysqldump is required to export database, but it is not available in your current stack. Please install it first."
fi

exit 0
;;

# Import database from file.
Expand All @@ -590,13 +612,15 @@ function db_operations() {

if "${MYSQLCLI}" -u "${DBUSER}" -p"${DBPASS}" -e "SHOW DATABASES;" | grep -qwE "${DBNAME}"; then
run "${MYSQLCLI}" -u "${DBUSER}" -p"${DBPASS}" "${DBNAME}" < "${DBFILE}"
echo "Database file '${DBFILE}' has been successfully imported to '${DBNAME}'."
success "Database file '${DBFILE}' has been successfully imported to '${DBNAME}'."
else
fail "The specified database '${DBNAME}' does not exist."
fi
else
fail "Please specifiy the database file (.sql) to import using --dbfile parameter."
fi

exit 0
;;

# Perform SQL query.
Expand All @@ -622,6 +646,8 @@ function db_operations() {
else
info "SQL query: \"${SQL_QUERY}\""
fi

exit 0
;;

*)
Expand Down Expand Up @@ -797,10 +823,13 @@ Example:
For help with each command run:
${CMD_PARENT} ${CMD_NAME} <command> -h|--help
EOL

exit 0
}

function cmd_version() {
echo "${CMD_PARENT} ${CMD_NAME} version ${PROG_VERSION}"
exit 0
}

##
Expand All @@ -815,11 +844,9 @@ function init_lemper_db() {
case ${SUBCMD} in
help | -h | --help)
cmd_help
exit 0
;;
version | -v | --version)
cmd_version
exit 0
;;
*)
if declare -F "cmd_${SUBCMD}" &>/dev/null; then
Expand Down

0 comments on commit 5541053

Please sign in to comment.