forked from vmware-archive/public-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf-admin-create-accounts.sh
executable file
·85 lines (68 loc) · 2.6 KB
/
cf-admin-create-accounts.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
#!/bin/bash
# --------------------------------------------------------------------------------------------------------------------------------------------------
# The script expects an input text file (e.g. pas-accounts.txt) or stdin lines representing an identifier for each user (typically an email)
# ... and so on
# The script will result in one PAS SpaceDeveloper user per line, each with their own like-named ORG and a SPACE named "dev".
# For simplicity all users will have their password set to "password"
# --------------------------------------------------------------------------------------------------------------------------------------------------
function printline() {
echo && printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' - && echo $1
}
SCRIPTDIR=$(cd $(dirname "$0") && pwd -P)
printline "Operation starting"
echo
if ! which cf > /dev/null ; then
echo "cf CLI not found. Go to https://console.run.pivotal.io/tools to install. Aborting"
exit 1
fi
TIMESTAMP=$(date "+%Y%m%d%H%M%S")
LOGDIR=${SCRIPTDIR}/logs
LOGFILE=${LOGDIR}/out-${TIMESTAMP}.log
printline "Log will be created here: ${LOGFILE}"
echo
mkdir -p ${LOGDIR}
cf target | tee -a ${LOGFILE}
if [ $? -ne 0 ] ; then
exit 1 # cf target will speak for itself in this case
fi
echo
echo "If you're not logged in as admin this action will likely fail"
read -p "Are you sure you want to create users using this target?>" -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Aborting"
exit 1
fi
while true # process next file
do
printline "Processing ${1-/dev/stdin}"
while read LINE # process next line
do
LINE=$(echo ${LINE} | sed "s/^ *//g" | tr -s " ") # trim the line
if [ ${#LINE} -eq 0 ]; then # ignore blank lines
continue
fi
if echo ${LINE} | grep -q "^#"; then # ignore commented lines
continue
fi
EMAIL=${LINE}
ORG=${EMAIL}
PASSWD=password # keep it simple
SPACE=dev
ROLE=SpaceDeveloper
set -x; cf create-org ${EMAIL} | tee -a ${LOGFILE}; { set +x; } 2>/dev/null
set -x; cf create-space dev -o ${ORG} | tee -a ${LOGFILE}; { set +x; } 2>/dev/null
set -x; cf create-user ${EMAIL} ${PASSWD} | tee -a ${LOGFILE}; { set +x; } 2>/dev/null
set -x; cf set-space-role ${EMAIL} ${ORG} ${SPACE} ${ROLE} | tee -a ${LOGFILE}; { set +x; } 2>/dev/null
done < "${1:-/dev/stdin}" # process next line of $1 or STDIN
# shift to next arg (if any), and break id we're at zero args
shift
if [ $# -eq 0 ]; then
break
fi
done
printline "Operation complete"