Made Image Builder less UGLY
This commit is contained in:
parent
2543e38158
commit
61e01998f9
|
@ -4,8 +4,8 @@
|
|||
See here : [JOURNAL.md](JOURNAL.md)
|
||||
|
||||
## What this Project covers?
|
||||
- Making Custom LXQ Container
|
||||
- Managing a Proxmox VE Environement
|
||||
- Making Custom LXC Container for Proxmox
|
||||
- Managing Containers with Proxmox VE Environement
|
||||
- Demonstrating example services
|
||||
|
||||
## The Basics
|
||||
|
|
|
@ -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
|
||||
|
||||
## 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
|
||||
2. Download the CT templates you wish to base your images on
|
||||
3. Copy settings.conf to for example debian-nginx-settings.conf
|
||||
|
|
|
@ -37,7 +37,6 @@ while [[ "$#" -gt 0 ]]; do
|
|||
--hostname) HOSTNAME="$2"; shift ;;
|
||||
--export-name) EXPORT_NAME="$2"; shift ;;
|
||||
--export-path) EXPORT_PATH="$2"; shift ;;
|
||||
--script) SCRIPT="$2"; shift ;;
|
||||
--min-id) MIN_ID="$2"; shift ;;
|
||||
--storage) STORAGE="$2"; shift ;;
|
||||
--disk-size) DISK_SIZE="$2"; shift ;;
|
||||
|
@ -64,7 +63,6 @@ HOSTNAME=${HOSTNAME:-nginx-debian}
|
|||
EXPORT_NAME=${EXPORT_NAME:-mycontainer_backup}
|
||||
CURRENT_DIR=$(pwd)
|
||||
EXPORT_PATH=${EXPORT_PATH:-$CURRENT_DIR}
|
||||
SCRIPT=${SCRIPT:-$CURRENT_DIR/script.sh}
|
||||
MIN_ID=${MIN_ID:-200}
|
||||
STORAGE=${STORAGE:-local-btrfs}
|
||||
DISK_SIZE=${DISK_SIZE:-8}
|
||||
|
@ -72,21 +70,28 @@ MOUNT_POINT=${MOUNT_POINT:-}
|
|||
PRIVILEGED=${PRIVILEGED:-0}
|
||||
ROOTFS="${STORAGE}:${DISK_SIZE}"
|
||||
|
||||
# Check if the script file exists
|
||||
if [ ! -f "$SCRIPT" ]; then
|
||||
echo "Script file $SCRIPT does not exist."
|
||||
# Check if the script content is provided
|
||||
if [ -z "$SCRIPT_CONTENT" ]; then
|
||||
echo "Script content not provided in the parameter file."
|
||||
exit 1
|
||||
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
|
||||
if pct status $CTID &>/dev/null; then
|
||||
echo "VM $CTID already exists on this node."
|
||||
rm "$TEMP_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 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
|
||||
echo "Failed to create the container."
|
||||
rm "$TEMP_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
@ -94,6 +99,7 @@ fi
|
|||
if [ -n "$MOUNT_POINT" ]; then
|
||||
if ! pct set $CTID -mp0 $MOUNT_POINT,backup=1; then
|
||||
echo "Failed to set the mount point."
|
||||
rm "$TEMP_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
@ -101,19 +107,25 @@ fi
|
|||
# Start the container
|
||||
if ! pct start $CTID; then
|
||||
echo "Failed to start the container."
|
||||
rm "$TEMP_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run the shell script in the container
|
||||
if ! pct exec $CTID -- /bin/bash -c "$(cat $SCRIPT)"; then
|
||||
# Copy the temporary script file to the container
|
||||
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."
|
||||
pct stop $CTID
|
||||
rm "$TEMP_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Stop the container
|
||||
if ! pct stop $CTID; then
|
||||
echo "Failed to stop the container."
|
||||
rm "$TEMP_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
@ -121,7 +133,11 @@ fi
|
|||
BACKUP_FILE=$EXPORT_PATH/$EXPORT_NAME.tar.gz
|
||||
if ! vzdump $CTID --dumpdir $EXPORT_PATH --compress gzip; then
|
||||
echo "Failed to export the container."
|
||||
rm "$TEMP_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Cleanup
|
||||
rm "$TEMP_SCRIPT"
|
||||
|
||||
echo "Container $CTID has been created, configured, and exported to $BACKUP_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
|
|
@ -28,9 +28,6 @@ EXPORT_NAME=mycontainer_backup
|
|||
EXPORT_PATH=
|
||||
# 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
|
||||
# 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
|
||||
# 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
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue