blob: 08a32e8419bed2236215a18e1b7062bebd7b1cff [file] [log] [blame]
#!/bin/bash
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
# This script adds a userid and configures it for Trafodion
#=========================================
# Default values
typeset TRAF_GROUP="trafodion"
export PDSH="pdsh -R exec"
export PDSH_SSH_CMD="ssh -q -n %h"
export PDCP="pdcp -R ssh"
#=========================================
# get the default $HOME directory for this system
if [[ $HOME_DIR == "/home" ]]; then
default_home=$(sudo cat /etc/default/useradd | grep -E "^HOME=" | sed -e "s@HOME=@@" )
else
default_home="$HOME_DIR"
fi
if [ "$default_home" == "" ]; then
default_home="$HOME_DIR"
fi
typeset TRAF_USER_DIR="$default_home/$TRAF_USER"
echo "***INFO: Trafodion userid's ($TRAF_USER) home directory: $TRAF_USER_DIR"
#============================
# check that we are running from a userid with sudo access
sudo echo "***INFO: testing sudo access"
if [ $? -ne 0 ]; then
echo "***ERROR: This script must be run from userid with sudo access."
exit -1
fi
#==============================
# Find an unused uid and gid on all nodes for the Trafodion userid.
# Need to make sure the uid and gid are free on all nodes in the cluster,
# just because they are unused on one node doesn’t mean they will be
# unused on another node.
# Create Trafodion group if there is not one already
if [[ "$SUSE_LINUX" == "true" ]]; then
traf_gid=$(cat /etc/group | grep $TRAF_GROUP | sed -e 's@^.*:!:\([0-9]*\):.*@\1@'| grep --max-count=1 -E "[0-9]")
else
traf_gid=$(cat /etc/group | grep $TRAF_GROUP | sed -e 's@^.*:x:\([0-9]*\):.*@\1@'| grep --max-count=1 -E "[0-9]")
fi
if [ -z "$traf_gid" ]; then
# Trafodion group does not exist need to find an unused gid and create it
found=1
traf_gid=500 # start at gid 500 (just a randomly chosen start)
while [ $found -ne 0 ]
do
((traf_gid++))
if [ $all_node_count -eq 1 ]; then
found=$(cat /etc/group | sed -e 's@^.*:x:\([0-9]*\):.*@\1@' | grep ^$traf_gid$ 2>/dev/null | wc -l)
else
found=$($TRAF_PDSH "cat /etc/group | sed -e 's@^.*:x:\([0-9]*\):.*@\1@' | grep ^$traf_gid$ " 2>/dev/null | wc -l)
fi
done
$TRAF_PDSH sudo /usr/sbin/groupadd --gid $traf_gid $TRAF_GROUP
if [ $? -ne 0 ]; then
if [ $? -ne 9]; then
echo "***ERROR: unable to create Trafodion group: $TRAF_GROUP"
exit -1
fi
fi
else
# Make sure Trafodion group exists on all nodes and create it if it is not
$TRAF_PDSH sudo /usr/sbin/groupadd --gid $traf_gid $TRAF_GROUP
return_code=$?
if [ $return_code -ne 0 ]; then
# if it is not a duplicate group error then we have a problem
if [ $return_code -ne 9 ]; then
echo "***ERROR: unable to create Trafodion group: $TRAF_GROUP"
exit -1
fi
fi
fi
#=================================================
# Create Trafodion user, if not already created
found=1
traf_uid=500 # start at uid 500 (just a randomly chosen start)
while [ $found -ne 0 ]
do
((traf_uid++))
if [ $all_node_count -eq 1 ]; then
found=$(cat /etc/passwd | sed -e 's@^.*:x:\([0-9]*\):\([0-9]*\):.*:.*$@\1@' | grep ^$traf_uid$ 2>/dev/null | wc -l)
else
found=$($TRAF_PDSH "cat /etc/passwd | sed -e 's@^.*:x:\([0-9]*\):\([0-9]*\):.*:.*\$@\1@' | grep ^$traf_uid$ " 2>/dev/null | wc -l)
fi
done
# Make sure all prefix directories of TRAF_USER_DIR exist or
# useradd command will fail
$TRAF_PDSH sudo mkdir -p $default_home
userid_already_exists="N"
$TRAF_PDSH sudo /usr/sbin/useradd \
--shell /bin/bash \
--home $TRAF_USER_DIR -m \
--gid $traf_gid \
--password "$(openssl passwd $TRAF_USER_PASSWORD)" \
--uid $traf_uid \
$TRAF_USER
return_code=$?
if [ $return_code -ne 0 ]; then
if [ $return_code -eq 9 ]; then
echo "***WARNING: username already exists, will use existing userid, $TRAF_USER..."
echo "***WARNING: ...and assume it was setup properly with passwordless ssh access to all nodes."
else
echo "***ERROR: unable to create Trafodion userid: $TRAF_USER.$TRAF_GROUP, RC=$return_code"
exit -1
fi
fi
#=================================================
# Setup passwordless ssh for this Trafodion userid
#if [ "$userid_already_exists" == "Y" ]; then
# echo "***INFO: skipping passwordless ssh setup because userid ($TRAF_USER) already exists"
#else
sudo su $TRAF_USER --command "echo -e 'y\n' | ssh-keygen -t rsa -N \"\" -f $TRAF_USER_DIR/.ssh/id_rsa"
if [[ "$SUSE_LINUX" == "true" ]]; then
sudo su $TRAF_USER --command "cp $TRAF_USER_DIR/.ssh/id_rsa.pub $TRAF_USER_DIR/.ssh/authorized_keys"
else
sudo su $TRAF_USER --command "cp $TRAF_USER_DIR/.ssh/id_rsa.pub $TRAF_USER_DIR/.ssh/authorized_keys"
fi
sudo su $TRAF_USER --command "echo localhost $(cat /etc/ssh/ssh_host_rsa_key.pub) > $TRAF_USER_DIR/.ssh/known_hosts"
sudo su $TRAF_USER --command "echo \"NoHostAuthenticationForLocalhost=yes\" >> $TRAF_USER_DIR/.ssh/config"
sudo chmod 600 $TRAF_USER_DIR/.ssh/config
#fi
if [ "$userid_already_exists" == "Y" ]; then
if [ -f $TRAF_USER_DIR/bashrc_backup ]; then
echo "***INFO: preserving bashrc_backup"
else
sudo cp $TRAF_USER_DIR/.bashrc $TRAF_USER_DIR/bashrc_backup
sudo chown $TRAF_USER.$TRAF_GROUP $TRAF_USER_DIR/bashrc_backup
fi
fi
sudo cp $LOCAL_WORKDIR/bashrc_default $TRAF_USER_DIR/.bashrc
if [[ -f $TRAF_WORKDIR/installer/sqenvcom.sh ]]; then
sudo cp $TRAF_WORKDIR/installer/sqenvcom.sh $TRAF_USER_DIR
sudo chown $TRAF_USER.$TRAF_GROUP $TRAF_USER_DIR/sqenvcom.sh
fi
sudo chown $TRAF_USER.$TRAF_GROUP $TRAF_USER_DIR/.bashrc
#=================================================
# copy TRAF_USER_DIR to all other nodes if more than one
if [ "$all_node_count" -ne 1 ]; then
# Have to copy in a round-about way since passwordless
# ssh is not setup yet for the Trafodion userid and pdcp
# may not work with sudo.
echo "***INFO: Setting up userid $TRAF_USER on all other nodes in cluster"
sudo rm -rf $TRAF_WORKDIR/$TRAF_USER 2>/dev/null
sudo cp -r $TRAF_USER_DIR $TRAF_WORKDIR
sudo rm -rf $TRAF_WORKDIR/$TRAF_USER/.pulse 2>/dev/null
sudo chown -R $USER.$(id -ng) $TRAF_WORKDIR/$TRAF_USER
$PDCP -r $ALL_NODES -x $HOSTNAME $TRAF_WORKDIR/$TRAF_USER $TRAF_WORKDIR
$PDSH $ALL_NODES -x $HOSTNAME $PDSH_SSH_CMD sudo cp -r $TRAF_WORKDIR/$TRAF_USER $default_home
$PDSH $ALL_NODES -x $HOSTNAME $PDSH_SSH_CMD sudo chown -R $TRAF_USER.$TRAF_GROUP $TRAF_USER_DIR
fi
#=================================================
# Cycle through all nodes to create known_hosts file, even if only one node
echo "***INFO: Creating known_hosts file for all nodes"
NODES_SHORT=""
for node in $ALL_NODE_LIST
do
newNode=$(ssh -q -n $node hostname -s)
NODES_SHORT="$NODES_SHORT $newNode"
done
NODES_LONG=""
for node in $ALL_NODE_LIST
do
newNode=$(ssh -q -n $node hostname -f)
NODES_LONG="$NODES_LONG $newNode"
done
./traf_createPasswordLessSSH --nodes "$ALL_NODE_LIST"
./traf_createPasswordLessSSH --nodes "$NODES_SHORT"
./traf_createPasswordLessSSH --nodes "$NODES_LONG"
if [ "$all_node_count" -ne 1 ]; then
# Copy known_hosts file to all other nodes
sudo su $TRAF_USER --command "$PDCP $ALL_NODES -x $HOSTNAME $TRAF_USER_DIR/.ssh/known_hosts $TRAF_USER_DIR/.ssh"
fi
#=================================================
echo "***INFO: $TRAF_USER user added successfully"