Skip to content

Commit eb4ecf0

Browse files
committed
Fill in region_name and project_id.
Signed-off-by: Kurt Garloff <kurt@garloff.de>
1 parent cd15714 commit eb4ecf0

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

04-cloud-secret.sh

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ source "$THISDIR/yaml_parse.sh"
1818
# Create namespace
1919
test -n "$CS_NAMESPACE"
2020
kubectl create namespace "$CS_NAMESPACE" || true
21+
# Default clouds.yaml location
22+
CLOUDS_YAML=${CLOUDS_YAML:-~/.config/openstack/clouds.yaml}
2123
# Use csp helper chart to create cloud secret
2224
# Notes on expected clouds.yaml:
2325
# - It should have the secrets (which you often keep in secure.yaml instead) merged into it
26+
# NOTE: This will only work if the auth section is the last entry for this cloud in clouds.yaml
2427
# - The cloud should be called openstack
2528
# - We will detect a cacert in there and pass it to the helper chart
2629
if ! test -r "$CLOUDS_YAML"; then echo "clouds.yaml $CLOUDS_YAML not readable"; exit 2; fi
@@ -29,18 +32,34 @@ OS_CACERT="${OS_CACERT:-$CA}"
2932
# Extract auth parts from secure.yaml if existent, assume same indentation
3033
SEC_YAML="${CLOUDS_YAML%clouds.yaml}secure.yaml"
3134
if test -r "$SEC_YAML"; then SECRETS=$(RMVTREE=1 RMVCOMMENT=1 extract_yaml clouds.$OS_CLOUD.auth < $SEC_YAML || true); fi
32-
# TODO:
33-
# We need a project_id in auth, determine it and add
34-
# - Alternative would be to create an application credential
35-
# TODO:
35+
if test -n "$SECRETS"; then
36+
echo "# Appending secrets from secure.yaml to clouds.yaml"
37+
fi
38+
# Determine whether we need to add project ID
39+
RAW_CLOUD=$(extract_yaml clouds.$OS_CLOUD <$CLOUDS_YAML)
40+
if ! echo "$RAW_CLOUD" | grep -q "^\s*project_id:"; then
41+
# Need openstack CLI for this
42+
PROJECT_NAME=$(echo "$RAW_CLOUD" | grep '^\s*project_name:' | sed 's/^\s*project_name: //')
43+
PROJECT_ID=$(openstack project show $PROJECT_NAME -c id -f value | tr -d '\r')
44+
INDENT=$(echo "$RAW_CLOUD" | grep '^\s*project_name:' | sed 's/^\(\s*\)project_name:.*$/\1/')
45+
SECRETS=$(echo -en "${INDENT}project_id: $PROJECT_ID\n$SECRETS")
46+
echo "# Appending project_id: $PROJECT_ID to clouds.yaml"
47+
fi
3648
# We need a region_name, add it in
37-
#
49+
if ! echo "$RAW_CLOUD" | grep -q '^\s*region_name:'; then
50+
# Need openstack CLI for this
51+
REGION=$(openstack region list -c Region -f value | head -n1 | tr -d '\r')
52+
INDENT=$(echo "$RAW_CLOUD" | grep '^\s*auth:' | sed 's/^\(\s*\)auth:.*$/\1/')
53+
export INSERT="${INDENT}region_name: $REGION"
54+
echo "# Inserting region_name: $REGION to clouds.yaml"
55+
fi
3856
# Construct a clouds.yaml (mode 0600):
3957
# - Only extracting one cloud addressed by $OS_CLOUD
4058
# - By merging secrets in from secure.yaml
4159
# - By renaming it to openstack (current CS limitation)
4260
# - By removing cacert setting
4361
# Store it securely in ~/tmp/clouds-$OS_CLOUD.yaml
62+
echo "# Generating ~/tmp/clouds-$OS_CLOUD.yaml ..."
4463
OLD_UMASK=$(umask)
4564
umask 0177
4665
APPEND="$SECRETS" RMVCOMMENT=1 REMOVE=cacert extract_yaml clouds.$OS_CLOUD < $CLOUDS_YAML | sed "s/^\\(\\s*\\)\\($OS_CLOUD\\):/\\1openstack:/" > ~/tmp/clouds-$OS_CLOUD.yaml
@@ -49,7 +68,7 @@ umask $OLD_UMASK
4968
#if test "$CS_CCMLB=octavia-ovn"; then OCTOVN="--set octavia_ovn=true"; else unset OCTOVN; fi
5069
OCTOVN="--set octavia_ovn=true"
5170
if test -n "$OS_CACERT"; then
52-
echo "Found CA cert file configured to be \"$OS_CACERT\""
71+
echo "# Found CA cert file configured to be \"$OS_CACERT\""
5372
OS_CACERT="$(ls ${OS_CACERT/\~/$HOME})"
5473
# This will error out as needed
5574
# Extract last cert if things get too long (heuristic!)

0 commit comments

Comments
 (0)