root/main/trunk/greenstone2/gsicontrol.bat @ 23966

Revision 23966, 11.0 KB (checked in by ak19, 9 years ago)

More changes to do with reset-gsdlhome.

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