| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
 | 2 |  | 
| Dean Troyer | 0f2d954 | 2013-02-20 17:51:19 -0600 | [diff] [blame] | 3 | # **create_userrc.sh** | 
 | 4 |  | 
 | 5 | # Pre-create rc files and credentials for the default users. | 
 | 6 |  | 
 | 7 | # Warning: This script just for development purposes | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 8 |  | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 9 | set -o errexit | 
 | 10 | set -o xtrace | 
 | 11 |  | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 12 | ACCOUNT_DIR=./accrc | 
 | 13 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 14 | function display_help { | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 15 | cat <<EOF | 
 | 16 |  | 
 | 17 | usage: $0 <options..> | 
 | 18 |  | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 19 | This script creates certificates and sourcable rc files per project/user. | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 20 |  | 
 | 21 | Target account directory hierarchy: | 
 | 22 | target_dir-| | 
 | 23 |            |-cacert.pem | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 24 |            |-project1-name| | 
 | 25 |            |              |- user1 | 
 | 26 |            |              |- user1-cert.pem | 
 | 27 |            |              |- user1-pk.pem | 
 | 28 |            |              |- user2 | 
 | 29 |            |              .. | 
 | 30 |            |-project2-name.. | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 31 |            .. | 
 | 32 |  | 
 | 33 | Optional Arguments | 
 | 34 | -P include password to the rc files; with -A it assume all users password is the same | 
 | 35 | -A try with all user | 
 | 36 | -u <username> create files just for the specified user | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 37 | -C <project_name> create user and project, the specifid project will be the user's project | 
 | 38 | -r <name> when combined with -C and the (-u) user exists it will be the user's project role in the (-C)project (default: Member) | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 39 | -p <userpass> password for the user | 
| Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 40 | --heat-url <heat_url> | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 41 | --os-username <username> | 
 | 42 | --os-password <admin password> | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 43 | --os-project-name <project_name> | 
 | 44 | --os-project-id <project_id> | 
| Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 45 | --os-user-domain-id <user_domain_id> | 
 | 46 | --os-user-domain-name <user_domain_name> | 
 | 47 | --os-project-domain-id <project_domain_id> | 
 | 48 | --os-project-domain-name <project_domain_name> | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 49 | --os-auth-url <auth_url> | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 50 | --os-cacert <cert file> | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 51 | --target-dir <target_directory> | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 52 | --skip-project <project-name> | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 53 | --debug | 
 | 54 |  | 
 | 55 | Example: | 
 | 56 | $0 -AP | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 57 | $0 -P -C myproject -u myuser -p mypass | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 58 | EOF | 
 | 59 | } | 
 | 60 |  | 
| Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 61 | if ! options=$(getopt -o hPAp:u:r:C: -l os-username:,os-password:,os-tenant-id:,os-tenant-name:,os-project-name:,os-project-id:,os-project-domain-id:,os-project-domain-name:,os-user-domain-id:,os-user-domain-name:,os-auth-url:,target-dir:,heat-url:,skip-project:,os-cacert:,help,debug -- "$@"); then | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 62 |     display_help | 
 | 63 |     exit 1 | 
 | 64 | fi | 
 | 65 | eval set -- $options | 
 | 66 | ADDPASS="" | 
| Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 67 | HEAT_URL="" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 68 |  | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 69 | # The services users usually in the service project. | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 70 | # rc files for service users, is out of scope. | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 71 | # Supporting different project for services is out of scope. | 
 | 72 | SKIP_PROJECT="service" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 73 | MODE="" | 
 | 74 | ROLE=Member | 
 | 75 | USER_NAME="" | 
 | 76 | USER_PASS="" | 
| Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 77 | while [ $# -gt 0 ]; do | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 78 |     case "$1" in | 
 | 79 |     -h|--help) display_help; exit 0 ;; | 
 | 80 |     --os-username) export OS_USERNAME=$2; shift ;; | 
 | 81 |     --os-password) export OS_PASSWORD=$2; shift ;; | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 82 |     --os-tenant-name) export OS_PROJECT_NAME=$2; shift ;; | 
 | 83 |     --os-tenant-id) export OS_PROJECT_ID=$2; shift ;; | 
 | 84 |     --os-project-name) export OS_PROJECT_NAME=$2; shift ;; | 
 | 85 |     --os-project-id) export OS_PROJECT_ID=$2; shift ;; | 
| Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 86 |     --os-user-domain-id) export OS_USER_DOMAIN_ID=$2; shift ;; | 
 | 87 |     --os-user-domain-name) export OS_USER_DOMAIN_NAME=$2; shift ;; | 
 | 88 |     --os-project-domain-id) export OS_PROJECT_DOMAIN_ID=$2; shift ;; | 
 | 89 |     --os-project-domain-name) export OS_PROJECT_DOMAIN_NAME=$2; shift ;; | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 90 |     --skip-tenant) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;; | 
 | 91 |     --skip-project) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;; | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 92 |     --os-auth-url) export OS_AUTH_URL=$2; shift ;; | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 93 |     --os-cacert) export OS_CACERT=$2; shift ;; | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 94 |     --target-dir) ACCOUNT_DIR=$2; shift ;; | 
| Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 95 |     --heat-url) HEAT_URL=$2; shift ;; | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 96 |     --debug) set -o xtrace ;; | 
 | 97 |     -u) MODE=${MODE:-one};  USER_NAME=$2; shift ;; | 
 | 98 |     -p) USER_PASS=$2; shift ;; | 
 | 99 |     -A) MODE=all; ;; | 
 | 100 |     -P) ADDPASS="yes" ;; | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 101 |     -C) MODE=create; PROJECT=$2; shift ;; | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 102 |     -r) ROLE=$2; shift ;; | 
 | 103 |     (--) shift; break ;; | 
 | 104 |     (-*) echo "$0: error - unrecognized option $1" >&2; display_help; exit 1 ;; | 
 | 105 |     (*)  echo "$0: error - unexpected argument $1" >&2; display_help; exit 1 ;; | 
 | 106 |     esac | 
 | 107 |     shift | 
 | 108 | done | 
 | 109 |  | 
 | 110 | if [ -z "$OS_PASSWORD" ]; then | 
 | 111 |     if [ -z "$ADMIN_PASSWORD" ];then | 
 | 112 |         echo "The admin password is required option!"  >&2 | 
 | 113 |         exit 2 | 
 | 114 |     else | 
 | 115 |         OS_PASSWORD=$ADMIN_PASSWORD | 
 | 116 |     fi | 
 | 117 | fi | 
 | 118 |  | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 119 | if [ -z "$OS_PROJECT_ID" -a "$OS_TENANT_ID" ]; then | 
 | 120 |     export OS_PROJECT_ID=$OS_TENANT_ID | 
 | 121 | fi | 
 | 122 |  | 
 | 123 | if [ -z "$OS_PROJECT_NAME" -a "$OS_TENANT_NAME" ]; then | 
 | 124 |     export OS_PROJECT_NAME=$OS_TENANT_NAME | 
 | 125 | fi | 
 | 126 |  | 
 | 127 | if [ -z "$OS_PROJECT_NAME" -a -z "$OS_PROJECT_ID" ]; then | 
 | 128 |     export OS_PROJECT_NAME=admin | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 129 | fi | 
 | 130 |  | 
 | 131 | if [ -z "$OS_USERNAME" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 132 |     export OS_USERNAME=admin | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 133 | fi | 
 | 134 |  | 
 | 135 | if [ -z "$OS_AUTH_URL" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 136 |     export OS_AUTH_URL=http://localhost:5000/v2.0/ | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 137 | fi | 
 | 138 |  | 
| Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 139 | if [ -z "$OS_USER_DOMAIN_ID" -a -z "$OS_USER_DOMAIN_NAME" ]; then | 
 | 140 |     # purposefully not exported as it would force v3 auth within this file. | 
 | 141 |     OS_USER_DOMAIN_ID=default | 
 | 142 | fi | 
 | 143 |  | 
 | 144 | if [ -z "$OS_PROJECT_DOMAIN_ID" -a -z "$OS_PROJECT_DOMAIN_NAME" ]; then | 
 | 145 |     # purposefully not exported as it would force v3 auth within this file. | 
 | 146 |     OS_PROJECT_DOMAIN_ID=default | 
 | 147 | fi | 
 | 148 |  | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 149 | USER_PASS=${USER_PASS:-$OS_PASSWORD} | 
 | 150 | USER_NAME=${USER_NAME:-$OS_USERNAME} | 
 | 151 |  | 
 | 152 | if [ -z "$MODE" ]; then | 
 | 153 |     echo "You must specify at least -A or -u parameter!"  >&2 | 
 | 154 |     echo | 
 | 155 |     display_help | 
 | 156 |     exit 3 | 
 | 157 | fi | 
 | 158 |  | 
| Andrey Pavlov | 485c962 | 2015-08-24 22:55:19 +0300 | [diff] [blame] | 159 | EC2_URL=$(openstack endpoint list --service ec2 --interface public --os-identity-api-version=3 -c URL -f value || true) | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 160 | if [[ -z $EC2_URL ]]; then | 
| Andrey Pavlov | 0ea8b72 | 2015-02-06 22:37:53 +0300 | [diff] [blame] | 161 |     EC2_URL=http://localhost:8773/ | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 162 | fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 163 |  | 
| Andrey Pavlov | 485c962 | 2015-08-24 22:55:19 +0300 | [diff] [blame] | 164 | S3_URL=$(openstack endpoint list --service s3 --interface public --os-identity-api-version=3 -c URL -f value || true) | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 165 | if [[ -z $S3_URL ]]; then | 
 | 166 |     S3_URL=http://localhost:3333 | 
 | 167 | fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 168 |  | 
 | 169 | mkdir -p "$ACCOUNT_DIR" | 
 | 170 | ACCOUNT_DIR=`readlink -f "$ACCOUNT_DIR"` | 
 | 171 | EUCALYPTUS_CERT=$ACCOUNT_DIR/cacert.pem | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 172 | if [ -e "$EUCALYPTUS_CERT" ]; then | 
 | 173 |     mv "$EUCALYPTUS_CERT" "$EUCALYPTUS_CERT.old" | 
 | 174 | fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 175 | if ! nova x509-get-root-cert "$EUCALYPTUS_CERT"; then | 
 | 176 |     echo "Failed to update the root certificate: $EUCALYPTUS_CERT" >&2 | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 177 |     if [ -e "$EUCALYPTUS_CERT.old" ]; then | 
 | 178 |         mv "$EUCALYPTUS_CERT.old" "$EUCALYPTUS_CERT" | 
 | 179 |     fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 180 | fi | 
 | 181 |  | 
 | 182 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 183 | function add_entry { | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 184 |     local user_id=$1 | 
 | 185 |     local user_name=$2 | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 186 |     local project_id=$3 | 
 | 187 |     local project_name=$4 | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 188 |     local user_passwd=$5 | 
 | 189 |  | 
 | 190 |     # The admin user can see all user's secret AWS keys, it does not looks good | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 191 |     local line | 
 | 192 |     line=$(openstack ec2 credentials list --user $user_id | grep " $project_id " || true) | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 193 |     if [ -z "$line" ]; then | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 194 |         openstack ec2 credentials create --user $user_id --project $project_id 1>&2 | 
 | 195 |         line=`openstack ec2 credentials list --user $user_id | grep " $project_id "` | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 196 |     fi | 
 | 197 |     local ec2_access_key ec2_secret_key | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 198 |     read ec2_access_key ec2_secret_key <<<  `echo $line | awk '{print $2 " " $4 }'` | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 199 |     mkdir -p "$ACCOUNT_DIR/$project_name" | 
 | 200 |     local rcfile="$ACCOUNT_DIR/$project_name/$user_name" | 
 | 201 |     # The certs subject part are the project ID "dash" user ID, but the CN should be the first part of the DN | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 202 |     # Generally the subject DN parts should be in reverse order like the Issuer | 
 | 203 |     # The Serial does not seams correctly marked either | 
 | 204 |     local ec2_cert="$rcfile-cert.pem" | 
 | 205 |     local ec2_private_key="$rcfile-pk.pem" | 
 | 206 |     # Try to preserve the original file on fail (best effort) | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 207 |     if [ -e "$ec2_private_key" ]; then | 
 | 208 |         mv -f "$ec2_private_key" "$ec2_private_key.old" | 
 | 209 |     fi | 
 | 210 |     if [ -e "$ec2_cert" ]; then | 
 | 211 |         mv -f "$ec2_cert" "$ec2_cert.old" | 
 | 212 |     fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 213 |     # It will not create certs when the password is incorrect | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 214 |     if ! nova --os-password "$user_passwd" --os-username "$user_name" --os-project-name "$project_name" x509-create-cert "$ec2_private_key" "$ec2_cert"; then | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 215 |         if [ -e "$ec2_private_key.old" ]; then | 
 | 216 |             mv -f "$ec2_private_key.old" "$ec2_private_key" | 
 | 217 |         fi | 
 | 218 |         if [ -e "$ec2_cert.old" ]; then | 
 | 219 |             mv -f "$ec2_cert.old" "$ec2_cert" | 
 | 220 |         fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 221 |     fi | 
 | 222 |     cat >"$rcfile" <<EOF | 
 | 223 | # you can source this file | 
| Attila Fazekas | 5b813bc | 2013-01-08 16:51:05 +0100 | [diff] [blame] | 224 | export EC2_ACCESS_KEY="$ec2_access_key" | 
 | 225 | export EC2_SECRET_KEY="$ec2_secret_key" | 
 | 226 | export EC2_URL="$EC2_URL" | 
 | 227 | export S3_URL="$S3_URL" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 228 | # OpenStack USER ID = $user_id | 
 | 229 | export OS_USERNAME="$user_name" | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 230 | # OpenStack project ID = $project_id | 
 | 231 | export OS_PROJECT_NAME="$project_name" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 232 | export OS_AUTH_URL="$OS_AUTH_URL" | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 233 | export OS_CACERT="$OS_CACERT" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 234 | export EC2_CERT="$ec2_cert" | 
 | 235 | export EC2_PRIVATE_KEY="$ec2_private_key" | 
 | 236 | export EC2_USER_ID=42 #not checked by nova (can be a 12-digit id) | 
 | 237 | export EUCALYPTUS_CERT="$ACCOUNT_DIR/cacert.pem" | 
 | 238 | export NOVA_CERT="$ACCOUNT_DIR/cacert.pem" | 
| Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 239 | export OS_AUTH_TYPE=v2password | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 240 | EOF | 
 | 241 |     if [ -n "$ADDPASS" ]; then | 
 | 242 |         echo "export OS_PASSWORD=\"$user_passwd\"" >>"$rcfile" | 
 | 243 |     fi | 
| Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 244 |     if [ -n "$HEAT_URL" ]; then | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 245 |         echo "export HEAT_URL=\"$HEAT_URL/$project_id\"" >>"$rcfile" | 
| Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 246 |         echo "export OS_NO_CLIENT_AUTH=True" >>"$rcfile" | 
 | 247 |     fi | 
| Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 248 |     for v in OS_USER_DOMAIN_ID OS_USER_DOMAIN_NAME OS_PROJECT_DOMAIN_ID OS_PROJECT_DOMAIN_NAME; do | 
 | 249 |         if [ ${!v} ]; then | 
 | 250 |             echo "export $v=${!v}" >>"$rcfile" | 
 | 251 |         else | 
 | 252 |             echo "unset $v" >>"$rcfile" | 
 | 253 |         fi | 
 | 254 |     done | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 255 | } | 
 | 256 |  | 
 | 257 | #admin users expected | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 258 | function create_or_get_project { | 
 | 259 |     local name=$1 | 
 | 260 |     local id | 
 | 261 |     eval $(openstack project show -f shell -c id $name) | 
 | 262 |     if [[ -z $id ]]; then | 
 | 263 |         eval $(openstack project create -f shell -c id $name) | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 264 |     fi | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 265 |     echo $id | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 266 | } | 
 | 267 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 268 | function create_or_get_role { | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 269 |     local name=$1 | 
 | 270 |     local id | 
 | 271 |     eval $(openstack role show -f shell -c id $name) | 
 | 272 |     if [[ -z $id ]]; then | 
 | 273 |         eval $(openstack role create -f shell -c id $name) | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 274 |     fi | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 275 |     echo $id | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 276 | } | 
 | 277 |  | 
 | 278 | # Provides empty string when the user does not exists | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 279 | function get_user_id { | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 280 |     openstack user list | grep " $1 " | cut -d " " -f2 | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 281 | } | 
 | 282 |  | 
 | 283 | if [ $MODE != "create" ]; then | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 284 |     # looks like I can't ask for all project related to a specified user | 
 | 285 |     openstack project list --long --quote none -f csv | grep ',True' | grep -v "${SKIP_PROJECT}" | while IFS=, read project_id project_name desc enabled; do | 
 | 286 |         openstack user list --project $project_id --long --quote none -f csv | grep ',True' | while IFS=, read user_id user_name project email enabled; do | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 287 |             if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 288 |                 continue; | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 289 |             fi | 
| Sahid Orentino Ferdjaoui | 1814e67 | 2014-02-11 17:56:07 +0100 | [diff] [blame] | 290 |  | 
 | 291 |             # Checks for a specific password defined for an user. | 
| Dean Troyer | 3324f19 | 2014-09-18 09:26:39 -0500 | [diff] [blame] | 292 |             # Example for an username johndoe: JOHNDOE_PASSWORD=1234 | 
| JordanP | 7c6d005 | 2014-10-06 23:08:50 +0200 | [diff] [blame] | 293 |             # This mechanism is used by lib/swift | 
 | 294 |             eval SPECIFIC_UPASSWORD="\$${user_name}_password" | 
| Sahid Orentino Ferdjaoui | 1814e67 | 2014-02-11 17:56:07 +0100 | [diff] [blame] | 295 |             if [ -n "$SPECIFIC_UPASSWORD" ]; then | 
 | 296 |                 USER_PASS=$SPECIFIC_UPASSWORD | 
 | 297 |             fi | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 298 |             add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 299 |         done | 
 | 300 |     done | 
 | 301 | else | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 302 |     project_name=$PROJECT | 
 | 303 |     project_id=$(create_or_get_project "$PROJECT") | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 304 |     user_name=$USER_NAME | 
 | 305 |     user_id=`get_user_id $user_name` | 
 | 306 |     if [ -z "$user_id" ]; then | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 307 |         eval $(openstack user create "$user_name" --project "$project_id" --password "$USER_PASS" --email "$user_name@example.com" -f shell -c id) | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 308 |         user_id=$id | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 309 |         add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 310 |     else | 
| Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 311 |         role_id=$(create_or_get_role "$ROLE") | 
| Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 312 |         openstack role add "$role_id" --user "$user_id" --project "$project_id" | 
 | 313 |         add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 314 |     fi | 
 | 315 | fi |