aboutsummaryrefslogtreecommitdiff
path: root/share/hydra/mass
blob: b66956b4746d6c74a6f7d80bf1d5ea2f6b979f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
#
# Execute commands on multiple nodes.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

# Issue commands with pssh
function mass_pssh {
  hydra_install_package pssh
  parallel-ssh -P -p 4 -t 0 -h $TMPWORK sudo $COMMAND
}

# Issue commands with a for loop
function mass_loop {
  for node in `cat $TMPWORK`; do
    echo "Connecting to $node..."
    $HYDRA_CONNECT $node sudo $COMMAND
  done
}

# Issue commands with dish
function mass_dish {
  hydra_install_package dish
  dish -f -p /dev/null -e "sudo $COMMAND" -g $TMPWORK
}

# Issue commands with mussh
function mass_mussh {
  hydra_install_package mussh
  mussh -H $TMPWORK -c "sudo $COMMAND"
}

# Issue commands with ansible
function mass_ansible {
  hydra_install_package ansible
  ansible -i $TMPWORK all -f 1 --sudo -a "$COMMAND"
}

# Set nodes
function mass_set_nodes {
  hydra_set_tmpfile nodes
  hydra $HYDRA nodes $1 > $TMPWORK
}

# Execute command on localhost
function mass_exec_localhost {
  if grep -q -e "^$LOCAL$" $TMPWORK; then
    # Remove localhost from node list
    grep -v -e "^$LOCAL$" $TMPWORK > $TMPWORK.tmp
    mv $TMPWORK.tmp $TMPWORK

    echo "Issuing command on localhost..."
    $SUDO $COMMAND
  fi
}

# Load
source $APP_BASE/lib/hydra/functions || exit 1
hydra_config_load

# Command line arguments
BASENAME="`basename $0`"
COMMAND="$*"

# Initial node list
BASEDIR="/tmp"
LOCAL="`facter fqdn`"
mass_set_nodes

# Set sudo config
if [ "$WHOAMI" != 'root' ]; then
  sudo="sudo"
else
  echo "Sorry, cannot run as root"
  exit 1
fi

# Validation
if [ "$BASENAME" == "mass-upgrade" ]; then
  COMMAND="DEBIAN_FRONTEND=noninteractive hydractl upgrade clean"
elif [ "$BASENAME" == "mass-web" ]; then
  mass_set_nodes web
fi

# Execute commands in hosts
echo "Issuing $COMMAND on multiple nodes..."
mass_exec_localhost
mass_ansible
hydra_unset_tmpfile $TMPWORK