#!/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

SHORT_COMMAND="euca_conf --register-nodes"
REAL_COMMAND="/usr/sbin/euca_conf --no-rsync --skip-scp-hostcheck --register-nodes "${IP}""
COMMAND_OUTPUT=$(eval $REAL_COMMAND)
STATUS=$?

if [ $STATUS -eq 0 ];then
   reglog "$SHORT_COMMAND returned SUCCESS"
else
   reglog "$SHORT_COMMAND returned FAILURE (error $STATUS): Command attempted was $REAL_COMMAND, and had the following output: $COMMAND_OUTPUT"
fi


