docker extend build bash script working

This commit is contained in:
2024-07-21 17:52:33 -07:00
parent 0c4bb4a289
commit 2bd83096a8
33 changed files with 547 additions and 189 deletions
+7
View File
@@ -0,0 +1,7 @@
#!/bin/bash
debug_print_helper_variables() {
for var in $(compgen -v | grep '^HLPR_'); do
echo "$var=${!var}"
done
}
+34
View File
@@ -0,0 +1,34 @@
#!/bin/bash
# Function to create directories based on volumes in docker-compose files
create_dirs() {
local project_path="$1"
local selected_dirs="$2"
local cookbook_dir="$3"
local data_dir="$project_path/data"
# Loop through selected directories
for dir in $selected_dirs; do
local compose_file="$cookbook_dir/$dir/docker-compose.yml"
# Check if the docker-compose file exists
if [ -f "$compose_file" ]; then
# Loop through each line in the file
while IFS= read -r line; do
trimmed_line=$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
# Check if the line starts with a dash
if [[ $trimmed_line =~ ^- ]]; then
if [[ $trimmed_line =~ VOL_PATH ]]; then
extracted=$(echo "$trimmed_line" | sed -n 's/.*\${VOL_PATH:-[^}]*}\([^:]*\):.*/\1/p' | sed 's/^\/\+//')
# Echo the line if it starts with a dash
mkdir -p $project_path/data/${extracted}
fi
fi
done < "$compose_file"
else
echo "Warning: No docker-compose.yml found in $dir"
fi
done
}
+30
View File
@@ -0,0 +1,30 @@
#!/bin/bash
# Function to create a new docker-compose file using extends
create_dockercompose() {
local project_path="$1"
local selected_dirs="$2"
local script_dir="$3" # Directory where helper.bash is located
# Initialize the new docker-compose file
local new_compose_file="$project_path/docker-compose.yml"
# Write the initial part of the docker-compose file
echo "version: '3.4'" > "$new_compose_file"
echo "" >> "$new_compose_file"
echo "services:" >> "$new_compose_file"
# Loop through selected directories
for dir in $selected_dirs; do
local sample_file="$script_dir/$dir/sample-extends.yml"
if [ -f "$sample_file" ]; then
# Extract services section from the sample file and append it
awk '/^services:/{flag=1; next}/^$/{flag=0}flag' "$sample_file" >> "$new_compose_file"
echo "" >> "$new_compose_file" # Add a newline after each file's content
else
echo "Warning: Create an issue: No sample-extends.yml found in $dir"
cp $script_dir/$dir/docker-compose.yml $new_compose_file
fi
done
}
+102
View File
@@ -0,0 +1,102 @@
#!/bin/bash
process_help_comments() {
local input_file="$1"
local output_file="$2"
local project_path="$3"
local cookbook_dir="$4"
local temp_file=$(mktemp)
while IFS= read -r line; do
# Handle headings (keep headings and process below lines)
if [[ "$line" =~ ^# ]]; then
echo "$line" >> "$temp_file"
elif [[ "$line" =~ ^([^=]+)= ]]; then
local var_name="${BASH_REMATCH[1]}"
local var_value="${line#*=}"
var_value=$(echo "$var_value" | sed 's/\s*#HELP:.*$//')
# Process #HELP comments
if [[ "$line" =~ \#HELP:\ project_path ]]; then
var_value="$HLPR_PROJECTPATH"
elif [[ "$line" =~ \#HELP:\ volpath ]]; then
var_value="$HLPR_PROJECTPATH/data"
elif [[ "$line" =~ \#HELP:\ cookbooks ]]; then
var_value="$HLPR_COOKBOOKDIR"
elif [[ "$line" =~ \#HELP:\ gen32 ]]; then
var_value=$(pwgen -cnsB1v 32)
fi
# Add the processed variable to the temp file
echo "${var_name}=${var_value}" >> "$temp_file"
else
# If the line doesn't match a variable, just echo it
echo "$line" >> "$temp_file"
fi
done < "$input_file"
# Call remove_duplicate_variables to process temp_file and write to output_file
cat "$temp_file" >> $output_file
rm "$temp_file"
}
remove_duplicate_variables() {
local input_file="$1"
local temp_file=$(mktemp)
declare -A seen_keys
while IFS= read -r line; do
# Skip lines that start with # or are blank
if [[ ! $line =~ ^# ]] && [[ -n $line ]]; then
# Extract the part before the equals sign
key=$(echo "$line" | awk -F '=' '{print $1}')
# Check if the key has already been seen
if [[ -z "${seen_keys[$key]}" ]]; then
# Mark the key as seen
seen_keys[$key]=1
# Output the key
echo "$line" >> "$temp_file"
fi
else
echo "$line" >> "$temp_file"
fi
done < "$input_file"
# Replace the original file with the deduplicated version
mv "$temp_file" "$input_file"
}
# Function to create .env file
create_env() {
local project_path="$1"
local selected_dirs="$2"
local cookbook_dir="$3"
local new_env_file="$project_path/.env"
local current_date=$(date)
local git_user=$(git config --get user.name)
if [[ -z "$git_user" ]]; then
local git_user=$USER
fi
# Create the file
touch $new_env_file
echo "# Auto Generated env file on $current_date by $git_user" >> "$new_env_file"
# Loop through selected directories and process sample.env files
for dir in $selected_dirs; do
local sample_file="$cookbook_dir/$dir/sample.env"
if [ -f "$sample_file" ]; then
process_help_comments "$sample_file" "$new_env_file" "$project_path" "$cookbook_dir"
else
echo "Warning: Create an issue: No sample.env found in $dir" >&2
echo "#No Sample File found, raise a bug" >> "$new_env_file"
fi
done
remove_duplicate_variables "$new_env_file"
}
+9
View File
@@ -0,0 +1,9 @@
#!/bin/bash
function display_footer() {
echo "======================================"
echo "All Done"
echo "Created Project: $HLPR_PROJECTNAME in $HLPR_PROJECTDIR"
echo "With the following containers:"
echo $HLPR_CONTAINERS
}
+22
View File
@@ -0,0 +1,22 @@
#!/bin/bash
# Function to create a .gitignore file with standard ignores
create_gitignore() {
local project_path="$1"
local gitignore_path="$project_path/.gitignore"
# Create the .gitignore file
cat <<EOL > "$gitignore_path"
data/
logs/
*.log
.vscode/
.idea/
*.suo
*.ntvs*
*.njsproj
*.sln
.DS_Store
Thumbs.db
EOL
}
+7
View File
@@ -0,0 +1,7 @@
#!/bin/bash
function display_header() {
echo "======================================"
echo " Docker Compose Project Setup Wizard"
echo "======================================"
}
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
# Function to check if fzf is installed and provide installation instructions if not
check_fzf() {
if ! command -v fzf &> /dev/null; then
echo "fzf is not installed."
echo "To install fzf:"
echo " - Fedora: sudo dnf install fzf"
echo " - Arch: sudo pacman -S fzf"
echo " - Debian: sudo apt install fzf"
exit 1
fi
}
# Function to check if pwgen is installed and provide installation instructions if not
check_pwgen() {
if ! command -v pwgen &> /dev/null; then
echo "pwgen is not installed."
echo "To install pwgen:"
echo " - Fedora: sudo dnf install pwgen"
echo " - Arch: sudo pacman -S pwgen"
echo " - Debian: sudo apt install pwgen"
exit 1
fi
}
+43
View File
@@ -0,0 +1,43 @@
#!/bin/bash
# Function to check if the project directory already exists
check_project() {
local path="$1"
if [ -d "$path" ]; then
echo "Error: Directory '$path' already exists."
return 1
fi
return 0
}
# Function to create the project directory
create_project() {
local path="$1"
mkdir -p "$path"
if [ $? -ne 0 ]; then
echo "Error: Failed to create directory '$path'. Exiting."
exit 1
fi
}
# Function to handle the project directory creation process
handle_project_creation() {
local project_path="$1"
local project_name="$2"
while true; do
check_project "$HLPR_PROJECTDIR"
if [ $? -eq 0 ]; then
create_project "$HLPR_PROJECTDIR"
break
else
read -p "Please enter a new directory path (must not exist): " new_project_path
if [ -z "$new_project_path" ]; then
echo "Error: Path cannot be empty. Exiting."
exit 1
fi
project_path="$new_project_path"
HLPR_PROJECTDIR="$project_path/$project_name"
fi
done
}
+13
View File
@@ -0,0 +1,13 @@
#!/bin/bash
# Function to get user input for project name
get_project_name() {
while true; do
read -p "Enter project name (usually the domain name, like www-example-com): " HLPR_PROJECTNAME
if [[ "$HLPR_PROJECTNAME" =~ ^[a-zA-Z0-9-]+$ ]]; then
break
else
echo "Invalid project name. Please use only letters, numbers, and dashes. No spaces allowed."
fi
done
}
+19
View File
@@ -0,0 +1,19 @@
#!/bin/bash
# Function to display directories using fzf with enhanced parameters
select_directory_with_fzf() {
# Find all directories under the user's home directory, excluding dot directories and suppressing permission errors
local fzf_input=$(find "$HOME" -type d -not -path '*/\.*' 2>/dev/null | while read -r dir; do [ -r "$dir" ] && echo "$dir"; done)
# Use fzf with enhanced parameters for better usability
selected_path=$(echo "$fzf_input" | fzf --height 40% --layout=reverse --border --preview 'echo {} | xargs -I % du -sh % 2>/dev/null' --preview-window=up:30% --prompt="Select a directory: ")
# Handle the case where no directory was selected
if [ -z "$selected_path" ]; then
echo "No directory selected. Exiting."
exit 1
fi
# Output the selected path
HLPR_PROJECTPATH="$selected_path"
}
+50
View File
@@ -0,0 +1,50 @@
#!/bin/bash
# Function to create a README.md file
create_readme() {
local project_path="$1"
local project_name="$2"
local selected_dirs="$3"
local readme_path="$project_path/README.md"
local current_date=$(date +"%Y-%m-%d")
# Retrieve the full name from the global Git config
local git_name=$(git config --global user.name)
if [ -z "$git_name" ]; then
echo 'WARNING: Set your git username: git config --global user.name "Your Name"'
git_name="$USER"
fi
# Create the README.md file
cat <<EOL > "$readme_path"
# $project_name
Project created on $current_date by $git_name
## Description
A brief description of your project goes here.
## Project Structure
This project includes the following containers:
EOL
# Add the selected directories to the README
if [ -n "$selected_dirs" ]; then
echo "$selected_dirs" | while read -r dir; do
echo "- $(basename "$dir")" >> "$readme_path"
done
else
echo "No containers selected." >> "$readme_path"
fi
cat <<EOL >> "$readme_path"
## Usage
podman-compose up -d
EOL
}
+21
View File
@@ -0,0 +1,21 @@
#!/bin/bash
# Function to list all directories in the same directory as the helper script and allow the user to select multiple directories
select_directories() {
# Get the directory where this script is located
local script_dir=$(dirname "$(realpath "$0")")
# Find all directories in the same directory as the helper script and get their basenames, excluding those starting with an underscore
local dir_list=$(find "$script_dir" -maxdepth 1 -type d -not -path '*/\.*' -not -path "$script_dir" -not -name '_*' | xargs -I {} basename {})
# Use fzf to allow the user to select multiple directories with checkboxes
local selected_dirs=$(echo "$dir_list" | fzf --multi --height 40% --layout=reverse --border --prompt="Select directories (TAB to toggle, ENTER to confirm): " --bind 'tab:toggle+down')
# Handle the case where no directory was selected
if [ -z "$selected_dirs" ]; then
echo "No directories selected. Exiting."
exit 1
fi
HLPR_CONTAINERS="$selected_dirs"
}