diff --git a/functions/containers/run_pods.sh b/functions/containers/run_pods.sh new file mode 100644 index 0000000..4adcbd4 --- /dev/null +++ b/functions/containers/run_pods.sh @@ -0,0 +1,132 @@ +if [[ -n "$ZSH_VERSION" ]]; then + source "$(dirname "$0")/../shared.sh" +else + source "$(dirname "${BASH_SOURCE[0]}")/../shared.sh" +fi +prevent_to_execute_directly + +kube_run_pod_python() { + local python_version="3.11" + local pod_name="python-${python_version//./}-pod" + local run_as_root=false + local force=false + local user_id=1000 + local group_id=1000 + local username="pyuser" + + while [[ "$#" -gt 0 ]]; do + case $1 in + --root) + run_as_root=true + shift + ;; + -f | --force) + force=true + shift + ;; + -u | --user) + user_id="$2" + shift 2 + ;; + -g | --group) + group_id="$2" + shift 2 + ;; + -v | --version) + python_version="$2" + pod_name="python-${python_version//./}-pod" + shift 2 + ;; + -h | --help) + echo "Usage: kube_run_pod_python [--root] [-f|--force] [-u|--user ] [-g|--group ] [-v|--version ]" + echo " --root Run as root user (default is non-root)" + echo " -f, --force Force creation of new pod if it already exists" + echo " -u, --user Specify user ID (default: 1000)" + echo " -g, --group Specify group ID (default: 1000)" + echo " -v, --version Specify Python version (default: 3.11)" + return 0 + ;; + *) + echo "Unknown parameter passed: $1" + return 1 + ;; + esac + done + + if kubectl get pod "$pod_name" &>/dev/null; then + if [ "$force" = true ]; then + echo "Pod $pod_name already exists. Deleting it..." + kubectl delete pod "$pod_name" + sleep 5 + else + echo "Pod $pod_name already exists. Use -f or --force to replace it." + return 1 + fi + fi + + echo "Creating new Python $python_version environment pod..." + + local yaml_template + yaml_template=$( + cat <> /home/$username/.bashrc + echo "PS1='\u@\h:\w\$ '" >> /home/$username/.bashrc + mkdir -p /home/$username/.cache/pip + chown -R $user_id:$group_id /home/$username + if [ $user_id -ne 0 ]; then + su - $username -c "pip install --user --upgrade pip" + else + pip install --upgrade pip + fi + echo "Environment setup complete. Running sleep loop..." + while true; do sleep 30; done + env: + - name: HOME + value: /home/$username + - name: PATH + value: /home/$username/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin +EOF + ) + + if [ "$run_as_root" = false ]; then + yaml_template+=$( + cat <