Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust setup script #164

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions pkg/controllers/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,35 @@ if [[ ! -s "$TFO_MAIN_MODULE_ADDONS/inline-module.tf" ]]; then
if [[ "$key" != *".tf" ]] || [[ "$key" != *".json" ]]; then
suffix=".tf" # select tf as default
fi
jq -r --arg key "$key" '.data[$key]' <<< $configmap_json > "${TFO_MAIN_MODULE}/${key}${suffix}"
jq -r --arg key "$key" '.data[$key]' <<<$configmap_json >"${TFO_MAIN_MODULE}/${key}${suffix}"
else
for key in $(jq -r '.data | keys[]' <<< $configmap_json); do
for key in $(jq -r '.data | keys[]' <<<$configmap_json); do
# No assumptions about the file types are made here. The user
# should create keys that are properly suffixed for terraform.
jq -r --arg key "$key" '.data[$key]' <<< $configmap_json > "${TFO_MAIN_MODULE}/${key}"
jq -r --arg key "$key" '.data[$key]' <<<$configmap_json >"${TFO_MAIN_MODULE}/${key}"
done
fi
# Check if this is a source directory instead of a git repo
elif [[ "$TFO_MAIN_MODULE_REPO" == file://* ]]; then
local_module_path="${TFO_MAIN_MODULE_REPO#"file://"}"
if [[ -d "$local_module_path" ]]; then
cp -r "$local_module_path" "$TFO_MAIN_MODULE"
else
echo "terraform module file source: $local_module_path, does not exist"
exit 1
fi
local_module_path="${TFO_MAIN_MODULE_REPO#"file://"}"
if [[ -d "$local_module_path" ]]; then
cp -r "$local_module_path" "$TFO_MAIN_MODULE"
else
echo "terraform module file source: $local_module_path, does not exist"
exit 1
fi
else
# The terraform module is a repo that must be downloaded
MAIN_MODULE_TMP=`mktemp -d`
git clone "$TFO_MAIN_MODULE_REPO" "$MAIN_MODULE_TMP/stack" || exit $?
cd "$MAIN_MODULE_TMP/stack"
cd $(mktemp -d)
git clone "$TFO_MAIN_MODULE_REPO" 2>&1 | tee .out
exit_code=${PIPESTATUS[0]}
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
reponame=$(sed -n "s/.*'\([^']*\)'.*/\1/p" .out)
cd "$reponame"
git checkout "$TFO_MAIN_MODULE_REPO_REF"
echo "Setting up module for $reponame/$TFO_MAIN_MODULE_REPO_SUBDIR"
cp -r "$TFO_MAIN_MODULE_REPO_SUBDIR" "$TFO_MAIN_MODULE"
fi
fi
Expand All @@ -85,24 +91,24 @@ if stat backend_override.tf >/dev/null 2>/dev/null; then
else
echo "Loading hashicorp backend"
set -x
envsubst < /backend.tf > "$TFO_ROOT_PATH/backend_override.tf"
envsubst </backend.tf >"$TFO_ROOT_PATH/backend_override.tf"
mv "$TFO_ROOT_PATH/backend_override.tf" .
fi

function join_by {
local d="$1" f=${2:-$(</dev/stdin)};
local d="$1" f=${2:-$(</dev/stdin)}
if [[ -z "$f" ]]; then return 1; fi
if shift 2; then
printf %s "$f" "${@/#/$d}"
else
join_by "$d" $f
join_by "$d" $f
fi
}

function add_file_as_next_index {
dir="$1"
file="$2"
idx=$(ls "$dir"|wc -l)
idx=$(ls "$dir" | wc -l)
cp "$file" "$dir/${idx}_$(basename $2)"
}

Expand All @@ -114,9 +120,9 @@ function fetch_git {
tfvar="$4"
branch="$5"
path="$TFO_MAIN_MODULE/$relpath"
if [[ "$files" == "." ]] && ( [[ "$relpath" == "." ]] || [[ "$relpath" == "" ]] ); then
a=$(basename $repo)
path="$TFO_MAIN_MODULE/${a%.*}"
if [[ "$files" == "." ]] && ([[ "$relpath" == "." ]] || [[ "$relpath" == "" ]]); then
a=$(basename $repo)
path="$TFO_MAIN_MODULE/${a%.*}"
fi
# printf -- 'mkdir -p "'$path'"\n'
mkdir -p "$path"
Expand All @@ -130,10 +136,10 @@ function fetch_git {

if [[ "$tfvar" == "true" ]]; then
for file in $files; do
if [[ -f "$file" ]]; then
# printf 'add_file_as_next_index "'$vardir'" "'$file'"'
add_file_as_next_index "$vardir" "$file"
fi
if [[ -f "$file" ]]; then
# printf 'add_file_as_next_index "'$vardir'" "'$file'"'
add_file_as_next_index "$vardir" "$file"
fi
done
fi
echo done
Expand All @@ -144,14 +150,14 @@ LENGTH=$(jq '.|length' $FILE)

for i in $(seq 0 $((LENGTH - 1))); do
DATA=$(mktemp)
jq --argjson i $i '.[$i]' $FILE > $DATA
jq --argjson i $i '.[$i]' $FILE >$DATA
fetchtype=$(jq -r '.detect' $DATA)
repo=$(jq -r '.repo' $DATA)
files=$(jq -r '.files[]' $DATA | join_by " ")
path=$(jq -r '.path' $DATA)
tfvar=$(jq -r '.useAsVar' $DATA)
branch=$(jq -r '.hash' $DATA)
if [[ "$fetchtype" == "git" ]];then
if [[ "$fetchtype" == "git" ]]; then
fetch_git "$repo" "$path" "$files" "$tfvar" "$branch"
fi
done
Loading