From 9a1fde8cb3e196781ba82435dc80bb847f45ac09 Mon Sep 17 00:00:00 2001 From: Serghey Rodin Date: Mon, 30 Jul 2012 22:48:54 +0300 Subject: [PATCH] add user package --- bin/v_add_user | 8 +- bin/v_add_user_package | 74 ++++++++++ bin/v_change_user_package | 3 +- bin/v_list_user_package | 92 ++++++++++++ func/main.sh | 5 +- web/add/package/index.php | 165 ++++++++++++++++++++++ web/list/package/index.php | 28 ++++ web/templates/admin/add_package.html | 82 +++++++++++ web/templates/admin/list_packages.html | 5 +- web/templates/admin/menu_add_package.html | 15 ++ web/templates/admin/menu_packages.html | 2 +- web/templates/admin/panel.html | 2 +- 12 files changed, 470 insertions(+), 11 deletions(-) create mode 100755 bin/v_add_user_package create mode 100755 bin/v_list_user_package create mode 100644 web/add/package/index.php create mode 100644 web/list/package/index.php create mode 100644 web/templates/admin/add_package.html create mode 100644 web/templates/admin/menu_add_package.html diff --git a/bin/v_add_user b/bin/v_add_user index b11f6ccf0..e8121f54d 100755 --- a/bin/v_add_user +++ b/bin/v_add_user @@ -55,10 +55,10 @@ is_package_valid "$package" #----------------------------------------------------------# # Parsing package data -package_data=$(cat $VESTA/data/packages/$package.pkg) +pkg_data=$(cat $VESTA/data/packages/$package.pkg |grep -v TIME |grep -v DATE) # Checking shell -shell_conf=$(echo "$package_data" | grep 'SHELL' | cut -f 2 -d \') +shell_conf=$(echo "$pkg_data" | grep 'SHELL' | cut -f 2 -d \') shell=$(/usr/bin/chsh --list-shells | grep -w "$shell_conf" |head -n1) # Adding user @@ -98,7 +98,7 @@ chmod -R a+x $HOMEDIR/$user # Checking quota if [ ! -z "$DISK_QUOTA" ] && [ "$DISK_QUOTA" != 'no' ]; then - DISK_QUOTA=$(echo "$package_data" | grep 'DISK_QUOTA' | cut -f 2 -d \') + DISK_QUOTA=$(echo "$pkg_data" | grep 'DISK_QUOTA' | cut -f 2 -d \') #$BIN/v_add_user_quota "$user" "$DISK_QUOTA" fi @@ -169,7 +169,7 @@ fi echo "FNAME='$fname' LNAME='$lname' PACKAGE='$package' -$package_data +$pkg_data CONTACT='$email' CRON_REPORTS='yes' MD5='$(awk -v user=$user -F : 'user == $1 {print $2}' /etc/shadow)' diff --git a/bin/v_add_user_package b/bin/v_add_user_package new file mode 100755 index 000000000..f5376a2c6 --- /dev/null +++ b/bin/v_add_user_package @@ -0,0 +1,74 @@ +#!/bin/bash +# info: adding user package +# options: pkg_dir package [rewrite] +# +# The function adds new user package to the system. + + +#----------------------------------------------------------# +# Variable&Function # +#----------------------------------------------------------# + +# Argument defenition +pkg_dir=$1 +package=$2 +rewrite=$3 + +# Includes +source $VESTA/conf/vesta.conf +source $VESTA/func/main.sh + +# Functions +is_package_new() { + if [ -e "$VESTA/data/packages/$package.pkg" ]; then + echo "Error: package $package already exists." + log_event "$E_EXISTS" "$EVENT" + exit $E_EXISTS + fi +} + +is_package_consistent() { + source $pkg_dir/$package.pkg + validate_format_int $WEB_DOMAINS + validate_format_int $WEB_ALIASES + validate_format_int $DNS_DOMAINS + validate_format_int $DNS_RECORDS + validate_format_int $MAIL_DOMAINS + validate_format_int $MAIL_ACCOUNTS + validate_format_int $DATABASES + validate_format_int $CRON_JOBS + validate_format_int $DISK_QUOTA + validate_format_int $BACKUPS + validate_format_shell $SHELL +} + + +#----------------------------------------------------------# +# Verifications # +#----------------------------------------------------------# + +check_args '2' "$#" 'pkg_dir package' 'rewrite' +validate_format 'pkg_dir' 'package' +if [ "$rewrite" != 'yes' ]; then + is_package_new +fi +is_package_valid "$pkg_dir" +is_package_consistent + + +#----------------------------------------------------------# +# Action # +#----------------------------------------------------------# + +cp -f $pkg_dir/$package.pkg $VESTA/data/packages/ +chmod 644 $VESTA/data/packages/$package.pkg + + +#----------------------------------------------------------# +# Vesta # +#----------------------------------------------------------# + +# Logging +log_event "$OK" "$EVENT" + +exit diff --git a/bin/v_change_user_package b/bin/v_change_user_package index d21bf5865..70069a165 100755 --- a/bin/v_change_user_package +++ b/bin/v_change_user_package @@ -32,7 +32,8 @@ is_package_avalable() { DISK_QUOTA='0' BANDWIDTH='0' - pkg_data=$(cat $VESTA/data/packages/$package.pkg) + pkg_data=$(cat $VESTA/data/packages/$package.pkg |grep -v TIME |\ + grep -v DATE) eval $pkg_data # Comparing user data with package diff --git a/bin/v_list_user_package b/bin/v_list_user_package new file mode 100755 index 000000000..dd89bbc87 --- /dev/null +++ b/bin/v_list_user_package @@ -0,0 +1,92 @@ +#!/bin/bash +# info: list user package +# options: package [format] +# +# The function for getting the list of system ip parameters. + + +#----------------------------------------------------------# +# Variable&Function # +#----------------------------------------------------------# + +# Argument defenition +package=$1 +PACKAGE=$package +format=${2-shell} + +# Includes +source $VESTA/func/main.sh + +# Json function +json_list_package() { + i=1 + fileds_count=$(echo "$fields" | wc -w) + pkg_data=$(cat $VESTA/data/packages/$PACKAGE.pkg) + echo '{' + eval $pkg_data + for field in $fields; do + eval value=$field + if [ $i -eq 1 ]; then + echo -e "\t\"$value\": {" + else + if [ $fileds_count -eq $i ]; then + echo -e "\t\t\"${field//$/}\": \"${value//,/, }\"" + else + echo -e "\t\t\"${field//$/}\": \"${value//,/, }\"," + fi + fi + (( ++i)) + done + if [ -n "$value" ]; then + echo -e ' }' + fi + echo -e '}' +} + +# Shell function +shell_list_package() { + line=$(cat $VESTA/data/packages/$PACKAGE.pkg) + eval $line + for field in $fields; do + eval key="$field" + if [ -z "$key" ]; then + key='NULL' + fi + echo "${field//$/}: $key " + done +} + + +#----------------------------------------------------------# +# Verifications # +#----------------------------------------------------------# + +# Checking args +check_args '1' "$#" 'package [format]' +validate_format 'package' +is_package_valid + + +#----------------------------------------------------------# +# Action # +#----------------------------------------------------------# + +# Defining fileds to select +fields='$PACKAGE $TEMPLATE $WEB_DOMAINS $WEB_ALIASES $DNS_DOMAINS $DNS_RECORDS + $MAIL_DOMAINS $MAIL_ACCOUNTS $DATABASES $CRON_JOBS $DISK_QUOTA $BANDWIDTH + $NS $SHELL $BACKUPS $TIME $DATE' + +# Listing ip +case $format in + json) json_list_package ;; + plain) shell_list_package ;; + shell) shell_list_package | column -t ;; + *) check_args '1' '0' 'ip [format]' +esac + + +#----------------------------------------------------------# +# Vesta # +#----------------------------------------------------------# + +exit diff --git a/func/main.sh b/func/main.sh index 1553af706..23dc6c104 100755 --- a/func/main.sh +++ b/func/main.sh @@ -121,7 +121,10 @@ gen_password() { # Package existance check is_package_valid() { - if [ ! -e "$VESTA/data/packages/$package.pkg" ]; then + if [ -z "$1" ]; then + pkg_dir="$VESTA/data/packages" + fi + if [ ! -e "$pkg_dir/$package.pkg" ]; then echo "Error: package $package not exist" log_event "$E_NOTEXIST $EVENT" exit $E_NOTEXIST diff --git a/web/add/package/index.php b/web/add/package/index.php new file mode 100644 index 000000000..ff4fd86e6 --- /dev/null +++ b/web/add/package/index.php @@ -0,0 +1,165 @@ + $error) { + if ( $i == 0 ) { + $error_msg = $error; + } else { + $error_msg = $error_msg.", ".$error; + } + } + $_SESSION['error_msg'] = "Error: field ".$error_msg." can not be blank."; + } else { + exec ('mktemp -d', $output, $return_var); + $tmpdir = $output[0]; + unset($output); + + // Create package + $pkg = "TEMPLATE=".$v_template."\n"; + $pkg .= "WEB_DOMAINS=".$v_web_domains."\n"; + $pkg .= "WEB_ALIASES=".$v_web_aliases."\n"; + $pkg .= "DNS_DOMAINS=".$v_dns_domains."\n"; + $pkg .= "DNS_RECORDS=".$v_dns_records."\n"; + $pkg .= "MAIL_DOMAINS=".$v_mail_domains."\n"; + $pkg .= "MAIL_ACCOUNTS=".$v_mail_accounts."\n"; + $pkg .= "DATABASES=".$v_databases."\n"; + $pkg .= "CRON_JOBS=".$v_cron_jobs."\n"; + $pkg .= "DISK_QUOTA=".$v_disk_quota."\n"; + $pkg .= "BANDWIDTH=".$v_bandwidth."\n"; + $pkg .= "NS=".$v_ns."\n"; + $pkg .= "SHELL=".$v_shell."\n"; + $pkg .= "BACKUPS=".$v_backups."\n"; + $pkg .= "TIME=".$v_time."\n"; + $pkg .= "DATE=".$v_date."\n"; + + // Write package + $fp = fopen($tmpdir."/".$_POST['v_package'].".pkg", 'w'); + fwrite($fp, $pkg); + fclose($fp); + + // Add new package + if (empty($_SESSION['error_msg'])) { + exec (VESTA_CMD."v_add_user_package ".$tmpdir." ".$v_package, $output, $return_var); + if ($return_var != 0) { + $error = implode('
', $output); + if (empty($error)) $error = 'Error: vesta did not return any output.'; + $_SESSION['error_msg'] = $error; + } + unset($output); + } + + // Remove tmpdir + exec ('rm -rf '.$tmdir, $output, $return_var); + unset($output); + + // Check output + if (empty($_SESSION['error_msg'])) { + $_SESSION['ok_msg'] = "OK: package ".$_POST['v_package']." has been created successfully."; + unset($v_package); + } + + } + } + + + exec (VESTA_CMD."v_list_web_templates json", $output, $return_var); + check_error($return_var); + $templates = json_decode(implode('', $output), true); + unset($output); + + exec (VESTA_CMD."v_list_sys_shells json", $output, $return_var); + check_error($return_var); + $shells = json_decode(implode('', $output), true); + unset($output); + + // Set default values + if (empty($v_template)) $v_template = 'default'; + if (empty($v_shell)) $v_shell = 'nologin'; + if (empty($v_web_domains)) $v_web_domains = "'0'"; + if (empty($v_web_aliases)) $v_web_aliases = "'0'"; + if (empty($v_dns_domains)) $v_dns_domains = "'0'"; + if (empty($v_dns_records)) $v_dns_records = "'0'"; + if (empty($v_mail_domains)) $v_mail_domains = "'0'"; + if (empty($v_mail_accounts)) $v_mail_accounts = "'0'"; + if (empty($v_databases)) $v_databases = "'0'"; + if (empty($v_cron_jobs)) $v_cron_jobs = "'0'"; + if (empty($v_backups)) $v_backups = "'0'"; + if (empty($v_disk_quota)) $v_disk_quota = "'0'"; + if (empty($v_bandwidth)) $v_bandwidth = "'0'"; + if (empty($v_ns1)) $v_ns1 = 'ns1.example.ltd'; + if (empty($v_ns2)) $v_ns2 = 'ns2.example.ltd'; + + + include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/menu_add_package.html'); + include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/add_package.html'); + unset($_SESSION['error_msg']); + unset($_SESSION['ok_msg']); +} + +// Footer +include($_SERVER['DOCUMENT_ROOT'].'/templates/footer.html'); diff --git a/web/list/package/index.php b/web/list/package/index.php new file mode 100644 index 000000000..2f9824007 --- /dev/null +++ b/web/list/package/index.php @@ -0,0 +1,28 @@ + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
Package Name
Default Template
System Shell
Web Domains
Web Aliases (per domain)
DNS Domains
DNS Records (per domain)
Mail Domains
Mail Accounts (per domain)
Databases
Cron Jobs
Backups
Disk Quota (in Mb)
Bandwidth (in Mb)
Name Servers
+ + + +
+ + + diff --git a/web/templates/admin/list_packages.html b/web/templates/admin/list_packages.html index ef67c90ed..7b1c9186c 100644 --- a/web/templates/admin/list_packages.html +++ b/web/templates/admin/list_packages.html @@ -37,7 +37,6 @@ foreach ($data as $key => $value) { - +
+
propagate edit @@ -58,9 +57,9 @@ foreach ($data as $key => $value) {
Bandwidth: -
Disk: -
diff --git a/web/templates/admin/menu_add_package.html b/web/templates/admin/menu_add_package.html new file mode 100644 index 000000000..b87a66545 --- /dev/null +++ b/web/templates/admin/menu_add_package.html @@ -0,0 +1,15 @@ + + + + + diff --git a/web/templates/admin/menu_packages.html b/web/templates/admin/menu_packages.html index 7689b385a..ac35a82fd 100644 --- a/web/templates/admin/menu_packages.html +++ b/web/templates/admin/menu_packages.html @@ -1,7 +1,7 @@