mirror of
https://github.com/nickyeoman/docker-compose-cookbooks.git
synced 2026-09-04 02:46:23 +00:00
added builder script
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#!/bin/bash
|
||||
|
||||
process_env_vars() {
|
||||
local env_vars="$1" # Accept the environment variables as the first parameter
|
||||
local APP_NAME="$(echo "$2" | tr '[:lower:]' '[:upper:]')" # Convert app name to uppercase
|
||||
local results=() # Array to collect results
|
||||
|
||||
# Loop through each environment variable
|
||||
while IFS= read -r env_var; do
|
||||
# Split into name and value parts based on '='
|
||||
var_name="${env_var%%=*}" # Variable name
|
||||
var_value="${env_var#*=}" # Variable value
|
||||
|
||||
# Handle variables with and without default values
|
||||
if [[ "$var_value" == *'${'*:*'}'* ]]; then
|
||||
# For variables with default values
|
||||
modified_var="${var_name}=\${${APP_NAME}_${var_value#\$\{}"
|
||||
else
|
||||
# For variables without default values
|
||||
modified_var="${var_name}=\${${APP_NAME}_${var_name}:-${var_value}}"
|
||||
fi
|
||||
|
||||
# Append the modified variable to results
|
||||
results+=("$modified_var")
|
||||
done <<< "$env_vars"
|
||||
|
||||
# Output the results joined with new lines
|
||||
printf "%s\n" "${results[@]}" | sort
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Function to parse and handle the variable
|
||||
extract_image_name() {
|
||||
local dirty_img="$1"
|
||||
|
||||
if [[ "$dirty_img" == *\$* ]]; then
|
||||
extracted_value=$(echo "$dirty_img" | sed -n 's/.*:-\([^}]*\)}.*/\1/p')
|
||||
else
|
||||
extracted_value=$dirty_img
|
||||
fi
|
||||
|
||||
echo "$extracted_value"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
|
||||
BLDR_IMAGE_VAR="\${${BLDR_APP^^}_IMAGE:-$BLDR_DC_IMAGE}"
|
||||
|
||||
# Generate Docker Compose content and save it to a variable
|
||||
BLDR_NEW_DCF=$(cat <<EOF
|
||||
version: '3.4'
|
||||
|
||||
services:
|
||||
stash:
|
||||
image: $BLDR_IMAGE_VAR
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
$(echo "$BLDR_DC_ENV" | sed 's/^/ - /')
|
||||
volumes:
|
||||
$(echo "$BLDR_DC_VOLS" | sed 's/^/ - /')
|
||||
EOF
|
||||
)
|
||||
|
||||
# Print the variable to verify the content
|
||||
echo "***** NEW docker-compose.yml *****"
|
||||
echo "$BLDR_NEW_DCF"
|
||||
@@ -0,0 +1,44 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Function to process volumes
|
||||
process_volumes() {
|
||||
local volumes="$1" # Accept volumes as the first parameter
|
||||
local results=() # Array to collect results
|
||||
|
||||
# Loop through each volume
|
||||
while IFS= read -r volume; do
|
||||
# Perform actions with each volume
|
||||
if [[ "$volume" == *\$* ]]; then
|
||||
if [[ "$volume" == *'VOL'* && "$volume" == *'PATH'* ]]; then
|
||||
results+=("$volume")
|
||||
else
|
||||
part="${volume#*\}}/" # Note the \} to handle the literal }
|
||||
part="${part#./}"
|
||||
part="${part#/}"
|
||||
results+=('${VOL_PATH:-./data}/'"$part")
|
||||
fi
|
||||
else
|
||||
if [[ "${volume:0:1}" == '/' ]]; then
|
||||
results+=("$volume")
|
||||
else
|
||||
if [[ "$volume" == './data'* ]]; then
|
||||
part="${volume#./data}"
|
||||
part="${part#/}"
|
||||
results+=('${VOL_PATH:-./data}/'"$part")
|
||||
elif [[ "$volume" == './config'* ]]; then
|
||||
part="${volume#./config}"
|
||||
part="${part#/}"
|
||||
results+=('${VOL_CONFIG_PATH:-./config}/'"$part")
|
||||
else
|
||||
part="${volume#./}"
|
||||
results+=("./$part")
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
# Add your commands here to handle each volume
|
||||
done <<< "$volumes"
|
||||
|
||||
# Output the results joined with new lines
|
||||
printf "%s\n" "${results[@]}" | sort
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
debug_print_helper_variables() {
|
||||
echo "DEBUGGING $(date +%s): "
|
||||
for var in $(compgen -v | grep '^BLDR_'); do
|
||||
echo "$var=${!var}"
|
||||
done
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Define the content to write to the file
|
||||
file_content=$(cat <<EOF
|
||||
version: '3.4'
|
||||
|
||||
services:
|
||||
$BLDR_APP:
|
||||
extends:
|
||||
file: \${COOKBOOK}/$BLDR_APP/docker-compose.yml
|
||||
service: $BLDR_APP
|
||||
EOF
|
||||
)
|
||||
|
||||
# Output the content to a file
|
||||
BLDR_NEW_SE="$file_content"
|
||||
|
||||
echo "***** Sample Extends *****"
|
||||
echo "$BLDR_NEW_SE"
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Function to get a directory using fzf
|
||||
get_directory() {
|
||||
# List all directories in the current directory that don't start with an underscore
|
||||
local directories
|
||||
directories=$(find . -maxdepth 1 -type d -not -name '_*' -exec basename {} \; | grep -v '[_\.]' | sort -r)
|
||||
|
||||
# Use fzf to allow the user to select a directory
|
||||
local selected_dir
|
||||
selected_dir=$(echo "$directories" | fzf --prompt="Select a directory: ")
|
||||
|
||||
# Check if a directory was selected
|
||||
if [[ -z "$selected_dir" ]]; then
|
||||
echo "No directory selected."
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Set the BLDR_DIR variable to the selected directory
|
||||
BLDR_DIR="${BLDR_PARENT_PATH}/${selected_dir}"
|
||||
BLDR_APP=${selected_dir}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Check if BLDR_DIR is set
|
||||
if [[ -z "$BLDR_DIR" ]]; then
|
||||
echo "Error: BLDR_DIR is not set. Please ensure it points to a valid directory."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Define the path to the docker-compose.yml file
|
||||
compose_file="$BLDR_DIR/docker-compose.yml"
|
||||
|
||||
# Check if the docker-compose.yml file exists
|
||||
if [[ ! -f "$compose_file" ]]; then
|
||||
echo "Error: No docker-compose.yml file found in $BLDR_DIR"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Read the contents of the docker-compose.yml file into BLDR_OLD_DCF variable
|
||||
BLDR_OLD_DCF=$(<"$compose_file")
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
get_vols() {
|
||||
local compose_content="$1"
|
||||
local volumes=""
|
||||
|
||||
# Use yq to extract volumes from services
|
||||
volumes=$(echo "$compose_content" | yq eval '.services.*.volumes[]' - 2>/dev/null)
|
||||
|
||||
# Print the volumes, preserving line breaks
|
||||
printf "%s\n" "$volumes"
|
||||
}
|
||||
|
||||
get_env_vars() {
|
||||
local compose_content="$1"
|
||||
local env_vars=""
|
||||
|
||||
# Use yq to extract environment variables from services
|
||||
env_vars=$(echo "$compose_content" | yq eval '.services.*.environment[]' - 2>/dev/null)
|
||||
|
||||
echo "$env_vars"
|
||||
}
|
||||
|
||||
get_image() {
|
||||
local compose_content="$1"
|
||||
local images=""
|
||||
|
||||
# Use yq to extract image names from services
|
||||
images=$(echo "$compose_content" | yq eval '.services.*.image' - 2>/dev/null)
|
||||
|
||||
echo "$images"
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Function to check if a command is installed and provide installation instructions if not
|
||||
check_command() {
|
||||
local command_name="$1"
|
||||
|
||||
if ! command -v "$command_name" &> /dev/null; then
|
||||
echo "$command_name is not installed."
|
||||
echo "To install $command_name:"
|
||||
|
||||
case "$(uname -s)" in
|
||||
Linux*)
|
||||
if [ -f /etc/os-release ]; then
|
||||
. /etc/os-release
|
||||
case "$ID" in
|
||||
fedora)
|
||||
echo " - Fedora: sudo dnf install $command_name"
|
||||
;;
|
||||
arch)
|
||||
echo " - Arch: sudo pacman -S $command_name"
|
||||
;;
|
||||
debian | ubuntu)
|
||||
echo " - Debian/Ubuntu: sudo apt install $command_name"
|
||||
;;
|
||||
*)
|
||||
echo " - On other Linux distros, refer to your package manager's documentation."
|
||||
;;
|
||||
esac
|
||||
else
|
||||
echo " - Linux: refer to your package manager's documentation for installation."
|
||||
fi
|
||||
;;
|
||||
Darwin*)
|
||||
echo " - macOS: brew install $command_name (Homebrew)"
|
||||
;;
|
||||
*)
|
||||
echo " - Unsupported OS. Please install $command_name manually."
|
||||
;;
|
||||
esac
|
||||
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Variables for README content
|
||||
DESCRIPTION="${DESCRIPTION:-This is a sample project.}"
|
||||
AUTHOR="${AUTHOR:-Nick Yeoman}"
|
||||
UPDATED=$(date +"%a %B %d, %Y %H:%M:%S %Z")
|
||||
|
||||
# Function to generate a README file
|
||||
generate_readme() {
|
||||
local readme_content=""
|
||||
|
||||
# Header
|
||||
readme_content+="# $BLDR_APP\n\n"
|
||||
readme_content+="Last Updated: $UPDATED\n\n"
|
||||
|
||||
# Description
|
||||
readme_content+="## Description\n"
|
||||
readme_content+="$DESCRIPTION\n\n"
|
||||
|
||||
# Usage
|
||||
readme_content+="## Usage\n"
|
||||
readme_content+="Provide examples of how to use the project:\n\n"
|
||||
readme_content+="\`\`\`bash\n"
|
||||
readme_content+="# Example command\n"
|
||||
readme_content+="\`\`\`\n\n"
|
||||
|
||||
# Environment Variables
|
||||
readme_content+="## Environment Variables\n"
|
||||
readme_content+="List of environment variables used in the project:\n\n"
|
||||
|
||||
# Extract environment variables from BLDR_NEW_SE
|
||||
while IFS= read -r line; do
|
||||
# Use regex to match and extract variable names and defaults
|
||||
if [[ $line =~ ^([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
|
||||
var_name="${BASH_REMATCH[1]}"
|
||||
var_value="${BASH_REMATCH[2]}"
|
||||
readme_content+="* \`$var_name\`: $var_value\n"
|
||||
fi
|
||||
done <<< "$BLDR_NEW_SE"
|
||||
|
||||
readme_content+="\n"
|
||||
|
||||
# Author
|
||||
readme_content+="## Author\n"
|
||||
readme_content+="Developed by $AUTHOR.\n"
|
||||
|
||||
# Output the README content to a file
|
||||
echo -e "$readme_content"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
|
||||
BLDR_NEW_SE+="# $BLDR_APP\n"
|
||||
|
||||
while IFS= read -r line; do
|
||||
# Use a regex to match and extract variable names and defaults
|
||||
if [[ $line =~ \$\{([A-Za-z_][A-Za-z0-9_]*)\} ]]; then
|
||||
# Matched ${VAR_NAME} (without default)
|
||||
var_name="${BASH_REMATCH[1]}"
|
||||
BLDR_NEW_SE+="${var_name}=\n"
|
||||
elif [[ $line =~ \$\{([A-Za-z_][A-Za-z0-9_]*)[:-](.*)\} ]]; then
|
||||
# Matched ${VAR_DEF:-default} (with default)
|
||||
var_name="${BASH_REMATCH[1]}"
|
||||
default_value="${BASH_REMATCH[2]:1}"
|
||||
BLDR_NEW_SE+="${var_name}=${default_value}\n"
|
||||
fi
|
||||
done <<< "$BLDR_NEW_DCF"
|
||||
|
||||
# Remove duplicates from the output
|
||||
BLDR_NEW_SE=$(echo -e "$BLDR_NEW_SE" | sort -u)
|
||||
|
||||
echo "***** NEW sample.env *****"
|
||||
echo "$BLDR_NEW_SE"
|
||||
Reference in New Issue
Block a user