source: main/trunk/greenstone2/gsicontrol.bat@ 23949

Last change on this file since 23949 was 23949, checked in by davidb, 13 years ago

In hand-crafted Greenstones configured with multi-OS binaries there needs to be separate folders for OS in cgi-bin. In the near future, the SVN configuration to Greenstone will also be changed to use this out-of-the-box. This change makes it easier to switch between the two structures.

File size: 9.8 KB
Line 
1@echo off
2
3setlocal
4::set testdone=0
5
6set cgibin=cgi-bin
7::set cgibin=cgi-bin\windows
8
9set target=%1
10set configfile=%2
11:: remove any quotes around configfile, if we were given parameter configfile
12:: To test for the existence of a cmdline parameter: http://ss64.com/nt/if.html
13if not [%2] == [] set configfile=%configfile:"=%
14
15:: Construct the signal using the unique GS2 installation
16:: directory (after replacing spaces, colons and backslashes)
17set signal=%GSDLHOME: =_%
18for /F "delims=*" %%T in ('"echo %signal%| bin\windows\sed.exe s@[\\:]@_@g"') do set signal=%%T
19set GS2_APACHE_HTTPD_SIGNAL=GS2_APACHE_HTTPD_%signal%
20set signal=
21rem echo Signal is: %GS2_APACHE_HTTPD_SIGNAL%
22
23:: Need to get greenstone installation directory
24:: set cwd=%CD%
25
26set MONITOR_SUCCESS=MAKE SUCCESSFUL
27set MONITOR_FAILED=MAKE FAILED
28set MONITOR_FINISHED=MAKE DONE
29
30:: check that we have >=1 and <=2 arguments
31:chkargs
32if "%target%" == "" goto usage
33if not "%3" == "" goto usage
34 goto begincmd
35
36
37:usage
38echo.
39echo Usage: %0 command
40echo where command is any of the following:
41echo web-start
42echo web-stop
43echo web-restart
44::echo web-status
45::echo web-graceful
46echo configure-admin
47echo configure-web [config-filename]
48echo configure-apache [config-filename]
49echo configure-cgi
50echo reset-gsdlhome [config-filename]
51echo (Save a copy of %cgibin%\gsdlsite.cfg to preserve any customisations)
52echo set-port
53echo test-gsdlhome
54echo web-stop-tested
55echo.
56goto exit
57
58
59:begincmd
60:: web-stop-tested command assumes GSDLHOME/greenstone environment is already set
61if "%target%" == "web-stop-tested" goto stoptest
62
63:: For all other commands, the greenstone environment needs to be set first before they can be run
64:testgsdl
65if NOT "%GSDLHOME%" == "" if NOT "%GSDLOS%" == "" goto commands
66echo.
67echo Environment variable GSDLHOME (or GSDLOS) not set.
68echo This needs to be set to run the gsicontrol command %target%.
69echo Have you run setup.bat?
70echo.
71goto exit
72
73
74::MAIN MENU
75:commands
76echo.
77echo Using:
78echo GSDLHOME = %GSDLHOME%
79echo GSDLOS = %GSDLOS%
80echo.
81
82if "%target%" == "web-start" goto start
83if "%target%" == "web-stop" goto stop
84if "%target%" == "web-restart" goto restart
85if "%target%" == "configure-admin" goto cfgadmin
86if "%target%" == "configure-web" goto cfgweb
87if "%target%" == "configure-apache" goto cfgapach
88if "%target%" == "configure-cgi" goto cfgcgi
89if "%target%" == "reset-gsdlhome" goto resethome
90:: already tested gsdlhome (and web-stop-tested) above, don't want to keep looping on it
91if "%target%" == "test-gsdlhome" goto exit
92rem if "%target%" == "web-stop-tested" goto exit
93rem if "%target%" == "web-status" goto status
94rem if "%target%" == "web-graceful" goto graceful
95if "%target%" == "set-port" goto setport
96:: unknown command
97echo Command unrecognised: %target%
98goto usage
99
100
101:start
102:: START starts the app in a new console named by the string following immediately thereafter
103:: then we start up apache-httpd and pass the signal that the stop command will respond to
104START "%GSDLHOME%\apachectl" "%GSDLHOME%\bin\windows\starthttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% "%GSDLHOME%\apache-httpd\windows\bin\httpd.exe"
105:: if the return value is >= 0, it's succeeded:
106if ERRORLEVEL 0 (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
107echo %MONITOR_FINISHED%
108goto exit
109
110:stop
111:stoptest
112if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% SILENT
113if "%target%" == "web-stop-tested" goto exit
114:: if the return value is >= 0, it's succeeded:
115if ERRORLEVEL 0 (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
116echo %MONITOR_FINISHED%
117goto exit
118
119
120:restart
121:: Need to stop server, wait and start it again.
122:: We're using Ping to implement batch file Wait
123if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL%
124
125:: Wait 5 seconds and then start. See http://ss64.com/nt/sleep.html (and http://malektips.com/dos0017.html)
126:: if loopback IP address (127.0.0.1) does not exist, we ask them to manually start it up again
127ping -n 1 -w 1000 127.0.0.1 |find "TTL=">nul || goto failmsg
128echo Waiting for re-start....
129ping -n 5 -w 1000 127.0.0.1> nul
130goto start
131
132:failmsg
133echo Unable to wait for restart. Manually run %0 web-start
134goto exit
135
136
137::status
138::graceful
139::echo Command %target% is not operational on this operating system
140::goto exit
141
142
143::configure-admin
144:cfgadmin
145echo.
146echo Configuring admin user password:
147for /F %%T in ('getpw') do set encrypted_password=%%T
148
149:: Have to create an intermediate file in the following, because echoing
150:: lines straight into a pipe adds spaces before the end of each line.
151:: When piping, need to double-escape the angle brackets with three hat signs,
152:: but when redirecting to a file, need to escape only once (one hat sign).
153if ERRORLEVEL 0 (
154 (
155 echo [admin]
156 echo ^<enabled^>true
157 echo ^<groups^>administrator,colbuilder,all-collections-editor
158 echo ^<password^>%encrypted_password%
159 echo ^<username^>admin
160 ) > "%GSDLHOME%\etc\users.txt"
161 type "%GSDLHOME%\etc\users.txt" | txt2db -append "%GSDLHOME%\etc\users.gdb"
162 del "%GSDLHOME%\etc\users.txt"
163) else (
164 echo Did not set password
165)
166echo.
167goto exit
168
169
170:: reset-gsdlhome forces configure-cgi by deleting gsdlsite.cfg
171:resethome
172echo.
173if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" (
174 echo **** WARNING. Local changes made to %GSDLHOME%\%cgibin%\gsdlsite.cfg will be lost.
175 del "%GSDLHOME%\%cgibin%\gsdlsite.cfg"
176)
177goto cfgweb
178
179::configure-web
180::configure-cgi
181:cfgweb
182:cfgcgi
183if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" goto cgimsg
184echo Configuring %cgibin%\gsdlsite.cfg
185for /F "delims=*" %%T in ('"echo %GSDLHOME%| bin\windows\sed.exe s@\\@\\\\@g"') do set safepath=%%T
186bin\windows\sed.exe "s@\*\*GSDLHOME\*\*@\"%safepath%\"@g" %cgibin%\gsdlsite.cfg.in > %cgibin%\gsdlsite.cfg
187set safepath=
188goto cgifin
189
190:cgimsg
191echo WARNING: Nothing done for configure-cgi.
192echo If you wish to regenerate the file
193echo %GSDLHOME%\%cgibin%\gsdlsite.cfg
194echo from scratch, delete the existing file first.
195echo.
196
197:cgifin
198if "%target%" == "configure-cgi" goto exit
199if "%target%" == "configure-web" goto cfgapach
200
201::configure-apache
202:cfgapach
203if not "%configfile%" == "" if exist "%configfile%" (goto cfgport) else (echo Config file %configfile% does not exist. Using default llssite.cfg)
204
205if exist "%GSDLHOME%\llssite.cfg" (
206 set configfile=%GSDLHOME%\llssite.cfg
207 goto cfgport
208)
209if not exist "%GSDLHOME%\llssite.cfg.in" (
210 echo Unable to proceed as neither %GSDLHOME%\llssite.cfg nor %GSDLHOME%\llssite.cfg.in could be found
211 goto exit
212)
213copy "%GSDLHOME%\llssite.cfg.in" "%GSDLHOME%\llssite.cfg"
214set configfile=%GSDLHOME%\llssite.cfg
215
216:cfgport
217echo Configuring the apache webserver...
218:: See http://ss64.com/nt/for_cmd.html, http://ss64.com/nt/findstr.html (and http://ss64.com/nt/find.html)
219FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^portnumber "%configfile%"') do set port=%%G
220FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hostIP "%configfile%"') do set hostIP=%%G
221FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hosts "%configfile%"') do set hosts=%%G
222FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^externalaccess "%configfile%"') do set allowfromall=%%G
223
224if "%allowfromall%" == "1" set allowfromall="Allow"& goto portcon
225set allowfromall="Deny"
226
227:: Using CALL to jump to labels means we can return from them. BUT need to ensure
228:: that command extensions are enabled to call labels. So just use GOTO instead.
229::http://ss64.com/nt/call.html
230goto portcon
231
232
233::configure-port-and-connection
234:setport
235set /p port=Enter port number to use:
236set /p hostIP=Enter host IP to allow (127.0.0.1 is included by default):
237set /p hosts=Enter hostname or list of hosts to allow (localhost included by default):
238set /p allowfromall=Allow external connections [yes/no]:
239
240if "%allowfromall%" == "yes" set allowfromall="Allow"& goto portcon
241if "%allowfromall%" == "y" set allowfromall="Allow"& goto portcon
242set allowfromall="Deny"
243goto portcon
244
245:portcon
246if "%port%" == "" (
247 echo Done
248 goto exit
249)
250set _sed="bin\windows\sed.exe"
251for /F "delims=*" %%T in ('"echo %GSDLHOME%| bin\windows\sed.exe s@\\@\/@g"') do set safepath=%%T
252
253if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=2" %%G IN ('findstr /R ^collecthome "%GSDLHOME%\%cgibin%\gsdlsite.cfg"') do set COLLECTHOME=%%G
254if "%COLLECTHOME%" == "" set COLLECTHOME=%GSDLHOME%\collect
255for /F "delims=*" %%T in ('"echo %COLLECTHOME%| bin\windows\sed.exe s@\\@\/@g"') do set safecollectpath=%%T
256
257
258echo Port: %port%
259echo Stopping web server (if running)
260if not exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" echo Missing conf file
261if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% SILENT
262echo Setting config file to use port %port%
263type "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf.in" | %_sed% "s@\*\*PORT\*\*@%port%@g" | %_sed% "s@\*\*CONNECTPERMISSION\*\*@%allowfromall%@g" | %_sed% "s@\*\*HOST_IP\*\*@%hostIP%@g" | %_sed% "s@\*\*HOSTS\*\*@%hosts%@g" | %_sed% "s@\*\*COLLECTHOME\*\*@%safecollectpath%@g" | %_sed% "s@\*\*GSDLHOME\*\*@%safepath%@g" | %_sed% "s@\*\*APACHE_HOME_OS\*\*@%safepath%\/apache-httpd\/%GSDLOS%@g" > "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf"
264echo Type '%0 web-start' to start the web server running on port %port%
265echo Done
266
267set allowfromall=
268set _sed=
269set safepath=
270set port=
271
272:: Extra processing for configure-web and configure-cgi command targets
273if not "%target%" == "configure-web" if not "%target%" == "configure-cgi" goto exit
274if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
275echo %MONITOR_FINISHED%
276goto exit
277
278:exit
279endlocal
Note: See TracBrowser for help on using the repository browser.