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 |
Sean Dague | 646085d | 2016-03-21 17:00:51 -0400 | [diff] [blame] | 10 | |
| 11 | # short_source prints out the current location of the caller in a way |
| 12 | # that strips redundant directories. This is useful for PS4 |
| 13 | # usage. Needed before we start tracing due to how we set |
| 14 | # PS4. Normally we'd pick this up from stackrc, but that's not sourced |
| 15 | # here. |
| 16 | function short_source { |
| 17 | saveIFS=$IFS |
| 18 | IFS=" " |
| 19 | called=($(caller 0)) |
| 20 | IFS=$saveIFS |
| 21 | file=${called[2]} |
| 22 | file=${file#$RC_DIR/} |
| 23 | printf "%-40s " "$file:${called[1]}:${called[0]}" |
| 24 | } |
| 25 | |
Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 26 | set -o xtrace |
| 27 | |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 28 | ACCOUNT_DIR=./accrc |
| 29 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 30 | function display_help { |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 31 | cat <<EOF |
| 32 | |
| 33 | usage: $0 <options..> |
| 34 | |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 35 | This script creates certificates and sourcable rc files per project/user. |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 36 | |
| 37 | Target account directory hierarchy: |
| 38 | target_dir-| |
| 39 | |-cacert.pem |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 40 | |-project1-name| |
| 41 | | |- user1 |
| 42 | | |- user1-cert.pem |
| 43 | | |- user1-pk.pem |
| 44 | | |- user2 |
| 45 | | .. |
| 46 | |-project2-name.. |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 47 | .. |
| 48 | |
| 49 | Optional Arguments |
| 50 | -P include password to the rc files; with -A it assume all users password is the same |
| 51 | -A try with all user |
| 52 | -u <username> create files just for the specified user |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 53 | -C <project_name> create user and project, the specifid project will be the user's project |
| 54 | -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] | 55 | -p <userpass> password for the user |
Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 56 | --heat-url <heat_url> |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 57 | --os-username <username> |
| 58 | --os-password <admin password> |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 59 | --os-project-name <project_name> |
| 60 | --os-project-id <project_id> |
Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 61 | --os-user-domain-id <user_domain_id> |
| 62 | --os-user-domain-name <user_domain_name> |
| 63 | --os-project-domain-id <project_domain_id> |
| 64 | --os-project-domain-name <project_domain_name> |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 65 | --os-auth-url <auth_url> |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 66 | --os-cacert <cert file> |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 67 | --target-dir <target_directory> |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 68 | --skip-project <project-name> |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 69 | --debug |
| 70 | |
| 71 | Example: |
| 72 | $0 -AP |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 73 | $0 -P -C myproject -u myuser -p mypass |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 74 | EOF |
| 75 | } |
| 76 | |
Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 77 | 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] | 78 | display_help |
| 79 | exit 1 |
| 80 | fi |
| 81 | eval set -- $options |
| 82 | ADDPASS="" |
Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 83 | HEAT_URL="" |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 84 | |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 85 | # The services users usually in the service project. |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 86 | # rc files for service users, is out of scope. |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 87 | # Supporting different project for services is out of scope. |
| 88 | SKIP_PROJECT="service" |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 89 | MODE="" |
| 90 | ROLE=Member |
| 91 | USER_NAME="" |
| 92 | USER_PASS="" |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 93 | while [ $# -gt 0 ]; do |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 94 | case "$1" in |
| 95 | -h|--help) display_help; exit 0 ;; |
| 96 | --os-username) export OS_USERNAME=$2; shift ;; |
| 97 | --os-password) export OS_PASSWORD=$2; shift ;; |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 98 | --os-tenant-name) export OS_PROJECT_NAME=$2; shift ;; |
| 99 | --os-tenant-id) export OS_PROJECT_ID=$2; shift ;; |
| 100 | --os-project-name) export OS_PROJECT_NAME=$2; shift ;; |
| 101 | --os-project-id) export OS_PROJECT_ID=$2; shift ;; |
Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 102 | --os-user-domain-id) export OS_USER_DOMAIN_ID=$2; shift ;; |
| 103 | --os-user-domain-name) export OS_USER_DOMAIN_NAME=$2; shift ;; |
| 104 | --os-project-domain-id) export OS_PROJECT_DOMAIN_ID=$2; shift ;; |
| 105 | --os-project-domain-name) export OS_PROJECT_DOMAIN_NAME=$2; shift ;; |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 106 | --skip-tenant) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;; |
| 107 | --skip-project) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;; |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 108 | --os-auth-url) export OS_AUTH_URL=$2; shift ;; |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 109 | --os-cacert) export OS_CACERT=$2; shift ;; |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 110 | --target-dir) ACCOUNT_DIR=$2; shift ;; |
Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 111 | --heat-url) HEAT_URL=$2; shift ;; |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 112 | --debug) set -o xtrace ;; |
| 113 | -u) MODE=${MODE:-one}; USER_NAME=$2; shift ;; |
| 114 | -p) USER_PASS=$2; shift ;; |
| 115 | -A) MODE=all; ;; |
| 116 | -P) ADDPASS="yes" ;; |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 117 | -C) MODE=create; PROJECT=$2; shift ;; |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 118 | -r) ROLE=$2; shift ;; |
| 119 | (--) shift; break ;; |
| 120 | (-*) echo "$0: error - unrecognized option $1" >&2; display_help; exit 1 ;; |
| 121 | (*) echo "$0: error - unexpected argument $1" >&2; display_help; exit 1 ;; |
| 122 | esac |
| 123 | shift |
| 124 | done |
| 125 | |
| 126 | if [ -z "$OS_PASSWORD" ]; then |
| 127 | if [ -z "$ADMIN_PASSWORD" ];then |
| 128 | echo "The admin password is required option!" >&2 |
| 129 | exit 2 |
| 130 | else |
| 131 | OS_PASSWORD=$ADMIN_PASSWORD |
| 132 | fi |
| 133 | fi |
| 134 | |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 135 | if [ -z "$OS_PROJECT_ID" -a "$OS_TENANT_ID" ]; then |
| 136 | export OS_PROJECT_ID=$OS_TENANT_ID |
| 137 | fi |
| 138 | |
| 139 | if [ -z "$OS_PROJECT_NAME" -a "$OS_TENANT_NAME" ]; then |
| 140 | export OS_PROJECT_NAME=$OS_TENANT_NAME |
| 141 | fi |
| 142 | |
| 143 | if [ -z "$OS_PROJECT_NAME" -a -z "$OS_PROJECT_ID" ]; then |
| 144 | export OS_PROJECT_NAME=admin |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 145 | fi |
| 146 | |
| 147 | if [ -z "$OS_USERNAME" ]; then |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 148 | export OS_USERNAME=admin |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 149 | fi |
| 150 | |
| 151 | if [ -z "$OS_AUTH_URL" ]; then |
Sean Dague | 74b91c3 | 2016-02-01 14:58:20 +0000 | [diff] [blame] | 152 | export OS_AUTH_URL=http://localhost:5000/v2.0/ |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 153 | fi |
| 154 | |
Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 155 | if [ -z "$OS_USER_DOMAIN_ID" -a -z "$OS_USER_DOMAIN_NAME" ]; then |
| 156 | # purposefully not exported as it would force v3 auth within this file. |
| 157 | OS_USER_DOMAIN_ID=default |
| 158 | fi |
| 159 | |
| 160 | if [ -z "$OS_PROJECT_DOMAIN_ID" -a -z "$OS_PROJECT_DOMAIN_NAME" ]; then |
| 161 | # purposefully not exported as it would force v3 auth within this file. |
| 162 | OS_PROJECT_DOMAIN_ID=default |
| 163 | fi |
| 164 | |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 165 | USER_PASS=${USER_PASS:-$OS_PASSWORD} |
| 166 | USER_NAME=${USER_NAME:-$OS_USERNAME} |
| 167 | |
| 168 | if [ -z "$MODE" ]; then |
| 169 | echo "You must specify at least -A or -u parameter!" >&2 |
| 170 | echo |
| 171 | display_help |
| 172 | exit 3 |
| 173 | fi |
| 174 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 175 | function add_entry { |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 176 | local user_id=$1 |
| 177 | local user_name=$2 |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 178 | local project_id=$3 |
| 179 | local project_name=$4 |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 180 | local user_passwd=$5 |
| 181 | |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 182 | mkdir -p "$ACCOUNT_DIR/$project_name" |
| 183 | local rcfile="$ACCOUNT_DIR/$project_name/$user_name" |
Ian Wienand | 3bf69e8 | 2016-03-15 12:21:34 +1100 | [diff] [blame] | 184 | |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 185 | cat >"$rcfile" <<EOF |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 186 | # OpenStack USER ID = $user_id |
| 187 | export OS_USERNAME="$user_name" |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 188 | # OpenStack project ID = $project_id |
| 189 | export OS_PROJECT_NAME="$project_name" |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 190 | export OS_AUTH_URL="$OS_AUTH_URL" |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 191 | export OS_CACERT="$OS_CACERT" |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 192 | export NOVA_CERT="$ACCOUNT_DIR/cacert.pem" |
Sean Dague | 74b91c3 | 2016-02-01 14:58:20 +0000 | [diff] [blame] | 193 | export OS_AUTH_TYPE=v2password |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 194 | EOF |
| 195 | if [ -n "$ADDPASS" ]; then |
| 196 | echo "export OS_PASSWORD=\"$user_passwd\"" >>"$rcfile" |
| 197 | fi |
Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 198 | if [ -n "$HEAT_URL" ]; then |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 199 | echo "export HEAT_URL=\"$HEAT_URL/$project_id\"" >>"$rcfile" |
Steve Baker | e389aed | 2014-09-23 17:10:39 +1200 | [diff] [blame] | 200 | echo "export OS_NO_CLIENT_AUTH=True" >>"$rcfile" |
| 201 | fi |
Jamie Lennox | c54d4ab | 2015-06-22 04:07:18 +0000 | [diff] [blame] | 202 | for v in OS_USER_DOMAIN_ID OS_USER_DOMAIN_NAME OS_PROJECT_DOMAIN_ID OS_PROJECT_DOMAIN_NAME; do |
| 203 | if [ ${!v} ]; then |
| 204 | echo "export $v=${!v}" >>"$rcfile" |
| 205 | else |
| 206 | echo "unset $v" >>"$rcfile" |
| 207 | fi |
| 208 | done |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | #admin users expected |
Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 212 | function create_or_get_project { |
| 213 | local name=$1 |
| 214 | local id |
| 215 | eval $(openstack project show -f shell -c id $name) |
| 216 | if [[ -z $id ]]; then |
| 217 | eval $(openstack project create -f shell -c id $name) |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 218 | fi |
Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 219 | echo $id |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 222 | function create_or_get_role { |
Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 223 | local name=$1 |
| 224 | local id |
| 225 | eval $(openstack role show -f shell -c id $name) |
| 226 | if [[ -z $id ]]; then |
| 227 | eval $(openstack role create -f shell -c id $name) |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 228 | fi |
Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 229 | echo $id |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | # Provides empty string when the user does not exists |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 233 | function get_user_id { |
Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 234 | openstack user list | grep " $1 " | cut -d " " -f2 |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if [ $MODE != "create" ]; then |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 238 | # looks like I can't ask for all project related to a specified user |
| 239 | openstack project list --long --quote none -f csv | grep ',True' | grep -v "${SKIP_PROJECT}" | while IFS=, read project_id project_name desc enabled; do |
| 240 | 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] | 241 | if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 242 | continue; |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 243 | fi |
Sahid Orentino Ferdjaoui | 1814e67 | 2014-02-11 17:56:07 +0100 | [diff] [blame] | 244 | |
| 245 | # Checks for a specific password defined for an user. |
Dean Troyer | 3324f19 | 2014-09-18 09:26:39 -0500 | [diff] [blame] | 246 | # Example for an username johndoe: JOHNDOE_PASSWORD=1234 |
JordanP | 7c6d005 | 2014-10-06 23:08:50 +0200 | [diff] [blame] | 247 | # This mechanism is used by lib/swift |
| 248 | eval SPECIFIC_UPASSWORD="\$${user_name}_password" |
Sahid Orentino Ferdjaoui | 1814e67 | 2014-02-11 17:56:07 +0100 | [diff] [blame] | 249 | if [ -n "$SPECIFIC_UPASSWORD" ]; then |
| 250 | USER_PASS=$SPECIFIC_UPASSWORD |
| 251 | fi |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 252 | add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 253 | done |
| 254 | done |
| 255 | else |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 256 | project_name=$PROJECT |
| 257 | project_id=$(create_or_get_project "$PROJECT") |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 258 | user_name=$USER_NAME |
| 259 | user_id=`get_user_id $user_name` |
| 260 | if [ -z "$user_id" ]; then |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 261 | 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] | 262 | user_id=$id |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 263 | add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 264 | else |
Steve Martinelli | bce8899 | 2014-03-14 00:40:06 -0500 | [diff] [blame] | 265 | role_id=$(create_or_get_role "$ROLE") |
Jamie Lennox | 9d6d8f8 | 2015-06-22 03:37:59 +0000 | [diff] [blame] | 266 | openstack role add "$role_id" --user "$user_id" --project "$project_id" |
| 267 | add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" |
Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 268 | fi |
| 269 | fi |