Update 2025-04-13_16:21:50
This commit is contained in:
59
lxc-ai-package.sh
Executable file
59
lxc-ai-package.sh
Executable file
@ -0,0 +1,59 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Check if a package name is provided
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: $0 <package_name>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PACKAGE_NAME="$1"
|
||||
|
||||
# Function to log messages with timestamp
|
||||
log_message() {
|
||||
echo "$(date +"%Y-%m-%d %H:%M:%S") - $1"
|
||||
}
|
||||
|
||||
# Function to check if a container is running
|
||||
is_container_running() {
|
||||
local container=$1
|
||||
lxc-info -n "$container" | grep -q 'RUNNING'
|
||||
}
|
||||
|
||||
# List all running Linux containers
|
||||
containers=$(lxc-ls -f G RUNNING | awk 'NR>1 {print $1}')
|
||||
|
||||
# Install the package inside each container
|
||||
install_package_in_container() {
|
||||
local container=$1
|
||||
local package=$2
|
||||
|
||||
if is_container_running "$container"; then
|
||||
log_message "Installing $package in container: $container"
|
||||
|
||||
lxc-attach -n "$container" -- apt update > /tmp/${container}_apt_update.log 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
log_message "Failed to update APT in container: $container. Check /tmp/${container}_apt_update.log for details."
|
||||
return 1
|
||||
fi
|
||||
|
||||
lxc-attach -n "$container" -- apt install -y "$package" > /tmp/${container}_apt_install_${package}.log 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
log_message "Failed to install $package in container: $container. Check /tmp/${container}_apt_install_${package}.log for details."
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_message "Successfully installed $package in container: $container"
|
||||
else
|
||||
log_message "Container $container is not running. Skipping."
|
||||
fi
|
||||
}
|
||||
|
||||
export -f log_message
|
||||
export -f is_container_running
|
||||
export -f install_package_in_container
|
||||
|
||||
# Process each container in parallel
|
||||
echo "$containers" | xargs -I{} -n1 -P4 bash -c 'install_package_in_container "{}" "$PACKAGE_NAME"' _ "$PACKAGE_NAME"
|
||||
|
||||
log_message "All containers have been processed."
|
||||
|
Reference in New Issue
Block a user