-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgerrit-create-user.sh
116 lines (106 loc) · 3.02 KB
/
gerrit-create-user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
set -e
# Usage
usage() {
echo "Usage:"
echo " ${0} -t <ACOUNT_TYPE:ldap or internal> -u <USER> -p <PASSWORD> -f <FULL_NAME> -A <ADMIN_USER> -P <ADMIN_PASSWORD> -g <TARGET_GROUP>"
exit 1
}
# Constants configurable via environment
GERRIT_URL=${GERRIT_LOCAL_URL:-'http://localhost:8080/gerrit'}
# Constants
SLEEP_TIME=10
MAX_RETRY=10
type="internal"
while getopts "t:u:p:f:A:P:g:" opt; do
case "${opt}" in
t)
type=${OPTARG}
;;
u)
username=${OPTARG}
;;
p)
password=${OPTARG}
;;
f)
full_name=${OPTARG}
;;
A)
admin_user=${OPTARG}
;;
P)
admin_password=${OPTARG}
;;
g)
target_group=${OPTARG}
;;
*)
echo "Invalid parameter(s) or option(s)."
usage
;;
esac
done
# Validate options
case "${type}" in
ldap)
if [ -z "${username}" ] || [ -z "${password}" ]; then
echo "Parameters missing"
usage
fi
;;
internal)
if [ -z "${admin_user}" ] || [ -z "${admin_password}" ] || [ -z "${username}" ] || [ -z "${full_name}" ]; then
echo "Parameters missing"
usage
fi
;;
*)
echo "Invalid parameter(s) or option(s)."
usage
;;
esac
echo "Testing Gerrit Connection"
until curl --location --output /dev/null --silent --write-out "%{http_code}\\n" "${GERRIT_URL}/login" | grep "401" &> /dev/null
do
echo "Gerrit unavailable, sleeping for ${SLEEP_TIME}"
sleep "${SLEEP_TIME}"
done
# Check exists
username=$(echo -e "${username}" | sed 's/ /%20/g')
ret=$(curl --output /dev/null --silent --write-out "%{http_code}" "${GERRIT_URL}/accounts/${username}")
if [[ ${ret} -eq 200 ]] ; then
echo "User already exists: ${username}"
exit 0
fi
# Add user
echo "Creating user: ${username}"
count=0
until [ ${count} -ge ${MAX_RETRY} ]
do
case "${type}" in
ldap)
ret=$(curl --request POST --data "username=${username}&password=${password}" --output /dev/null --silent --write-out "%{http_code}" "${GERRIT_URL}/login")
if [[ ${ret} -eq 302 ]]; then
echo "LDAP user ${username} was found in database"
break
fi
echo "Unable to find user ${username} in LDAP database, response code ${ret}, retry ... ${count}"
;;
internal)
if [[ -z "${target_group}" ]]; then
target_group="Non-Interactive Users"
echo "Target group was not specified, defaulting to non-interactive"
fi
json_request="{ \"name\": \"${full_name}\", \"groups\": [ \"${target_group}\" ] }"
ret=$(curl --request PUT --user "${admin_user}:${admin_password}" --header 'Content-Type: application/json; charset=UTF-8' --data "${json_request}" --output /dev/null --silent --write-out "%{http_code}" "${GERRIT_URL}/a/accounts/${username}")
if [[ ${ret} -eq 201 ]]; then
echo "User ${username} was created"
break
fi
echo "Unable to create user ${username}, response code ${ret}, retry ... ${count}"
;;
esac
count=$((count+1))
sleep ${SLEEP_TIME}
done