Initial commit.

This commit is contained in:
Adsooi 2022-01-08 14:07:34 +01:00
commit 53693b9b7b
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
8 changed files with 1030 additions and 0 deletions

98
libraries/common.sh Normal file
View file

@ -0,0 +1,98 @@
#!/bin/bash
# ____ _ ____
# | _ \ ___ _ __ ___ ___ | |_ ___ ___| _ \ _ _ _ __
# | |_) / _ \ '_ ` _ \ / _ \| __/ _ \/ __| |_) | | | | '_ \
# | _ < __/ | | | | | (_) | || __/\__ \ _ <| |_| | | | |
# |_| \_\___|_| |_| |_|\___/ \__\___||___/_| \_\\__,_|_| |_|
#
# Run several scripts on remote servers automatically.
# Copyright (C) 2022 Ad5001
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This file contains library common functions for all update files.
# Ansi codes
# Resets all ansi codes.
RESET="\e[0m"
# Inverts foreground color and background color.
INVERT="\e[7m"
# Deactivates invertion of foreground color and background color.
RESET_INVERT="\e[27m"
# Sets the forground color to gray.
GRAY_FG="\e[90m"
# Sets the forground color to green.
GREEN_FG="\e[92m"
# Sets the forground color to blue.
BLUE_FG="\e[94m"
# Sets the background color to gray.
GRAY_BG="\e[100m"
# Sets the background color to green.
GREEN_BG="\e[102m"
# Sets the background color to blue.
BLUE_BG="\e[104m"
# Sets the forground color to black.
BLACK_FG="\e[30m"
# Sets the forground color to light green.
L_GREEN_FG="\e[32m"
# Sets the forground color to orange.
ORANGE_FG="\e[33m"
# Sets the forground color to red.
RED_FG="\e[38;5;204m"
# Sets the background color to black.
BLACK_BG="\e[40m"
# Sets the background color to light green.
L_GREEN_BG="\e[42m"
# Sets the background color to orange.
ORANGE_BG="\e[43m"
# Sets the background color to red.
RED_FG="\e[48;5;204m"
# Run a command as root.
# Signature: (<string command>) -> string
run_as_root() {
echo "$PASSWORD" | sudo -p "" -i -S bash -c "$@"
}
# Simple info blue command.
# Signature: (<string message>) -> string
info() {
echo -e "$BLUE_FG 🗘 $@ $RESET"
}
# Simple error command.
# Signature: (<string message>) -> string
error() {
echo -e "$RED_FG 🗙 $@ $RESET"
}
# Simple success green command.
# Signature: (<string message>) -> string
success() {
echo -e "$GREEN_FG$@ $RESET"
}
# Display text in a box
# Signature: (<string message>) -> string
box() {
len=$(expr length "$1")
echo "┌─$(yes ─ | head -$len | tr -d "\n")─┐"
echo "$1"
echo "└─$(yes ─ | head -$len | tr -d "\n")─┘"
}

View file

@ -0,0 +1,60 @@
#!/bin/bash
# ____ _ ____
# | _ \ ___ _ __ ___ ___ | |_ ___ ___| _ \ _ _ _ __
# | |_) / _ \ '_ ` _ \ / _ \| __/ _ \/ __| |_) | | | | '_ \
# | _ < __/ | | | | | (_) | || __/\__ \ _ <| |_| | | | |
# |_| \_\___|_| |_| |_|\___/ \__\___||___/_| \_\\__,_|_| |_|
#
# Run several scripts on remote servers automatically.
# Copyright (C) 2022 Ad5001
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This library contains docker related utilities
# Updates a docker container to a newer version of the image by tag,
# and relaunches the container with a specified script.
# Signature: (<string container_name>, <string container_tag>, <string run_script>) -> void
update_docker() {
container_name=$1
container_tag=$2
run_script=$3
info "Seaching for updates for $container_name..."
run_as_root "docker pull $container_tag"
eq=$(run_as_root "docker ps" | awk -F ' {2,}' '$7 == "'$container_name'" { print $2 }')
if [ -z "$eq" ]; then
# No published ports, so we check field 6.
eq=$(run_as_root "docker ps" | awk -F ' {2,}' '$6 == "'$container_name'" { print $2 }')
fi
# If the image doesn't have the awaited tag, then
if [[ $eq != "$container_tag" ]]; then
# On relance le conteneur.
info "Restarting container $container_name..."
run_as_root "bash $run_script"
# On supprime l'ancienne image pour libérer de l'espace.
success "$container_name restarted!"
info "Deleting old image version of $container_name..."
sudo -p "" -i -S bash -c "docker rmi $eq"
if [ $? -eq 0 ]; then
success "Old image of $container_name deleted!"
else
error "Old image of $container_name could not be removed."
fi
else
success "$container_name up to date!"
fi
echo ""
}

View file

@ -0,0 +1,45 @@
#!/bin/bash
# ____ _ ____
# | _ \ ___ _ __ ___ ___ | |_ ___ ___| _ \ _ _ _ __
# | |_) / _ \ '_ ` _ \ / _ \| __/ _ \/ __| |_) | | | | '_ \
# | _ < __/ | | | | | (_) | || __/\__ \ _ <| |_| | | | |
# |_| \_\___|_| |_| |_|\___/ \__\___||___/_| \_\\__,_|_| |_|
#
# Run several scripts on remote servers automatically.
# Copyright (C) 2022 Ad5001
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This library contains information for redhat based systems (fedora, centos, redhat).
# Updates a redhat system when the user isn't root (runs with sudo)
# Signature: () -> void
update_non_root() {
info "Checking for updates..."
run_as_root "yum check-update"
info "Updating..."
run_as_root "yum update -y"
success "Updated!"
}
# Updates a redhat system when the user is root (runs without sudo)
# Signature: () -> void
update_as_root() {
info "Checking for updates..."
yum check-update
info "Updating..."
yum update -y
success "Updated!"
}

View file

@ -0,0 +1,57 @@
#!/bin/bash
# ____ _ ____
# | _ \ ___ _ __ ___ ___ | |_ ___ ___| _ \ _ _ _ __
# | |_) / _ \ '_ ` _ \ / _ \| __/ _ \/ __| |_) | | | | '_ \
# | _ < __/ | | | | | (_) | || __/\__ \ _ <| |_| | | | |
# |_| \_\___|_| |_| |_|\___/ \__\___||___/_| \_\\__,_|_| |_|
#
# Run several scripts on remote servers automatically.
# Copyright (C) 2022 Ad5001
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This library contains information for debian & ubuntu based systems.
export DEBIAN_FRONTEND=noninteractive
# Updates a debian system when the user isn't root (runs with sudo)
# Signature: () -> void
update_non_root() {
info "Checking for updates..."
run_as_root "apt-get update"
count=$(apt list --upgradeable | wc -l) # Count, with 1 additional line
if [[ $count -ne 1 ]]; then
info "Updating $(expr $count - 1) packets..."
run_as_root "apt-get dist-upgrade -y"
success "System has been updated!"
else
success "System up to date."
fi
}
# Updates a redhat system when the user is root (runs without sudo)
# Signature: () -> void
update_as_root() {
info "Checking for updates..."
apt-get update
count=$(apt list --upgradeable | wc -l) # Count, with 1 additional line
if [[ $count -ne 1 ]]; then
info "Updating $(expr $count - 1) packets..."
apt-get dist-upgrade -y
success "System has been updated!"
else
success "System up to date."
fi
}