diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..04204c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config diff --git a/bin/pct-create b/bin/pct-create new file mode 100755 index 0000000..15e3aa3 --- /dev/null +++ b/bin/pct-create @@ -0,0 +1,87 @@ +#!/usr/bin/env bash + +# CONSTANTS + +BASEDIR=$(dirname "$0") +CONTAINER_NAME=$1 +TEMPLATE_DIR=/var/lib/vz/template/cache +TAG_NAME=debian13 +SSH_PUBKEYS="$BASEDIR/pct-administrators.pubkeys" + +# FUNCTIONS + +function usage { + echo "$(basename "$0") " +} + +function debian13_template { + ls -1 $TEMPLATE_DIR | grep ^debian-13 +} + +function show_defaults { + echo -n "STORAGE_DISK=$STORAGE_DISK," + echo -n "STORAGE_SIZE=$STORAGE_SIZE," + echo -n "MEMORY=$MEMORY," + echo -n "SWAP=$SWAP," + echo -n "CPU=$CPU," + echo +} + +function convert_human_size_to_megabyte { + value=$1 + echo "$(numfmt --from=iec-i "$value"i --to-unit=Mi)" | cut -dM -f1 +} + +function convert_human_size_to_gigabyte { + value=$1 + echo "$(numfmt --from=iec-i "$value"i --to-unit=Gi)" | cut -dG -f1 +} + +function create_container { + local new_id=$($BASEDIR/pct-nextid) + local template_file="$TEMPLATE_DIR/$(debian13_template)" + local vol_in_gb=$(convert_human_size_to_gigabyte $STORAGE_SIZE) + local mem_in_mb=$(convert_human_size_to_megabyte $MEMORY) + local swp_in_mb=$(convert_human_size_to_megabyte $SWAP) + + pct create \ + $new_id \ + $template_file \ + --ostype debian \ + --rootfs "volume=$STORAGE_DISK:$vol_in_gb" \ + --cores $CPU \ + --memory $mem_in_mb \ + --swap $swp_in_mb \ + --hostname $CONTAINER_NAME \ + --net0 name=eth0,bridge=vmbr0,ip=dhcp,firewall=1 \ + --start \ + --onboot true \ + --ssh-public-keys $SSH_PUBKEYS \ + --unprivileged true \ + --features nesting=1 \ + + [[ -n $TAG_NAME ]] && pct set $new_id --tags $TAG_NAME + echo "container: $CONTAINER_NAME succesfully created with vmid=$new_id!" +} + +function existing_container { + local vmid=$($BASEDIR/pct-lookup "$CONTAINER_NAME") + if [[ -z $vmid ]]; then + false + else + echo "container $CONTAINER_NAME already exists with vmid=$vmid!" + exit 2 + fi +} + +# MAIN + +set -Eue +[[ "$#" -lt 1 ]] && usage && exit 1 +source "$BASEDIR/pct-default.config" + +# show_defaults +existing_container && false || create_container + + + diff --git a/bin/pct-destroy b/bin/pct-destroy new file mode 100755 index 0000000..593aeb7 --- /dev/null +++ b/bin/pct-destroy @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +# CONSTANTS + +BASEDIR=$(dirname "$0") +CONTAINER_NAME=$1 +FORCE=$2 + +# FUNCTIONS + +function usage { + echo "$(basename "$0") --force" +} + +function set_force { + if [[ $FORCE == '--force' ]]; then + force=true + else + force=false + fi +} + +function destroy { + if vmid=$($BASEDIR/pct-lookup "$CONTAINER_NAME"); then + [[ $FORCE ]] && pct stop $vmid + pct destroy $vmid + else + exit 1 + fi +} + +# MAIN + +set -Eue +[[ "$#" -lt 1 ]] && usage && exit 1 +set_force +destroy diff --git a/bin/pct-login b/bin/pct-login new file mode 100755 index 0000000..1a13be1 --- /dev/null +++ b/bin/pct-login @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# CONSTANTS + +BASEDIR=$(dirname "$0") +CONTAINER_NAME=$1 +USER=${2:-root} + +# FUNCTIONS + +function usage { + echo "$(basename "$0") [USER=root]" +} + +function login { + if vmid=$($BASEDIR/pct-lookup $CONTAINER_NAME); then + pct exec $vmid -- su - $USER + else + exit 10 + fi +} + +# MAIN + +set -Eue +[[ "$#" -lt 1 ]] && usage && exit 1 +login diff --git a/bin/pct-lookup b/bin/pct-lookup new file mode 100755 index 0000000..a9aff4c --- /dev/null +++ b/bin/pct-lookup @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# CONSTANTS + +CONTAINER_NAME=$1 + +# FUNCTIONS + +function usage { + echo "$(basename "$0") |--all" +} + +function lookup { + cmd="pvesh get /cluster/resources --type vm --output=json | jq --raw-output '.[] | \"\(.name) \(.vmid)\"'" + if [[ $CONTAINER_NAME == "--all" ]]; then + eval "$cmd" + else + cmd="$cmd | egrep '^$CONTAINER_NAME '" + found=$(eval "$cmd") || ( >&2 echo "ERROR: container <$CONTAINER_NAME> not found!" && exit 10 ) + echo "$found" | cut -d' ' -f2 + fi +} + + +# MAIN + +set -Eue +[[ "$#" -lt 1 ]] && usage && exit 1 +lookup + diff --git a/bin/pct-nextid b/bin/pct-nextid new file mode 100755 index 0000000..7c3c91d --- /dev/null +++ b/bin/pct-nextid @@ -0,0 +1,3 @@ +#!/bin/bash + +pvesh get /cluster/nextid diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..fcd144b --- /dev/null +++ b/install.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +# CONSTANTS + + +# FUNCTIONS + +# MAIN +