blob: c858bc8e439608dc5e6c8c74f03e4646bcfb01ae [file] [log] [blame]
Fang Dengc7b80a22015-08-19 15:33:19 -07001#!/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
Aviv Keshet9741daf2016-06-27 14:27:04 -070073if [ -z "${ROLE}" ] && [ -z "${STATUS}" ] && [ -z "${HOSTNAME}" ]; then
Fang Dengc7b80a22015-08-19 15:33:19 -070074 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
Allen Li982f8cc2017-05-01 16:20:17 -070089 run_puppet="ssh ${host} -- 'sudo /root/chromeos-admin/puppet/sync_and_run_puppet -f'"
Fang Dengc7b80a22015-08-19 15:33:19 -070090 echo -e "\n********** Processing ${host} ****************\n"
Fang Dengc7b80a22015-08-19 15:33:19 -070091 echo "[Running] ${run_puppet}"
92 if [ "${DRYRUN}" != "TRUE" ]; then
93 eval ${run_puppet}
94 fi
95 echo -e "\n********* Finished processing ${host} *******\n"
96done