hgforest.sh revision 1045:b84fc0b1efc2
1#!/bin/sh
2#
3# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
4# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5#
6# This code is free software; you can redistribute it and/or modify it
7# under the terms of the GNU General Public License version 2 only, as
8# published by the Free Software Foundation.
9#
10# This code is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13# version 2 for more details (a copy is included in the LICENSE file that
14# accompanied this code).
15#
16# You should have received a copy of the GNU General Public License version
17# 2 along with this work; if not, write to the Free Software Foundation,
18# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21# or visit www.oracle.com if you need additional information or have any
22# questions.
23#
24
25# Shell script for a fast parallel forest/trees command
26
27usage() {
28      echo "usage: $0 [-h|--help] [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] <command> [commands...]" > ${status_output}
29      echo "Environment variables which modify behaviour:"
30      echo "   HGFOREST_QUIET      : (boolean) If 'true' then standard output is redirected to /dev/null"
31      echo "   HGFOREST_VERBOSE    : (boolean) If 'true' then Mercurial asked to produce verbose output"
32      echo "   HGFOREST_SEQUENTIAL : (boolean) If 'true' then repos are processed sequentially. Disables concurrency"
33      echo "   HGFOREST_GLOBALOPTS : (string, must begin with space) Additional Mercurial global options"
34      echo "   HGFOREST_REDIRECT   : (file path) Redirect standard output to specified file"
35      echo "   HGFOREST_FIFOS      : (boolean) Default behaviour for FIFO detection. Does not override FIFOs disabled"
36      echo "   HGFOREST_CONCURRENCY: (positive integer) Number of repos to process concurrently"
37      echo "   HGFOREST_DEBUG      : (boolean) If 'true' then temp files are retained"
38      exit 1
39}
40
41global_opts="${HGFOREST_GLOBALOPTS:-}"
42status_output="${HGFOREST_REDIRECT:-/dev/stdout}"
43qflag="${HGFOREST_QUIET:-false}"
44vflag="${HGFOREST_VERBOSE:-false}"
45sflag="${HGFOREST_SEQUENTIAL:-false}"
46while [ $# -gt 0 ]
47do
48  case $1 in
49    -h | --help )
50      usage
51      ;;
52
53    -q | --quiet )
54      qflag="true"
55      ;;
56
57    -v | --verbose )
58      vflag="true"
59      ;;
60
61    -s | --sequential )
62      sflag="true"
63      ;;
64
65    '--' ) # no more options
66      shift; break
67      ;;
68
69    -*)  # bad option
70      usage
71      ;;
72
73     * )  # non option
74      break
75      ;;
76  esac
77  shift
78done
79
80# silence standard output?
81if [ ${qflag} = "true" ] ; then
82  global_opts="${global_opts} -q"
83  status_output="/dev/null"
84fi
85
86# verbose output?
87if [ ${vflag} = "true" ] ; then
88  global_opts="${global_opts} -v"
89fi
90
91# Make sure we have a command.
92if [ $# -lt 1 -o -z "${1:-}" ] ; then
93  echo "ERROR: No command to hg supplied!"
94  usage
95fi
96
97command="$1"; shift
98command_args="${@:-}"
99
100# Clean out the temporary directory that stores the pid files.
101tmp=/tmp/forest.$$
102rm -f -r ${tmp}
103mkdir -p ${tmp}
104
105
106if [ "${HGFOREST_DEBUG:-false}" = "true" ] ; then
107  echo "DEBUG: temp files are in: ${tmp}"
108fi
109
110# Check if we can use fifos for monitoring sub-process completion.
111echo "1" > ${tmp}/read
112while_subshell=1
113while read line; do
114  while_subshell=0
115  break;
116done < ${tmp}/read
117rm ${tmp}/read
118
119on_windows=`uname -s | egrep -ic -e 'cygwin|msys'`
120
121if [ ${while_subshell} = "1" -o ${on_windows} = "1" ]; then
122  # cygwin has (2014-04-18) broken (single writer only) FIFOs
123  # msys has (2014-04-18) no FIFOs.
124  # older shells create a sub-shell for redirect to while
125  have_fifos="false"
126else
127  have_fifos="${HGFOREST_FIFOS:-true}"
128fi
129
130safe_interrupt () {
131  if [ -d ${tmp} ]; then
132    if [ "`ls ${tmp}/*.pid`" != "" ]; then
133      echo "Waiting for processes ( `cat ${tmp}/.*.pid ${tmp}/*.pid 2> /dev/null | tr '\n' ' '`) to terminate nicely!" > ${status_output}
134      sleep 1
135      # Pipe stderr to dev/null to silence kill, that complains when trying to kill
136      # a subprocess that has already exited.
137      kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
138      wait
139      echo "Interrupt complete!" > ${status_output}
140    fi
141    rm -f -r ${tmp}
142  fi
143  exit 130
144}
145
146nice_exit () {
147  if [ -d ${tmp} ]; then
148    if [ "`ls -A ${tmp} 2> /dev/null`" != "" ]; then
149      wait
150    fi
151    if [ "${HGFOREST_DEBUG:-false}" != "true" ] ; then
152      rm -f -r ${tmp}
153    fi
154  fi
155}
156
157trap 'safe_interrupt' INT QUIT
158trap 'nice_exit' EXIT
159
160subrepos="corba jaxp jaxws langtools jdk hotspot nashorn"
161subrepos_extra="closed jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs"
162
163# Only look in specific locations for possible forests (avoids long searches)
164pull_default=""
165repos=""
166repos_extra=""
167if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
168  # we must be a clone
169  if [ ! -f .hg/hgrc ] ; then
170    echo "ERROR: Need initial repository to use this script" > ${status_output}
171    exit 1
172  fi
173
174  # the clone must know where it came from (have a default pull path).
175  pull_default=`hg paths default`
176  if [ "${pull_default}" = "" ] ; then
177    echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output}
178    exit 1
179  fi
180
181  # determine which sub repos need to be cloned.
182  for i in ${subrepos} ; do
183    if [ ! -f ${i}/.hg/hgrc ] ; then
184      repos="${repos} ${i}"
185    fi
186  done
187
188  pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`
189
190  if [ -n "${command_args}" ] ; then
191    # if there is an "extra sources" path then reparent "extra" repos to that path
192    if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
193      echo "ERROR: Need initial clone from non-local source" > ${status_output}
194      exit 1
195    fi
196    pull_extra="${command_args}/${pull_default_tail}"
197
198    # determine which extra subrepos need to be cloned.
199    for i in ${subrepos_extra} ; do
200      if [ ! -f ${i}/.hg/hgrc ] ; then
201        repos_extra="${repos_extra} ${i}"
202      fi
203    done
204  else
205    if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
206      # local source repo. Clone the "extra" subrepos that exist there.
207      for i in ${subrepos_extra} ; do
208        if [ -f ${pull_default}/${i}/.hg/hgrc -a ! -f ${i}/.hg/hgrc ] ; then
209          # sub-repo there in source but not here
210          repos_extra="${repos_extra} ${i}"
211        fi
212      done
213    fi
214  fi
215
216  # Any repos to deal with?
217  if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then
218    echo "No repositories to process." > ${status_output}
219    exit
220  fi
221
222  # Repos to process concurrently. Clone does better with low concurrency.
223  at_a_time="${HGFOREST_CONCURRENCY:-2}"
224else
225  # Process command for all of the present repos
226  for i in . ${subrepos} ${subrepos_extra} ; do
227    if [ -d ${i}/.hg ] ; then
228      repos="${repos} ${i}"
229    fi
230  done
231
232  # Any repos to deal with?
233  if [ "${repos}" = "" ] ; then
234    echo "No repositories to process." > ${status_output}
235    exit
236  fi
237
238  # any of the repos locked?
239  locked=""
240  for i in ${repos} ; do
241    if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then
242      locked="${i} ${locked}"
243    fi
244  done
245  if [ "${locked}" != "" ] ; then
246    echo "ERROR: These repositories are locked: ${locked}" > ${status_output}
247    exit 1
248  fi
249
250  # Repos to process concurrently.
251  at_a_time="${HGFOREST_CONCURRENCY:-8}"
252fi
253
254# Echo out what repositories we do a command on.
255echo "# Repositories: ${repos} ${repos_extra}" > ${status_output}
256
257if [ "${command}" = "serve" ] ; then
258  # "serve" is run for all the repos as one command.
259  (
260    (
261      cwd=`pwd`
262      serving=`basename ${cwd}`
263      (
264        echo "[web]"
265        echo "description = ${serving}"
266        echo "allow_push = *"
267        echo "push_ssl = False"
268
269        echo "[paths]"
270        for i in ${repos} ; do
271          if [ "${i}" != "." ] ; then
272            echo "/${serving}/${i} = ${i}"
273          else
274            echo "/${serving} = ${cwd}"
275          fi
276        done
277      ) > ${tmp}/serve.web-conf
278
279      echo "serving root repo ${serving}" > ${status_output}
280
281      echo "hg${global_opts} serve" > ${status_output}
282      (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 &
283    ) 2>&1 | sed -e "s@^@serve:   @" > ${status_output}
284  ) &
285else
286  # Run the supplied command on all repos in parallel.
287
288  # n is the number of subprocess started or which might still be running.
289  n=0
290  if [ ${have_fifos} = "true" ]; then
291    # if we have fifos use them to detect command completion.
292    mkfifo ${tmp}/fifo
293    exec 3<>${tmp}/fifo
294  fi
295
296  # iterate over all of the subrepos.
297  for i in ${repos} ${repos_extra} ; do
298    n=`expr ${n} '+' 1`
299    repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
300    reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
301    pull_base="${pull_default}"
302
303    # regular repo or "extra" repo?
304    for j in ${repos_extra} ; do
305      if [ "${i}" = "${j}" ] ; then
306        # it's an "extra"
307        pull_base="${pull_extra}"
308      fi
309    done
310
311    # remove trailing slash
312    pull_base="`echo ${pull_base} | sed -e 's@[/]*$@@'`"
313
314    # execute the command on the subrepo
315    (
316      (
317        if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
318          # some form of clone
319          clone_newrepo="${pull_base}/${i}"
320          parent_path="`dirname ${i}`"
321          if [ "${parent_path}" != "." ] ; then
322            times=0
323            while [ ! -d "${parent_path}" ] ; do  ## nested repo, ensure containing dir exists
324              if [ "${sflag}" = "true" ] ; then
325                # Missing parent is fatal during sequential operation.
326                echo "ERROR: Missing parent path: ${parent_path}" > ${status_output}
327                exit 1
328              fi
329              times=`expr ${times} '+' 1)`
330              if [ `expr ${times} '%' 10` -eq 0 ] ; then
331                echo "${parent_path} still not created, waiting..." > ${status_output}
332              fi
333              sleep 5
334            done
335          fi
336          # run the clone command.
337          echo "hg${global_opts} clone ${clone_newrepo} ${i}" > ${status_output}
338          (PYTHONUNBUFFERED=true hg${global_opts} clone ${clone_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
339        else
340          # run the command.
341          echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}
342          cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
343        fi
344
345        echo $! > ${tmp}/${repopidfile}.pid
346      ) 2>&1 | sed -e "s@^@${reponame}:   @" > ${status_output}
347      # tell the fifo waiter that this subprocess is done.
348      if [ ${have_fifos} = "true" ]; then
349        echo "${i}" >&3
350      fi
351    ) &
352
353    if [ "${sflag}" = "true" ] ; then
354      # complete this task before starting another.
355      wait
356    else
357      if [ "${have_fifos}" = "true" ]; then
358        # check on count of running subprocesses and possibly wait for completion
359        if [ ${n} -ge ${at_a_time} ] ; then
360          # read will block until there are completed subprocesses
361          while read repo_done; do
362            n=`expr ${n} '-' 1`
363            if [ ${n} -lt ${at_a_time} ] ; then
364              # we should start more subprocesses
365              break;
366            fi
367          done <&3
368        fi
369      else
370        # Compare completions to starts
371        completed="`(ls -a1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
372        while [ `expr ${n} '-' ${completed}` -ge ${at_a_time} ] ; do
373          # sleep a short time to give time for something to complete
374          sleep 1
375          completed="`(ls -a1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
376        done
377      fi
378    fi
379  done
380fi
381
382# Wait for all subprocesses to complete
383wait
384
385# Terminate with exit 0 only if all subprocesses were successful
386ec=0
387if [ -d ${tmp} ]; then
388  rcfiles="`(ls -a ${tmp}/*.pid.rc 2> /dev/null) || echo ''`"
389  for rc in ${rcfiles} ; do
390    exit_code=`cat ${rc} | tr -d ' \n\r'`
391    if [ "${exit_code}" != "0" ] ; then
392      repo="`echo ${rc} | sed -e 's@^'${tmp}'@@' -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"
393      echo "WARNING: ${repo} exited abnormally (${exit_code})" > ${status_output}
394      ec=1
395    fi
396  done
397fi
398exit ${ec}
399