You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.1 KiB

#!/bin/bash -i
# CONSTANTS
CONFIG_FILE=/opt/miaou-proxmox/config/default.conf
# FUNCTIONS
function halt_on_error {
>&2 echo "ERROR: $1"
exit ${2:-1}
}
function assert_root {
[ "$(id -u)" -ne 0 ] && halt_on_error 'root privilege required!' 1
}
function assert_proxmox {
grep -q ^ID=debian /etc/os-release || halt_on_error 'distro debian required!' 2
command -v pct >/dev/null || halt_on_error 'command `pct` not found!' 3
command -v curl >/dev/null || halt_on_error 'command `curl` not found!' 4
}
function install_miaou_bash {
if [[ ! -d /opt/miaou-bash ]]; then
echo 'installing miaou-bash...'
curl https://git.artcode.re/miaou/miaou-bash/raw/branch/main/install.sh | sudo bash -s
fi
}
function install_miaou_proxmox {
if [[ ! -d /opt/miaou-proxmox ]]; then
echo 'installing miaou-proxmox...'
git clone https://git.artcode.re/miaou/miaou-proxmox.git /opt/miaou-proxmox
else
cd /opt/miaou-proxmox
git pull
cd - >/dev/null
fi
status=$(/opt/miaou-bash/tools/append_or_replace '^PATH=\$PATH:/opt/miaou-proxmox/bin' 'PATH=$PATH:/opt/miaou-proxmox/bin' $HOME/.bashrc)
[[ $status == 'appended' ]] && echo 'successfully installed! please run `source $HOME/.bashrc`'
true
}
function compute_cpu_cores {
cores=$(nproc)
if [[ $cores -ge 16 ]]; then
cores=$(( cores / 4 ))
else
if [[ $cores -ge 4 ]]; then
cores=$(( cores / 2 ))
else
cores=1
fi
fi
echo $cores
}
function choose_default {
echo 'initializing default values...'
storage_size='20G'
memory='2G'
swap='2G'
cpu=$(compute_cpu_cores)
mapfile -t storages <<< $(pvesm status | grep zfspool | cut -d' ' -f1)
if [[ ! "$-" =~ i ]]; then
# non-interactive, default to first item
interactive=false
storage_disk="${storages[0]}"
echo "----------------------------------------------------"
echo "first TYPE=ZFSPOOL found: $storage_disk"
echo "to change, please launch one more time: `install.sh`"
echo "----------------------------------------------------"
else
# interactive, ask for default
interactive=true
PS3="Choose default storage number: "
echo "TYPE=ZFSPOOL STORAGE LIST:"
echo "--------------------------"
select storage_disk in "${storages[@]}"; do
[[ -n $storage_disk ]] && break
done
fi
cat > $CONFIG_FILE <<EOF
INTERACTIVE=$interactive
STORAGE_DISK=\${STORAGE_DISK:-$storage_disk}
STORAGE_SIZE=\${STORAGE_SIZE:-$storage_size}
MEMORY=\${MEMORY:-$memory}
SWAP=\${SWAP:-$swap}
CPU=\${CPU:-$cpu}
EOF
}
function initialize {
mkdir -p /opt/miaou-proxmox/config
if [[ ! -f $CONFIG_FILE ]] || ! grep -q 'INTERACTIVE=true' $CONFIG_FILE; then
choose_default
echo '------------------------'
echo 'successfully configured!'
echo '------------------------'
else
echo '------------------------------'
echo 'no change: already configured!'
echo '------------------------------'
fi
echo 'you can display or edit default values with:'
echo ' `pct-default`'
echo ' `pct-default --edit`'
echo '------------------------'
echo 'try out any of the `pct-*` commands (--help might be useful)'
}
# MAIN
set Eue
assert_root
assert_proxmox
install_miaou_bash
install_miaou_proxmox
initialize