2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-31 10:06:39 +00:00
Miro aaacca80cd Fix merging in xdcpmerge.sh (#4328)
* Fixes in xdcpmerge.sh

Two fixes:
1. The grep pattern when finding duplicate usernames is missing ":" at the end. So, for example user "test" would also match "test2, etc.". Adding the ":" delimiter fixes the issue.
2. Another issue happens when the file to be merged is a superset of the files on the nodes. For example, if a new user is added and entire passwd file (that is otherwise identical) is sent to be merged. In this case, the $filebackup.nodups file, i.e. the original file with duplicates removed, becomes empty and the condition "if [ -s "$filebackup.nodups" ]" does not execute. Then the merged file ends up being original file with the merge file fully appended, clearly not what was intended.

This is solved by changing the condition to check for file existence "-a" rather then for size. Additionally, I also turn the logic around so that the duplicates are removed from the merge file and then added to the original file. I think this makes logic a bit cleaner and also ensures that existing entries are not reordered or changed in any way.

* Streamlining previous commit

Adjustment to previous commit, streamlining and simplifying logic. Once $mergefile.nodups is created, just concatenate it the original file.

* Update to xdcpmerge

No need to copy $filebackup to $curfile, they are the same.
2017-11-26 20:20:49 -06:00

111 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# This script is used by the xdcp MERGE: function to perform the
# merge operation on the nodes for the /etc/passwd, /etc/shadow and
# and /etc/group files. These are the only supported files for merge.
# The MERGE function is also only supported on Linux.
#First parameter is nodesyncfiledir
#The next parameter is the MERGE clause put in the following format
#mergefile1:currfile1 mergefile2:currfile2.....
#For example:
#/tmp/myusers:/etc/passwd /tmp/mypasswds:/etc/shadow /tmp/mygrps:/etc/group
#
# Check for Linux, AIX not supported
if [ "$(uname -s)" != "Linux" ]; then
logger -t xcat -p local4.err "Merge: xdcp merge is only supported on Linux"
exit 1
fi
#this is the base path to the merge directory
nodesyncfiledir=$1
#this is the backup copy of the original current file
nodesyncfiledirorg="$nodesyncfiledir/org"
#this is the path to merge working files
nodesyncfiledirmerge="$nodesyncfiledir/merge"
#this is the path to the files containing the merge data
nodesynfiledirmergefiles="$nodesyncfiledir/merge/mergefiles"
skip=0
# for each input parameter
for i in $*; do
# skip first parm
if [ $skip -eq 0 ]; then
skip=1
continue
fi
# get the merge file path and name, for example /tmp/myusers
mergefilename=`echo "$i"|cut -d ':' -f 1`
# get location on the node
mergefile="$nodesynfiledirmergefiles$mergefilename"
# get the file path and name to merge into, for example /etc/passwd
curfile=`echo "$i"|cut -d ':' -f 2`
# if curfile not /etc/passwd or /etc/shadow or /etc/group
# exit error
#if [ "$curfile" != "/etc/passwd" ] && [ "$curfile" != "/etc/shadow" ] && [ "$curfile" != "/etc/group" ]; then
# logger -t xcat -p local4.err "Merge: $curfile is not /etc/passwd or /etc/shadow or /etc/group. It cannot be processes."
# exit 1
#fi
# get the directory to backup the original file
curfiledir=`dirname $curfile`
curfilename=`basename $curfile`
filebackupdir="$nodesyncfiledirorg$curfiledir"
# name of the path to the backup of original file
filebackup="$nodesyncfiledirorg$curfile"
# now do the work
# make the necessary directories
mkdir -p $filebackupdir
# copy current to backup
cp -p $curfile $filebackup
# Go though the backup copy and remove duplicate lines that are
# in the merge file and create a new backup
# first get a list of duplicate lines to remove
# based on only username: the first field in the file
cut -d: -f1 $filebackup > $filebackup.userlist
cut -d: -f1 $mergefile > $mergefile.userlist
comm -12 <(sort $filebackup.userlist | uniq) <(sort $mergefile.userlist | uniq) > $filebackup.remove
# now if there is a remove file, use it to remove the dup lines in backup
# Need to buit a command like the following with all users
#grep -v -E ^(root|bin|...) $filebackup > $filebackup.nodups
if [ -s "$filebackup.remove" ]; then
grepcmd="grep -v -E "
removeusers=`cat $filebackup.remove`
startlist="'^("
userlist=$startlist
delim="|"
for u in $removeusers
do
# For special char +/-, need to escape
[[ "${u:0:1}x" =~ ^\+|\-x ]] && uu="\\$u" || uu="$u"
userlist=$userlist$uu$delim
done
# remove the last delimiter
userlisttmp="${userlist%?}"
listend="):'"
userlist=$userlisttmp$listend
grepcmd=$grepcmd$userlist
#set -x
grepcmd="$grepcmd $mergefile > $mergefile.nodups"
#echo "grepcmd=$grepcmd"
# now run it
eval $grepcmd
fi
# add new entries from mergefile, if any
if [ -a "$mergefile.nodups" ]; then
cat $mergefile.nodups >> $curfile
rm $mergefile.nodups
fi
# now cleanup
rm $filebackup.userlist
# echo "rm $filebackup.userlist"
rm $filebackup.remove
# echo "rm $filebackup.remove"
rm $mergefile.userlist
# echo "rm $mergefile.userlist"
done
exit 0