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 while [ $# -gt 0 ]
  33 do
  34   case $1 in
  35     -q | --quiet )
  36       qflag="true"
  37       global_opts="${global_opts} -q"
  38       status_output="/dev/null"
  39       ;;
  40 
  41     -v | --verbose )
  42       vflag="true"
  43       global_opts="${global_opts} -v"
  44       ;;
  45 
  46     '--' ) # no more options
  47       shift; break
  48       ;;
  49 
  50     -*)  # bad option
  51       usage
  52       ;;
  53 
  54      * )  # non option
  55       break
  56       ;;
  57   esac
  58   shift
  59 done
  60 
  61 
  62 command="$1"; shift
  63 command_args="$@"
  64 
  65 usage() {
  66       echo "usage: $0 [-q|--quiet] [-v|--verbose] [--] <command> [commands...]" > ${status_output}
  67       exit 1
  68 }
  69 
  70 
  71 if [ "x" = "x$command" ] ; then
  72   echo "ERROR: No command to hg supplied!"
  73   usage
  74 fi
  75 
  76 # Clean out the temporary directory that stores the pid files.
  77 tmp=/tmp/forest.$$
  78 rm -f -r ${tmp}
  79 mkdir -p ${tmp}
  80 
  81 safe_interrupt () {
  82   if [ -d ${tmp} ]; then
  83     if [ "`ls ${tmp}/*.pid`" != "" ]; then
  84       echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!" > ${status_output}
  85       sleep 1
  86       # Pipe stderr to dev/null to silence kill, that complains when trying to kill
  87       # a subprocess that has already exited.
  88       kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
  89       wait
  90       echo "Interrupt complete!" > ${status_output}
  91     fi
  92     rm -f -r ${tmp}
  93   fi
  94   exit 130
  95 }
  96 
  97 nice_exit () {
  98   if [ -d ${tmp} ]; then
  99     if [ "`ls ${tmp}`" != "" ]; then
 100       wait
 101     fi
 102     rm -f -r ${tmp}
 103   fi
 104 }
 105 
 106 trap 'safe_interrupt' INT QUIT
 107 trap 'nice_exit' EXIT
 108 
 109 subrepos="corba jaxp jaxws langtools jdk hotspot nashorn"
 110 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"
 111 
 112 # Only look in specific locations for possible forests (avoids long searches)
 113 pull_default=""
 114 repos=""
 115 repos_extra=""
 116 if [ "${command}" = "clone" -o "${command}" = "fclone" ] ; then
 117   if [ ! -f .hg/hgrc ] ; then
 118     echo "ERROR: Need initial repository to use this script" > ${status_output}
 119     exit 1
 120   fi
 121 
 122   pull_default=`hg paths default`
 123   if [ "${pull_default}" = "" ] ; then
 124     echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output}
 125     exit 1
 126   fi
 127   pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`
 128   if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
 129       echo "ERROR: Need initial clone from non-local source" > ${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_extra="${command_args}/${pull_default_tail}"
 140     for i in ${subrepos_extra} ; do
 141       if [ ! -f ${i}/.hg/hgrc ] ; then
 142         repos_extra="${repos_extra} ${i}"
 143       fi
 144     done
 145   fi
 146   at_a_time=2
 147   # Any repos to deal with?
 148   if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then
 149     echo "No repositories to process." > ${status_output}
 150     exit
 151   fi
 152 else
 153   for i in . ${subrepos} ${subrepos_extra} ; do
 154     if [ -d ${i}/.hg ] ; then
 155       repos="${repos} ${i}"
 156     fi
 157   done
 158 
 159   # Any repos to deal with?
 160   if [ "${repos}" = "" ] ; then
 161     echo "No repositories to process." > ${status_output}
 162     exit
 163   fi
 164 
 165   # any of the repos locked?
 166   for i in ${repos} ; do
 167     if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then
 168       locked="${i} ${locked}"
 169     fi
 170   done
 171   if [ "${locked}" != "" ] ; then
 172     echo "ERROR: These repositories are locked: ${locked}" > ${status_output}
 173     exit 1
 174   fi
 175   at_a_time=8
 176 fi
 177 
 178 # Echo out what repositories we do a command on.
 179 echo "# Repositories: ${repos} ${repos_extra}" > ${status_output}
 180 
 181 if [ "${command}" = "serve" ] ; then
 182   # "serve" is run for all the repos.
 183   (
 184     (
 185       (
 186         echo "[web]"
 187         echo "description = $(basename $(pwd))"
 188         echo "allow_push = *"
 189         echo "push_ssl = False"
 190 
 191         echo "[paths]"
 192         for i in ${repos} ${repos_extra} ; do
 193           if [ "${i}" != "." ] ; then
 194             echo "/$(basename $(pwd))/${i} = ${i}"
 195           else
 196             echo "/$(basename $(pwd)) = $(pwd)"
 197           fi
 198         done
 199       ) > ${tmp}/serve.web-conf
 200 
 201       echo "serving root repo $(basename $(pwd))"
 202 
 203       (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 &
 204     ) 2>&1 | sed -e "s@^@serve:   @" > ${status_output}
 205   ) &
 206 else
 207   # Run the supplied command on all repos in parallel.
 208   n=0
 209   for i in ${repos} ${repos_extra} ; do
 210     n=`expr ${n} '+' 1`
 211     repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
 212     reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
 213     pull_base="${pull_default}"
 214     for j in $repos_extra ; do
 215       if [ "$i" = "$j" ] ; then
 216           pull_base="${pull_extra}"
 217       fi
 218     done
 219     (
 220       (
 221         if [ "${command}" = "clone" -o "${command}" = "fclone" ] ; then
 222           pull_newrepo="`echo ${pull_base}/${i} | sed -e 's@\([^:]/\)//*@\1@g'`"
 223           echo "hg clone ${pull_newrepo} ${i}" > ${status_output}
 224           path="`dirname ${i}`"
 225           if [ "${path}" != "." ] ; then
 226             times=0
 227             while [ ! -d "${path}" ]   ## nested repo, ensure containing dir exists
 228             do
 229               times=`expr ${times} '+' 1`
 230               if [ `expr ${times} '%' 10` -eq 0 ] ; then
 231                 echo "${path} still not created, waiting..." > ${status_output}
 232               fi
 233               sleep 5
 234             done
 235           fi
 236           (PYTHONUNBUFFERED=true hg${global_opts} clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
 237         else
 238           echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}
 239           cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
 240         fi
 241 
 242         echo $! > ${tmp}/${repopidfile}.pid
 243       ) 2>&1 | sed -e "s@^@${reponame}:   @" > ${status_output}
 244     ) &
 245 
 246     if [ `expr ${n} '%' ${at_a_time}` -eq 0 ] ; then
 247       sleep 2
 248       echo "Waiting 5 secs before spawning next background command." > ${status_output}
 249       sleep 3
 250     fi
 251   done
 252 fi
 253 
 254 # Wait for all hg commands to complete
 255 wait
 256 
 257 # Terminate with exit 0 only if all subprocesses were successful
 258 ec=0
 259 if [ -d ${tmp} ]; then
 260   for rc in ${tmp}/*.pid.rc ; do
 261     exit_code=`cat ${rc} | tr -d ' \n\r'`
 262     if [ "${exit_code}" != "0" ] ; then
 263       repo="`echo ${rc} | sed -e s@^${tmp}@@ -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"
 264       echo "WARNING: ${repo} exited abnormally." > ${status_output}
 265       ec=1
 266     fi
 267   done
 268 fi
 269 exit ${ec}