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