-
Notifications
You must be signed in to change notification settings - Fork 0
/
createUsers.sh
executable file
·133 lines (123 loc) · 4.26 KB
/
createUsers.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
#
# Create dummy users in SDC
# Usage:
# ./createUsers.sh <number>
# Where number is the number of users to create
#
# After executing this script, you can lauch createSMs.sh
#
NB_USERS=$1
if [ -z $NB_USERS ]
then
echo "Usage: "
echo " ./createUsers.sh <number>"
echo " Where number is the number of users to create"
exit 1
fi
###############################################
# Change accordingly #
###############################################
# Cred & hosts infos for CAPI
ADMIN="admin"
PASS="password"
CAPI="10.99.99.11:8080"
#Timeout for `curl` requests is 10 secs
TIMEOUT="10"
# Log file
LOG_FILE="./output_createUsers.log"
echo "" > $LOG_FILE
#SSH Pub Key file location
KEY_FILE="${HOME}/.ssh/id_rsa.pub"
# Used for usernames when generating clients
DICTIONARY="./propernames"
DATACENTER_NAME="PAR1FR"
RAISE_LIMIT_TO="5"
###############################################
###############################################
###############################################
#
# Get the max clients that is possible to create
# (= number of names in the dict)
#
function maxClient {
MAX_CLIENTS=`wc -l $DICTIONARY | awk '{print $1}'`
if [ $NB_USERS -gt $MAX_CLIENTS ]
then
NB_USERS=$MAX_CLIENTS;
echo -e "Cannot create more than $MAX_CLIENTS clients due to dictionary limitations.\nNumber of clients to generate has been set to $NB_USERS."
fi
}
#
# Verify that there is a key file in the default location
# Edit KEY_FILE if necessary
#
function verifyKeys {
if [ ! -f $KEY_FILE ]
then
echo "Your RSA Public Key cannot be found in $KEY_FILE"
echo "Edit the KEY_FILE variable and/or create a new key using:"
echo " ssh-keygen -t rsa"
exit 1
fi
}
#
# This where the job is done
# Create NB_USERS thanks to CAPI
#
function createUsers {
i=1
while [ $i -le $NB_USERS ]
do
#Get ith line from the dictionnary and lower case it
name=`sed -n "${i}"p $DICTIONARY|tr '[A-Z]' '[a-z]'`
echo "[`date "+%y-%m-%d|%H:%M:%S"`] -----------Creating user ${name}-------------" >> ${LOG_FILE}
#Do the request
ERROR=`curl -is -u ${ADMIN}:${PASS} --connect-timeout ${TIMEOUT} -H "Accept:application/json" --url http://${CAPI}/customers -X POST -d customer='{"login":"'${name}'","email_address":"'${name}'@local.lan","password":"secret","password_confirmation":"secret"}' | tee -a ${LOG_FILE} | grep errors`
echo "[`date "+%y-%m-%d|%H:%M:%S"`] ------------------------------------------------------------------------------------"
if [ -n "$ERROR" ];
then
echo "[`date "+%y-%m-%d|%H:%M:%S"`] Creating user number $i: $name - NOT OK - $ERROR"
else
echo "[`date "+%y-%m-%d|%H:%M:%S"`] Creating user number $i: $name - OK"
# Add the RSA PUB KEY to the user and raise default provisionning limit
addKey
raiseLimits
fi
echo -e "\n[`date "+%y-%m-%d|%H:%M:%S"`] -----------End Creating user ${name}-------------" >> ${LOG_FILE}
i=`expr $i + 1`
done
}
#
# Add the ssh rsa pub key to the customer
# so we can provision with the createSMs.sh script
#
function addKey {
RSA_KEY=`cat ${KEY_FILE}`
ERROR=`curl -is -u ${ADMIN}:${PASS} --connect-timeout ${TIMEOUT} -H "Accept:application/json" --url http://${CAPI}/customers/${name}/keys -F key="${RSA_KEY}" -F name=pubkey -X POST | tee -a ${LOG_FILE} | grep errors`
if [ -n "$ERROR" ]
then
echo "[`date "+%y-%m-%d|%H:%M:%S"`] Adding SSH RSA Public Key to user $name - NOT OK - $ERROR"
else
echo "[`date "+%y-%m-%d|%H:%M:%S"`] Adding SSH RSA Public Key to user $name - OK"
fi
}
#
# Default limit is set to 1 machine type / DC
# We raise that to RAISE_LIMIT_TO for smartos type machines
#
function raiseLimits {
ERROR=`curl -is -u ${ADMIN}:${PASS} --connect-timeout ${TIMEOUT} -H "Accept:application/json" --url http://${CAPI}/customers/${name}/limits/${DATACENTER_NAME}/smartos -d ${RAISE_LIMIT_TO} -X PUT | tee -a ${LOG_FILE} | grep errors`
if [ -n "$ERROR" ]
then
echo "[`date "+%y-%m-%d|%H:%M:%S"`] Raising limit of $name in ${DATACENTER_NAME} to ${RAISE_LIMIT_TO} SmartOS SmartMachines - NOT OK"
else
echo "[`date "+%y-%m-%d|%H:%M:%S"`] Raising limit of $name in ${DATACENTER_NAME} to ${RAISE_LIMIT_TO} SmartOS SmartMachines - OK"
fi
echo "[`date "+%y-%m-%d|%H:%M:%S"`] ------------------------------------------------------------------------------------"
}
#Call the functions
verifyKeys
maxClient
#createUsers
exit 0