aboutsummaryrefslogtreecommitdiff
path: root/share/hydra/newnode
blob: 7cb7ae44edb72e7f999f9b59ec6e829cface9c4c (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
#!/bin/bash
#
# Create new node configuration.
#
# 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

# Parameters
BASENAME="`basename $0`"
NODE="$1"
NODES="$HYDRA_FOLDER/puppet/manifests/site.pp"

# Check configuration and parameters
if [ -z "$NODE" ]; then
  echo "usage: hydra $BASENAME <node>"
  exit 1
elif [ ! -e "$HYDRA_FOLDER/config/domain" ]; then
  echo "fatal: please configure your domain at $HYDRA_FOLDER/config/domain"
  exit 1
elif [ ! -f "$NODES" ]; then
  echo "fatal: $NODES not found"
  exit 1
elif grep -qe "^import \"nodes/$NODE.pp\"$" $NODES &> /dev/null; then
  echo "fatal: node $NODE already defined"
  exit 1
fi

# Domain
DOMAIN="`cat $HYDRA_FOLDER/config/domain`"

# Create node
echo "import \"nodes/$NODE.$DOMAIN.pp\"" >> $NODES

# Set YAML template
if [ -e "$HYDRA_FOLDER/config/templates/node/nodo.example.org.yaml" ]; then
  YAML="$HYDRA_FOLDER/config/templates/node/nodo.example.org.yaml"
else
  YAML="$APP_BASE/share/config/templates/node/nodo.example.org.yaml"
fi

# Set node template
if [ -e "$HYDRA_FOLDER/config/templates/node/nodo.pp" ]; then
  PP="$HYDRA_FOLDER/config/templates/node/nodo.example.org.pp"
else
  PP="$APP_BASE/share/config/templates/node/nodo.example.org.pp"
fi

# Copy YAML template
mkdir -p $HYDRA_FOLDER/puppet/hiera/production/domain/$DOMAIN/node
cp $YAML $HYDRA_FOLDER/puppet/hiera/production/domain/$DOMAIN/node/$NODE.$DOMAIN.yaml

# Copy node template
mkdir -p $HYDRA_FOLDER/puppet/manifests/nodes
cp $PP $HYDRA_FOLDER/puppet/manifests/nodes/$NODE.$DOMAIN.pp

# Edit the template
sed -i -e "s/nodename/$NODE/g"      $HYDRA_FOLDER/puppet/manifests/nodes/$NODE.pp
sed -i -e "s/example.org/$DOMAIN/g" $HYDRA_FOLDER/puppet/manifests/nodes/$NODE.pp

# Add to git
(
  cd $HYDRA_FOLDER/puppet
  git add manifests/site.pp manifests/nodes/$NODE.pp hiera/production/domain/$DOMAIN/node/$NODE.$DOMAIN.yaml
)