add distro module

enables discovering and setting distro information and settings including
the correct command for install and upgrade of packages
master
David Kebler 2024-02-21 11:48:45 -08:00
parent e42b9fe72a
commit 5b6243d9ef
3 changed files with 66 additions and 48 deletions

1
distro/distros.csv Symbolic link
View File

@ -0,0 +1 @@
/data/Hacking/computing/docker/uci-docker-build/core/rootfs/opt/lib/distros.csv
1 /data/Hacking/computing/docker/uci-docker-build/core/rootfs/opt/lib/distros.csv

65
modules/distro.lib Normal file
View File

@ -0,0 +1,65 @@
#!/bin/bash
load_distros () {
# $BASH_SHELL_HOST/distro/distros.csv
if [ -f "$1" ]; then
sed -e '$a\' "$1";
else
sed -e '$a\' <<< $(valid_distros)
fi |
sed -e '/\s*#.*$/d' |
sed -e '/^\s*$/d' |
sed 's/\s*,\s*/,/g'
}
set_distro () {
_distro="$(echo "$(load_distros)" | grep $(get_distro))"
INSTALL_PKGS=$(echo $_distro | cut -d',' -f3)
UPDATE_PKGS=$(echo $_distro | cut -d',' -f4)
export INSTALL_PKGS
export UPDATE_PKGS
[ "$1" = "-v" ] && env | grep _PKGS
}
get_distro_install () {
set_distro
echo $INSTALL_PKGS
}
get_distro_update () {
set_distro
echo $UPDATE_PKGS
}
get_distro() {
local file=/etc/upstream-release/lsb-release
if [[ -f $file ]]; then
echo $(cat $file | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq)
return 0
else
file=/etc/os-release
if [[ -f $file ]]; then
echo $(cat $file | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq)
fi
fi
}
# get_distro_install
valid_distros () {
# valid distros list
# the distro must be the name used in /etc/os-release
# <distro>,<core image name>,<install command>,<update command>
echo -e "\
alpine,alpine, apk add --no-cache, apk update \n\
debian,debian, apt-get install -y, apt-get update \n\
arch, archlinux,pacman -S --noconfirm --needed, pacman -Syu \n\
ubuntu, ubuntu, apt-get install -y, apt-get update \n\
"
}

View File

@ -43,51 +43,3 @@ hostinfo () {
[[ -f /etc/upstream-release/lsb-release ]] && cat /etc/upstream-release/lsb-release
}
# function get_distro() {
# source /etc/os-release
# declare distros=${@:-"arch alpine debian ubuntu"}
# # echo $ID $ID_LIKE
# for distro in $distros; do
# echo $distro $ID $ID_LIKE
# [[ $ID =~ $distro ]] && echo $distro && return 0
# [[ $ID_LIKE =~ $distro ]] && echo $distro && return 0
# done
# return 1
# }
# function get_release() {
# }
get_distro() {
local file=/etc/upstream-release/lsb-release
if [[ -f $file ]]; then
echo $(cat $file | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq)
return 0
else
file=/etc/os-release
if [[ -f $file ]]; then
echo $(cat $file | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq)
fi
fi
}
# function get_distro() {
# /bin/cp /etc/os-release /tmp/os-release.tmp
# if [[ $1 == "-d" ]]; then
# shift 1
# # docker run -it --name get_container_os --rm --entrypoint cat $1 /etc/os-release
# docker create --name dummy $1 > /dev/null
# docker cp -L dummy:/etc/os-release /tmp/os-release.tmp
# docker rm -f dummy > /dev/null
# # docker run -it --name get_container_os --rm --entrypoint cat $1 /etc/os-release > /tmp/container-os.tmp 2> /dev/null
# shift 1
# fi
# source /tmp/os-release.tmp
# declare valid=${@:-"alpine debian ubuntu"}
# # echo $ID $ID_LIKE
# [[ "${valid}" =~ $ID ]] && echo $ID && return 0
# [[ "${valid}" =~ $ID_LIKE ]] && echo $ID_LIKE && return 0
# return 1
# }