#!/bin/bash

# Node controller registration script
# Registers NC at IP $2 with cluster $1 (on CC)

# Source common functions
. /usr/share/eucalyptus/registration/common

# Parameter sanitizing
CLUSTERNAME=${1% node*}
IP=$2
testip "${IP}"

# Check if we are on the target cluster
. /etc/eucalyptus/eucalyptus-cc.conf
if [ "${CLUSTERNAME}" != "${CC_NAME}" ]; then
  reglog "Node is not a candidate for local cluster."
  exit 1
fi

# Check if node isn't already registered
. /etc/eucalyptus/eucalyptus.conf
NODES_LIST=$(cat /var/lib/eucalyptus/nodes.list 2>/dev/null || true)
for nip in "$NODES" $NODES_LIST; do
  if [ "${nip# }" == "${IP}" ]; then
    reglog "Node $IP is already registered."
    exit 1
  fi
done

/usr/sbin/euca_conf --no-rsync --skip-scp-hostcheck --register-nodes "${IP}"
reglog "euca_conf --register-nodes returned $?"
