1 #!/bin/sh 2 3 # 4 # Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved. 5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 # 7 # This code is free software; you can redistribute it and/or modify it 8 # under the terms of the GNU General Public License version 2 only, as 9 # published by the Free Software Foundation. 10 # 11 # This code is distributed in the hope that it will be useful, but WITHOUT 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 # version 2 for more details (a copy is included in the LICENSE file that 15 # accompanied this code). 16 # 17 # You should have received a copy of the GNU General Public License version 18 # 2 along with this work; if not, write to the Free Software Foundation, 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 # 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 # or visit www.oracle.com if you need additional information or have any 23 # questions. 24 # 25 26 # Shell script for a fast parallel forest command 27 28 global_opts="" 29 status_output="/dev/stdout" 30 qflag="false" 31 vflag="false" 32 sflag="false" 33 while [ $# -gt 0 ] 34 do 35 case $1 in 36 -q | --quiet ) 37 qflag="true" 38 global_opts="${global_opts} -q" 39 status_output="/dev/null" 40 ;; 41 42 -v | --verbose ) 43 vflag="true" 44 global_opts="${global_opts} -v" 45 ;; 46 47 -s | --sequential ) 48 sflag="true" 49 ;; 50 51 '--' ) # no more options 52 shift; break 53 ;; 54 55 -*) # bad option 56 usage 57 ;; 58 59 * ) # non option 60 break 61 ;; 62 esac 63 shift 64 done 65 66 67 command="$1"; shift 68 command_args="$@" 69 70 usage() { 71 echo "usage: $0 [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] <command> [commands...]" > ${status_output} 72 exit 1 73 } 74 75 76 if [ "x" = "x$command" ] ; then 77 echo "ERROR: No command to hg supplied!" 78 usage 79 fi 80 81 # Clean out the temporary directory that stores the pid files. 82 tmp=/tmp/forest.$$ 83 rm -f -r ${tmp} 84 mkdir -p ${tmp} 85 86 safe_interrupt () { 87 if [ -d ${tmp} ]; then 88 if [ "`ls ${tmp}/*.pid`" != "" ]; then 89 echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!" > ${status_output} 90 sleep 1 91 # Pipe stderr to dev/null to silence kill, that complains when trying to kill 92 # a subprocess that has already exited. 93 kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null 94 wait 95 echo "Interrupt complete!" > ${status_output} 96 fi 97 rm -f -r ${tmp} 98 fi 99 exit 130 100 } 101 102 nice_exit () { 103 if [ -d ${tmp} ]; then 104 if [ "`ls ${tmp}`" != "" ]; then 105 wait 106 fi 107 rm -f -r ${tmp} 108 fi 109 } 110 111 trap 'safe_interrupt' INT QUIT 112 trap 'nice_exit' EXIT 113 114 subrepos="corba jaxp jaxws langtools jdk hotspot nashorn" 115 subrepos_extra="closed jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs" 116 117 # Only look in specific locations for possible forests (avoids long searches) 118 pull_default="" 119 repos="" 120 repos_extra="" 121 if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then 122 if [ ! -f .hg/hgrc ] ; then 123 echo "ERROR: Need initial repository to use this script" > ${status_output} 124 exit 1 125 fi 126 127 pull_default=`hg paths default` 128 if [ "${pull_default}" = "" ] ; then 129 echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output} 130 exit 1 131 fi 132 133 for i in ${subrepos} ; do 134 if [ ! -f ${i}/.hg/hgrc ] ; then 135 repos="${repos} ${i}" 136 fi 137 done 138 if [ "${command_args}" != "" ] ; then 139 pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'` 140 if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then 141 echo "ERROR: Need initial clone from non-local source" > ${status_output} 142 exit 1 143 fi 144 pull_extra="${command_args}/${pull_default_tail}" 145 for i in ${subrepos_extra} ; do 146 if [ ! -f ${i}/.hg/hgrc ] ; then 147 repos_extra="${repos_extra} ${i}" 148 fi 149 done 150 fi 151 at_a_time=2 152 # Any repos to deal with? 153 if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then 154 echo "No repositories to process." > ${status_output} 155 exit 156 fi 157 else 158 for i in . ${subrepos} ${subrepos_extra} ; do 159 if [ -d ${i}/.hg ] ; then 160 repos="${repos} ${i}" 161 fi 162 done 163 164 # Any repos to deal with? 165 if [ "${repos}" = "" ] ; then 166 echo "No repositories to process." > ${status_output} 167 exit 168 fi 169 170 # any of the repos locked? 171 for i in ${repos} ; do 172 if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then 173 locked="${i} ${locked}" 174 fi 175 done 176 if [ "${locked}" != "" ] ; then 177 echo "ERROR: These repositories are locked: ${locked}" > ${status_output} 178 exit 1 179 fi 180 at_a_time=8 181 fi 182 183 # Echo out what repositories we do a command on. 184 echo "# Repositories: ${repos} ${repos_extra}" > ${status_output} 185 186 if [ "${command}" = "serve" ] ; then 187 # "serve" is run for all the repos. 188 ( 189 ( 190 ( 191 echo "[web]" 192 echo "description = $(basename $(pwd))" 193 echo "allow_push = *" 194 echo "push_ssl = False" 195 196 echo "[paths]" 197 for i in ${repos} ${repos_extra} ; do 198 if [ "${i}" != "." ] ; then 199 echo "/$(basename $(pwd))/${i} = ${i}" 200 else 201 echo "/$(basename $(pwd)) = $(pwd)" 202 fi 203 done 204 ) > ${tmp}/serve.web-conf 205 206 echo "serving root repo $(basename $(pwd))" 207 208 (PYTHONUNBUFFERED=true hg${global_opts} serve -A ${status_output} -E ${status_output} --pid-file ${tmp}/serve.pid --web-conf ${tmp}/serve.web-conf; echo "$?" > ${tmp}/serve.pid.rc ) 2>&1 & 209 ) 2>&1 | sed -e "s@^@serve: @" > ${status_output} 210 ) & 211 else 212 # Run the supplied command on all repos in parallel. 213 n=0 214 for i in ${repos} ${repos_extra} ; do 215 n=`expr ${n} '+' 1` 216 repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'` 217 reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'` 218 pull_base="${pull_default}" 219 for j in $repos_extra ; do 220 if [ "$i" = "$j" ] ; then 221 pull_base="${pull_extra}" 222 fi 223 done 224 ( 225 ( 226 if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then 227 pull_newrepo="`echo ${pull_base}/${i} | sed -e 's@\([^:]/\)//*@\1@g'`" 228 path="`dirname ${i}`" 229 if [ "${path}" != "." ] ; then 230 times=0 231 while [ ! -d "${path}" ] ## nested repo, ensure containing dir exists 232 do 233 times=`expr ${times} '+' 1` 234 if [ `expr ${times} '%' 10` -eq 0 ] ; then 235 echo "${path} still not created, waiting..." > ${status_output} 236 fi 237 sleep 5 238 done 239 fi 240 echo "hg clone ${pull_newrepo} ${i}" > ${status_output} 241 (PYTHONUNBUFFERED=true hg${global_opts} clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 & 242 else 243 echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output} 244 cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 & 245 fi 246 247 echo $! > ${tmp}/${repopidfile}.pid 248 ) 2>&1 | sed -e "s@^@${reponame}: @" > ${status_output} 249 ) & 250 251 if [ `expr ${n} '%' ${at_a_time}` -eq 0 -a "${sflag}" = "false" ] ; then 252 sleep 2 253 echo "Waiting 5 secs before spawning next background command." > ${status_output} 254 sleep 3 255 fi 256 257 if [ "${sflag}" = "true" ] ; then 258 wait 259 fi 260 done 261 fi 262 263 # Wait for all hg commands to complete 264 wait 265 266 # Terminate with exit 0 only if all subprocesses were successful 267 ec=0 268 if [ -d ${tmp} ]; then 269 for rc in ${tmp}/*.pid.rc ; do 270 exit_code=`cat ${rc} | tr -d ' \n\r'` 271 if [ "${exit_code}" != "0" ] ; then 272 repo="`echo ${rc} | sed -e s@^${tmp}@@ -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`" 273 echo "WARNING: ${repo} exited abnormally." > ${status_output} 274 ec=1 275 fi 276 done 277 fi 278 exit ${ec}