22ef5731 |
#!/usr/bin/env bash
|
0f2d954b |
# **create_userrc.sh**
# Pre-create rc files and credentials for the default users.
# Warning: This script just for development purposes |
22ef5731 |
|
c85ade77 |
set -o errexit |
646085d7 |
# short_source prints out the current location of the caller in a way
# that strips redundant directories. This is useful for PS4
# usage. Needed before we start tracing due to how we set
# PS4. Normally we'd pick this up from stackrc, but that's not sourced
# here.
function short_source {
saveIFS=$IFS
IFS=" "
called=($(caller 0))
IFS=$saveIFS
file=${called[2]}
file=${file#$RC_DIR/}
printf "%-40s " "$file:${called[1]}:${called[0]}"
} |
daa7a41e |
# PS4 is exported to child shells and uses the 'short_source' function, so
# export it so child shells have access to the 'short_source' function also.
export -f short_source |
646085d7 |
|
c85ade77 |
set -o xtrace
|
22ef5731 |
ACCOUNT_DIR=./accrc
|
aee18c74 |
function display_help { |
22ef5731 |
cat <<EOF
usage: $0 <options..>
|
9d6d8f80 |
This script creates certificates and sourcable rc files per project/user. |
22ef5731 |
Target account directory hierarchy:
target_dir-|
|-cacert.pem |
9d6d8f80 |
|-project1-name|
| |- user1
| |- user1-cert.pem
| |- user1-pk.pem
| |- user2
| ..
|-project2-name.. |
22ef5731 |
..
Optional Arguments
-P include password to the rc files; with -A it assume all users password is the same
-A try with all user
-u <username> create files just for the specified user |
9d6d8f80 |
-C <project_name> create user and project, the specifid project will be the user's project
-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) |
22ef5731 |
-p <userpass> password for the user |
e389aed5 |
--heat-url <heat_url> |
22ef5731 |
--os-username <username>
--os-password <admin password> |
9d6d8f80 |
--os-project-name <project_name>
--os-project-id <project_id> |
c54d4ab9 |
--os-user-domain-id <user_domain_id>
--os-user-domain-name <user_domain_name>
--os-project-domain-id <project_domain_id>
--os-project-domain-name <project_domain_name> |
22ef5731 |
--os-auth-url <auth_url> |
bd24a8d0 |
--os-cacert <cert file> |
22ef5731 |
--target-dir <target_directory> |
9d6d8f80 |
--skip-project <project-name> |
22ef5731 |
--debug
Example:
$0 -AP |
9d6d8f80 |
$0 -P -C myproject -u myuser -p mypass |
22ef5731 |
EOF
}
|
c54d4ab9 |
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 |
22ef5731 |
display_help
exit 1
fi
eval set -- $options
ADDPASS="" |
e389aed5 |
HEAT_URL="" |
22ef5731 |
|
9d6d8f80 |
# The services users usually in the service project. |
22ef5731 |
# rc files for service users, is out of scope. |
9d6d8f80 |
# Supporting different project for services is out of scope.
SKIP_PROJECT="service" |
22ef5731 |
MODE=""
ROLE=Member
USER_NAME=""
USER_PASS="" |
86a8e976 |
while [ $# -gt 0 ]; do |
22ef5731 |
case "$1" in
-h|--help) display_help; exit 0 ;;
--os-username) export OS_USERNAME=$2; shift ;;
--os-password) export OS_PASSWORD=$2; shift ;; |
9d6d8f80 |
--os-tenant-name) export OS_PROJECT_NAME=$2; shift ;;
--os-tenant-id) export OS_PROJECT_ID=$2; shift ;;
--os-project-name) export OS_PROJECT_NAME=$2; shift ;;
--os-project-id) export OS_PROJECT_ID=$2; shift ;; |
c54d4ab9 |
--os-user-domain-id) export OS_USER_DOMAIN_ID=$2; shift ;;
--os-user-domain-name) export OS_USER_DOMAIN_NAME=$2; shift ;;
--os-project-domain-id) export OS_PROJECT_DOMAIN_ID=$2; shift ;;
--os-project-domain-name) export OS_PROJECT_DOMAIN_NAME=$2; shift ;; |
9d6d8f80 |
--skip-tenant) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
--skip-project) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;; |
22ef5731 |
--os-auth-url) export OS_AUTH_URL=$2; shift ;; |
bd24a8d0 |
--os-cacert) export OS_CACERT=$2; shift ;; |
22ef5731 |
--target-dir) ACCOUNT_DIR=$2; shift ;; |
e389aed5 |
--heat-url) HEAT_URL=$2; shift ;; |
22ef5731 |
--debug) set -o xtrace ;;
-u) MODE=${MODE:-one}; USER_NAME=$2; shift ;;
-p) USER_PASS=$2; shift ;;
-A) MODE=all; ;;
-P) ADDPASS="yes" ;; |
9d6d8f80 |
-C) MODE=create; PROJECT=$2; shift ;; |
22ef5731 |
-r) ROLE=$2; shift ;;
(--) shift; break ;;
(-*) echo "$0: error - unrecognized option $1" >&2; display_help; exit 1 ;;
(*) echo "$0: error - unexpected argument $1" >&2; display_help; exit 1 ;;
esac
shift
done
if [ -z "$OS_PASSWORD" ]; then
if [ -z "$ADMIN_PASSWORD" ];then
echo "The admin password is required option!" >&2
exit 2
else
OS_PASSWORD=$ADMIN_PASSWORD
fi
fi
|
9d6d8f80 |
if [ -z "$OS_PROJECT_ID" -a "$OS_TENANT_ID" ]; then
export OS_PROJECT_ID=$OS_TENANT_ID
fi
if [ -z "$OS_PROJECT_NAME" -a "$OS_TENANT_NAME" ]; then
export OS_PROJECT_NAME=$OS_TENANT_NAME
fi
if [ -z "$OS_PROJECT_NAME" -a -z "$OS_PROJECT_ID" ]; then
export OS_PROJECT_NAME=admin |
22ef5731 |
fi
if [ -z "$OS_USERNAME" ]; then |
0b865a55 |
export OS_USERNAME=admin |
22ef5731 |
fi
if [ -z "$OS_AUTH_URL" ]; then |
f8dbfd39 |
export OS_AUTH_URL=http://localhost:5000/v3/ |
22ef5731 |
fi
|
c54d4ab9 |
if [ -z "$OS_USER_DOMAIN_ID" -a -z "$OS_USER_DOMAIN_NAME" ]; then
# purposefully not exported as it would force v3 auth within this file.
OS_USER_DOMAIN_ID=default
fi
if [ -z "$OS_PROJECT_DOMAIN_ID" -a -z "$OS_PROJECT_DOMAIN_NAME" ]; then
# purposefully not exported as it would force v3 auth within this file.
OS_PROJECT_DOMAIN_ID=default
fi
|
22ef5731 |
USER_PASS=${USER_PASS:-$OS_PASSWORD}
USER_NAME=${USER_NAME:-$OS_USERNAME}
if [ -z "$MODE" ]; then
echo "You must specify at least -A or -u parameter!" >&2
echo
display_help
exit 3
fi
|
aee18c74 |
function add_entry { |
22ef5731 |
local user_id=$1
local user_name=$2 |
9d6d8f80 |
local project_id=$3
local project_name=$4 |
22ef5731 |
local user_passwd=$5
|
9d6d8f80 |
mkdir -p "$ACCOUNT_DIR/$project_name"
local rcfile="$ACCOUNT_DIR/$project_name/$user_name" |
3bf69e83 |
|
22ef5731 |
cat >"$rcfile" <<EOF
# OpenStack USER ID = $user_id
export OS_USERNAME="$user_name" |
9d6d8f80 |
# OpenStack project ID = $project_id
export OS_PROJECT_NAME="$project_name" |
22ef5731 |
export OS_AUTH_URL="$OS_AUTH_URL" |
bd24a8d0 |
export OS_CACERT="$OS_CACERT" |
22ef5731 |
export NOVA_CERT="$ACCOUNT_DIR/cacert.pem"
EOF
if [ -n "$ADDPASS" ]; then
echo "export OS_PASSWORD=\"$user_passwd\"" >>"$rcfile"
fi |
e389aed5 |
if [ -n "$HEAT_URL" ]; then |
9d6d8f80 |
echo "export HEAT_URL=\"$HEAT_URL/$project_id\"" >>"$rcfile" |
e389aed5 |
echo "export OS_NO_CLIENT_AUTH=True" >>"$rcfile"
fi |
c54d4ab9 |
for v in OS_USER_DOMAIN_ID OS_USER_DOMAIN_NAME OS_PROJECT_DOMAIN_ID OS_PROJECT_DOMAIN_NAME; do
if [ ${!v} ]; then
echo "export $v=${!v}" >>"$rcfile"
else
echo "unset $v" >>"$rcfile"
fi
done |
22ef5731 |
}
#admin users expected |
bce8899c |
function create_or_get_project {
local name=$1
local id
eval $(openstack project show -f shell -c id $name)
if [[ -z $id ]]; then
eval $(openstack project create -f shell -c id $name) |
22ef5731 |
fi |
bce8899c |
echo $id |
22ef5731 |
}
|
aee18c74 |
function create_or_get_role { |
bce8899c |
local name=$1
local id
eval $(openstack role show -f shell -c id $name)
if [[ -z $id ]]; then
eval $(openstack role create -f shell -c id $name) |
22ef5731 |
fi |
bce8899c |
echo $id |
22ef5731 |
}
# Provides empty string when the user does not exists |
aee18c74 |
function get_user_id { |
bce8899c |
openstack user list | grep " $1 " | cut -d " " -f2 |
22ef5731 |
}
if [ $MODE != "create" ]; then |
9d6d8f80 |
# looks like I can't ask for all project related to a specified user
openstack project list --long --quote none -f csv | grep ',True' | grep -v "${SKIP_PROJECT}" | while IFS=, read project_id project_name desc enabled; do
openstack user list --project $project_id --long --quote none -f csv | grep ',True' | while IFS=, read user_id user_name project email enabled; do |
22ef5731 |
if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then |
0b865a55 |
continue; |
22ef5731 |
fi |
1814e671 |
# Checks for a specific password defined for an user. |
3324f19f |
# Example for an username johndoe: JOHNDOE_PASSWORD=1234 |
7c6d005e |
# This mechanism is used by lib/swift
eval SPECIFIC_UPASSWORD="\$${user_name}_password" |
1814e671 |
if [ -n "$SPECIFIC_UPASSWORD" ]; then
USER_PASS=$SPECIFIC_UPASSWORD
fi |
9d6d8f80 |
add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" |
22ef5731 |
done
done
else |
9d6d8f80 |
project_name=$PROJECT
project_id=$(create_or_get_project "$PROJECT") |
22ef5731 |
user_name=$USER_NAME
user_id=`get_user_id $user_name`
if [ -z "$user_id" ]; then |
9d6d8f80 |
eval $(openstack user create "$user_name" --project "$project_id" --password "$USER_PASS" --email "$user_name@example.com" -f shell -c id) |
bce8899c |
user_id=$id |
9d6d8f80 |
add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" |
22ef5731 |
else |
bce8899c |
role_id=$(create_or_get_role "$ROLE") |
9d6d8f80 |
openstack role add "$role_id" --user "$user_id" --project "$project_id"
add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS" |
22ef5731 |
fi
fi |