#!/bin/sh #----------------------------------------------------------------------------- # /var/install/bin/openvz-edit-vm.cui - edit VE # # Last Update: $Id:$ # # Copyright (c) 2006-2009 the eisfair team # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #----------------------------------------------------------------------------- selvznum="$3" . /var/install/include/cuilib . /var/install/include/cuivmlib #============================================================================ # global constants #============================================================================ IDC_VMDLG_BUTOK='10' # dlg OK button ID IDC_VMDLG_BUTCANCEL='11' # dlg Cancel button ID IDC_VMDLG_LABEL1='12' # dlg label ID IDC_VMDLG_LABEL2='13' # dlg label ID IDC_VMDLG_LABEL3='14' # dlg label ID IDC_VMDLG_LABEL4='15' # dlg label ID IDC_VMDLG_LABEL5='16' # dlg label ID IDC_VMDLG_LABEL6='17' # dlg label ID IDC_VMDLG_LABEL7='18' # dlg label ID IDC_VMDLG_EDHOSTNAME='19' # dlg edit ID IDC_VMDLG_EDDOMAIN='20' # dlg edit ID IDC_VMDLG_EDPASSWD1='21' # dlg edit ID IDC_VMDLG_EDPASSWD2='22' # dlg edit ID IDC_VMDLG_EDQUOTA='23' # dlg edit ID IDC_VMDLG_EDIP='24' # dlg edit ID IDC_VMDLG_EDDNS='25' # dlg edit ID IDC_VMDLG_CHKBOOTRUN='26' # dlg edit ID #============================================================================ # VM edit/create dialog #============================================================================ #---------------------------------------------------------------------------- # vmdlg_ok_clicked # Ok button clicked hook # expects: $1 : window handle of dialog window # $2 : button control id # returns: 1 : event handled #---------------------------------------------------------------------------- function vmdlg_ok_clicked() { local win="$p2" local ctrl cui_window_getctrl "$win" "$IDC_VMDLG_EDHOSTNAME" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_hostname="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDDOMAIN" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_domain="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDPASSWD1" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_passwd1="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDPASSWD2" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_passwd2="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDQUOTA" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_quota="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDIP" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_ip="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDDNS" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_dns="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_CHKBOOTRUN" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_checkbox_getcheck "$ctrl" && vmdlg_bootrun="$p2" fi if [ -n "${vmdlg_quota}" ] then if ! cuivm_check_is_quota "${vmdlg_quota}" then cui_message "$win" "Soft/hardquota: value not correct! Please enter: 18G:20G " \ "Invalid data" "$MB_ERROR" cui_return 1 return fi fi if [ -n "${vmdlg_ip}" ] then if ! cuivm_check_is_ipaddress "${vmdlg_ip}" then cui_message "$win" "IP address: no valid ip address!" \ "Invalid data" "$MB_ERROR" cui_return 1 return fi fi if [ -n "${vmdlg_dns}" ] then if ! cuivm_check_is_ipaddress "${vmdlg_dns}" then cui_message "$win" "DNS server: no valid ip address!" \ "Invalid data" "$MB_ERROR" cui_return 1 return fi fi if [ "${vmdlg_passwd1}" != "${vmdlg_passwd2}" ] then cui_message "$win" "Passwords do not match. Please reenter passwords." \ "Missing data" "$MB_ERROR" cui_return 1 return fi cui_window_close "$win" "$IDOK" cui_return 1 } #---------------------------------------------------------------------------- # vmdlg_cancel_clicked # Cancel button clicked hook # expects: $1 : window handle of dialog window # $2 : button control id # returns: 1 : event handled #---------------------------------------------------------------------------- function vmdlg_cancel_clicked() { cui_window_close "$p2" "$IDCANCEL" cui_return 1 } #---------------------------------------------------------------------------- # vmdlg_create_hook # Dialog create hook - create dialog controls # expects: $1 : window handle of dialog window # returns: 1 : event handled #---------------------------------------------------------------------------- function vmdlg_create_hook() { local dlg="$p2" local ctrl if cui_label_new "$dlg" "Host name:" 2 1 14 1 "$IDC_VMDLG_LABEL1" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "Domain:" 2 3 14 1 "$IDC_VMDLG_LABEL2" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "Root password:" 2 5 14 1 "$IDC_VMDLG_LABEL3" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "Retype passwd.:" 2 7 14 1 "$IDC_VMDLG_LABEL4" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "Soft/hardquota:" 2 9 14 1 "$IDC_VMDLG_LABEL5" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "IP address:" 2 11 14 1 "$IDC_VMDLG_LABEL6" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "DNS server:" 2 13 14 1 "$IDC_VMDLG_LABEL7" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi cui_edit_new "$dlg" "" 17 1 25 1 255 "$IDC_VMDLG_EDHOSTNAME" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_hostname}" fi cui_edit_new "$dlg" "" 17 3 25 1 255 "$IDC_VMDLG_EDDOMAIN" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_domain}" fi cui_edit_new "$dlg" "" 17 5 25 1 255 "$IDC_VMDLG_EDPASSWD1" "$EF_PASSWORD" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_passwd1}" fi cui_edit_new "$dlg" "" 17 7 25 1 255 "$IDC_VMDLG_EDPASSWD2" "$EF_PASSWORD" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_passwd2}" fi cui_edit_new "$dlg" "" 17 9 25 1 255 "$IDC_VMDLG_EDQUOTA" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_quota}" fi cui_edit_new "$dlg" "" 17 11 25 1 255 "$IDC_VMDLG_EDIP" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_ip}" fi cui_edit_new "$dlg" "" 17 13 25 1 255 "$IDC_VMDLG_EDDNS" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_dns}" fi cui_checkbox_new "$dlg" "boots after a reboot" 17 15 24 1 "$IDC_VMDLG_CHKBOOTRUN" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_checkbox_setcheck "$ctrl" "${vmdlg_bootrun}" fi cui_button_new "$dlg" "&OK" 11 17 10 1 $IDC_VMDLG_BUTOK $CWS_DEFOK $CWS_NONE && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_button_callback "$ctrl" "$BUTTON_CLICKED" "$dlg" vmdlg_ok_clicked cui_window_create "$ctrl" fi cui_button_new "$dlg" "&Cancel" 22 17 10 1 $IDC_VMDLG_BUTCANCEL $CWS_DEFCANCEL $CWS_NONE && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_button_callback "$ctrl" "$BUTTON_CLICKED" "$dlg" vmdlg_cancel_clicked cui_window_create "$ctrl" fi cui_return 1 } #============================================================================ # invoke VM dialog due to key or menu selection #============================================================================ #---------------------------------------------------------------------------- # vm_edit_dialog # Modify an existing VM # returns: 0 : created (reload data) # 1 : not modified (don't reload data) #---------------------------------------------------------------------------- function vm_edit_dialog() { local win="$1" local dlg local result="$IDCANCEL" local ctrl local vznum="$selvznum" local scriptfile local oldip local olddns local softq local hardq if [ -f "/etc/vz/conf/${vznum}.conf" ] then . /etc/vz/conf/${vznum}.conf fi vmdlg_hostname="$HOSTNAME" vmdlg_domain="$SEARCHDOMAIN" softq=`echo $DISKSPACE | awk -F: '{print $1 / 1048576}'` hardq=`echo $DISKSPACE | awk -F: '{print $2 / 1048576}'` vmdlg_quota="${softq}G:${hardq}G" softq="$vmdlg_quota" vmdlg_ip="`echo $IP_ADDRESS | awk '{print $1}'`" vmdlg_dns="`echo $NAMESERVER | awk '{print $1}'`" oldip=$vmdlg_ip olddns=$vmdlg_dns vmdlg_bootrun="0" [ "$ONBOOT" = "yes" ] && vmdlg_bootrun="1" vmdlg_passwd1="" vmdlg_passwd2="" cui_window_new "$win" 0 0 46 20 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED] && dlg="$p2" if cuivm_valid_handle $dlg then cui_window_setcolors "$dlg" "DIALOG" cui_window_settext "$dlg" "Edit VE $vznum $vmdlg_hostname" cui_window_sethook "$dlg" "$HOOK_CREATE" vmdlg_create_hook cui_window_create "$dlg" cui_window_modal "$dlg" && result="$p2" if [ "$result" == "$IDOK" ] then cui_window_destroy "$dlg" scriptfile="/var/lib/vz/edit-${vznum}.sh" cat > ${scriptfile} << EOF #!/bin/sh echo "" EOF [ -n "$vmdlg_hostname" -a "$vmdlg_hostname" != "$HOSTNAME" ] && echo "echo '* Set hostname:' && vzctl set $vznum --hostname ${vmdlg_hostname} --save" >> ${scriptfile} [ -n "$vmdlg_domain" -a "$vmdlg_domain" != "$SEARCHDOMAIN" ] && echo "echo '* Set domain:' && vzctl set $vznum --searchdomain ${vmdlg_domain} --save" >> ${scriptfile} if [ -n "$vmdlg_ip" -a "$vmdlg_ip" != "$oldip" ] then echo "echo '* Change IP address:'" >> ${scriptfile} echo "vzctl set $vznum --ipdel ${IP_ADDRESS} --ipadd ${vmdlg_ip} --save " >> ${scriptfile} fi [ -n "$vmdlg_dns" -a "$vmdlg_dns" != "$olddns" ] && echo "echo '* Set nameserver:' && vzctl set $vznum --nameserver ${vmdlg_dns} --save" >> ${scriptfile} [ -n "$vmdlg_quota" -a "$vmdlg_quota" != "$softq" ] && echo "echo '* Set new quota:' && vzctl set $vznum --diskspace ${vmdlg_quota} --save" >> ${scriptfile} if [ "$vmdlg_bootrun" = "1" ] then [ "$ONBOOT" = "no" ] && echo "echo '* Start on boot:' && vzctl set $vznum --onboot yes --save " >> ${scriptfile} else [ "$ONBOOT" = "yes" ] && echo "echo '* No start on boot:' && vzctl set $vznum --onboot no --save " >> ${scriptfile} fi if [ -n "$vmdlg_passwd2" ] then echo "echo '* Change root password:' && vzctl set $vznum --userpasswd root:$vmdlg_passwd2 --save" >> ${scriptfile} fi echo "sleep 2" >> ${scriptfile} echo "rm -f ${scriptfile}" >> ${scriptfile} chmod 0750 ${scriptfile} cuivm_create_terminal "$mainwin" "${scriptfile}" "Edit VE" else cui_window_destroy "$dlg" fi fi [ "$result" == "$IDOK" ] return "$?" } #============================================================================ # main window hooks #============================================================================ #---------------------------------------------------------------------------- # init routine (entry point of all shellrun.cui based programs) # $p2 --> desktop window handle #---------------------------------------------------------------------------- function init() { local win="$p2" # setup main window cui_window_new "$win" 0 0 0 0 $[$CWS_POPUP + $CWS_CAPTION + $CWS_STATUSBAR + $CWS_MAXIMIZED] && mainwin="$p2" if cuivm_valid_handle $mainwin then cui_window_setcolors "$mainwin" "DESKTOP" cui_window_settext "$mainwin" "OpenVZ VE administration " cui_window_setrstatustext "$mainwin" "V1.0.0" cui_window_create "$mainwin" vm_edit_dialog $mainwin cui_window_quit 0 fi cui_return 0 } #---------------------------------------------------------------------------- # main routines (always at the bottom of the file) #---------------------------------------------------------------------------- cui_init cui_run exit 0