Search This Blog

Wednesday, November 23, 2011

Xe script to shutdown a Xen server and vm guests tested with APC software

Xe script to shutdown a Xen server and vm guests tested with APC software

home made one - tested by tm to be working

"C:\program files\citrix\xencenter\xe" -s 192.168.1.90 -u root -pw mypw vm-shutdown vm="termserv"
"C:\program files\citrix\xencenter\xe" -s 192.168.1.90 -u root -pw mypw vm-shutdown vm="myvoomlumberPDC"
"C:\program files\citrix\xencenter\xe" -s 192.168.1.90 -u root -pw mypw host-disable host="myvoomLUMBER-XEN01"
"C:\program files\citrix\xencenter\xe" -s 192.168.1.90 -u root -pw mypw host-shutdown host="myvoomLUMBER-XEN01"


the one need customize
::
:: should have customize it , and test it
:: its a xen-shutdown.cmd to work with apc shutdown script
:: like xen username / password ; server IP address
::

::
:: Xen shutdown script
::
:: Jason Poyner
:: jason.poyner@deptive.co.nz
:: 8 April 2009
:: v0.5
::
@echo off
:: Configure these variable for your environment
:: ---------------------------------------------------
set xe=C:\Program Files\Citrix\XenCenter\xe.exe
set xuser=root
set xpwd=
:: Set the xmaster variable to the host you expect to be the pool master
:: The script will attempt to connect to this host first and if it is not the pool master
:: the pool master will be retrieved.
set xmaster=
set xmaster_backup=
::  Specify the delay after shutting down the VMs, before shutting down the hosts
set xshutdowndelay=120000
set logfile=XenShutdownError.log
:: ---------------------------------------------------


:GetMaster
:: Get pool master
"%xe%" -s %xmaster% -u %xuser% -pw %xpwd% pool-list params=master>master.txt
IF %ERRORLEVEL%==0 goto GotMaster
:: The first xe command failed. Either the xmaster variable is set to a slave host,
:: or a non-Xen host. If we connected to slave host, master.txt will have the IP
:: address of the master - lets get it:
FOR /F "skip=1 tokens=2 delims=:" %%H IN (master.txt) DO SET xmaster=%%H
:: Ok, now see if we have a valid master address:
"%xe%" -s %xmaster% -u %xuser% -pw %xpwd% pool-list params=master>master.txt
IF %ERRORLEVEL%==0 goto GotMaster
:: We do not have a valid master address
:: Have we already tried the second host?
if "%xmaster%"=="%xmaster_backup%" goto ERROR_NoMaster
:: We have not tried the second host yet, so try now:
set xmaster=%xmaster_backup%
goto GetMaster

:GotMaster
:: trim leading spaces
FOR /F "tokens=* delims= " %%a in ("%xmaster%") DO SET xmaster=%%a
@echo Master: %xmaster%
:: Disable all hosts and shutdown all VMs
"%xe%" -s %xmaster% -u %xuser% -pw %xpwd% host-list params=address>hosts.txt
FOR /F "tokens=2 delims=:" %%H IN (hosts.txt) DO CALL :DisableHost %%H
CALL :ShutdownVMs

:: WAIT FOR VMs TO SHUTDOWN
:: This could be scripted to check the status of VMs on a host and wait until
:: all VMs have been shutdown before continuing with the shutdown of the host
::
:: Or the easy, but not so clever way is to use the following line to delay for a while
PING 1.1.1.1 -n 1 -w %xshutdowndelay% >NUL

:: Shutdown slave hosts
FOR /F "tokens=2 delims=:" %%H IN (hosts.txt) DO CALL :ShutdownHost %%H NoMasterShutdown
:: Shutdown master host
CALL :ShutdownHost %xmaster%
:: --------------------------------------------------
::Cleanup the files created during the batch file runtime.
del Master.txt
del hosts.txt
::We leave the XenShutdownError.txt file for problem solving reasons.
goto :eof
:: --------------------------------------------------
:ERROR_NoMaster
@Echo.>>%logfile%
@Echo ---------------------------------------------------------------------------------------------------------------------------------------------------->>%logfile%
@Echo %date% %time%>>%logfile%
@Echo The cause of this was due to both the xmaster and the backup_xmaster servers specified in the beginning of the script were not rechable.>>%logfile%
@Echo.---------------------------------------------------------------------------------------------------------------------------------------------------->>%logfile%
@Echo.>>%logfile%
goto :eof
:: --------------------------------------------------
:: --------------------------------------------------
:: Routine  to disable a host
:: Usage:
::   CALL :DisableHost hostname
::
:DisableHost
@echo Disable host: %1 
@echo Pool master: %xmaster%
:: Stop any VMs from being started on the host
"%xe%" -s %xmaster% -u %xuser% -pw %xpwd% host-disable address=%1
goto :eof
:: --------------------------------------------------
:: --------------------------------------------------
:: Routine  to shutdown all VMs on a host
:: Usage:
::   CALL :ShutdownVMs
::
:ShutdownVMs
@echo Pool master: %xmaster%
echo Shutdown all VMs
:: Shutdown all running VMs
"%xe%" -s %xmaster% -u %xuser% -pw %xpwd% vm-shutdown power-state=running force=false --multiple
goto :eof
:: --------------------------------------------------
:: --------------------------------------------------
:: Routine  to shutdown a host
:: Usage:
::   CALL :ShutdownHost hostname [NoMasterShutdown]
::   if NoMasterShutdown is present, the pool master
::   will not be shutdown
::
:ShutdownHost
if "%2"=="NoMasterShutdown" goto CheckMaster
goto :AnyHost
:CheckMaster
if "%1"=="%xmaster%" goto :eof
:AnyHost
echo Shutdown host: %1
:: Shutdown host
"%xe%" -s %xmaster% -u %xuser% -pw %xpwd% host-shutdown address=%1
goto :eof

No comments:

Post a Comment