Browse code

Merge "Update create_userrc to openstackclient commands"

Jenkins authored on 2014/05/08 14:34:26
Showing 1 changed files
... ...
@@ -34,7 +34,7 @@ Optional Arguments
34 34
 -P include password to the rc files; with -A it assume all users password is the same
35 35
 -A try with all user
36 36
 -u <username> create files just for the specified user
37
--C <tanent_name> create user and tenant, the specifid tenant will be the user's tenant
37
+-C <tenant_name> create user and tenant, the specifid tenant will be the user's tenant
38 38
 -r <name> when combined with -C and the (-u) user exists it will be the user's tenant role in the (-C)tenant (default: Member)
39 39
 -p <userpass> password for the user
40 40
 --os-username <username>
... ...
@@ -62,8 +62,8 @@ ADDPASS=""
62 62
 
63 63
 # The services users usually in the service tenant.
64 64
 # rc files for service users, is out of scope.
65
-# Supporting different tanent for services is out of scope.
66
-SKIP_TENANT=",service," # tenant names are between commas(,)
65
+# Supporting different tenant for services is out of scope.
66
+SKIP_TENANT="service"
67 67
 MODE=""
68 68
 ROLE=Member
69 69
 USER_NAME=""
... ...
@@ -126,15 +126,15 @@ fi
126 126
 
127 127
 export -n SERVICE_TOKEN SERVICE_ENDPOINT OS_SERVICE_TOKEN OS_SERVICE_ENDPOINT
128 128
 
129
-EC2_URL=http://localhost:8773/service/Cloud
130
-S3_URL=http://localhost:3333
131
-
132
-ec2=`keystone endpoint-get --service ec2 | awk '/\|[[:space:]]*ec2.publicURL/ {print $4}'`
133
-[ -n "$ec2" ] && EC2_URL=$ec2
134
-
135
-s3=`keystone endpoint-get --service s3 | awk '/\|[[:space:]]*s3.publicURL/ {print $4}'`
136
-[ -n "$s3" ] && S3_URL=$s3
129
+EC2_URL=`openstack endpoint show ec2 | grep " ec2.publicURL " | cut -d " " -f4`
130
+if [[ -z $EC2_URL ]]; then
131
+    EC2_URL=http://localhost:8773/service/Cloud
132
+fi
137 133
 
134
+S3_URL=`openstack endpoint show s3 | grep " s3.publicURL " | cut -d " " -f4`
135
+if [[ -z $S3_URL ]]; then
136
+    S3_URL=http://localhost:3333
137
+fi
138 138
 
139 139
 mkdir -p "$ACCOUNT_DIR"
140 140
 ACCOUNT_DIR=`readlink -f "$ACCOUNT_DIR"`
... ...
@@ -158,13 +158,13 @@ function add_entry {
158 158
     local user_passwd=$5
159 159
 
160 160
     # The admin user can see all user's secret AWS keys, it does not looks good
161
-    local line=`keystone ec2-credentials-list --user_id $user_id | grep -E "^\\|[[:space:]]*($tenant_name|$tenant_id)[[:space:]]*\\|" | head -n 1`
161
+    local line=`openstack ec2 credentials list --user $user_id | grep " $tenant_id "`
162 162
     if [ -z "$line" ]; then
163
-        keystone ec2-credentials-create --user-id $user_id --tenant-id $tenant_id 1>&2
164
-        line=`keystone ec2-credentials-list --user_id $user_id | grep -E "^\\|[[:space:]]*($tenant_name|$tenant_id)[[:space:]]*\\|" | head -n 1`
163
+        openstack ec2 credentials create --user $user_id --project $tenant_id 1>&2
164
+        line=`openstack ec2 credentials list --user $user_id | grep " $tenant_id "`
165 165
     fi
166 166
     local ec2_access_key ec2_secret_key
167
-    read ec2_access_key ec2_secret_key <<<  `echo $line | awk '{print $4 " " $6 }'`
167
+    read ec2_access_key ec2_secret_key <<<  `echo $line | awk '{print $2 " " $4 }'`
168 168
     mkdir -p "$ACCOUNT_DIR/$tenant_name"
169 169
     local rcfile="$ACCOUNT_DIR/$tenant_name/$user_name"
170 170
     # The certs subject part are the tenant ID "dash" user ID, but the CN should be the first part of the DN
... ...
@@ -212,41 +212,35 @@ EOF
212 212
 }
213 213
 
214 214
 #admin users expected
215
-function create_or_get_tenant {
216
-    local tenant_name=$1
217
-    local tenant_id=`keystone tenant-list | awk '/\|[[:space:]]*'"$tenant_name"'[[:space:]]*\|.*\|/ {print $2}'`
218
-    if [ -n "$tenant_id" ]; then
219
-        echo $tenant_id
220
-    else
221
-        keystone tenant-create --name "$tenant_name" | awk '/\|[[:space:]]*id[[:space:]]*\|.*\|/ {print $4}'
215
+function create_or_get_project {
216
+    local name=$1
217
+    local id
218
+    eval $(openstack project show -f shell -c id $name)
219
+    if [[ -z $id ]]; then
220
+        eval $(openstack project create -f shell -c id $name)
222 221
     fi
222
+    echo $id
223 223
 }
224 224
 
225 225
 function create_or_get_role {
226
-    local role_name=$1
227
-    local role_id=`keystone role-list| awk '/\|[[:space:]]*'"$role_name"'[[:space:]]*\|/ {print $2}'`
228
-    if [ -n "$role_id" ]; then
229
-        echo $role_id
230
-    else
231
-        keystone role-create --name "$role_name" |awk '/\|[[:space:]]*id[[:space:]]*\|.*\|/ {print $4}'
226
+    local name=$1
227
+    local id
228
+    eval $(openstack role show -f shell -c id $name)
229
+    if [[ -z $id ]]; then
230
+        eval $(openstack role create -f shell -c id $name)
232 231
     fi
232
+    echo $id
233 233
 }
234 234
 
235 235
 # Provides empty string when the user does not exists
236 236
 function get_user_id {
237
-    local user_name=$1
238
-    keystone user-list | awk '/^\|[^|]*\|[[:space:]]*'"$user_name"'[[:space:]]*\|.*\|/ {print $2}'
237
+    openstack user list | grep " $1 " | cut -d " " -f2
239 238
 }
240 239
 
241 240
 if [ $MODE != "create" ]; then
242
-# looks like I can't ask for all tenant related to a specified  user
243
-    for tenant_id_at_name in `keystone tenant-list | awk 'BEGIN {IGNORECASE = 1} /true[[:space:]]*\|$/ {print  $2 "@" $4}'`; do
244
-        read tenant_id tenant_name <<< `echo "$tenant_id_at_name" | sed 's/@/ /'`
245
-        if echo $SKIP_TENANT| grep -q ",$tenant_name,"; then
246
-            continue;
247
-        fi
248
-        for user_id_at_name in `keystone user-list --tenant-id $tenant_id | awk 'BEGIN {IGNORECASE = 1} /true[[:space:]]*\|[^|]*\|$/ {print  $2 "@" $4}'`; do
249
-            read user_id user_name <<< `echo "$user_id_at_name" | sed 's/@/ /'`
241
+# looks like I can't ask for all tenant related to a specified user
242
+    openstack project list --long --quote none -f csv | grep ',True' | grep -v "${SKIP_TENANT}" | while IFS=, read tenant_id tenant_name desc enabled; do
243
+        openstack user list --project $tenant_id --long --quote none -f csv | grep ',True' | while IFS=, read user_id user_name project email enabled; do
250 244
             if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then
251 245
                 continue;
252 246
             fi
... ...
@@ -263,18 +257,16 @@ if [ $MODE != "create" ]; then
263 263
     done
264 264
 else
265 265
     tenant_name=$TENANT
266
-    tenant_id=`create_or_get_tenant "$TENANT"`
266
+    tenant_id=$(create_or_get_project "$TENANT")
267 267
     user_name=$USER_NAME
268 268
     user_id=`get_user_id $user_name`
269 269
     if [ -z "$user_id" ]; then
270
-        #new user
271
-        user_id=`keystone user-create --name "$user_name" --tenant-id "$tenant_id" --pass "$USER_PASS" --email "$user_name@example.com" | awk '/\|[[:space:]]*id[[:space:]]*\|.*\|/ {print $4}'`
272
-        #The password is in the cmd line. It is not a good thing
270
+        eval $(openstack user create "$user_name" --project "$tenant_id" --password "$USER_PASS" --email "$user_name@example.com" -f shell -c id)
271
+        user_id=$id
273 272
         add_entry "$user_id" "$user_name" "$tenant_id" "$tenant_name" "$USER_PASS"
274 273
     else
275
-        #new role
276
-        role_id=`create_or_get_role "$ROLE"`
277
-        keystone user-role-add --user-id "$user_id" --tenant-id "$tenant_id" --role-id "$role_id"
274
+        role_id=$(create_or_get_role "$ROLE")
275
+        openstack role add "$role_id" --user "$user_id" --project "$tenant_id"
278 276
         add_entry "$user_id" "$user_name" "$tenant_id" "$tenant_name" "$USER_PASS"
279 277
     fi
280 278
 fi