aboutsummaryrefslogtreecommitdiff
path: root/lib/hydra/deploy
blob: 771c42eac5ded301608ca904bf0df84c2b1bf853 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash

# Setup deployment parameters
function hydra_deploy_setup {
  # Common parameters
  # Exclude eventual keys and version control files
  DEPLOY_DEPENDENCIES="puppet-common ruby-sqlite3 ruby-activerecord ruby-activerecord-deprecated-finders augeas-tools"
  DEPLOY_RSYNC="rsync -CrltDvpq --no-owner --exclude=/ssl --exclude=keys --exclude=site_keys --exclude=hiera/secrets --delete --rsync-path"
  RSYNC_PATH="rsync -q"

  if [ "$1" == "remote" ]; then
    # Deploy in a remote host
    if [ ! -z "$2" ]; then
      NODE="$2"
      DEPLOY_BASE="/etc"
      DEPLOY_COMMAND="$HYDRA_CONNECT $NODE sudo"
      FQDN="`$DEPLOY_COMMAND cat /etc/hostname`"
      DEPLOY_OPTS="$HYDRA_FOLDER/puppet/ $NODE:/etc/puppet/"
      DEPLOY_DEST="$FQDN:"
      RSYNC_PATH="sudo rsync -q"
      REMOTE_ENV="LC_ALL=C"
      hydra_deploy_set_manifest
    else
      echo "No folder specified."
      exit 1
    fi
  elif [ "$1" == "folder" ]; then
    # Deploy in a local folder
    if [ ! -z "$2" ]; then
      FOLDER="$2"
      DEPLOY_BASE="/etc"
      DEPLOY_COMMAND="$SUDO chroot $FOLDER"
      DEPLOY_RSYNC="$SUDO $DEPLOY_RSYNC"
      DEPLOY_OPTS="$HYDRA_FOLDER/puppet/ $FOLDER/etc/puppet/"
      DEPLOY_DEST="$FOLDER"

      # Fix hostname
      if [ -s "$FOLDER/etc/hostname" ]; then
        FQDN="`cat $FOLDER/etc/hostname`"
      fi

      hydra_deploy_set_manifest

      if [ ! -d "$FOLDER" ]; then
        echo "folder not found: $FOLDER"
        exit 1
      fi
    else
      echo "No node specified."
      exit 1
    fi
  else
    # Deploy on the localhost
    DEPLOY_BASE="$HYDRA_FOLDER"
    DEPLOY_COMMAND="$SUDO"
    DEPLOY_RSYNC=""
    FQDN="`cat /etc/hostname`"
    PUPPET_OPTS="--confdir=$HYDRA_FOLDER/puppet --modulepath=$HYDRA_FOLDER/puppet/modules"
    hydra_deploy_set_manifest $HYDRA_FOLDER
  fi

  # Common parameters
  DOMAIN="`echo $FQDN | cut -d . -f 2-`"
  ROLE="`hydra_yaml_param nodo::role $HYDRA_FOLDER/puppet/hiera/node/$FQDN.yaml`"
  LOCATION="`hydra_yaml_param nodo::location $HYDRA_FOLDER/puppet/hiera/node/$FQDN.yaml`"

  # Puppet command
  if [ -e "$HYDRA_FOLDER/puppet/bin/deploy" ]; then
    DEPLOY_PUPPET="$DEPLOY_BASE/puppet/bin/deploy"
  else
    DEPLOY_PUPPET="$REMOTE_ENV puppet apply $PUPPET_OPTS $PUPPET_MANIFEST"
  fi

  # Deployment command
  DEPLOY_APPLY="$DEPLOY_COMMAND $DEPLOY_PUPPET"
}

# Manifest
function hydra_deploy_set_manifest {
  local prefix="$1"

  if [ -z "$prefix" ]; then
    prefix="/etc"
  fi

  if [ -e "$HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp" ]; then
    PUPPET_MANIFEST="$prefix/puppet/manifests/nodes/$FQDN.pp"
  elif [ -e "$HYDRA_FOLDER/puppet/manifests/nodes/default.pp" ]; then
    PUPPET_MANIFEST="$prefix/puppet/manifests/nodes/default.pp"
  else
    echo "no manifest found for $FQDN"
    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
}

# Copy hiera secrets
function hydra_deploy_copy_secrets {
  local location="$1"

  if [ -z "$location" ]; then
    location="remote"
  fi

  $DEPLOY_COMMAND mkdir -p $FOLDER/etc/puppet/hiera/secrets/{domain,location,node,role}

  if [ ! -z "$DOMAIN" ] && [ -e "$HYDRA_FOLDER/puppet/hiera/secrets/domain/$DOMAIN.yaml" ]; then
    hydra_deploy_copy $location $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
    hydra_deploy_copy $location $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
    hydra_deploy_copy $location $HYDRA_FOLDER/puppet/hiera/secrets/role/$ROLE.yaml $DEPLOY_DEST/etc/puppet/hiera/secrets/role/
  fi

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

# Copy a single file
function hydra_deploy_copy {
  local location="$1"
  local orig="$2"
  local dest="$3"

  if [ "$location" == "folder" ]; then
    $SUDO mkdir -p $dest
    $SUDO cp $orig $dest
  elif [ "$location" == "remote" ]; then
    $DEPLOY_RSYNC "$RSYNC_PATH" $orig $dest
  fi
}