Skip to content

Commit c0693dd

Browse files
committed
Restructured Ansible functionality
1 parent ce52756 commit c0693dd

File tree

7 files changed

+465
-400
lines changed

7 files changed

+465
-400
lines changed

bin/spin

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@ export SPIN_GROUP_ID
1616
# Default Images
1717
SPIN_PHP_IMAGE=${SPIN_PHP_IMAGE:-"serversideup/php:cli"}
1818
SPIN_NODE_IMAGE=${SPIN_NODE_IMAGE:-"node:20"}
19-
SPIN_ANSIBLE_IMAGE=${SPIN_ANSIBLE_IMAGE:-"docker.io/serversideup/ansible-core:2.17-alpine"}
20-
SPIN_ANSIBLE_COLLECTION_NAME=${SPIN_ANSIBLE_COLLECTION_NAME:-"serversideup.spin"}
21-
SPIN_YQ_IMAGE=${SPIN_YQ_IMAGE:-"docker.io/mikefarah/yq:4.44.2"}
19+
SPIN_ANSIBLE_IMAGE=${SPIN_ANSIBLE_IMAGE:-"docker.io/serversideup/ansible-core:2.18-alpine"}
2220
SPIN_GH_CLI_IMAGE=${SPIN_GH_CLI_IMAGE:-"docker.io/serversideup/github-cli:alpine"}
2321

2422
# Script Configuration
2523
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
2624
SPIN_HOME=$(dirname "$SCRIPT_DIR") #Assume the parent directory of this script is the home
2725
SPIN_CACHE_DIR=${SPIN_CACHE_DIR:-$SPIN_HOME/cache}
2826
SPIN_CONFIG_FILE_LOCATION=${SPIN_CONFIG_FILE_LOCATION:-"$SPIN_HOME/conf/spin.conf"}
27+
SPIN_CI_FOLDER=${SPIN_CI_FOLDER:-".infrastructure/conf/ci"}
2928
AUTO_UPDATE_INTERVAL_IN_DAYS=${AUTO_UPDATE_INTERVAL_IN_DAYS:-14}
3029
AUTO_PULL_INTERVAL_IN_DAYS=${AUTO_PULL_INTERVAL_IN_DAYS:-1}
3130

31+
# Ansible Variables
32+
SPIN_ANSIBLE_COLLECTION_NAME="${SPIN_ANSIBLE_COLLECTION_NAME:-"serversideup.spin"}"
33+
SPIN_INVENTORY_FILE="${SPIN_INVENTORY_FILE:-"/etc/ansible/collections/ansible_collections/serversideup/spin/plugins/inventory/spin-dynamic-inventory.sh"}"
34+
SPIN_ANSIBLE_COLLECTIONS_PATH="$SPIN_CACHE_DIR/collections"
35+
3236
# Import common functions
3337
source "$SPIN_HOME/lib/functions.sh"
3438
setup_color

lib/actions/configure.sh

Lines changed: 90 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
# Main Action Handler
55
#################################
66
action_configure() {
7+
8+
validate_project_setup
9+
710
case "$1" in
811
gha)
912
shift
@@ -21,88 +24,73 @@ action_configure() {
2124
# Helper Functions
2225
#################################
2326
configure_gha() {
27+
local deploy_public_key_content=''
28+
local environment_file=''
29+
30+
# Ensure environment is specified
2431
if [ $# -eq 0 ]; then
2532
echo "${BOLD}${RED}❌ No environment specified${RESET}"
2633
echo "Usage: spin configure gha <environment>"
2734
echo "Example: spin configure gha production"
2835
return 1
2936
fi
3037

31-
local gha_environment="$1"
38+
# Check if GitHub CLI image exists locally
39+
if ! docker image inspect "$SPIN_GH_CLI_IMAGE" >/dev/null 2>&1; then
40+
echo "${BOLD}${BLUE}🐳 Pulling GitHub CLI image...${RESET}"
41+
if ! docker pull "$SPIN_GH_CLI_IMAGE"; then
42+
echo "${BOLD}${RED}❌ Failed to pull GitHub CLI image${RESET}"
43+
exit 1
44+
fi
45+
fi
46+
47+
# Set and validate envioronment
48+
gha_environment="$1"
3249
shift # Remove the first argument
33-
local gha_environment_uppercase=$(echo "$gha_environment" | tr '[:lower:]' '[:upper:]')
34-
35-
validate_repository_setup || exit 1
36-
37-
local environment_file
50+
gha_environment_uppercase=$(echo "$gha_environment" | tr '[:lower:]' '[:upper:]')
51+
validate_github_repository_setup || exit 1
3852
environment_file=$(validate_environment_file "$gha_environment") || exit 1
3953

54+
# Set ENV_BASE_64
4055
gh_set_env --base64 --variable "${gha_environment_uppercase}_ENV_FILE_BASE64" --file "$environment_file"
4156

42-
configure_gha_deployment_key "$@"
43-
configure_gha_authorized_keys
44-
45-
}
46-
47-
configure_gha_deployment_key() {
48-
local inventory_file="${SPIN_INVENTORY_FILE:-"/etc/ansible/collections/ansible_collections/serversideup/spin/plugins/inventory/spin-dynamic-inventory.sh"}"
49-
local infrastructure_folder=".infrastructure"
50-
51-
if [ ! -d "$infrastructure_folder" ]; then
52-
echo "${BOLD}${RED}❌ Infrastructure folder not found${RESET}"
53-
echo "Please ensure you're in the root of your project."
54-
return 1
55-
fi
56-
57-
if [ ! -d "$infrastructure_folder/deploy-keys" ] || [ ! -f "$infrastructure_folder/deploy-keys/.gitignore" ]; then
58-
mkdir -p "$infrastructure_folder/deploy-keys"
59-
echo "*" > "$infrastructure_folder/deploy-keys/.gitignore"
60-
echo "!.gitignore" >> "$infrastructure_folder/deploy-keys/.gitignore"
61-
fi
62-
63-
if [ ! -f "$infrastructure_folder/deploy-keys/id_ed25519_deploy" ]; then
64-
echo "🔑 Generating deployment key"
65-
ssh-keygen -t ed25519 -C "deploy-key" -f "$infrastructure_folder/deploy-keys/id_ed25519_deploy" -N ""
66-
echo "${BOLD}${GREEN}✅ Deployment key generated${RESET}"
67-
else
68-
echo "🔑 Using existing deployment key found at \"$infrastructure_folder/deploy-keys/id_ed25519_deploy\""
69-
fi
70-
71-
# Read the public key content
72-
local deploy_public_key
73-
deploy_public_key=$(cat "$infrastructure_folder/deploy-keys/id_ed25519_deploy.pub")
74-
75-
echo "🔑 Adding deployment key to GitHub repository"
76-
gh_set_env --variable "SSH_DEPLOY_PRIVATE_KEY" --file "$infrastructure_folder/deploy-keys/id_ed25519_deploy"
77-
78-
echo "🔐 Adding deployment key to servers"
79-
prepare_ansible_args "$@"
80-
run_ansible --allow-ssh --mount-path "$(pwd)" \
81-
ansible-playbook serversideup.spin.update_deploy_key \
82-
--inventory "$inventory_file" \
83-
--extra-vars @./.spin.yml \
84-
--extra-vars "deploy_public_key='$deploy_public_key'" \
85-
"${SPIN_ANSIBLE_ARGS[@]}" \
86-
"${SPIN_UNPROCESSED_ARGS[@]}"
87-
88-
echo "${BOLD}${GREEN}✅ Deployment key added to servers${RESET}"
89-
}
90-
91-
configure_gha_authorized_keys() {
92-
echo "🔑 Gathering authorized keys for sudo users"
93-
local authorized_keys
94-
authorized_keys=$(run_ansible --minimal-output --mount-path "$(pwd)" \
95-
ansible-playbook serversideup.spin.get_sudo_keys \
96-
--extra-vars @./.spin.yml \
97-
| sed -n 's/.*"msg": "\(.*\)".*/\1/p' \
98-
| sed 's/\\\\n/\n/g') # Handle the double escaped newlines
99-
100-
echo "🔑 Adding authorized keys to GitHub repository"
101-
echo "$authorized_keys"
57+
# Ensure deployment key exists
58+
if [ ! -f "$SPIN_CI_FOLDER/SSH_DEPLOY_PRIVATE_KEY" ]; then
59+
echo "🔑 Generating deployment key"
60+
ssh-keygen -t ed25519 -C "deploy-key" -f "$SPIN_CI_FOLDER/SSH_DEPLOY_PRIVATE_KEY" -N ""
61+
echo "${BOLD}${GREEN}✅ Deployment key generated${RESET}"
62+
else
63+
echo "🔑 Using existing deployment key found at \"$SPIN_CI_FOLDER/SSH_DEPLOY_PRIVATE_KEY\""
64+
fi
10265

103-
# Add the authorized keys to GitHub secrets
104-
gh_set_env --variable "AUTHORIZED_KEYS" --value "$authorized_keys"
105-
66+
deploy_public_key_content=$(cat "$SPIN_CI_FOLDER/SSH_DEPLOY_PRIVATE_KEY.pub")
67+
68+
# Prepare CI variables with Ansible
69+
echo "🔑 Preparing CI variables with Ansible"
70+
prepare_ansible_run "$@"
71+
run_ansible --allow-ssh --mount-path "$(pwd):/ansible" \
72+
ansible-playbook serversideup.spin.prepare_ci_environment \
73+
--inventory "$SPIN_INVENTORY_FILE" \
74+
--extra-vars @./.spin.yml \
75+
--extra-vars "spin_environment=$gha_environment" \
76+
--extra-vars "spin_ci_folder=$SPIN_CI_FOLDER" \
77+
--extra-vars "deploy_public_key='$deploy_public_key_content'" \
78+
"${SPIN_ANSIBLE_ARGS[@]}" \
79+
"${SPIN_UNPROCESSED_ARGS[@]}"
80+
81+
echo "🔑 Adding GitHub Actions secrets..."
82+
# Loop through all files in the CI folder (sorted alphabetically)
83+
find "$SPIN_CI_FOLDER" -type f -maxdepth 1 | sort | while read -r filepath; do
84+
file=$(basename "$filepath")
85+
# Skip files with file extensions and .gitignore
86+
if [[ "$file" != *.* ]]; then
87+
# Convert filename to uppercase for secret name
88+
secret_name=$(echo "$file" | tr '[:lower:]' '[:upper:]')
89+
gh_set_env --variable "$secret_name" --file "$SPIN_CI_FOLDER/$file"
90+
fi
91+
done
92+
93+
echo "${BOLD}${BLUE}🚀 You're now ready to push to deploy!${RESET}"
10694
}
10795

10896
gh_set_env() {
@@ -191,6 +179,38 @@ repository_exists() {
191179
git rev-parse --is-inside-work-tree >/dev/null 2>&1
192180
}
193181

182+
validate_project_setup() {
183+
184+
# Validate infrastructure folder is present
185+
if [ ! -d ".infrastructure" ]; then
186+
echo "${BOLD}${RED}❌ Infrastructure folder not found${RESET}"
187+
echo "Please ensure you're in the root of your project."
188+
return 1
189+
fi
190+
191+
if [ ! -f ".spin.yml" ]; then
192+
echo "${BOLD}${RED}❌ .spin.yml not found${RESET}"
193+
echo "Please ensure you're in the root of your project and a .spin.yml file exists."
194+
return 1
195+
fi
196+
197+
if is_encrypted_with_ansible_vault ".spin.yml" && \
198+
[ ! -f ".vault-password" ]; then
199+
echo "${BOLD}${RED}❌Error: .spin.yml is encrypted with Ansible Vault, but '.vault-password' file is missing.${RESET}"
200+
echo "${BOLD}${YELLOW}Please save your vault password in '.vault-password' in your project root and try again.${RESET}"
201+
return 1
202+
fi
203+
204+
# Create ci folder if it doesn't exist
205+
if [ ! -d "$SPIN_CI_FOLDER" ] || [ ! -f "$SPIN_CI_FOLDER/.gitignore" ]; then
206+
mkdir -p "$SPIN_CI_FOLDER"
207+
echo "*" > "$SPIN_CI_FOLDER/.gitignore"
208+
echo "!.gitignore" >> "$SPIN_CI_FOLDER/.gitignore"
209+
fi
210+
211+
return 0
212+
}
213+
194214
validate_environment_file() {
195215
local gha_environment="$1"
196216
local env_file=".env.$gha_environment"
@@ -207,7 +227,7 @@ validate_environment_file() {
207227
fi
208228
}
209229

210-
validate_repository_setup() {
230+
validate_github_repository_setup() {
211231
if ! repository_exists; then
212232
echo "${BOLD}${RED}❌ Repository not detected.${RESET}"
213233
echo "Please ensure you're in the root of your project. If you need to create a repository, run \`git init\` then \`spin gh repo create\` to create one."

0 commit comments

Comments
 (0)