blob: f4a4edcbe28fe3e1b26e919e2e81fb48065541ee [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}
John L. Villalovosdaa7a412016-05-05 12:50:52 -070025# 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.
27export -f short_source
Sean Dague646085d2016-03-21 17:00:51 -040028
Adam Spiersc85ade72013-10-01 00:35:16 +010029set -o xtrace
30
Attila Fazekas22ef5732012-12-16 14:03:06 +010031ACCOUNT_DIR=./accrc
32
Ian Wienandaee18c72014-02-21 15:35:08 +110033function display_help {
Attila Fazekas22ef5732012-12-16 14:03:06 +010034cat <<EOF
35
36usage: $0 <options..>
37
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000038This script creates certificates and sourcable rc files per project/user.
Attila Fazekas22ef5732012-12-16 14:03:06 +010039
40Target account directory hierarchy:
41target_dir-|
42 |-cacert.pem
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000043 |-project1-name|
44 | |- user1
45 | |- user1-cert.pem
46 | |- user1-pk.pem
47 | |- user2
48 | ..
49 |-project2-name..
Attila Fazekas22ef5732012-12-16 14:03:06 +010050 ..
51
52Optional 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 Lennox9d6d8f82015-06-22 03:37:59 +000056-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 Fazekas22ef5732012-12-16 14:03:06 +010058-p <userpass> password for the user
Steve Bakere389aed2014-09-23 17:10:39 +120059--heat-url <heat_url>
Attila Fazekas22ef5732012-12-16 14:03:06 +010060--os-username <username>
61--os-password <admin password>
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000062--os-project-name <project_name>
63--os-project-id <project_id>
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +000064--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 Fazekas22ef5732012-12-16 14:03:06 +010068--os-auth-url <auth_url>
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100069--os-cacert <cert file>
Attila Fazekas22ef5732012-12-16 14:03:06 +010070--target-dir <target_directory>
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000071--skip-project <project-name>
Attila Fazekas22ef5732012-12-16 14:03:06 +010072--debug
73
74Example:
75$0 -AP
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000076$0 -P -C myproject -u myuser -p mypass
Attila Fazekas22ef5732012-12-16 14:03:06 +010077EOF
78}
79
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +000080if ! 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 +010081 display_help
82 exit 1
83fi
84eval set -- $options
85ADDPASS=""
Steve Bakere389aed2014-09-23 17:10:39 +120086HEAT_URL=""
Attila Fazekas22ef5732012-12-16 14:03:06 +010087
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000088# The services users usually in the service project.
Attila Fazekas22ef5732012-12-16 14:03:06 +010089# rc files for service users, is out of scope.
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000090# Supporting different project for services is out of scope.
91SKIP_PROJECT="service"
Attila Fazekas22ef5732012-12-16 14:03:06 +010092MODE=""
93ROLE=Member
94USER_NAME=""
95USER_PASS=""
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010096while [ $# -gt 0 ]; do
Attila Fazekas22ef5732012-12-16 14:03:06 +010097 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 Lennox9d6d8f82015-06-22 03:37:59 +0000101 --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 Lennoxc54d4ab2015-06-22 04:07:18 +0000105 --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 Lennox9d6d8f82015-06-22 03:37:59 +0000109 --skip-tenant) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
110 --skip-project) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100111 --os-auth-url) export OS_AUTH_URL=$2; shift ;;
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000112 --os-cacert) export OS_CACERT=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100113 --target-dir) ACCOUNT_DIR=$2; shift ;;
Steve Bakere389aed2014-09-23 17:10:39 +1200114 --heat-url) HEAT_URL=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100115 --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 Lennox9d6d8f82015-06-22 03:37:59 +0000120 -C) MODE=create; PROJECT=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100121 -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
127done
128
129if [ -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
136fi
137
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000138if [ -z "$OS_PROJECT_ID" -a "$OS_TENANT_ID" ]; then
139 export OS_PROJECT_ID=$OS_TENANT_ID
140fi
141
142if [ -z "$OS_PROJECT_NAME" -a "$OS_TENANT_NAME" ]; then
143 export OS_PROJECT_NAME=$OS_TENANT_NAME
144fi
145
146if [ -z "$OS_PROJECT_NAME" -a -z "$OS_PROJECT_ID" ]; then
147 export OS_PROJECT_NAME=admin
Attila Fazekas22ef5732012-12-16 14:03:06 +0100148fi
149
150if [ -z "$OS_USERNAME" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400151 export OS_USERNAME=admin
Attila Fazekas22ef5732012-12-16 14:03:06 +0100152fi
153
154if [ -z "$OS_AUTH_URL" ]; then
Paulo Ewerton75bf9722016-01-22 19:13:31 +0000155 export OS_AUTH_URL=http://localhost:5000/v3/
Attila Fazekas22ef5732012-12-16 14:03:06 +0100156fi
157
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +0000158if [ -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
161fi
162
163if [ -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
166fi
167
Attila Fazekas22ef5732012-12-16 14:03:06 +0100168USER_PASS=${USER_PASS:-$OS_PASSWORD}
169USER_NAME=${USER_NAME:-$OS_USERNAME}
170
171if [ -z "$MODE" ]; then
172 echo "You must specify at least -A or -u parameter!" >&2
173 echo
174 display_help
175 exit 3
176fi
177
Ian Wienandaee18c72014-02-21 15:35:08 +1100178function add_entry {
Attila Fazekas22ef5732012-12-16 14:03:06 +0100179 local user_id=$1
180 local user_name=$2
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000181 local project_id=$3
182 local project_name=$4
Attila Fazekas22ef5732012-12-16 14:03:06 +0100183 local user_passwd=$5
184
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000185 mkdir -p "$ACCOUNT_DIR/$project_name"
186 local rcfile="$ACCOUNT_DIR/$project_name/$user_name"
Ian Wienand3bf69e82016-03-15 12:21:34 +1100187
Attila Fazekas22ef5732012-12-16 14:03:06 +0100188 cat >"$rcfile" <<EOF
Attila Fazekas22ef5732012-12-16 14:03:06 +0100189# OpenStack USER ID = $user_id
190export OS_USERNAME="$user_name"
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000191# OpenStack project ID = $project_id
192export OS_PROJECT_NAME="$project_name"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100193export OS_AUTH_URL="$OS_AUTH_URL"
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000194export OS_CACERT="$OS_CACERT"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100195export NOVA_CERT="$ACCOUNT_DIR/cacert.pem"
196EOF
197 if [ -n "$ADDPASS" ]; then
198 echo "export OS_PASSWORD=\"$user_passwd\"" >>"$rcfile"
199 fi
Steve Bakere389aed2014-09-23 17:10:39 +1200200 if [ -n "$HEAT_URL" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000201 echo "export HEAT_URL=\"$HEAT_URL/$project_id\"" >>"$rcfile"
Steve Bakere389aed2014-09-23 17:10:39 +1200202 echo "export OS_NO_CLIENT_AUTH=True" >>"$rcfile"
203 fi
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +0000204 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 Fazekas22ef5732012-12-16 14:03:06 +0100211}
212
213#admin users expected
Steve Martinellibce88992014-03-14 00:40:06 -0500214function 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 Fazekas22ef5732012-12-16 14:03:06 +0100220 fi
Steve Martinellibce88992014-03-14 00:40:06 -0500221 echo $id
Attila Fazekas22ef5732012-12-16 14:03:06 +0100222}
223
Ian Wienandaee18c72014-02-21 15:35:08 +1100224function create_or_get_role {
Steve Martinellibce88992014-03-14 00:40:06 -0500225 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 Fazekas22ef5732012-12-16 14:03:06 +0100230 fi
Steve Martinellibce88992014-03-14 00:40:06 -0500231 echo $id
Attila Fazekas22ef5732012-12-16 14:03:06 +0100232}
233
234# Provides empty string when the user does not exists
Ian Wienandaee18c72014-02-21 15:35:08 +1100235function get_user_id {
Steve Martinellibce88992014-03-14 00:40:06 -0500236 openstack user list | grep " $1 " | cut -d " " -f2
Attila Fazekas22ef5732012-12-16 14:03:06 +0100237}
238
239if [ $MODE != "create" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000240 # 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 Fazekas22ef5732012-12-16 14:03:06 +0100243 if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400244 continue;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100245 fi
Sahid Orentino Ferdjaoui1814e672014-02-11 17:56:07 +0100246
247 # Checks for a specific password defined for an user.
Dean Troyer3324f192014-09-18 09:26:39 -0500248 # Example for an username johndoe: JOHNDOE_PASSWORD=1234
JordanP7c6d0052014-10-06 23:08:50 +0200249 # This mechanism is used by lib/swift
250 eval SPECIFIC_UPASSWORD="\$${user_name}_password"
Sahid Orentino Ferdjaoui1814e672014-02-11 17:56:07 +0100251 if [ -n "$SPECIFIC_UPASSWORD" ]; then
252 USER_PASS=$SPECIFIC_UPASSWORD
253 fi
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000254 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100255 done
256 done
257else
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000258 project_name=$PROJECT
259 project_id=$(create_or_get_project "$PROJECT")
Attila Fazekas22ef5732012-12-16 14:03:06 +0100260 user_name=$USER_NAME
261 user_id=`get_user_id $user_name`
262 if [ -z "$user_id" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000263 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 -0500264 user_id=$id
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000265 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100266 else
Steve Martinellibce88992014-03-14 00:40:06 -0500267 role_id=$(create_or_get_role "$ROLE")
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000268 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 Fazekas22ef5732012-12-16 14:03:06 +0100270 fi
271fi