Permalink
Browse files

create vhost: allow options on cmd line and tar ball file

added command line options and reading configuration options from the tar
ball (file .devpanel/app.ini inside the tar ball) to remove hardcoded
assumptions on subsystem type and database name.
  • Loading branch information...
vmmello committed Jan 4, 2019
1 parent a8eda36 commit 2ca61d0cff02c2dc84740d44d1fccbc0efbb74cb
Showing with 175 additions and 26 deletions.
  1. +17 −5 lib/cli/sbin/create-lamp-vhost
  2. +72 −16 lib/cli/sbin/create-vhost
  3. +71 −0 lib/functions
  4. +15 −0 libexec/archive-vhost
  5. +0 −5 libexec/archive-vhost-web
@@ -8,11 +8,13 @@ set_usage_msg \
--domain domain.tld add the specified domain (can be used more than
once to add more than one domain)
--mysql-instance name the mysql instance to use
--mysql-dbs name of the mysql databases to create
--subsystem name name of the app subsystem (e.g. drupal,
wordpress, etc)
--mysql-instance name the mysql instance to use
--mysql-dbs name of the mysql databases to create
--db-name name the name of the main database of the app
--php-version <version> the PHP version to use on this vhost (e.g.
5.6, 7.1)
@@ -31,7 +33,7 @@ devpanel_cmd__create_lamp_vhost() {
[ $# -eq 0 -o -z "$1" ] && usage

local activate_now=yes
local name vhost subsystem php_version description
local name vhost subsystem php_version description db_name
local -a domains_ar apache_args_ar opts_ar

while [ -n "$1" ]; do
@@ -71,6 +73,12 @@ devpanel_cmd__create_lamp_vhost() {
shift 2
;;

--db-name)
[ -z "$2" ] && { error_missing_value "$name" ; return $?; }
db_name="$2"
shift 2
;;

--dont-activate-now)
unset activate_now
shift
@@ -115,8 +123,12 @@ devpanel_cmd__create_lamp_vhost() {
fi

if [ -n "$subsystem" ]; then
opts_ar+=( "app.subsystem = $subsystem" )
opts_ar+=( "app.database_name = $subsystem" )
if [ -z "$db_name" ]; then
db_name="$subsystem"
fi

opts_ar+=( "app.subsystem = $subsystem" )
opts_ar+=( "app.database_name = $db_name" )
fi

if [ -n "$description" ]; then
@@ -25,6 +25,11 @@ set_usage_msg \
--subsystem <name> name of the app subsystem (e.g. drupal,
wordpress, etc)
--db-name name name of the main database of the app
--dont-config-app only extract the files, don't try to configure
the app internals
--config-function name name of the config function to run to setup
the app
@@ -39,9 +44,9 @@ devpanel_cmd__create_vhost() {
[ $# -eq 0 -o -z "$1" ] && usage

local name vhost from source_type title desc
local branch app_password read_pw_stdin php_version subsystem
local branch app_password read_pw_stdin php_version subsystem db_name
local source_type publisher uri_part app_url
local print_summary=yes
local print_summary=yes do_config_app=yes
local -a cmd_args=() lamp_cmd_args=() app_cmd_ar=()

while [ -n "$1" ]; do
@@ -68,7 +73,6 @@ devpanel_cmd__create_vhost() {
--php-version)
[ -z "$2" ] && error_missing_value "$name"
php_version="$2"
lamp_cmd_args+=( "$name" "$php_version" )
shift 2
;;

@@ -105,6 +109,12 @@ devpanel_cmd__create_vhost() {
shift 2
;;

--db-name)
[ -z "$2" ] && error_missing_value "$name"
db_name="$2"
shift 2
;;

--config-function|--install-profile)
[ -z "$2" ] && error_missing_value "$name"
if [[ "$2" =~ ^[A-Za-z0-9_-]+$ ]]; then
@@ -116,6 +126,11 @@ devpanel_cmd__create_vhost() {
shift 2
;;

--dont-config-app)
unset do_config_app
shift
;;

--[A-Za-z0-9_-]*)
error "unknown argument $name"
;;
@@ -248,10 +263,44 @@ devpanel_cmd__create_vhost() {
esac
fi # // if [ -n $from ]

if [ -z "$subsystem" ]; then
# guess app type from tar ball when --subsystem not specified
subsystem=$(guess_subsystem_from_app_archive "$src_archive" ) || \
return $?
read_devpanel_ini_from_app_archive --missing-ok "$src_archive" || \
return $?

if [ -n "$subsystem" ]; then
src__app__subsystem="$subsystem"
else
if [ -n "$src__app__subsystem" ]; then
subsystem="$src__app__subsystem"
else
# guess app type from tar ball when --subsystem not specified
if ! subsystem=$(guess_subsystem_from_app_archive "$src_archive" ); then
error "couldn't detect the subsystem of the app. Please specify it on command line." -
return $?
fi
fi
fi

if is_known_seedapp_subsystem "$subsystem"; then
lamp_cmd_args+=( --subsystem "$subsystem" )
else
error "got an unknown app subsystem: $subsystem" -
return $?
fi

if [ -n "$php_version" ]; then
src__php__version="$php_version"
lamp_cmd_args+=( --php-version "$php_version" )
elif [ -n "$src__php__version" ]; then
php_version="$src__php__version"
lamp_cmd_args+=( --php-version "$php_version" )
fi

if [ -n "$db_name" ]; then
lamp_cmd_args+=( --db-name "$db_name" )
src__app__database_name="$db_name"
elif [ -n "$src__app__database_name" ]; then
db_name="$src__app__database_name"
lamp_cmd_args+=( --db-name "$db_name" )
fi

if ! devpanel_run create lamp vhost "${lamp_cmd_args[@]}" --vhost "$vhost" \
@@ -267,18 +316,20 @@ devpanel_cmd__create_vhost() {
return $?
fi

app_cmd_ar+=( --first-setup )
devpanel_run config app "${app_cmd_ar[@]}" --vhost "$vhost" || return $?
if [ -n "$do_config_app" ]; then
app_cmd_ar+=( --first-setup )
devpanel_run config app "${app_cmd_ar[@]}" --vhost "$vhost" || return $?

if ! load_vhost_config "$vhost"; then
echo "Warning: unable to load config for the newly created vhost" 1>&2
if [ -n "$app_password" ]; then
echo "$app_password" | devpanel_run reset app password --vhost "$vhost"
if [ $? -ne 0 ]; then
echo "Warning: failed setting the password for app..." 1>&2
fi
fi
fi

if [ -n "$app_password" ]; then
echo "$app_password" | devpanel_run reset app password --vhost "$vhost"
if [ $? -ne 0 ]; then
echo "Warning: failed setting the password for app..." 1>&2
fi
if ! load_vhost_config "$vhost"; then
echo "Warning: unable to load config for the newly created vhost" 1>&2
fi

if ! devpanel_run rewrite vhost config --vhost "$vhost"; then
@@ -288,5 +339,10 @@ devpanel_cmd__create_vhost() {

[ -n "$print_summary" ] && print_vhost_summary "$vhost"

if [ -z "$do_config_app" ]; then
echo "NOTE: as requested, the app internal config was not initialized."
echo
fi

echo "Successfully created vhost $vhost"
}
@@ -2785,6 +2785,77 @@ guess_subsystem_from_app_archive() {
fi
}

read_devpanel_ini_from_app_archive() {
local opt_name
local missing_ok
while [ -n "$1" -a "${1:0:1}" == - ]; do
opt_name="$1"
case $opt_name in
--missing-ok)
missing_ok=1
shift
;;

*)
echo "$FUNCNAME(): unknown option '$opt_name'" 1>&2
return 1
;;
esac
done


local archive="$1"
local tmp_dir subsystem _sub_dir _tmp_subsystem
local ini_file tst_file_1 tst_file_2 _tst_file
local has_ini_file

if [ ! -e "$archive" ]; then
echo "$FUNCNAME(): path '$archive' doesn't exist" 1>&2
return 1
elif [ ! -f "$archive" ]; then
echo "$FUNCNAME(): path '$archive' is not a regular file" 1>&2
return 1
fi

tst_file_1="./.devpanel/app.ini"
tst_file_2=".devpanel/app.ini"
for _tst_file in "$tst_file_1" "$tst_file_2"; do
if tar -ztf "$archive" --no-recursion "$_tst_file" &>/dev/null; then
has_ini_file=yes

if ! tmp_dir=$(mktemp -d); then
echo "$FUNCNAME(): unable to create temp dir" 1>&2
return 1
fi

if tar -zxf "$archive" -C "$tmp_dir" "$_tst_file"; then
ini_file="$tmp_dir/${_tst_file#./}"
break
else
rm -rf "$tmp_dir"
echo "$FUNCNAME(): failed to extract '$_tst_file' from '$archive'" 1>&2
return 1
fi
fi
done

if [ -z "$has_ini_file" ]; then
# archive doesn't have an ini file
if [ -n "$missing_ok" ]; then
return 0
else
return 1
fi
fi

if ! read_ini_file_into_namespace "$ini_file" src; then
echo "$FUNCNAME(): failed to read from '$ini_file'" 1>&2
return 1
fi

return 0
}

translate_dayname_to_number() {
local name="${1,,}"
local n
@@ -362,6 +362,21 @@ if [ -n "$v__mysql__instance" ]; then
fi
fi

# write devpanel.ini with the app metadata
for var in app__database_name app__subsystem php__version; do
_real_var="v__$var"
if [ -n "${!_real_var}" ]; then
opts_ar+=( "${var//__/.} = ${!_real_var}" )
fi
done
unset var _real_var

if [ -n "${opts_ar[*]}" ]; then
tmp_app_ini_file="$tmp_archive_dir/.devpanel/app.ini"
mkdir -p "${tmp_app_ini_file%/*}"
write_ini_file "$tmp_app_ini_file" "${opts_ar[@]}"
fi

echo "Starting final tar ball archiving..." 1>&2
base_tmp_target_file=$(basename "$tmp_target_file")
tar -C "$tmp_archive_dir" --exclude="$base_tmp_target_file" \
@@ -161,11 +161,6 @@ if [ -f "$virtwww_file" ]; then
fi
fi

if php_ver=$(get_php_version_from_vhost "$vhost" 2>/dev/null ); then
echo "php.version = $php_ver" | \
"$sys_dir/bin/update-ini-file" -q -c "$archive_dir/devpanel.ini"
fi

echo "Archiving $logs_dir to $archive_logs"
if ! run mkdir -p "$archive_dir"; then
echo "Error: unable to create dir '$archive_dir'" 1>&2

0 comments on commit 2ca61d0

Please sign in to comment.