Skip to content

Instantly share code, notes, and snippets.

@sanmadjack
Created January 23, 2013 22:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sanmadjack/4615123 to your computer and use it in GitHub Desktop.
Save sanmadjack/4615123 to your computer and use it in GitHub Desktop.
Jenkins User Import/Export
# This should be changed to your system's jenkins root JENKINS_ROOT="/var/lib/jenkins"
JENKINS_USERS="$JENKINS_ROOT/users"
JENKINS_JOBS="$JENKINS_ROOT/jobs"
JENKINS_CONFIG="$JENKINS_ROOT/config.xml"
# This variable is used so that we can verify that a user is not found
FAIL_TEST=0
exportUserFolder() {
if [ -d "$JENKINS_USERS/$1" ]; then
cp -R "$JENKINS_USERS/$1" ./
else
FAIL_TEST=`expr $FAIL_TEST + 1`
fi
}
exportUserPermissions() {
if [ $(grep -c "^.*.*:$1<\/permission>.*" $JENKINS_CONFIG) -ne 0 ]; then
grep "^.*.*:$1<\/permission>.*" $JENKINS_CONFIG > $1.perm
else
FAIL_TEST=`expr $FAIL_TEST + 1`
fi
}
exportUsers() {
# Pre-work cleanup
if [ -d "working" ]; then
rm -R -f working
fi
if [ -f "user.tar" ]; then
rm -R -f user.tar
fi
mkdir working
cd working
# Checks if any users have been specified
if [ ! -z $1 ]; then
# Iterates over the users if they have
for u in $*; do
exportUserFolder $u
exportUserPermissions $u
# if FAIL_TEST is 2, that means both of the above functions failed to find a user
if [ "$FAIL_TEST" -ge "2" ]; then
echo "ERROR: User $u is not found!"
exit 1
fi
FAIL_TEST=0
done
else
# Otherwise we export everything!
# Copies ALL user folders
for f in `ls "$JENKINS_USERS"`; do
exportUserFolder $f
done
# Extracts all the users for who we have global permissions
for u in `sed -n -e 's/^.*.*:\(.*\)<\/permission>.*/\1/p' $JENKINS_CONFIG | uniq | sort`; do
exportUserPermissions $u
done
fi
# create the user.tar
# We only do this once, at the very end, so that if this fails for any reason a user.tar is not created,
# so we don't risk having a bad artifact built from the job
tar cv --file user.tar *
# Pop up to the workspace
cd ..
# move user.tar out of working into the workspace so that it can by archvied by jenkins
mv working/user.tar ./user.tar
# post-work cleanup
rm -R -f working
}
exportUsers $USERS
INPUT_FILE="user.tar"
# This should be changed to your system's jenkins root JENKINS_ROOT="/Users/Shared/Jenkins/Home"
JENKINS_USERS="$JENKINS_ROOT/users"
JENKINS_JOBS="$JENKINS_ROOT/jobs"
JENKINS_CONFIG="$JENKINS_ROOT/config.xml"
# This variable is used so that we can verify that a user is not found
FAIL_TEST=0
importUserPermissions() {
if [ $PERMISSIONS = "true" ]; then
if [ -f "$1.perm" ]; then
# delete existing permissions
sed -i -e "/^.*.*:$1<\/permission>.*/d" $JENKINS_CONFIG
# Insert new permissions
while read line; do
sed -i -e "/^.*<\/authorizationStrategy>.*/ i\\
$line" $JENKINS_CONFIG
done < "$1.perm"
else
FAIL_TEST=`expr $FAIL_TEST + 1`
fi
fi
}
importUserFolder() {
if [ -d "$JENKINS_USERS/$1" ] && [ "$REPLACE_CONFIG" = "false" ]; then
echo "User folder for $1 already exists."
echo "Enable REPLACE_CONFIG to overwrite existing user folders"
else
if [ -d "$1" ]; then
cp -R "./$1" "$JENKINS_USERS/"
else
FAIL_TEST=`expr $FAIL_TEST + 1`
fi
fi
}
importUsers() {
if [ -d "working" ]; then
rm -R -f working
fi
mkdir working
cd working
tar xf "../$INPUT_FILE"
# Checks if any users have been specified
if [ ! -z $1 ]; then
# Iterates over the users if they have
for u in $*; do
importUserFolder $u
importUserPermissions $u
# if FAIL_TEST is 2, that means both of the above functions failed to find a user
if [ "$FAIL_TEST" -ge "2" ]; then
echo "ERROR: User $u is not found!"
exit 1
fi
FAIL_TEST=0
done
else
# Otherwise we import everything!
# Copies ALL user folders
for f in `ls -l | awk '/^d.*/{print $(NF);}'`; do
importUserFolder $f
done
for f in `ls *.perm | sed 's/\(.*\)\..*/\1/'`; do
importUserPermissions $f
done
fi
# Pop up to the workspace
cd ..
# post-work cleanup
rm -R -f working
rm user.tar
}
importUsers $USERS
@douglasawh
Copy link

There is a config.xml in my JENKINS_ROOT for each of the users I am trying to import, but they don't actually work. Any thoughts as to why?

@sanmadjack
Copy link
Author

There is a config.xml in my JENKINS_ROOT for each of the users I am trying to import, but they don't actually work. Any thoughts as to why?

Unfortunately, no. This script was written 10 years ago, I don't even know if it still works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment