aboutsummaryrefslogtreecommitdiff
path: root/share/keyringer/newkeys
blob: 84d1c275027b736252a5234a4e40db4a3891dcdd (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
#!/bin/bash
#
# Create keys for new nodes.
#

function newkeys_nodes {
  # See http://www.mail-archive.com/puppet-users@googlegroups.com/msg01615.html
  grep ^node $* | sed -e 's/^node //' | awk -F, '{for(i=1;i<=NF;i++) {print $i}}' | cut -d "'" -f2
}

function newkeys_puppet {
  # Generates ssh and gpg keys for new nodes
  # GPG keys should be manually imported in the nodes

  if [ -e "$PUPPET/manifests/nodes.pp" ]; then
    nodes="`newkeys_nodes $PUPPET/manifests/nodes.pp`"
  fi

  if [ -d "$PUPPET/manifests/nodes" ]; then
    nodes="$nodes `newkeys_nodes $PUPPET/manifests/nodes/*`"
  fi

  for host in $nodes; do
    node="`echo $host | cut -d . -f 1`"
    privkey="$PUPPET_KEYS/"$node"_id_dsa"
    pubkey="$privkey.pub"
    if [ ! -e "$privkey" ] || [ ! -e "$pubkey" ]; then
      keyringer_exec genpair $BASEDIR ssh $node/ssh/id_dsa $host $privkey
      keyringer_exec genpair $BASEDIR gpg $node/gpg/key    $host

      # Add key into puppet git repository
      ( cd $PUPPET_KEYS && git add $privkey $pubkey )
    fi
  done
}

# Load functions
LIB="`dirname $0`/../../lib/keyringer"
source $LIB/functions || exit 1

# Command parser
keyringer_get_command $2

source $OPTIONS
keyringer_config_load_preferences

if [ -z "$PUPPET" ]; then
  "Error: you have to setup PUPPET path at your preferences for this keyring."
fi

if [ -z "$PUPPET_KEYS" ]; then
  PUPPET_KEYS="$PUPPET/files/keys"
fi

# Right now just puppet backend is supported
if [ "$COMMAND" == "puppet" ]; then
  newkeys_puppet
else
  echo "No such option $COMMAND"
  exit 1
fi