Monday, July 7, 2014

Windows batch script for managing computer farm

Problem Description

When I have joined Todacell at September of 2011 the company had only 8 ad servers. While we were facing some problem or we had to install a new version then we accepted with Remote Desktop Connection (RDC) our ad-servers and one by one manually performed the same set of repeated actions such as deletion, copy and etc. As far as it was less then 10 servers it was more or less acceptable but when the number of servers increased and comes close to 20 then the every tiny action that should be taken on ad server tier took more then one hour and a lot of hard manual work from me or one of my fellows. So we needed some number of scripts that will enable us from one remote computer to launch a script that will do the same work on all our ad servers.

Solution

In order to illustrate the way to the solution, let focus on one of the simplest tasks we needed to deal like installing new version of adserver_maintenance application.

Single computer


Script for a single computer looks relative simple:



1
2
3
4
5
6
sc stop Tomcat6
ping -n 6 127.0.0.1 > nul
cd "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps"
rmdir /s /q adserver_maintenance 
copy "C:\\shared\adserver_maintenance.war" "*.*" /y
sc start Tomcat6


The line 1 stops tomcat windows service,
the line 2 waits 6 seconds to confirm that tomcat will be stopped before deleting the directory,
the line 4 deletes directory with old version of the application,
the line 5 put the new version of the application into installation directory,
the line 6 start tomcat.

Notice that before we run the script (let name it remote_install_adserver_maintenance.bat), we have to put a new version of adserver_maintenance.war into the folder “C:\\shared”.


PsExec tool


In order to copy and launch commands on remote computer I've used PsExec utility, for more information on this utility you can jump to http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx. I've done it with PsExec utility in the following way for server called 'ad01' 


1
2
3
4
echo ad01
copy "C:\shared\adserver_maintenance.war" "\\ad01\shared" /y
copy ".\remote_install_adserver_maintenance.bat" "\\ad01\shared\remote_install_adserver_maintenance.bat" /y
C:\Users\Administrator\Downloads\PSTools\PsExec \\ad01 "C:\\shared\remote_install_adserver_maintenance.bat"



The line 2 copies the war file with updated version to folder named 'shared' of server 'ad01' of course we should bother that it is a directory 'C:\\shared',
the line 3 copies the above mentioned batch file remote_install_adserver_maintenance.bat from our central computer that serves for me as kind of 'Central Panel' to ad01 server,
the line 4 launches the batch file on ad01 server.


Iteration through all ad servers



So we have local script that does the task for one single machine,we have a way to control and copy all needed files from 'central panel' to one of  adservers, what we left to do is to iterate through all adservers and on every server to launch what we done in the previous part.

Here is the code for this matter:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@echo off
setlocal

set servers=01,02,03,04,05,06,07,08,09,10,11

call :parse "%servers%"

goto :eos

:parse

set list=%1
set list=%list:"=%

FOR /f "tokens=1* delims=," %%a IN ("%list%") DO (
  if not "%%a" == "" call :sub %%a
  if not "%%b" == "" call :parse "%%b"
)

goto :eos

:sub

echo %1
copy "C:\shared\adserver_maintenance.war" "\\ad%1\shared" /y
copy ".\remote_install_adserver_maintenance.b" "\\ad%1\shared\remote_install_adserver_maintenance.bat" /y
C:\Users\Administrator\Downloads\PSTools\PsExec \\ad%1 "C:\\shared\remote_install_adserver_maintenance.bat"

goto :eos

:eos
endlocal


No comments:

Post a Comment