Fang Deng | c7b80a2 | 2015-08-19 15:33:19 -0700 | [diff] [blame] | 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 | |
Aviv Keshet | 9741daf | 2016-06-27 14:27:04 -0700 | [diff] [blame] | 73 | if [ -z "${ROLE}" ] && [ -z "${STATUS}" ] && [ -z "${HOSTNAME}" ]; then |
Fang Deng | c7b80a2 | 2015-08-19 15:33:19 -0700 | [diff] [blame] | 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 | git_pull="ssh -t ${host} -- 'sudo git --work-tree=/root/chromeos-admin --git-dir=/root/chromeos-admin/.git pull'" |
| 90 | run_puppet="ssh ${host} -- 'sudo /root/chromeos-admin/puppet/run_puppet'" |
| 91 | echo -e "\n********** Processing ${host} ****************\n" |
| 92 | echo "[Running] ${git_pull}" |
| 93 | if [ "${DRYRUN}" != "TRUE" ]; then |
| 94 | eval ${git_pull} |
| 95 | fi |
| 96 | echo "[Running] ${run_puppet}" |
| 97 | if [ "${DRYRUN}" != "TRUE" ]; then |
| 98 | eval ${run_puppet} |
| 99 | fi |
| 100 | echo -e "\n********* Finished processing ${host} *******\n" |
| 101 | done |