Created
May 25, 2012 09:03
-
-
Save kssminus/2786818 to your computer and use it in GitHub Desktop.
Gitlab 2.5.0 script automatically add all users to all projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
############################### | |
## | |
baseurl=YOUR_GITLAB_URL | |
logdir=YOUR_LOG_DIRECTORY | |
basedir=YOUR_BASE_DIRECTORY | |
usermail=YOUR_ADMIN_MAIL | |
userpass=YOUR_ADMIN_PASSWORD | |
project_access=20 #00=deined 10=read 20=report 30=admin | |
## SIMPLE LOGGER | |
## $logdir default => /var/log | |
## logfile path & name : $logdir/2012-XX-XX.log | |
## log type : [16:00:36]loggingtest | |
function logger(){ | |
user=`whoami` | |
filename=`date +%Y-%m-%d` | |
filename=$filename.log | |
if [ ! $logdir ]; then | |
logdir=/var/log | |
fi | |
if [ ! -d $logdir ]; then | |
mkdir -p $logdir && chown -R $user $logdir | |
fi | |
if [ "$1" ]; then | |
now=`date +%T` | |
echo [$now]$1 >> $logdir/$filename | |
fi | |
} | |
## Is there excuted process? | |
if [ -e $basedir/cookies.txt ] | |
then | |
logger "There is running script : add_users_to_projects" | |
exit | |
fi | |
start=`date +%s` | |
logger "===========================================================================================" | |
logger "adding user process start" | |
#get login page's authtoken | |
authtoken=`curl -s -c $basedir/cookies.txt $baseurl/users/sign_in | grep -Po '(?<=<meta content=")([^"]+)(?=" name="csrf-token" />)'` | |
logger "Login page's auth token & cookies acquired : $authtoken" | |
# login | |
authtoken=${authtoken//+/%2b} #when csrf token includes '+' character, authentication is failed, it must be URI encoded | |
curl -s -b $basedir/cookies.txt -c $basedir/cookies.txt -d "utf8=%E2%9C%93&authenticity_token=$authtoken&user[email]=$usermail&user[password]=$userpass&commit=Sign+in" $baseurl/users/sign_in | |
logger "Login completed " | |
#get projects list | |
projectslist=`mysql -ugitlab -pYOUR_GITLAB_DB_ACCOUNT -h YOUR_DB_HOST < $basedir/select_projects.sql` | |
projectscount=`echo "$projectslist" | wc -l` | |
let "projectscount-=1" #remove column name | |
logger "Gitlab project count : $projectscount" | |
#adding users loop | |
for project in $projectslist | |
do | |
if [ "$project" != "name" ]; #database column name pass | |
then | |
logger "Project name : $project" | |
# get admin project pages and store it to project_page.html | |
curl -i -s -b $basedir/cookies.txt $baseurl/admin/projects/$project > $basedir/project_page.html | |
# parse user_ids | |
userIdsStr=`grep -Po '(<select id="user_ids")([^\}]+)(<select)' $basedir/project_page.html | grep -Po '(?<=option value=")\d+(?=">)'|tr '\n' ','|sed 's/,/\&user_ids\[\]=/g'` | |
userIdsLog=`grep -Po '(<select id="user_ids")([^\}]+)(<select)' $basedir/project_page.html | grep -Po '(?<=option value=")\d+(?=">)'|tr '\n' ','` | |
if [ ${#userIdsLog} = 0 ]; | |
then | |
logger "Added user Ids : no new users" | |
else | |
postdata="utf8=%E2%9C%93&_method=put&authenticity_token=$authtoken&user_ids[]=$userIdsStr&project_access=$project_access&commit=Add" | |
echo | |
curl -s -b $basedir/cookies.txt -d $postdata $baseurl/admin/projects/$project/team_update | |
logger "Added user Ids : $userIdsLog" | |
fi | |
fi | |
done | |
end=`date +%s` | |
let "end-=$start" | |
logger "elapsed time : $end" | |
logger "batch ended" | |
rm -f $basedir/cookies.txt $basedir/project_page.html # remove temp files | |
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Gitlab adding users to projects | |
*/10 8-23 * * * /home/gitlab/bin/add_user/add_users_to_projects.sh >> /home/gitlab/logs/adduser_user_gitlab/`date +\%Y-\%m`_system.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use gitlab;select path from projects; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment