blob: a7278e47b3f422cf6852c3aa9136c92c641474c1 [file] [log] [blame]
Attila Fazekas22ef5732012-12-16 14:03:06 +01001#!/usr/bin/env bash
2
Dean Troyer0f2d9542013-02-20 17:51:19 -06003# **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 Fazekas22ef5732012-12-16 14:03:06 +01008
Adam Spiersc85ade72013-10-01 00:35:16 +01009set -o errexit
Sean Dague646085d2016-03-21 17:00:51 -040010
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.
16function 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 Spiersc85ade72013-10-01 00:35:16 +010026set -o xtrace
27
Attila Fazekas22ef5732012-12-16 14:03:06 +010028ACCOUNT_DIR=./accrc
29
Ian Wienandaee18c72014-02-21 15:35:08 +110030function display_help {
Attila Fazekas22ef5732012-12-16 14:03:06 +010031cat <<EOF
32
33usage: $0 <options..>
34
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000035This script creates certificates and sourcable rc files per project/user.
Attila Fazekas22ef5732012-12-16 14:03:06 +010036
37Target account directory hierarchy:
38target_dir-|
39 |-cacert.pem
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000040 |-project1-name|
41 | |- user1
42 | |- user1-cert.pem
43 | |- user1-pk.pem
44 | |- user2
45 | ..
46 |-project2-name..
Attila Fazekas22ef5732012-12-16 14:03:06 +010047 ..
48
49Optional 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 Lennox9d6d8f82015-06-22 03:37:59 +000053-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 Fazekas22ef5732012-12-16 14:03:06 +010055-p <userpass> password for the user
Steve Bakere389aed2014-09-23 17:10:39 +120056--heat-url <heat_url>
Attila Fazekas22ef5732012-12-16 14:03:06 +010057--os-username <username>
58--os-password <admin password>
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000059--os-project-name <project_name>
60--os-project-id <project_id>
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +000061--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 Fazekas22ef5732012-12-16 14:03:06 +010065--os-auth-url <auth_url>
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100066--os-cacert <cert file>
Attila Fazekas22ef5732012-12-16 14:03:06 +010067--target-dir <target_directory>
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000068--skip-project <project-name>
Attila Fazekas22ef5732012-12-16 14:03:06 +010069--debug
70
71Example:
72$0 -AP
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000073$0 -P -C myproject -u myuser -p mypass
Attila Fazekas22ef5732012-12-16 14:03:06 +010074EOF
75}
76
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +000077if ! 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 Fazekas22ef5732012-12-16 14:03:06 +010078 display_help
79 exit 1
80fi
81eval set -- $options
82ADDPASS=""
Steve Bakere389aed2014-09-23 17:10:39 +120083HEAT_URL=""
Attila Fazekas22ef5732012-12-16 14:03:06 +010084
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000085# The services users usually in the service project.
Attila Fazekas22ef5732012-12-16 14:03:06 +010086# rc files for service users, is out of scope.
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000087# Supporting different project for services is out of scope.
88SKIP_PROJECT="service"
Attila Fazekas22ef5732012-12-16 14:03:06 +010089MODE=""
90ROLE=Member
91USER_NAME=""
92USER_PASS=""
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010093while [ $# -gt 0 ]; do
Attila Fazekas22ef5732012-12-16 14:03:06 +010094 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 Lennox9d6d8f82015-06-22 03:37:59 +000098 --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 Lennoxc54d4ab2015-06-22 04:07:18 +0000102 --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 Lennox9d6d8f82015-06-22 03:37:59 +0000106 --skip-tenant) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
107 --skip-project) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100108 --os-auth-url) export OS_AUTH_URL=$2; shift ;;
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000109 --os-cacert) export OS_CACERT=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100110 --target-dir) ACCOUNT_DIR=$2; shift ;;
Steve Bakere389aed2014-09-23 17:10:39 +1200111 --heat-url) HEAT_URL=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100112 --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 Lennox9d6d8f82015-06-22 03:37:59 +0000117 -C) MODE=create; PROJECT=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100118 -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
124done
125
126if [ -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
133fi
134
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000135if [ -z "$OS_PROJECT_ID" -a "$OS_TENANT_ID" ]; then
136 export OS_PROJECT_ID=$OS_TENANT_ID
137fi
138
139if [ -z "$OS_PROJECT_NAME" -a "$OS_TENANT_NAME" ]; then
140 export OS_PROJECT_NAME=$OS_TENANT_NAME
141fi
142
143if [ -z "$OS_PROJECT_NAME" -a -z "$OS_PROJECT_ID" ]; then
144 export OS_PROJECT_NAME=admin
Attila Fazekas22ef5732012-12-16 14:03:06 +0100145fi
146
147if [ -z "$OS_USERNAME" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400148 export OS_USERNAME=admin
Attila Fazekas22ef5732012-12-16 14:03:06 +0100149fi
150
151if [ -z "$OS_AUTH_URL" ]; then
Sean Dague74b91c32016-02-01 14:58:20 +0000152 export OS_AUTH_URL=http://localhost:5000/v2.0/
Attila Fazekas22ef5732012-12-16 14:03:06 +0100153fi
154
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +0000155if [ -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
158fi
159
160if [ -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
163fi
164
Attila Fazekas22ef5732012-12-16 14:03:06 +0100165USER_PASS=${USER_PASS:-$OS_PASSWORD}
166USER_NAME=${USER_NAME:-$OS_USERNAME}
167
168if [ -z "$MODE" ]; then
169 echo "You must specify at least -A or -u parameter!" >&2
170 echo
171 display_help
172 exit 3
173fi
174
Ian Wienandaee18c72014-02-21 15:35:08 +1100175function add_entry {
Attila Fazekas22ef5732012-12-16 14:03:06 +0100176 local user_id=$1
177 local user_name=$2
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000178 local project_id=$3
179 local project_name=$4
Attila Fazekas22ef5732012-12-16 14:03:06 +0100180 local user_passwd=$5
181
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000182 mkdir -p "$ACCOUNT_DIR/$project_name"
183 local rcfile="$ACCOUNT_DIR/$project_name/$user_name"
Ian Wienand3bf69e82016-03-15 12:21:34 +1100184
Attila Fazekas22ef5732012-12-16 14:03:06 +0100185 cat >"$rcfile" <<EOF
Attila Fazekas22ef5732012-12-16 14:03:06 +0100186# OpenStack USER ID = $user_id
187export OS_USERNAME="$user_name"
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000188# OpenStack project ID = $project_id
189export OS_PROJECT_NAME="$project_name"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100190export OS_AUTH_URL="$OS_AUTH_URL"
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000191export OS_CACERT="$OS_CACERT"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100192export NOVA_CERT="$ACCOUNT_DIR/cacert.pem"
Sean Dague74b91c32016-02-01 14:58:20 +0000193export OS_AUTH_TYPE=v2password
Attila Fazekas22ef5732012-12-16 14:03:06 +0100194EOF
195 if [ -n "$ADDPASS" ]; then
196 echo "export OS_PASSWORD=\"$user_passwd\"" >>"$rcfile"
197 fi
Steve Bakere389aed2014-09-23 17:10:39 +1200198 if [ -n "$HEAT_URL" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000199 echo "export HEAT_URL=\"$HEAT_URL/$project_id\"" >>"$rcfile"
Steve Bakere389aed2014-09-23 17:10:39 +1200200 echo "export OS_NO_CLIENT_AUTH=True" >>"$rcfile"
201 fi
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +0000202 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 Fazekas22ef5732012-12-16 14:03:06 +0100209}
210
211#admin users expected
Steve Martinellibce88992014-03-14 00:40:06 -0500212function 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 Fazekas22ef5732012-12-16 14:03:06 +0100218 fi
Steve Martinellibce88992014-03-14 00:40:06 -0500219 echo $id
Attila Fazekas22ef5732012-12-16 14:03:06 +0100220}
221
Ian Wienandaee18c72014-02-21 15:35:08 +1100222function create_or_get_role {
Steve Martinellibce88992014-03-14 00:40:06 -0500223 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 Fazekas22ef5732012-12-16 14:03:06 +0100228 fi
Steve Martinellibce88992014-03-14 00:40:06 -0500229 echo $id
Attila Fazekas22ef5732012-12-16 14:03:06 +0100230}
231
232# Provides empty string when the user does not exists
Ian Wienandaee18c72014-02-21 15:35:08 +1100233function get_user_id {
Steve Martinellibce88992014-03-14 00:40:06 -0500234 openstack user list | grep " $1 " | cut -d " " -f2
Attila Fazekas22ef5732012-12-16 14:03:06 +0100235}
236
237if [ $MODE != "create" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000238 # 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 Fazekas22ef5732012-12-16 14:03:06 +0100241 if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400242 continue;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100243 fi
Sahid Orentino Ferdjaoui1814e672014-02-11 17:56:07 +0100244
245 # Checks for a specific password defined for an user.
Dean Troyer3324f192014-09-18 09:26:39 -0500246 # Example for an username johndoe: JOHNDOE_PASSWORD=1234
JordanP7c6d0052014-10-06 23:08:50 +0200247 # This mechanism is used by lib/swift
248 eval SPECIFIC_UPASSWORD="\$${user_name}_password"
Sahid Orentino Ferdjaoui1814e672014-02-11 17:56:07 +0100249 if [ -n "$SPECIFIC_UPASSWORD" ]; then
250 USER_PASS=$SPECIFIC_UPASSWORD
251 fi
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000252 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100253 done
254 done
255else
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000256 project_name=$PROJECT
257 project_id=$(create_or_get_project "$PROJECT")
Attila Fazekas22ef5732012-12-16 14:03:06 +0100258 user_name=$USER_NAME
259 user_id=`get_user_id $user_name`
260 if [ -z "$user_id" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000261 eval $(openstack user create "$user_name" --project "$project_id" --password "$USER_PASS" --email "$user_name@example.com" -f shell -c id)
Steve Martinellibce88992014-03-14 00:40:06 -0500262 user_id=$id
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000263 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100264 else
Steve Martinellibce88992014-03-14 00:40:06 -0500265 role_id=$(create_or_get_role "$ROLE")
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000266 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 Fazekas22ef5732012-12-16 14:03:06 +0100268 fi
269fi