#!/bin/bash ############################################################ # Description: install an config raspberry # Usage: ./inst_raspberry.sh # Author: Gilles Mouchet (gilles.mouchet@gmail.com) # Creation Date: 26-Mar-2021 # Version: 1.0 # # History: # 1.0 - 26-Mar-2021: Creation of script from scratch # ############################################################ # server vars #hostName=actarus01p #hostIp=192.168.1.12 # check if is not on mac os=$(uname) os_mac="Darwin" t=1 #if [ "$t" -eq "1" ]; then if [ "$(uname)" = "Darwin" ]; then echo "do not execute on mac" exit 1 fi # scripts vars (do not edit) scriptName=`echo $0 | sed -e 's|.*/||g' | cut -f1 -d.` dayOfWeek=`/bin/date +%a` daySuffix="_$dayOfWeek" logPath=`pwd` # log path is from execute script path logFile=$logPath/$scriptName$daySuffix.log tempOutputFile=$logPath/$progName.$$ # temp output console file backupFolder=/root/backup # destination folder backup hostName=actarus01p # constants RED="\e[31m" GREEN="\e[32m" YELLOW="\e[33m" BLUE="\e[36m" ENDCOLOR="\e[0m" #----------------------------------------------------------------------------- # Function #----------------------------------------------------------------------------- function saveFile(){ if [ -f $1 ]; then cp $1 $backupFolder >/dev/null 2>&1 if [ $? -ne 0 ]; then printError "Problem to save $1 to $backupFolder";exit; fi fi } function printSuccess() { echo -e "[${GREEN}SUCCESS${ENDCOLOR}] - $1" } function printWarning() { echo -e "[${YELLOW}WARNING${ENDCOLOR}] - $1" } function printError() { echo -e "[${RED}ERROR${ENDCOLOR}] - $1" } function printInfo() { echo -e "\n[${BLUE}INFO${ENDCOLOR}] - $1" } function printSubInfo() { echo -e " - $1" } #----------------------------------------------------------------------------- # install rootfs-expand #----------------------------------------------------------------------------- printInfo "Install rootfs-expand package" dnf -q install rocky-release-rpi-9.0-5.el9.noarch -y > /dev/null if [ "$?" -ne "0" ]; then printError "Problem to install rootfs-extand package";exit; fi printSuccess "rootfs-expand package installed successfully" #----------------------------------------------------------------------------- # extend root part #----------------------------------------------------------------------------- printInfo "Extend root partition" #rootfs-expand partSize=`df --output=size -B 1 "$PWD" |tail -n 1` # For prod #if [ $partSize -gt 61780000000 ]; then # For dev if [ $partSize -gt 21407727600 ]; then printSuccess "Root partion already extended" else rootfs-expand if [ $retVal -ne 0 ]; then printError "Problem to extend root partition";exit; fi printSuccess "Root partition extended" # parted /dev/mmcblk0 resizepart 3 100% >/dev/null 2>&1 # error=$? # resize2fs /dev/mmcblk0p3 >/dev/null 2>&1 # let retVal=retVal+$error # if [ $retVal -ne 0 ]; then printError "Problem to extend root partition";exit; fi # printSuccess "Root partition extended" fi #----------------------------------------------------------------------------- # install package python #----------------------------------------------------------------------------- printInfo "Install package" printSubInfo "install python" yum install -y python3 python3-pip >/dev/null 2>&1 if [ $? -ne 0 ]; then printError "Problem to install packages";exit; fi printSuccess "Packages installed successfully" #----------------------------------------------------------------------------- # update pip #----------------------------------------------------------------------------- printInfo "Upgrade pip3" pip3 install --upgrade pip >/dev/null 2>&1 if [ $? -ne 0 ]; then printError "Problem to upgrade pip3";exit; fi printSuccess "Pip3 upgraded successfully" #----------------------------------------------------------------------------- # install ansible #----------------------------------------------------------------------------- printInfo "Install ansible. Please wait ..." pip3 install --user ansible >/dev/null 2>&1 if [ $? -ne 0 ]; then printError "Problem to install ansible";exit; fi printSuccess "Ansible installed successfully" #----------------------------------------------------------------------------- # add path .local/bin #----------------------------------------------------------------------------- printInfo "Add ./.local/bin in path" echo "export PATH=~/.local/bin:\$PATH" >> ~/.bashrc if [ $? -ne 0 ]; then printError "Problem to install ansible";exit; fi printSuccess "./.local/bin added in path successfully " #----------------------------------------------------------------------------- # update date and time #----------------------------------------------------------------------------- printInfo "Update date and time" chronyc -a makestep if [ $? -ne 0 ]; then printError "Problem to update date and time";exit; fi printSuccess "Date and Time updated successfully" #----------------------------------------------------------------------------- # execute playbook #----------------------------------------------------------------------------- printInfo "Execute playbook" ~/.local/bin/ansible-playbook main.yml --vault-password-file ~/ansible/.vault_pass.txt if [ $? -ne 0 ]; then printError "Problem to execute playbook";exit; fi printSuccess "Ansible playbook finished" #----------------------------------------------------------------------------- # check /root/ansible/.vault_pass.txt exits #----------------------------------------------------------------------------- printInfo "Check /root/ansible/.vault_pass.txt" if [ ! -f "/root/ansible/.vault_pass.txt"]; then printError "You must create /root/ansible/.vault_pass.txt" exit fi printSuccess "/root/ansible/.vault_pass.txt exist" #----------------------------------------------------------------------------- # set timezone #----------------------------------------------------------------------------- #printInfo "Set timzone" #timedatectl set-timezone Europe/Zurich #if [ $? -ne 0 ]; then printError "Problem to set timezone";exit; fi #printSuccess "Timezone setted successfully" #----------------------------------------------------------------------------- # set locale #----------------------------------------------------------------------------- #printInfo "Set timzone" #timedatectl set-timezone Europe/Zurich #if [ $? -ne 0 ]; then printError "Problem to set timezone";exit; fi #printSuccess "Timezone setted successfully"