Skip to content

Commit

Permalink
Add default config and inventory files from GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydrogers committed Dec 19, 2023
1 parent e2a4389 commit 2ce5efe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/actions/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ action_init() {
fi
done < <(find "$SPIN_HOME/templates/common" -type f)

# Download default config and inventory from GitHub
get_file_from_github_release "serversideup/ansible-collection-spin" "stable" ".spin-inventory.example.ini" "$project_directory/.spin-inventory.ini"
get_file_from_github_release "serversideup/ansible-collection-spin" "stable" ".spin.example.yml" "$project_directory/.spin.yml"

# Encrpytion check
if ! is_encrypted_with_ansible_vault "$project_directory/.spin.yml" || ! is_encrypted_with_ansible_vault "$project_directory/.spin-inventory.ini"; then
Expand Down
28 changes: 28 additions & 0 deletions lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,34 @@ filter_out_spin_arguments() {
echo "${filtered_args[@]}"
}

get_github_release() {
release_type="$1"
local repo="$2"

if [[ $release_type == "stable" ]]; then
local release=$(curl --silent "https://api.github.com/repos/$repo/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
else
local release=$(curl --silent "https://api.github.com/repos/$repo/releases/" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') | head -n 1
fi
echo "$release"
}

get_file_from_github_release() {
local repo="$1"
local release_type="$2"
local source_file="$3"
local destination_file="$4"
local trimmed_destination_file=${destination_file#*/}

if [[ -f "$destination_file" ]]; then
echo "👉 \"$trimmed_destination_file\" already exists. Skipping..."
return 0
fi

curl --silent --location --output "$destination_file" "https://raw.githubusercontent.com/$repo/$(get_github_release "$release_type" "$repo")/$source_file"
echo "\"$trimmed_destination_file\" has been created."
}

is_encrypted_with_ansible_vault() {
local file="$1"

Expand Down

0 comments on commit 2ce5efe

Please sign in to comment.