Made Image Builder less UGLY

This commit is contained in:
crt 2024-06-29 11:28:51 +02:00
parent 2543e38158
commit 61e01998f9
5 changed files with 53 additions and 34 deletions

View File

@ -4,8 +4,8 @@
See here : [JOURNAL.md](JOURNAL.md) See here : [JOURNAL.md](JOURNAL.md)
## What this Project covers? ## What this Project covers?
- Making Custom LXQ Container - Making Custom LXC Container for Proxmox
- Managing a Proxmox VE Environement - Managing Containers with Proxmox VE Environement
- Demonstrating example services - Demonstrating example services
## The Basics ## The Basics

View File

@ -4,6 +4,8 @@
This is a little bash script that aids in making custom proxmox container images. Automating the process of making them instead of having to create them manually This is a little bash script that aids in making custom proxmox container images. Automating the process of making them instead of having to create them manually
## How do I use it? ## How do I use it?
Not quiet up to date, todo is update this:
1. Copy the image-builder directory to your proxmox servers root users home directory 1. Copy the image-builder directory to your proxmox servers root users home directory
2. Download the CT templates you wish to base your images on 2. Download the CT templates you wish to base your images on
3. Copy settings.conf to for example debian-nginx-settings.conf 3. Copy settings.conf to for example debian-nginx-settings.conf

View File

@ -37,7 +37,6 @@ while [[ "$#" -gt 0 ]]; do
--hostname) HOSTNAME="$2"; shift ;; --hostname) HOSTNAME="$2"; shift ;;
--export-name) EXPORT_NAME="$2"; shift ;; --export-name) EXPORT_NAME="$2"; shift ;;
--export-path) EXPORT_PATH="$2"; shift ;; --export-path) EXPORT_PATH="$2"; shift ;;
--script) SCRIPT="$2"; shift ;;
--min-id) MIN_ID="$2"; shift ;; --min-id) MIN_ID="$2"; shift ;;
--storage) STORAGE="$2"; shift ;; --storage) STORAGE="$2"; shift ;;
--disk-size) DISK_SIZE="$2"; shift ;; --disk-size) DISK_SIZE="$2"; shift ;;
@ -64,7 +63,6 @@ HOSTNAME=${HOSTNAME:-nginx-debian}
EXPORT_NAME=${EXPORT_NAME:-mycontainer_backup} EXPORT_NAME=${EXPORT_NAME:-mycontainer_backup}
CURRENT_DIR=$(pwd) CURRENT_DIR=$(pwd)
EXPORT_PATH=${EXPORT_PATH:-$CURRENT_DIR} EXPORT_PATH=${EXPORT_PATH:-$CURRENT_DIR}
SCRIPT=${SCRIPT:-$CURRENT_DIR/script.sh}
MIN_ID=${MIN_ID:-200} MIN_ID=${MIN_ID:-200}
STORAGE=${STORAGE:-local-btrfs} STORAGE=${STORAGE:-local-btrfs}
DISK_SIZE=${DISK_SIZE:-8} DISK_SIZE=${DISK_SIZE:-8}
@ -72,21 +70,28 @@ MOUNT_POINT=${MOUNT_POINT:-}
PRIVILEGED=${PRIVILEGED:-0} PRIVILEGED=${PRIVILEGED:-0}
ROOTFS="${STORAGE}:${DISK_SIZE}" ROOTFS="${STORAGE}:${DISK_SIZE}"
# Check if the script file exists # Check if the script content is provided
if [ ! -f "$SCRIPT" ]; then if [ -z "$SCRIPT_CONTENT" ]; then
echo "Script file $SCRIPT does not exist." echo "Script content not provided in the parameter file."
exit 1 exit 1
fi fi
# Write the script content to a temporary file
TEMP_SCRIPT=$(mktemp)
echo "$SCRIPT_CONTENT" > "$TEMP_SCRIPT"
chmod +x "$TEMP_SCRIPT"
# Check if the container already exists on the local node # Check if the container already exists on the local node
if pct status $CTID &>/dev/null; then if pct status $CTID &>/dev/null; then
echo "VM $CTID already exists on this node." echo "VM $CTID already exists on this node."
rm "$TEMP_SCRIPT"
exit 1 exit 1
fi fi
# Create the container with the correct storage format # Create the container with the correct storage format
if ! pct create $CTID $TEMPLATE --rootfs $ROOTFS --cores $CORES --memory $RAM --swap $SWAP --net0 name=eth0,bridge=$BRIDGE,ip=$IP --hostname $HOSTNAME --unprivileged $PRIVILEGED; then if ! pct create $CTID $TEMPLATE --rootfs $ROOTFS --cores $CORES --memory $RAM --swap $SWAP --net0 name=eth0,bridge=$BRIDGE,ip=$IP --hostname $HOSTNAME --unprivileged $PRIVILEGED; then
echo "Failed to create the container." echo "Failed to create the container."
rm "$TEMP_SCRIPT"
exit 1 exit 1
fi fi
@ -94,6 +99,7 @@ fi
if [ -n "$MOUNT_POINT" ]; then if [ -n "$MOUNT_POINT" ]; then
if ! pct set $CTID -mp0 $MOUNT_POINT,backup=1; then if ! pct set $CTID -mp0 $MOUNT_POINT,backup=1; then
echo "Failed to set the mount point." echo "Failed to set the mount point."
rm "$TEMP_SCRIPT"
exit 1 exit 1
fi fi
fi fi
@ -101,19 +107,25 @@ fi
# Start the container # Start the container
if ! pct start $CTID; then if ! pct start $CTID; then
echo "Failed to start the container." echo "Failed to start the container."
rm "$TEMP_SCRIPT"
exit 1 exit 1
fi fi
# Run the shell script in the container # Copy the temporary script file to the container
if ! pct exec $CTID -- /bin/bash -c "$(cat $SCRIPT)"; then pct push $CTID $TEMP_SCRIPT /root/script.sh -perms 755
# Run the script inside the container
if ! pct exec $CTID -- /root/script.sh; then
echo "Failed to run the script inside the container." echo "Failed to run the script inside the container."
pct stop $CTID pct stop $CTID
rm "$TEMP_SCRIPT"
exit 1 exit 1
fi fi
# Stop the container # Stop the container
if ! pct stop $CTID; then if ! pct stop $CTID; then
echo "Failed to stop the container." echo "Failed to stop the container."
rm "$TEMP_SCRIPT"
exit 1 exit 1
fi fi
@ -121,7 +133,11 @@ fi
BACKUP_FILE=$EXPORT_PATH/$EXPORT_NAME.tar.gz BACKUP_FILE=$EXPORT_PATH/$EXPORT_NAME.tar.gz
if ! vzdump $CTID --dumpdir $EXPORT_PATH --compress gzip; then if ! vzdump $CTID --dumpdir $EXPORT_PATH --compress gzip; then
echo "Failed to export the container." echo "Failed to export the container."
rm "$TEMP_SCRIPT"
exit 1 exit 1
fi fi
# Cleanup
rm "$TEMP_SCRIPT"
echo "Container $CTID has been created, configured, and exported to $BACKUP_FILE" echo "Container $CTID has been created, configured, and exported to $BACKUP_FILE"

View File

@ -1,22 +0,0 @@
#!/bin/sh
# Ensure the locale environment variables are set correctly
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# Install locales and configure locale settings
apt-get update
apt-get install -y locales
locale-gen en_US.UTF-8
# Update and install packages for Debian
apt-get update
apt-get upgrade -y
apt-get install -y nginx
# Enable nginx service
systemctl enable nginx
# Clean up
apt-get clean

View File

@ -28,9 +28,6 @@ EXPORT_NAME=mycontainer_backup
EXPORT_PATH= EXPORT_PATH=
# Leave this empty to use the current directory # Leave this empty to use the current directory
SCRIPT=install_nginx.sh
# Leave this empty to use script.sh in the current directory
MIN_ID=200 MIN_ID=200
# Define minimum ID for CT to avoid conflicts with other nodes if in a cluster # Define minimum ID for CT to avoid conflicts with other nodes if in a cluster
@ -45,3 +42,29 @@ MOUNT_POINT=local-lvm:8,mp=/data,size=8G
PRIVILEGED=0 PRIVILEGED=0
# Set to 1 for privileged container, 0 for unprivileged # Set to 1 for privileged container, 0 for unprivileged
SCRIPT_CONTENT=$(cat <<'EOF'
#!/bin/bash
# Update package list and install nginx
apt-get update
apt-get install -y nginx
# Start nginx service
systemctl start nginx
systemctl enable nginx
# Example of cat with EOF inside the script
cat << 'EOT' > /etc/nginx/conf.d/example.conf
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8080;
}
}
EOT
echo "Nginx has been installed and configured."
EOF
)