Home | History | Annotate | Download | only in contrib
      1 #!/bin/bash
      2 #
      3 # Copyright 2015 The Chromium OS Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 USAGE='Usage: deploy_puppet.sh [-rsndh]'
      8 HELP="${USAGE}\n\n\
      9 Force puppet deployment.\n\
     10 The script will first fetch server hostnames from server db,\n\
     11 given the server role and status and/or server name.\n\
     12 And then force puppet deployment on the selected servers.\n\n\
     13 Requirement:
     14  - Run on the machine that has access to server db and
     15  - Run it as chromeos-test.\n\
     16  - The server must exist in server db, even if -n is used.
     17 
     18 It should be safe to rerun the script multiple time, \n\
     19 as it doesn't hurt to deploy puppet multiple times.\n\n\
     20 Options:\n\
     21   -r server role as in server db, e.g. 'drone'.\n\
     22   -s server status as in server db, e.g. 'primary'.\n\
     23   -n server hostname.\n\
     24   -d dryrun.\n\
     25   -h help."
     26 
     27 ROLE=
     28 ROLE_OPT=
     29 STATUS=
     30 STATUS_OPT=
     31 HOSTNAME=
     32 DRYRUN="FALSE"
     33 AUTOTEST_ROOT="/usr/local/autotest"
     34 while getopts ":s:r:n:dh" opt; do
     35   case $opt in
     36     r)
     37       ROLE=$OPTARG
     38       ;;
     39     s)
     40       STATUS=$OPTARG
     41       ;;
     42     n)
     43       HOSTNAME=$OPTARG
     44       ;;
     45     d)
     46       DRYRUN="TRUE"
     47       ;;
     48     h)
     49       echo -e "${HELP}" >&2
     50       exit 0
     51       ;;
     52     \?)
     53       echo "Invalid option: -$OPTARG" >&2
     54       echo -e "${HELP}" >&2
     55       exit 1
     56       ;;
     57     :)
     58       echo "Option -$OPTARG requires an argument." >&2
     59       echo -e "${HELP}" >&2
     60       exit 1
     61       ;;
     62   esac
     63 done
     64 
     65 if [ -n "${ROLE}" ]; then
     66   ROLE_OPT="-r ${ROLE}"
     67 fi
     68 
     69 if [ -n "${STATUS}" ]; then
     70   STATUS_OPT="-s ${STATUS}"
     71 fi
     72 
     73 if [ -z "${ROLE}" ] && [ -z "${STATUS}" ] && [ -z "${HOSTNAME}" ]; then
     74   echo "You must specify at least one of -r, -s or -n"
     75   exit 1
     76 fi
     77 
     78 hosts="$(${AUTOTEST_ROOT}/cli/atest server list ${STATUS_OPT} ${ROLE_OPT} ${HOSTNAME}| grep Hostname| awk {'print $3'})"
     79 
     80 echo -e "\n******* Will update the following servers ********\n "
     81 
     82 for host in ${hosts}; do
     83   echo ${host}
     84 done
     85 
     86 echo -e "\n**************************************************\n"
     87 
     88 for host in ${hosts}; do
     89   run_puppet="ssh ${host} -- 'sudo /root/chromeos-admin/puppet/sync_and_run_puppet -f'"
     90   echo -e "\n********** Processing ${host} ****************\n"
     91   echo "[Running] ${run_puppet}"
     92   if [ "${DRYRUN}" != "TRUE" ]; then
     93     eval ${run_puppet}
     94   fi
     95   echo -e "\n********* Finished processing ${host} *******\n"
     96 done
     97