#!/bin/bash # # Library: base.lib # Modified: 2024/11/30 15:27:00 # Derechos de Autor (C) [2024] [Mauro Rosero P. ] # # Este programa es software libre: usted puede redistribuirlo y/o modificarlo # bajo los términos de la Licencia Pública Affero General de GNU tal como # lo publica la Free Software Foundation, ya sea la versión 3 de la licencia, # o (a su elección) cualquier versión posterior. # # Este programa se distribuye con la esperanza de que sea útil, # pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de # COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la # Licencia Pública Affero General de GNU para obtener más detalles. # # Debería haber recibido una copia de la Licencia Pública Affero General # junto con este programa. Si no la recibió, consulte . DEVS_NAME=config/project.head GIT_IGNORE=.gitignore DOCKER_LOCAL=dockerfile.local DOCKER_BASE=dockerfile.base DOCKER_SAAS=dockerfile.saas DOCKER_BUILD=build DOCKER_FILE=Dockerfile DOCKER_ENTRY=entrypoint.sh # Test library function baselib_test() { echo "Base Library loaded!" exit 1 } # Load messages function load_messages() { local BIN_PATH=$1 local MSG_PATH=$2 local LANGUAGE=$3 local MSG_FILE=$4 if [ -f $BIN_PATH/$MSG_PATH/$MSG_FILE.$LANGUAGE ] then source $BIN_PATH/$MSG_PATH/$MSG_FILE.$LANGUAGE else source $BIN_PATH/$MSG_PATH/$MSG_FILE.es fi } # Display text header apps function display_text_header() { local PROJECT=$1 local tittle=$2 clear echo "$(cat < $PROJECT/config/project.head) $tittle" echo "============================================================" } # Verify if your program or command is installed function command_installed() { local PROGRAM=$1 if command -v $PROGRAM &> /dev/null; then return 0 fi # No program or command is installed return 1 } # Install os packages function os_pkgs_install() { local PACKAGE=$1 echo "${pkg_install_begin} ${PACKAGE}" if [ "$(uname)" == "Darwin" ]; then # En macOS, a través de Homebrew /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install $PACKAGE elif [ -f /etc/debian_version ] || [ -f /etc/os-release ]; then # En sistemas Debian y derivados, a través de apt apt update apt install -y $PACKAGE elif [ -f /etc/redhat-release ]; then # En sistemas Red Hat y derivados, a través de dnf dnf install -y $PACKAGE elif [ -f /etc/arch-release ]; then # En Arch Linux, a través de pacman pacman -Sy --noconfirm $PACKAGE elif [ -f /etc/rc.conf ]; then # En BSD, a través de pkg pkg install -y $PACKAGE else echo "${os_nofound}" exit 1 fi echo "${pkg_install_success} ${PACKAGE}" } # Update or upgrade OS Packages function os_update() { if [ "$(uname)" == "Darwin" ]; then echo "$os_update (BREW)" brew upgrade elif [ -f /etc/debian_version ] || [ -f /etc/os-release ]; then echo "$os_update (APT)" apt update && apt upgrade -y elif [ -f /etc/redhat-release ]; then echo "$os_update (DNF)" dnf update -y elif [ -f /etc/arch-release ]; then echo "$os_update (PACMAN)" pacman -Syu elif [ -f /etc/rc.conf ]; then echo "$os_update (PKG)" pkg update && pkg upgrade else echo "${os_nofound}" exit 1 fi } # Check for container manager installed function container_mode() { command_installed "podman" if [ $? -eq 0 ] then return 0 else command_installed "docker" if [ $? -eq 0 ] then return 1 fi fi # Exit with code 255 if not docker or podman installed return 255 } # Git init repository function git_init() { local REPO_PATH=$1 local BRANCH=$2 git init "$REPO_PATH" -b $BRANCH return $? } # Git add tracked files to repository function git_add_full() { local REPO_PATH=$1 cd $REPO_PATH if [ $? -eq 0 ]; then git add . return $? else return 255 fi } # Git add tracked files to repository function git_commit() { local REPO_PATH=$1 local GIT_MESSAGES=$2 cd $REPO_PATH if [ $? -eq 0 ]; then git commit -m "$GIT_MESSAGES" return $? else return 255 fi } # Git clone repository function git_clone_pull() { local REPO_PATH=$1 local REPO_REMOTE=$2 local APPS=$3 if [ ! -d $REPO_PATH/$APPS ] then cd $REPO_PATH if [ $? -eq 0 ]; then git clone $REPO_REMOTE $APPS return $? else return 255 fi else cd $REPO_PATH/$APPS if [ $? -eq 0 ]; then git pull return $? else return 255 fi fi } # Check for valid os system function get_osname() { if [ "$(uname)" == "Darwin" ]; then # En macOS, instalamos o actualizamos a través de Homebrew os_name=$(sw_vers -productVersion | awk -F '.' '{print "macOS " $1 "." $2}') elif [ -f /etc/debian_version ] || [ -f /etc/os-release ]; then # En sistemas Debian y derivados, instalamos o actualizamos a través de apt os_name=$(grep "^ID_LIKE=" /etc/os-release | cut -d= -f2) if grep -qi "${head_ubuntu}" /etc/os-release then os_name="${head_ubuntu}" fi elif [ -f /etc/redhat-release ]; then # En sistemas Red Hat, instalamos o actualizamos a través de dnf os_name=$(awk '{print $1}' /etc/redhat-release) elif [ -f /etc/arch-release ]; then # En Arch Linux, instalamos o actualizamos a través de pacman os_name=$(grep '^NAME=' /etc/os-release | awk -F '"' '{print $2}') elif [ -f /etc/rc.conf ]; then # En BSD, instalamos o actualizamos a través de pkg os_name=$(awk '{print $2}' /etc/version | awk -F '-' '{print $1}') else os_name="${head_unknow}" fi } function docker_build() { local CONTAINER=$1 local BASE_IMAGE=$2 docker build ./build -t ${CONTAINER} --build-arg BASE_IMAGE=${BASE_IMAGE} return $? } function podman_build() { local CONTAINER=$1 local BASE_IMAGE=$2 local registry_path=~/.config/containers if [ ! -d ${registry_path} ] then mkdir ${registry_path} fi echo 'unqualified-search-registries = ["docker.io"]' > "${registry_path}/registries.conf" podman build ./build -t ${CONTAINER} --format docker --build-arg BASE_IMAGE=${BASE_IMAGE} return $? } function build_container() { local CONTAINER=$1 local BASE_IMAGE=$2 # Verificar si Docker está instalado if command -v docker &> /dev/null then docker_build "$CONTAINER" "$BASE_IMAGE" return $? fi # Verificar si Podman está instalado if command -v podman &> /dev/null then podman_build "$CONTAINER" "$BASE_IMAGE" return $? fi return 1 }