hgforest.sh revision 1133:f91649eb1db2
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# debug mode
81if [ "${HGFOREST_DEBUG:-false}" = "true" ] ; then
82  global_opts="${global_opts} --debug"
83fi
84
85# silence standard output?
86if [ ${qflag} = "true" ] ; then
87  global_opts="${global_opts} -q"
88  status_output="/dev/null"
89fi
90
91# verbose output?
92if [ ${vflag} = "true" ] ; then
93  global_opts="${global_opts} -v"
94fi
95
96# Make sure we have a command.
97if [ ${#} -lt 1 -o -z "${1:-}" ] ; then
98  echo "ERROR: No command to hg supplied!" > ${status_output}
99  usage > ${status_output}
100fi
101
102# grab command
103command="${1}"; shift
104
105if [ ${vflag} = "true" ] ; then
106  echo "# Mercurial command: ${command}" > ${status_output}
107fi
108
109
110# capture command options and arguments (if any)
111command_args="${@:-}"
112
113if [ ${vflag} = "true" ] ; then
114  echo "# Mercurial command arguments: ${command_args}" > ${status_output}
115fi
116
117# Clean out the temporary directory that stores the pid files.
118tmp=/tmp/forest.$$
119rm -f -r ${tmp}
120mkdir -p ${tmp}
121
122
123if [ "${HGFOREST_DEBUG:-false}" = "true" ] ; then
124  # ignores redirection.
125  echo "DEBUG: temp files are in: ${tmp}" >&2
126fi
127
128# Check if we can use fifos for monitoring sub-process completion.
129echo "1" > ${tmp}/read
130while_subshell=1
131while read line; do
132  while_subshell=0
133  break;
134done < ${tmp}/read
135rm ${tmp}/read
136
137on_windows=`uname -s | egrep -ic -e 'cygwin|msys'`
138
139if [ ${while_subshell} = "1" -o ${on_windows} = "1" ]; then
140  # cygwin has (2014-04-18) broken (single writer only) FIFOs
141  # msys has (2014-04-18) no FIFOs.
142  # older shells create a sub-shell for redirect to while
143  have_fifos="false"
144else
145  have_fifos="${HGFOREST_FIFOS:-true}"
146fi
147
148safe_interrupt () {
149  if [ -d ${tmp} ]; then
150    if [ "`ls ${tmp}/*.pid`" != "" ]; then
151      echo "Waiting for processes ( `cat ${tmp}/.*.pid ${tmp}/*.pid 2> /dev/null | tr '\n' ' '`) to terminate nicely!" > ${status_output}
152      sleep 1
153      # Pipe stderr to dev/null to silence kill, that complains when trying to kill
154      # a subprocess that has already exited.
155      kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
156      wait
157      echo "Interrupt complete!" > ${status_output}
158    fi
159    rm -f -r ${tmp}
160  fi
161  exit 130
162}
163
164nice_exit () {
165  if [ -d ${tmp} ]; then
166    if [ "`ls -A ${tmp} 2> /dev/null`" != "" ]; then
167      wait
168    fi
169    if [ "${HGFOREST_DEBUG:-false}" != "true" ] ; then
170      rm -f -r ${tmp}
171    fi
172  fi
173}
174
175trap 'safe_interrupt' INT QUIT
176trap 'nice_exit' EXIT
177
178subrepos="corba jaxp jaxws langtools jdk hotspot nashorn"
179subrepos_extra="closed jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs"
180
181# Only look in specific locations for possible forests (avoids long searches)
182pull_default=""
183repos=""
184repos_extra=""
185if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
186  # we must be a clone
187  if [ ! -f .hg/hgrc ] ; then
188    echo "ERROR: Need initial repository to use this script" > ${status_output}
189    exit 1
190  fi
191
192  # the clone must know where it came from (have a default pull path).
193  pull_default=`hg paths default`
194  if [ "${pull_default}" = "" ] ; then
195    echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output}
196    exit 1
197  fi
198
199  # determine which sub repos need to be cloned.
200  for i in ${subrepos} ; do
201    if [ ! -f ${i}/.hg/hgrc ] ; then
202      repos="${repos} ${i}"
203    fi
204  done
205
206  pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`
207
208  if [ -n "${command_args}" ] ; then
209    # if there is an "extra sources" path then reparent "extra" repos to that path
210    if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
211      echo "ERROR: Need initial clone from non-local source" > ${status_output}
212      exit 1
213    fi
214    pull_extra="${command_args}/${pull_default_tail}"
215
216    # determine which extra subrepos need to be cloned.
217    for i in ${subrepos_extra} ; do
218      if [ ! -f ${i}/.hg/hgrc ] ; then
219        repos_extra="${repos_extra} ${i}"
220      fi
221    done
222  else
223    if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
224      # local source repo. Clone the "extra" subrepos that exist there.
225      for i in ${subrepos_extra} ; do
226        if [ -f ${pull_default}/${i}/.hg/hgrc -a ! -f ${i}/.hg/hgrc ] ; then
227          # sub-repo there in source but not here
228          repos_extra="${repos_extra} ${i}"
229        fi
230      done
231    fi
232  fi
233
234  # Any repos to deal with?
235  if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then
236    echo "No repositories to process." > ${status_output}
237    exit
238  fi
239
240  # Repos to process concurrently. Clone does better with low concurrency.
241  at_a_time="${HGFOREST_CONCURRENCY:-2}"
242else
243  # Process command for all of the present repos
244  for i in . ${subrepos} ${subrepos_extra} ; do
245    if [ -d ${i}/.hg ] ; then
246      repos="${repos} ${i}"
247    fi
248  done
249
250  # Any repos to deal with?
251  if [ "${repos}" = "" ] ; then
252    echo "No repositories to process." > ${status_output}
253    exit
254  fi
255
256  # any of the repos locked?
257  locked=""
258  for i in ${repos} ; do
259    if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then
260      locked="${i} ${locked}"
261    fi
262  done
263  if [ "${locked}" != "" ] ; then
264    echo "ERROR: These repositories are locked: ${locked}" > ${status_output}
265    exit 1
266  fi
267
268  # Repos to process concurrently.
269  at_a_time="${HGFOREST_CONCURRENCY:-8}"
270fi
271
272# Echo out what repositories we do a command on.
273echo "# Repositories: ${repos} ${repos_extra}" > ${status_output}
274
275if [ "${command}" = "serve" ] ; then
276  # "serve" is run for all the repos as one command.
277  (
278    (
279      cwd=`pwd`
280      serving=`basename ${cwd}`
281      (
282        echo "[web]"
283        echo "description = ${serving}"
284        echo "allow_push = *"
285        echo "push_ssl = False"
286
287        echo "[paths]"
288        for i in ${repos} ; do
289          if [ "${i}" != "." ] ; then
290            echo "/${serving}/${i} = ${i}"
291          else
292            echo "/${serving} = ${cwd}"
293          fi
294        done
295      ) > ${tmp}/serve.web-conf
296
297      echo "serving root repo ${serving}" > ${status_output}
298
299      echo "hg${global_opts} serve" > ${status_output}
300      (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 &
301    ) 2>&1 | sed -e "s@^@serve:   @" > ${status_output}
302  ) &
303else
304  # Run the supplied command on all repos in parallel.
305
306  # n is the number of subprocess started or which might still be running.
307  n=0
308  if [ ${have_fifos} = "true" ]; then
309    # if we have fifos use them to detect command completion.
310    mkfifo ${tmp}/fifo
311    exec 3<>${tmp}/fifo
312  fi
313
314  # iterate over all of the subrepos.
315  for i in ${repos} ${repos_extra} ; do
316    n=`expr ${n} '+' 1`
317    repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
318    reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
319    pull_base="${pull_default}"
320
321    # regular repo or "extra" repo?
322    for j in ${repos_extra} ; do
323      if [ "${i}" = "${j}" ] ; then
324        # it's an "extra"
325        pull_base="${pull_extra}"
326      fi
327    done
328
329    # remove trailing slash
330    pull_base="`echo ${pull_base} | sed -e 's@[/]*$@@'`"
331
332    # execute the command on the subrepo
333    (
334      (
335        if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
336          # some form of clone
337          clone_newrepo="${pull_base}/${i}"
338          parent_path="`dirname ${i}`"
339          if [ "${parent_path}" != "." ] ; then
340            times=0
341            while [ ! -d "${parent_path}" ] ; do  ## nested repo, ensure containing dir exists
342              if [ "${sflag}" = "true" ] ; then
343                # Missing parent is fatal during sequential operation.
344                echo "ERROR: Missing parent path: ${parent_path}" > ${status_output}
345                exit 1
346              fi
347              times=`expr ${times} '+' 1`
348              if [ `expr ${times} '%' 10` -eq 0 ] ; then
349                echo "${parent_path} still not created, waiting..." > ${status_output}
350              fi
351              sleep 5
352            done
353          fi
354          # run the clone command.
355          echo "hg${global_opts} clone ${clone_newrepo} ${i}" > ${status_output}
356          (PYTHONUNBUFFERED=true hg${global_opts} clone ${clone_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
357        else
358          # run the command.
359          echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}
360          cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
361        fi
362
363        echo $! > ${tmp}/${repopidfile}.pid
364      ) 2>&1 | sed -e "s@^@${reponame}:   @" > ${status_output}
365      # tell the fifo waiter that this subprocess is done.
366      if [ ${have_fifos} = "true" ]; then
367        echo "${i}" >&3
368      fi
369    ) &
370
371    if [ "${sflag}" = "true" ] ; then
372      # complete this task before starting another.
373      wait
374    else
375      if [ "${have_fifos}" = "true" ]; then
376        # check on count of running subprocesses and possibly wait for completion
377        if [ ${n} -ge ${at_a_time} ] ; then
378          # read will block until there are completed subprocesses
379          while read repo_done; do
380            n=`expr ${n} '-' 1`
381            if [ ${n} -lt ${at_a_time} ] ; then
382              # we should start more subprocesses
383              break;
384            fi
385          done <&3
386        fi
387      else
388        # Compare completions to starts
389        completed="`(ls -a1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
390        while [ `expr ${n} '-' ${completed}` -ge ${at_a_time} ] ; do
391          # sleep a short time to give time for something to complete
392          sleep 1
393          completed="`(ls -a1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
394        done
395      fi
396    fi
397  done
398
399  if [ ${have_fifos} = "true" ]; then
400    # done with the fifo
401    exec 3>&-
402  fi
403fi
404
405# Wait for all subprocesses to complete
406wait
407
408# Terminate with exit 0 only if all subprocesses were successful
409# Terminate with highest exit code of subprocesses
410ec=0
411if [ -d ${tmp} ]; then
412  rcfiles="`(ls -a ${tmp}/*.pid.rc 2> /dev/null) || echo ''`"
413  for rc in ${rcfiles} ; do
414    exit_code=`cat ${rc} | tr -d ' \n\r'`
415    if [ "${exit_code}" != "0" ] ; then
416      if [ ${exit_code} -gt 1 ]; then
417        # mercurial exit codes greater than "1" signal errors.
418      repo="`echo ${rc} | sed -e 's@^'${tmp}'@@' -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"
419      echo "WARNING: ${repo} exited abnormally (${exit_code})" > ${status_output}
420      fi
421      if [ ${exit_code} -gt ${ec} ]; then
422        # assume that larger exit codes are more significant
423        ec=${exit_code}
424      fi
425    fi
426  done
427fi
428exit ${ec}
429