Browse Source

bin folder

main
root 1 week ago
parent
commit
e560154ef8
  1. 1
      .gitignore
  2. 87
      bin/pct-create
  3. 37
      bin/pct-destroy
  4. 27
      bin/pct-login
  5. 30
      bin/pct-lookup
  6. 3
      bin/pct-nextid
  7. 9
      install.sh

1
.gitignore

@ -0,0 +1 @@
config

87
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") <CONTAINER_NAME>"
}
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

37
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") <CONTAINER_NAME> --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

27
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") <CONTAINER_NAME> [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

30
bin/pct-lookup

@ -0,0 +1,30 @@
#!/usr/bin/env bash
# CONSTANTS
CONTAINER_NAME=$1
# FUNCTIONS
function usage {
echo "$(basename "$0") <CONTAINER_NAME>|--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

3
bin/pct-nextid

@ -0,0 +1,3 @@
#!/bin/bash
pvesh get /cluster/nextid

9
install.sh

@ -0,0 +1,9 @@
#!/usr/bin/env bash
# CONSTANTS
# FUNCTIONS
# MAIN
Loading…
Cancel
Save