added builder script

This commit is contained in:
2024-07-25 12:20:36 -07:00
parent 6bf73092df
commit da629c05d8
24 changed files with 506 additions and 54 deletions
+29
View File
@@ -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
}
+15
View File
@@ -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"
}
+22
View File
@@ -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"
+44
View File
@@ -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
}
+8
View File
@@ -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
}
+19
View File
@@ -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"
+22
View File
@@ -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}
}
+19
View File
@@ -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")
+32
View 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"
}
+43
View File
@@ -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
}
+50
View File
@@ -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"
}
+23
View File
@@ -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"
+4
View File
@@ -0,0 +1,4 @@
# Bookstack
Dockerhub: https://hub.docker.com/r/solidnerd/bookstack
Reverse Proxy Port: 8080
+7 -43
View File
@@ -1,56 +1,20 @@
################################################################################
# Bookstack
#
# Sample .env
#
# DB_USER=bookstack
# DB_PASSWORD=ChangeThisPassword
# APP_URL=http://localhost:8248
# APP_KEY=32CharacterKey123456789012345678
# NETWORKNAME=admin_web
################################################################################
version: '3.8'
networks:
default:
external:
name: ${NETWORKNAME:-default}
services:
bookstack-db:
image: mariadb:latest # https://hub.docker.com/_/mariadb
restart: always
volumes:
- ./data/db:/var/lib/mysql
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
command: --max_allowed_packet=32505856
environment:
MYSQL_ROOT_PASSWORD: ChangeThisToRandom
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_USER}
bookstack:
image: solidnerd/bookstack:latest # https://hub.docker.com/r/solidnerd/bookstack
depends_on:
- bookstack-db
image: ${BOOKSTACK_IMAGE:-solidnerd/bookstack:latest}
environment:
- DB_HOST=bookstack-db:3306
- DB_DATABASE=${DB_USER}
- DB_USERNAME=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_DATABASE=dbuser
- DB_USERNAME=dbuser
- DB_PASSWORD=dbpass
- APP_URL=${APP_URL:-http://localhost:8248}
- REVISION_LIMIT=false
- APP_KEY=${APP_KEY}
- APP_KEY=APP_KEY
- APP_TIMEZONE=America/Vancouver
volumes:
- "/etc/timezone:/etc/timezone:ro"
- ./data/uploads:/var/www/bookstack/public/uploads
- ./data/storage:/var/www/bookstack/storage/uploads
- ./php.ini:/usr/local/etc/php/php.ini
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
ports:
- "8248:8080"
- ./config/php.ini:/usr/local/etc/php/php.ini
+12
View File
@@ -0,0 +1,12 @@
version: '3.4'
services:
bookstack:
extends:
file: ${COOKBOOK}/bookstack/docker-compose.yml
service: bookstack
bookstack-mariadb:
extends:
file: ${COOKBOOK}/mariadb/docker-compose.yml
service: mariadb
+7
View File
@@ -0,0 +1,7 @@
# Bookstack App
PRODDIR=/var/www/stashdomain-com/ #HELP: project_path
COOKBOOK=/home/user/git/docker-compose-cookbooks #HELP: cookbooks
VOL_PATH=/project-dir/project-name/data #HELP: volpath
# Bookstack Container
BOOKSTACK_IMAGE=solidnerd/bookstack:latest
APP_KEY=012345679801234567980123645678921 #HELP: gen32
+89
View File
@@ -0,0 +1,89 @@
#!/bin/bash
BLDR_DIR=""
BLDR_APP=""
BLDR_PARENT_PATH=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
BLDR_OLD_DCF="" # Content of Old Docker Compose File
BLDR_NEW_DCF="" # Content of New Docker Compose File
BLDR_NEW_SE="" # sample-extends
BLDR_NEW_README=""
BLDR_DC_VOLS=""
BLDR_DC_IMAGE=""
BLDR_DC_ENV=""
BLDR_ENV_FILE=""
source ./_builder/debug.sh
#debug_print_helper_variables
################################################
# Pre Check
################################################
source ./_builder/precheck.sh
check_command "fzf"
check_command "yq"
################################################
# Get Directory we are working with
################################################
source ./_builder/getdir.bash
get_directory
################################################
# Save file to work with
################################################
source ./_builder/olddcf.bash
################################################
# Take out the stuff we need
################################################
source ./_builder/parse.bash
BLDR_DC_VOLS=$(get_vols "$BLDR_OLD_DCF")
BLDR_DC_ENV=$(get_env_vars "$BLDR_OLD_DCF")
BLDR_DC_IMAGE=$(get_image "$BLDR_OLD_DCF")
unset BLDR_OLD_DCF # Done, got the stuff we need
################################################
# Clean up the IMAGE
################################################
source ./_builder/dc-image.sh
BLDR_DC_IMAGE=$(extract_image_name "$BLDR_DC_IMAGE")
################################################
# Clean up VOLS
################################################
source ./_builder/dc-vols.sh
BLDR_DC_VOLS=$(process_volumes "$BLDR_DC_VOLS")
################################################
# Clean up ENV
################################################
source ./_builder/dc-env.sh
BLDR_DC_ENV=$(process_env_vars "$BLDR_DC_ENV" "$BLDR_APP")
################################################
# Compile New Docker Var BLDR_NEW_DCF
################################################
source ./_builder/dc-new.sh
################################################
# Create sample-env
################################################
source ./_builder/se-new.sh
################################################
# Create sample-extends
################################################
source ./_builder/extends-new.sh
################################################
# Create Readme.md
################################################
source ./_builder/readme-new.sh
BLDR_NEW_README=$(generate_readme)
echo "***** README *****"
echo "$BLDR_NEW_README"
# TODO: for readme file do mkdir -p data/vols for vols
# Do a git diff on the directory
# fuzzy find latest image using skopeo
# skopeo list-tags docker://docker.io/solidnerd/book
+27
View File
@@ -0,0 +1,27 @@
# Mariadb
Dockerhub: https://hub.docker.com/_/mariadb
It will be quite uncommon to just use this compose file, it's likely you are extending this from another project.
That's the main context of this container.
## Sample
### Run multiple databases
initdb/init.sql
```sql
-- Create BookStack database and user
CREATE DATABASE IF NOT EXISTS bookstack_db;
CREATE USER IF NOT EXISTS 'bookstack_user'@'%' IDENTIFIED BY 'bookstack_password';
GRANT ALL PRIVILEGES ON bookstack_db.* TO 'bookstack_user'@'%';
-- Create WordPress database and user
CREATE DATABASE IF NOT EXISTS wordpress_db;
CREATE USER IF NOT EXISTS 'wordpress_user'@'%' IDENTIFIED BY 'wordpress_password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'%';
FLUSH PRIVILEGES;
```
+17
View File
@@ -0,0 +1,17 @@
version: '3.8' # or any version you prefer
services:
mariadb:
image: ${MARIADB_IMAGE:-mariadb:latest}
environment:
MARIADB_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD:-ChangeThisPassword}
MARIADB_DATABASE: ${MARIADB_DATABASE:-mydb}
MARIADB_USER: ${MARIADB_USER:-dbuser}
MARIADB_PASSWORD: ${MARIADB_PASSWORD:-AlsoChangeThisPassword}
volumes:
- ${VOL_PATH:-./data/}mariadb_data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- mariadb_network
+7
View File
@@ -0,0 +1,7 @@
version: '3.4'
services:
mariadb:
extends:
file: ${COOKBOOK}/mariadb/docker-compose.yml
service: mariadb
+9
View File
@@ -0,0 +1,9 @@
# Mariadb
PRODDIR=/var/www/stashdomain-com/ #HELP: project_path
COOKBOOK=/home/user/git/docker-compose-cookbooks #HELP: cookbooks
VOL_PATH=/project-dir/project-name/data #HELP: volpath
MARIADB_IMAGE=mariadb:latest
MARIADB_ROOT_PASSWORD=ChangeThisPassword #HELP: gen32
MARIADB_DATABASE=mydb
MARIADB_USER=dbuser
MARIADB_PASSWORD=AlsoChangeThisPassword #HELP: gen32
-5
View File
@@ -7,11 +7,6 @@
################################################################################
version: '3.8'
networks:
default:
external:
name: ${NETWORKNAME:-default}
services:
paisa:
image: ananthakumaran/paisa:latest
+1 -5
View File
@@ -3,11 +3,6 @@ version: '3.4'
services:
stash:
image: ${STASH_IMAGE:-stashapp/stash:latest}
logging:
driver: "json-file"
options:
max-file: "100"
max-size: ${MAX_SIZE:-200m}
restart: unless-stopped
environment:
- STASH_STASH=${STASH_STASH:-/data/}
@@ -21,5 +16,6 @@ services:
- "${VOL_PATH:-./data}/stash-cache:/cache"
- "${VOL_PATH:-./data}/stash-generated:/generated"
- "${VOL_PATH:-./data}/stash-root:/root"
# Uncomment and use if needed
# network_mode: "host"
-1
View File
@@ -6,7 +6,6 @@ VOL_PATH=/project-dir/project-name/data #HELP: volpath
STASH_IMAGE=stashapp/stash:v0.26.2
# Stash Container
STASH_DOMAIN_NAME=stash.4lt.ca
MAX_SIZE=200m
STASH_STASH=/data/
STASH_GENERATED=/generated/
STASH_METADATA=/metadata/