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
7USAGE='Usage: deploy_puppet.sh [-rsndh]'
8HELP="${USAGE}\n\n\
9Force puppet deployment.\n\
10The script will first fetch server hostnames from server db,\n\
11given the server role and status and/or server name.\n\
12And then force puppet deployment on the selected servers.\n\n\
13Requirement:
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
18It should be safe to rerun the script multiple time, \n\
19as it doesn't hurt to deploy puppet multiple times.\n\n\
20Options:\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
27ROLE=
28ROLE_OPT=
29STATUS=
30STATUS_OPT=
31HOSTNAME=
32DRYRUN="FALSE"
33AUTOTEST_ROOT="/usr/local/autotest"
34while 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
63done
64
65if [ -n "${ROLE}" ]; then
66  ROLE_OPT="-r ${ROLE}"
67fi
68
69if [ -n "${STATUS}" ]; then
70  STATUS_OPT="-s ${STATUS}"
71fi
72
73if [ -z "${ROLE}" ] && [ -z "${STATUS}" ] && [ -z "${HOSTNAME}"]; then
74  echo "You must specify at least one of -r, -s or -n"
75  exit 1
76fi
77
78hosts="$(${AUTOTEST_ROOT}/cli/atest server list ${STATUS_OPT} ${ROLE_OPT} ${HOSTNAME}| grep Hostname| awk {'print $3'})"
79
80echo -e "\n******* Will update the following servers ********\n "
81
82for host in ${hosts}; do
83  echo ${host}
84done
85
86echo -e "\n**************************************************\n"
87
88for 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"
101done
102