aboutsummaryrefslogtreecommitdiff
path: root/share/hydra/deploy
blob: 940c17bdc65abf1c9a1b9805f8e6f7f1ff81286e (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
#!/bin/bash
#
# Deploy a node using automated recipes.
#
# 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/>.

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

# Command line arguments
NODES="$*"

# Build node list
if [ -z "$NODES" ]; then
  NODES="`hydra $HYDRA nodes`"
fi

# Deploy
for node in $NODES; do
  echo "Deploying to $node..."

  # Saner defaults
  $HYDRA_CONNECT $node sudo mkdir -p       /etc/puppet
  $HYDRA_CONNECT $node sudo chown -R root. /etc/puppet
  $HYDRA_CONNECT $node sudo chmod -R 640   /etc/puppet

  # Sync repository to server
  # Exclude eventual keys and version control files
  rsync -CrltDv --no-perms --exclude=keys --exclude=hiera/secrets --delete --rsync-path "sudo rsync" $HYDRA_FOLDER/puppet/ $node:/etc/puppet/

  # Setup custom facts
  FQDN="`$HYDRA_CONNECT $node facter fqdn`"
  DOMAIN="`echo $FQDN | cut -d . -f 2-`"
  ROLE="`grep "^nodo::role:         " domain/$DOMAIN/$FQDN.yaml | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'`"
  LOCATION="`grep "^nodo::location: " domain/$DOMAIN/$FQDN.yaml | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'`"

  # Check for manifest
  if [ ! -e "$HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp" ]; then
    echo "Not found: $HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp"
    exit 1
  fi

  # Copy needed hiera YAMLs
  $HYDRA_CONNECT $node sudo mkdir -p $FOLDER/etc/puppet/secrets/{domain,location,node,role}
  $HYDRA_CONNECT $node sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/domain/$DOMAIN.yaml     $FOLDER/etc/puppet/hiera/domain/
  $HYDRA_CONNECT $node sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/location/$LOCATION.yaml $FOLDER/etc/puppet/hiera/location/
  $HYDRA_CONNECT $node sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/location/$ROLE.yaml     $FOLDER/etc/puppet/hiera/role/

  # Run puppet
  $HYDRA_CONNECT $node <<EOF
  ##### BEGIN REMOTE SCRIPT #####
  sudo chown -R root. /etc/puppet
  sudo chmod -R 640   /etc/puppet
  LC_ALL=C FACTER_role=$ROLE FACTER_location=$LOCATION \
  sudo puppet apply /etc/puppet/manifests/nodes/$FQDN.pp
  ##### END REMOTE SCRIPT #######
EOF
done