aboutsummaryrefslogtreecommitdiff
path: root/lib/hydra/deploy
blob: d5df1a0f3263d5ed654bdf16d15db415451162ac (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
102
103
#!/bin/bash

# Setup deployment parameters
function hydra_deploy_setup {
  # Common parameters
  # Exclude eventual keys and version control files
  DEPLOY_RSYNC="rsync -CrltDv --no-perms --exclude=keys --exclude=hiera/secrets --delete"

  if [ "$1" == "remote" ]; then
    # Deploy in a local folder
    if [ ! -z "$2" ]; then
      NODE="$2"
      DEPLOY_COMMAND="$HYDRA_CONNECT $NODE sudo"
      DEPLOY_RSYNC="$DEPLOY_RSYNC --rsync-path \"sudo rsync\" $HYDRA_FOLDER/puppet/ $NODE:/etc/puppet/"
      FQDN="`$DEPLOY_COMMAND facter fqdn`"
      DEPLOY_DEST="$FQDN:"
      DEPLOY_COPY="$DEPLOY_RSYNC"
      PUPPET_MANIFEST="/etc/puppet/manifests/nodes/$FQDN.pp"
    else
      echo "No folder specified."
      exit 1
    fi
  elif [ "$1" == "folder" ]; then
    # Deploy in a remote host
    if [ ! -z "$2" ]; then
      FOLDER="$2"
      DEPLOY_COMMAND="$SUDO chroot $FOLDER"
      DEPLOY_RSYNC="$DEPLOY_RSYNC $HYDRA_FOLDER/puppet/ $FOLDER/etc/puppet/"
      DEPLOY_COPY="$SUDO cp"
      DEPLOY_DEST="$FOLDER"
      PUPPET_MANIFEST="/etc/puppet/manifests/nodes/$FQDN.pp"

      if [ ! -d "$FOLDER"]; then
        echo "folder not found: $FOLDER"
        exit 1
      fi

      # Fix hostname
      if [ -s "$FOLDER/etc/hostname" ]; then
        FQDN="`cat $FOLDER/etc/hostname`"
      fi
    else
      echo "No node specified."
      exit 1
    fi
  else
    # Deploy on the localhost
    DEPLOY_COMMAND="$SUDO"
    FQDN="`facter fqdn`"
    PUPPET_OPTS="--confdir=$HYDRA_FOLDER/puppet --modulepath=$HYDRA_FOLDER/modules"
    PUPPET_MANIFEST="$HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp"
  fi

  # Common parameters
  DOMAIN="`echo $FQDN | cut -d . -f 2-`"
  ROLE="`hydra_yaml_param nodo::role $HYDRA_FOLDER/$DOMAIN/$FQDN.yaml`"
  LOCATION="`hydra_yaml_param nodo::location $HYDRA_FOLDER/$DOMAIN/$FQDN.yaml`"
  DEPLOY_PUPPET="puppet appy $PUPPET_OPTS $PUPPET_MANIFEST"
  DEPLOY_APPLY="$DEPLOY_COMMAND $DEPLOY_PUPPET"

  # 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
}

# Return a simple parameter from an YAML file
function hydra_yaml_param {
  local param="$1"
  local file="$2"

  grep "^$param: " $file | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'
}

# Create puppet folder structure
function hydra_deploy_mkdirs {
  # Saner defaults
  $DEPLOY_COMMAND mkdir -p       /etc/puppet
  $DEPLOY_COMMAND chown -R root. /etc/puppet
  $DEPLOY_COMMAND chmod -R 640   /etc/puppet
}

# Create hiera folder structure
function hydra_hiera_copy {
  $DEPLOY_COMMAND mkdir -p $FOLDER/etc/puppet/secrets/{domain,location,node,role}

  if [ ! -z "$DOMAIN" ] && [ -e "$HYDRA_FOLDER/puppet/hiera/secrets/domain/$DOMAIN.yaml" ]; then
    $DEPLOY_COPY $HYDRA_FOLDER/puppet/hiera/secrets/domain/$DOMAIN.yaml $DEPLOY_DEST/etc/puppet/hiera/secrets/domain/
  fi

  if [ ! -z "$LOCATION" ] && [ -e "$HYDRA_FOLDER/puppet/hiera/secrets/location/$LOCATION.yaml" ]; then
    $DEPLOY_COPY $HYDRA_FOLDER/puppet/hiera/secrets/location/$LOCATION.yaml $DEPLOY_DEST/etc/puppet/hiera/secrets/location/
  fi

  if [ ! -z "$ROLE" ] && [ -e "$HYDRA_FOLDER/puppet/hiera/secrets/role/$ROLE.yaml" ]; then
    $DEPLOY_COPY $HYDRA_FOLDER/puppet/hiera/secrets/role/$ROLE.yaml $DEPLOY_DEST/etc/puppet/hiera/secrets/role/
  fi

  if [ ! -z "$NODE" ] && [ -e "$HYDRA_FOLDER/puppet/hiera/secrets/node/$NODE.yaml" ]; then
    $DEPLOY_COPY $HYDRA_FOLDER/puppet/hiera/secrets/node/$NODE.yaml $DEPLOY_DEST/etc/puppet/hiera/secrets/node/
  fi
}