From ac785d569eed345858234e5ab4d64527a1c12b3f Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 7 Feb 2021 21:38:50 +0000 Subject: [PATCH 01/13] added autoclean --- update.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/update.yml b/update.yml index 61800fd..939d90c 100644 --- a/update.yml +++ b/update.yml @@ -8,9 +8,12 @@ update_cache: yes cache_valid_time: 3600 upgrade: full + - name: Manually update snaps + shell: snap refresh - name: Remove unused packages and kernels apt: autoremove: yes + autoclean: yes - name: Check if a reboot is required stat: path: /var/run/reboot-required From 1f3c2b86b669cdf28b3de1a4c85911d3ce2a3ed9 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 7 Feb 2021 21:47:30 +0000 Subject: [PATCH 02/13] removed copy --- update.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/update.yml b/update.yml index 939d90c..acbe4ac 100644 --- a/update.yml +++ b/update.yml @@ -10,10 +10,12 @@ upgrade: full - name: Manually update snaps shell: snap refresh + - name: Remove useless packages from the cache + apt: + autoclean: yes - name: Remove unused packages and kernels apt: autoremove: yes - autoclean: yes - name: Check if a reboot is required stat: path: /var/run/reboot-required From e2f8e1455070ca6c4e2e0812e9ef4a096c834289 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 7 Feb 2021 21:51:07 +0000 Subject: [PATCH 03/13] add autoclean --- update.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/update.yml b/update.yml index acbe4ac..178d177 100644 --- a/update.yml +++ b/update.yml @@ -10,12 +10,13 @@ upgrade: full - name: Manually update snaps shell: snap refresh - - name: Remove useless packages from the cache - apt: - autoclean: yes + # - name: Remove useless packages from the cache + # apt: + # autoclean: yes - name: Remove unused packages and kernels apt: autoremove: yes + autoclean: yes - name: Check if a reboot is required stat: path: /var/run/reboot-required From 4e3b64e1c68ed6bc0ba3d2c5fa22a179159441df Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 7 Feb 2021 22:01:52 +0000 Subject: [PATCH 04/13] revert --- update.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/update.yml b/update.yml index 178d177..a679bb1 100644 --- a/update.yml +++ b/update.yml @@ -10,13 +10,9 @@ upgrade: full - name: Manually update snaps shell: snap refresh - # - name: Remove useless packages from the cache - # apt: - # autoclean: yes - name: Remove unused packages and kernels apt: autoremove: yes - autoclean: yes - name: Check if a reboot is required stat: path: /var/run/reboot-required From 3616641fea6b3c3c8d8d77be65f91f001ce35844 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 10 Apr 2021 12:28:52 +0100 Subject: [PATCH 05/13] non-interactive --- media_setup.sh | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/media_setup.sh b/media_setup.sh index 8440d78..eacc904 100755 --- a/media_setup.sh +++ b/media_setup.sh @@ -186,8 +186,17 @@ install_sonarr () { sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 2009837CBFFD68F45BC180471F4F90DE2A9B4BF8 echo "deb https://apt.sonarr.tv/ubuntu $VERSION main" | sudo tee /etc/apt/sources.list.d/sonarr.list - # Install Sonarr - sudo apt -qq update && sudo apt -qq install -y $SERVICE + # Install Dependencies + sudo apt -qq update && sudo apt -qq install -y mediainfo debconf-utils + + # debconf selections + cat << EOF | sudo debconf-set-selections +sonarr sonarr/owning_group string $USER_ID +sonarr sonarr/owning_user string $USER_ID +EOF + + # Install Sonarr non-interactive + sudo DEBIAN_FRONTEND=noninteractive apt install $SERVICE -y # Create systemd service if [ -f "$SYSD" ]; then @@ -203,7 +212,7 @@ After=network.target mnt-extHD.mount [Service] User=$USER_ID Group=$USER_ID -UMask=002 +UMask=0002 Type=simple ExecStart=/usr/bin/mono --debug /usr/lib/sonarr/bin/Sonarr.exe -nobrowser -data=/var/lib/sonarr From 30fd8bec3a09d307dad7dd7f9cb4166fcb575c6c Mon Sep 17 00:00:00 2001 From: Chris Welsh Date: Wed, 24 Jan 2024 09:53:45 +0000 Subject: [PATCH 06/13] scorched earth --- README.md | 2 - dfs_ubuntu.sh | 220 --------------------------------- media_setup.sh | 315 ------------------------------------------------ media_setup.yml | 58 --------- update.yml | 27 ----- 5 files changed, 622 deletions(-) delete mode 100644 README.md delete mode 100644 dfs_ubuntu.sh delete mode 100755 media_setup.sh delete mode 100755 media_setup.yml delete mode 100644 update.yml diff --git a/README.md b/README.md deleted file mode 100644 index d4124c6..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# scripts -Installing things on OSMC, RPi and Linux diff --git a/dfs_ubuntu.sh b/dfs_ubuntu.sh deleted file mode 100644 index 5294d6e..0000000 --- a/dfs_ubuntu.sh +++ /dev/null @@ -1,220 +0,0 @@ -#!/bin/bash - -# Written by welshch@ -# Mount DFS and/or Active Directory home directory on Ubuntu - -set -eu -o pipefail - -# Colours -RED='\033[1;31m' -YELLOW='\033[1;33m' -GREEN='\033[1;32m' -NC='\033[0m' # No Colour - -# Get User Details -USERNAME="$(logname)" - -check_sudo () { - if [ "$EUID" -ne 0 ] - then - echo -e "${YELLOW}Sudo permissions required, please run this using;${NC}\nsudo ./$(basename "$0")" - exit - fi -} - -amend_files () { - # Add 'wins' option to /etc/nsswitch.conf - sed -i.bak 's/^\(hosts: *files\) \(mdns4_minimal\)/\1 wins \2/' /etc/nsswitch.conf - # Replace '-c' flag with '-t' in /etc/request-key.conf - sed -Ei.bak 's/^(create\s+cifs\.spnego.*cifs.upcall\s+)-c/\1-t/' /etc/request-key.conf - # Add '-t' flag to /etc/request-key.d/cifs.spnego.conf - sed -i.bak 's|create cifs.spnego \* \* /usr/sbin/cifs.upcall %k|create cifs.spnego \* \* /usr/sbin/cifs.upcall -t %k|g' \ - /etc/request-key.d/cifs.spnego.conf -} - -packages_to_install () { - # Add packages required to array - PKG_NAMES=("cifs-utils" "keyutils") - # Run the run_install function if any of the applications are missing - dpkg -s "${PKG_NAMES[@]}" >/dev/null 2>&1 || ( sudo apt -qq update && sudo apt -qq install --install-suggests -y "${PKG_NAMES[@]}" && amend_files )# <--calling function above -} - -create_home_mount () { - # Create mountpoint - DFS_NAME="homeDrive" - DFS_MOUNT="$HOME/$DFS_NAME" - sudo -u "$USERNAME" mkdir -p "$DFS_MOUNT" - # Get filepath - FILEPATH="$(/usr/bin/ldapsearch -Y GSSAPI -Q -H ldap:///dc%3Dant%2Cdc%3Damazon%2Cdc%3Dcom -b \ - DC=ant,DC=amazon,DC=com -s sub cn="$USERNAME" homeDirectory | sed -n -e 's/^homeDirectory: //' -e 's/\\/\//gp')" - # Check if home folder exists in fstab - if grep -Fq "$FILEPATH" /etc/fstab - then - echo -e "${YELLOW}$FILEPATH already exists in fstab, exiting back to menu...${NC}" - sleep 3 - main_menu - else - cp /etc/fstab /etc/fstab.bak - cat >> /etc/fstab << EOF -# -# Active Directory Home Folder -$FILEPATH $DFS_MOUNT cifs cruid=$USERNAME,sec=krb5,noauto,users,noserverino,vers=2.1,rw 0 0 -EOF - fi -} - -create_DFS_share () { - ## Get DFS filepath - echo -e "${YELLOW}What is the DFS file path of the directory? (include all backslashes \)${NC}" - read -r -p "> " FILEPATH - # Replace backslashes with forward slashes - FILEPATH=${FILEPATH//\\//} - # Check for Whitespace - WHITESPACE=" " - if [[ $FILEPATH =~ $WHITESPACE ]] - then - # Replace whitespace with \040 for fstab compliance - FILEPATH=${FILEPATH// /\\040} - fi - # Check fstab for duplicates - if grep -Fq "$FILEPATH" /etc/fstab - then - echo -e "${RED}$FILEPATH already exists in fstab, exiting back to menu...${NC}" - sleep 3 - main_menu - fi - - ## Name mount point - echo -e "${YELLOW}What name you wish to call the mount?${NC}" - read -r -p "> " DFS_MOUNT - DFS_NAME="$DFS_MOUNT" - # Check for Whitespace - if [[ $DFS_MOUNT =~ $WHITESPACE ]] - then - DFS_MOUNT=${DFS_MOUNT// /_} - echo -e "${RED}Whitespace is a bad idea for mountpoints, I am replacing it with underscores!${NC}" - echo -e "${GREEN}$DFS_MOUNT${NC}" - fi - # Check fstab for duplicates - if grep -Fq "$DFS_MOUNT" /etc/fstab - then - echo -e "${RED}$DFS_MOUNT already exists in fstab, exiting back to menu...${NC}" - sleep 3 - main_menu - else - # Create directory in $HOME - DFS_MOUNT="$HOME/$DFS_MOUNT" - sudo -u "$USERNAME" mkdir -p "$DFS_MOUNT" - # Backup fstab - cp /etc/fstab /etc/fstab.bak - # Add to fstab - cat >> /etc/fstab << EOF -# -# DFS File Share -$FILEPATH $DFS_MOUNT cifs cruid=$USERNAME,sec=krb5,noauto,users,noserverino,vers=2.1,rw 0 0 -EOF - fi -} - -check_mount () { - if ( sudo -u "$USERNAME" mount "$DFS_MOUNT" ) - then - echo -e "${GREEN}$DFS_MOUNT is mounted${NC}" - sleep 3 - else - echo -e "${RED}$DFS_MOUNT is not mounted, check your filepath and permissions, restoring fstab${NC}" - mv /etc/fstab.bak /etc/fstab - sleep 3 - main_menu - fi -} - -create_autostart () { - # add autostart file to $HOME to allow mounting after logging in - if [ ! -f "$HOME/.config/autostart/dfs-mount-$DFS_NAME.desktop" ]; then - cat >> "$HOME/.config/autostart/dfs-mount-$DFS_NAME".desktop << EOF -[Desktop Entry] -Type=Application -Name=Mount $DFS_NAME -Comment=Mount $DFS_NAME -Exec=mount $DFS_MOUNT -X-GNOME-Autostart-enabled=true -Hidden=false -NoDisplay=false -EOF - # Change permission to match $HOME - chown --reference="$HOME" "$HOME/.config/autostart/dfs-mount-$DFS_NAME".desktop - else - echo -e "${RED}$DFS_MOUNT already exists in autostart, exiting back to menu...${NC}" - sleep 3 - main_menu - fi -} - -mount_on_vpn () { - # If zz-mount-dfs doesn't exist, create it - if [ ! -f "/etc/NetworkManager/dispatcher.d/zz-mount-dfs" ]; then - cat >> /etc/NetworkManager/dispatcher.d/zz-mount-dfs << EOF -#!/bin/bash - -# Kerberos Ticket -KRB_TICKET="\$(ls /tmp/krb5cc_"\$(id -ru $USERNAME)"_*)" -export KRB5CCNAME="\$KRB_TICKET" - -INTERFACE="\$1" -STATUS="\$2" - -if [ "\$STATUS" = "up" ]; then - if [ "\$INTERFACE" = "vpn0" ] || [ "\$INTERFACE" = "cscotun0" ]; then - # Add Rules Below - sudo -u $USERNAME mount $DFS_MOUNT - fi -fi -EOF - # If zz-mount-dfs does exist, append mount point to "Add Rules Below" - elif grep -Fq "sudo -u $USERNAME mount $DFS_MOUNT" /etc/NetworkManager/dispatcher.d/zz-mount-dfs - then - echo -e "${RED}$DFS_MOUNT already exists in dispatcher script for VPN, exiting back to menu...${NC}" - sleep 3 - main_menu - else - # Backup zz-mount-dfs - cp /etc/NetworkManager/dispatcher.d/zz-mount-dfs /etc/NetworkManager/dispatcher.d/zz-mount-dfs.bak - # Add to zz-mount-dfs - sed -i "/# Add Rules Below/a\\\t\tsudo -u $USERNAME mount $DFS_MOUNT" /etc/NetworkManager/dispatcher.d/zz-mount-dfs - fi -} - -main_menu () { - clear - PS3='Please enter your choice: ' - OPTIONS=("Mount DFS Share" "Mount AD Home Folder" "Quit") - select OPT in "${OPTIONS[@]}" - do - case $OPT in - "Mount DFS Share") - create_DFS_share - check_mount - create_autostart - mount_on_vpn - break - ;; - "Mount AD Home Folder") - create_home_mount - check_mount - create_autostart - mount_on_vpn - break - ;; - "Quit") - exit - ;; - *) echo -e "${RED}Invalid Option: $REPLY${NC}, Please try again";; - esac - done -} - -### SCRIPT COMMANDS ### -check_sudo -packages_to_install -main_menu \ No newline at end of file diff --git a/media_setup.sh b/media_setup.sh deleted file mode 100755 index eacc904..0000000 --- a/media_setup.sh +++ /dev/null @@ -1,315 +0,0 @@ -#!/bin/bash - -## Variables -RED='\033[1;31m' -YELLOW='\033[1;33m' -GREEN='\033[1;32m' -NC='\033[0m' # No colour -VERSION="$(lsb_release -cs)" - -check_sudo () { - ## Get username & check running as sudo - USER_ID="$(logname)" - if [ "$EUID" -ne 0 ]; then - echo -e "${YELLOW}Please run this using; \nsudo ./$(basename "$0")${NC}" - exit - fi -} - -up_to_date () { - echo -e "${YELLOW}Checking /etc/apt.sources..." - sudo sed -i.bak 's|# deb http://ports.ubuntu.com/ubuntu-ports focal-backports main restricted universe multiverse\ - |deb http://ports.ubuntu.com/ubuntu-ports focal-backports main restricted universe multiverse|g' /etc/apt/sources.list - sudo sed -i.bak 's|# deb http://archive.canonical.com/ubuntu focal partner\ - |deb http://archive.canonical.com/ubuntu focal partner|g' /etc/apt/sources.list - echo -e "${YELLOW}Checking for updates..." - sudo apt -qq update && sudo apt -qq dist-upgrade -y -} - -mount_hdd_fstab () { - sudo mkdir -p /mnt/extHD - sudo chown "$USER_ID":"$USER_ID" /mnt/extHD - cat >> /etc/fstab << EOF -# ExtHD -UUID=f3224a20-0cab-4fea-9670-45e42a9550b6 /mnt/extHD ext4 defaults 0 0 -EOF -} - -packages_to_install () { - echo -e "${YELLOW}Checking for dependencies..." - PKG_NAMES=("git" "unrar" "unzip") - # Run the run_install function if any of the applications are missing - dpkg -s "${PKG_NAMES[@]}" >/dev/null 2>&1 || sudo apt install -qqq "${PKG_NAMES[@]}" -} - -check_installed () { - if sudo systemctl is-active --quiet "$SERVICE" - then - echo -e "${RED}${SERVICE} is installed, skipping${NC}" - sleep 3 - main_menu - fi -} - -startup () { - # Enable $SERVICE at boot & start - sudo systemctl enable "$SERVICE" - sudo systemctl start "$SERVICE" - if ! sudo systemctl is-active --quiet "$SERVICE" - then - echo "${RED}${SERVICE} is not running, please check the logs${NC}" - exit - fi - # get internal IP & display URL - echo -e "${GREEN}$SERVICE is running${NC}" -} - -## NZBGet -install_nzbget () { - # Variables - SERVICE="nzbget" - SYSD="/lib/systemd/system/$SERVICE.service" - # Call "check_installed" function - check_installed - # Install NZBGet - echo -e "${YELLOW}Installing NZBGet...${NC}" - sleep 3 - sudo apt -qq install -y nzbget - EXEPATH=$(which nzbget) - # Create systemd service - if [ -f "$SYSD" ]; then - echo -e "${RED}$SERVICE already exists, exiting back to menu...${NC}" - sleep 3 - main_menu - else - cat > "$SYSD" << EOF -[Unit] -Description=NZBGet -After=network.target mnt-extHD.mount - -[Service] -User=$USER_ID -Group=$USER_ID -Type=forking -ExecStart=$EXEPATH -D -ExecStop=$EXEPATH -Q -ExecReload=$EXEPATH -O -KillMode=process -Restart=on-failure - -[Install] -WantedBy=multi-user.target -EOF - fi - # Change Control IP to allow access to WebGUI - sudo sed -i.bak 's|ControlIP=127.0.0.1|ControlIP=0.0.0.0|g' /etc/nzbget.conf - # Change permissons on nzbget.conf file - sudo chmod 666 /etc/nzbget.conf - # Call "startup" function to enable service at boot - startup -} - -## Plex Media Server -install_plex () { - # Variables - SERVICE="plexmediaserver" - # Call "check_installed" function - check_installed - # Install Plex - echo -e "${YELLOW}Installing Plex Media Server...${NC}" - sudo snap install $SERVICE -} - -## Transmission -install_transmission () { - # Variables - SERVICE="transmission" - SYSD="/lib/systemd/system/$SERVICE-daemon.service" - # Call "check_installed" function - check_installed - # Install Transmission - echo -e "${YELLOW}Installing Transmission...${NC}" - sleep 3 - sudo apt -qq install -y transmission-daemon - EXEPATH=$(which transmission-daemon) - sudo systemctl stop transmission-daemon - # Create systemd service - if [ -f "$SYSD" ]; then - echo -e "${RED}${SERVICE} already exists, exiting back to menu...${NC}" - sleep 3 - main_menu - else - cat > "$SYSD" << EOF -[Unit] -Description=Transmission BitTorrent Daemon -After=network.target mnt-extHD.mount - -[Service] -User=$USER_ID -Group=$USER_ID -Type=notify -ExecStart=$EXEPATH -f --log-error --allowed *.*.*.* -ExecStop=/bin/kill -s STOP $MAINPID -ExecReload=/bin/kill -s HUP $MAINPID -Restart=on-failure - -[Install] -WantedBy=multi-user.target -EOF - fi - sudo systemctl daemon-reload - # Call "startup" function to Enable NZBGet at boot - startup -} - -## Sonarr -install_sonarr () { - # Variables - SERVICE="sonarr" - SYSD="/lib/systemd/system/$SERVICE.service" - # Call "check_installed" function - check_installed - # Installing - echo -e "${YELLOW}Installing Sonarr...${NC}" - sleep 3 - - # Add Mono Repo - sudo apt -qq install -y gnupg ca-certificates - sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF - echo "deb https://download.mono-project.com/repo/ubuntu stable-$VERSION main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list - - # Add MediaInfo Repo - wget https://mediaarea.net/repo/deb/repo-mediaarea_1.0-12_all.deb && \ - sudo dpkg -i repo-mediaarea_1.0-12_all.deb - - # Add Sonarr Repo - sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 2009837CBFFD68F45BC180471F4F90DE2A9B4BF8 - echo "deb https://apt.sonarr.tv/ubuntu $VERSION main" | sudo tee /etc/apt/sources.list.d/sonarr.list - - # Install Dependencies - sudo apt -qq update && sudo apt -qq install -y mediainfo debconf-utils - - # debconf selections - cat << EOF | sudo debconf-set-selections -sonarr sonarr/owning_group string $USER_ID -sonarr sonarr/owning_user string $USER_ID -EOF - - # Install Sonarr non-interactive - sudo DEBIAN_FRONTEND=noninteractive apt install $SERVICE -y - - # Create systemd service - if [ -f "$SYSD" ]; then - echo -e "${RED}${SERVICE} already exists, exiting back to menu...${NC}" - sleep 3 - main_menu - else - cat > "$SYSD" << EOF -[Unit] -Description=Sonarr Daemon -After=network.target mnt-extHD.mount - -[Service] -User=$USER_ID -Group=$USER_ID -UMask=0002 - -Type=simple -ExecStart=/usr/bin/mono --debug /usr/lib/sonarr/bin/Sonarr.exe -nobrowser -data=/var/lib/sonarr -TimeoutStopSec=20 -KillMode=process -Restart=on-failure - -[Install] -WantedBy=multi-user.target -EOF - fi - # Call "startup" function to Enable Service at boot - startup -} - -## Radarr -install_radarr () { - # Variables - SERVICE="radarr" - SYSD="/etc/systemd/system/$SERVICE.service" - # Call "check_installed" function - check_installed - # Installing - echo -e "${YELLOW}Installing Radarr...${NC}" - sleep 3 - # Install dependency - sudo apt install libmono-i18n4.0-all -y - # Clone Radarr git repo - curl -L -O "$( curl -s https://api.github.com/repos/Radarr/Radarr/releases \ - | grep linux.tar.gz | grep browser_download_url | head -1 | cut -d \" -f 4 )" - tar -xvzf Radarr.develop.*.linux.tar.gz - sudo mv Radarr /opt - - # Create systemd service - if [ -f "$SYSD" ]; then - echo -e "${RED}${SERVICE} already exists, exiting back to menu...${NC}" - sleep 3 - main_menu - else - cat > "$SYSD" << EOF -[Unit] -Description=Radarr -After=network.target mnt-extHD.mount - -[Service] -User=$USER_ID -Group=$USER_ID - -Type=simple -ExecStart=/opt/Radarr/Radarr -nobrowser -data=/home/chris/.config/Radarr/ -TimeoutStopSec=20 -KillMode=process -Restart=on-failure - -[Install] -WantedBy=multi-user.target -EOF - fi - # Call "startup" function to Enable Service at boot - startup -} - -# main menu function -main_menu () { - clear - PS3="Select a number to install application: " - OPTIONS=("NZBGet" "Transmission" "Radarr" "Sonarr" "Plex" "Quit") - select OPT in "${OPTIONS[@]}" - do - case $OPT in - "NZBGet") - install_nzbget - ;; - "Transmission") - install_transmission - ;; - "Radarr") - install_radarr - ;; - "Sonarr") - install_sonarr - ;; - "Plex") - install_plex - ;; - "Quit") - break - ;; - *) - echo -e "${RED}Invalid option, please try again${NC}";; - esac - done -} - -## SCRIPT COMMANDS ## -check_sudo -up_to_date -packages_to_install -mount_hdd_fstab -main_menu diff --git a/media_setup.yml b/media_setup.yml deleted file mode 100755 index 207201b..0000000 --- a/media_setup.yml +++ /dev/null @@ -1,58 +0,0 @@ ---- - -- name: Install and Configure Media Packages - hosts: server - become: true - tasks: - - name: Update all packages to the latest version - apt: - upgrade: dist - autoremove: yes - - - name: Mount External HDD in fstab - mount: - path: /mnt/extHD - src: UUID=f3224a20-0cab-4fea-9670-45e42a9550b6 - fstype: ext4 - state: mounted - - - name: Add apt-key for Sonarr - apt_key: - keyserver: hkp://keyserver.ubuntu.com:80 - id: 2009837CBFFD68F45BC180471F4F90DE2A9B4BF8 - - - name: Add Sonarr Repository - apt_repository: - repo: deb https://apt.sonarr.tv/ubuntu {{ ansible_distribution_release }} main - state: present - - - name: Add apt-key for Mono - apt_key: - keyserver: hkp://keyserver.ubuntu.com:80 - id: 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF - - - name: Add Mono Repository - apt_repository: - repo: deb https://download.mono-project.com/repo/ubuntu stable-{{ ansible_distribution_release }} main - state: present - - - name: Install Mediainfo repository - apt: - deb: https://mediaarea.net/repo/deb/repo-mediaarea_1.0-14_all.deb - - - name: Update apt cache - apt: - update_cache: yes - - - name: Install Media Downloader packages - apt: - pkg: - - gnupg - - ca-certificates - - sonarr - - transmission-daemon - - nzbget - - - name: Install Plex Media Server - snap: - name: plexmediaserver diff --git a/update.yml b/update.yml deleted file mode 100644 index a679bb1..0000000 --- a/update.yml +++ /dev/null @@ -1,27 +0,0 @@ ---- -- name: Upgrade packages - hosts: all - become: true - tasks: - - name: Update all packages to the latest version - apt: - update_cache: yes - cache_valid_time: 3600 - upgrade: full - - name: Manually update snaps - shell: snap refresh - - name: Remove unused packages and kernels - apt: - autoremove: yes - - name: Check if a reboot is required - stat: - path: /var/run/reboot-required - register: reboot_required_file - - name: Rebooting the server to apply updates - reboot: - connect_timeout: 5 - reboot_timeout: 300 - pre_reboot_delay: 0 - post_reboot_delay: 30 - test_command: uptime - when: reboot_required_file.stat.exists \ No newline at end of file From 8e25ad0676ed2df27a517cad194490ea4a543e66 Mon Sep 17 00:00:00 2001 From: Chris Welsh Date: Wed, 24 Jan 2024 09:59:58 +0000 Subject: [PATCH 07/13] git copied files --- dfs_ubuntu.sh | 220 +++++++++++++++++++++++++++++++++++ dir.py | 21 ++++ media_setup.sh | 310 +++++++++++++++++++++++++++++++++++++++++++++++++ rename_file.py | 17 +++ templates.py | 184 +++++++++++++++++++++++++++++ update.yml | 70 +++++++++++ 6 files changed, 822 insertions(+) create mode 100755 dfs_ubuntu.sh create mode 100755 dir.py create mode 100755 media_setup.sh create mode 100755 rename_file.py create mode 100755 templates.py create mode 100644 update.yml diff --git a/dfs_ubuntu.sh b/dfs_ubuntu.sh new file mode 100755 index 0000000..5294d6e --- /dev/null +++ b/dfs_ubuntu.sh @@ -0,0 +1,220 @@ +#!/bin/bash + +# Written by welshch@ +# Mount DFS and/or Active Directory home directory on Ubuntu + +set -eu -o pipefail + +# Colours +RED='\033[1;31m' +YELLOW='\033[1;33m' +GREEN='\033[1;32m' +NC='\033[0m' # No Colour + +# Get User Details +USERNAME="$(logname)" + +check_sudo () { + if [ "$EUID" -ne 0 ] + then + echo -e "${YELLOW}Sudo permissions required, please run this using;${NC}\nsudo ./$(basename "$0")" + exit + fi +} + +amend_files () { + # Add 'wins' option to /etc/nsswitch.conf + sed -i.bak 's/^\(hosts: *files\) \(mdns4_minimal\)/\1 wins \2/' /etc/nsswitch.conf + # Replace '-c' flag with '-t' in /etc/request-key.conf + sed -Ei.bak 's/^(create\s+cifs\.spnego.*cifs.upcall\s+)-c/\1-t/' /etc/request-key.conf + # Add '-t' flag to /etc/request-key.d/cifs.spnego.conf + sed -i.bak 's|create cifs.spnego \* \* /usr/sbin/cifs.upcall %k|create cifs.spnego \* \* /usr/sbin/cifs.upcall -t %k|g' \ + /etc/request-key.d/cifs.spnego.conf +} + +packages_to_install () { + # Add packages required to array + PKG_NAMES=("cifs-utils" "keyutils") + # Run the run_install function if any of the applications are missing + dpkg -s "${PKG_NAMES[@]}" >/dev/null 2>&1 || ( sudo apt -qq update && sudo apt -qq install --install-suggests -y "${PKG_NAMES[@]}" && amend_files )# <--calling function above +} + +create_home_mount () { + # Create mountpoint + DFS_NAME="homeDrive" + DFS_MOUNT="$HOME/$DFS_NAME" + sudo -u "$USERNAME" mkdir -p "$DFS_MOUNT" + # Get filepath + FILEPATH="$(/usr/bin/ldapsearch -Y GSSAPI -Q -H ldap:///dc%3Dant%2Cdc%3Damazon%2Cdc%3Dcom -b \ + DC=ant,DC=amazon,DC=com -s sub cn="$USERNAME" homeDirectory | sed -n -e 's/^homeDirectory: //' -e 's/\\/\//gp')" + # Check if home folder exists in fstab + if grep -Fq "$FILEPATH" /etc/fstab + then + echo -e "${YELLOW}$FILEPATH already exists in fstab, exiting back to menu...${NC}" + sleep 3 + main_menu + else + cp /etc/fstab /etc/fstab.bak + cat >> /etc/fstab << EOF +# +# Active Directory Home Folder +$FILEPATH $DFS_MOUNT cifs cruid=$USERNAME,sec=krb5,noauto,users,noserverino,vers=2.1,rw 0 0 +EOF + fi +} + +create_DFS_share () { + ## Get DFS filepath + echo -e "${YELLOW}What is the DFS file path of the directory? (include all backslashes \)${NC}" + read -r -p "> " FILEPATH + # Replace backslashes with forward slashes + FILEPATH=${FILEPATH//\\//} + # Check for Whitespace + WHITESPACE=" " + if [[ $FILEPATH =~ $WHITESPACE ]] + then + # Replace whitespace with \040 for fstab compliance + FILEPATH=${FILEPATH// /\\040} + fi + # Check fstab for duplicates + if grep -Fq "$FILEPATH" /etc/fstab + then + echo -e "${RED}$FILEPATH already exists in fstab, exiting back to menu...${NC}" + sleep 3 + main_menu + fi + + ## Name mount point + echo -e "${YELLOW}What name you wish to call the mount?${NC}" + read -r -p "> " DFS_MOUNT + DFS_NAME="$DFS_MOUNT" + # Check for Whitespace + if [[ $DFS_MOUNT =~ $WHITESPACE ]] + then + DFS_MOUNT=${DFS_MOUNT// /_} + echo -e "${RED}Whitespace is a bad idea for mountpoints, I am replacing it with underscores!${NC}" + echo -e "${GREEN}$DFS_MOUNT${NC}" + fi + # Check fstab for duplicates + if grep -Fq "$DFS_MOUNT" /etc/fstab + then + echo -e "${RED}$DFS_MOUNT already exists in fstab, exiting back to menu...${NC}" + sleep 3 + main_menu + else + # Create directory in $HOME + DFS_MOUNT="$HOME/$DFS_MOUNT" + sudo -u "$USERNAME" mkdir -p "$DFS_MOUNT" + # Backup fstab + cp /etc/fstab /etc/fstab.bak + # Add to fstab + cat >> /etc/fstab << EOF +# +# DFS File Share +$FILEPATH $DFS_MOUNT cifs cruid=$USERNAME,sec=krb5,noauto,users,noserverino,vers=2.1,rw 0 0 +EOF + fi +} + +check_mount () { + if ( sudo -u "$USERNAME" mount "$DFS_MOUNT" ) + then + echo -e "${GREEN}$DFS_MOUNT is mounted${NC}" + sleep 3 + else + echo -e "${RED}$DFS_MOUNT is not mounted, check your filepath and permissions, restoring fstab${NC}" + mv /etc/fstab.bak /etc/fstab + sleep 3 + main_menu + fi +} + +create_autostart () { + # add autostart file to $HOME to allow mounting after logging in + if [ ! -f "$HOME/.config/autostart/dfs-mount-$DFS_NAME.desktop" ]; then + cat >> "$HOME/.config/autostart/dfs-mount-$DFS_NAME".desktop << EOF +[Desktop Entry] +Type=Application +Name=Mount $DFS_NAME +Comment=Mount $DFS_NAME +Exec=mount $DFS_MOUNT +X-GNOME-Autostart-enabled=true +Hidden=false +NoDisplay=false +EOF + # Change permission to match $HOME + chown --reference="$HOME" "$HOME/.config/autostart/dfs-mount-$DFS_NAME".desktop + else + echo -e "${RED}$DFS_MOUNT already exists in autostart, exiting back to menu...${NC}" + sleep 3 + main_menu + fi +} + +mount_on_vpn () { + # If zz-mount-dfs doesn't exist, create it + if [ ! -f "/etc/NetworkManager/dispatcher.d/zz-mount-dfs" ]; then + cat >> /etc/NetworkManager/dispatcher.d/zz-mount-dfs << EOF +#!/bin/bash + +# Kerberos Ticket +KRB_TICKET="\$(ls /tmp/krb5cc_"\$(id -ru $USERNAME)"_*)" +export KRB5CCNAME="\$KRB_TICKET" + +INTERFACE="\$1" +STATUS="\$2" + +if [ "\$STATUS" = "up" ]; then + if [ "\$INTERFACE" = "vpn0" ] || [ "\$INTERFACE" = "cscotun0" ]; then + # Add Rules Below + sudo -u $USERNAME mount $DFS_MOUNT + fi +fi +EOF + # If zz-mount-dfs does exist, append mount point to "Add Rules Below" + elif grep -Fq "sudo -u $USERNAME mount $DFS_MOUNT" /etc/NetworkManager/dispatcher.d/zz-mount-dfs + then + echo -e "${RED}$DFS_MOUNT already exists in dispatcher script for VPN, exiting back to menu...${NC}" + sleep 3 + main_menu + else + # Backup zz-mount-dfs + cp /etc/NetworkManager/dispatcher.d/zz-mount-dfs /etc/NetworkManager/dispatcher.d/zz-mount-dfs.bak + # Add to zz-mount-dfs + sed -i "/# Add Rules Below/a\\\t\tsudo -u $USERNAME mount $DFS_MOUNT" /etc/NetworkManager/dispatcher.d/zz-mount-dfs + fi +} + +main_menu () { + clear + PS3='Please enter your choice: ' + OPTIONS=("Mount DFS Share" "Mount AD Home Folder" "Quit") + select OPT in "${OPTIONS[@]}" + do + case $OPT in + "Mount DFS Share") + create_DFS_share + check_mount + create_autostart + mount_on_vpn + break + ;; + "Mount AD Home Folder") + create_home_mount + check_mount + create_autostart + mount_on_vpn + break + ;; + "Quit") + exit + ;; + *) echo -e "${RED}Invalid Option: $REPLY${NC}, Please try again";; + esac + done +} + +### SCRIPT COMMANDS ### +check_sudo +packages_to_install +main_menu \ No newline at end of file diff --git a/dir.py b/dir.py new file mode 100755 index 0000000..7c4d8a5 --- /dev/null +++ b/dir.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 + +# import required module +import os +from pathlib import Path + +# Get filepath +path = input("Directory file path?: ") + +# Error handling +try: + # Iterate over directory + for filename in os.listdir(path): + print(filename) + +except FileNotFoundError: + print("Directory: {0} does not exist".format(path)) +except NotADirectoryError: + print("{0} is not a directory".format(path)) +except PermissionError: + print("You do not have permissions to change to {0}".format(path)) diff --git a/media_setup.sh b/media_setup.sh new file mode 100755 index 0000000..64ca65a --- /dev/null +++ b/media_setup.sh @@ -0,0 +1,310 @@ +#!/bin/bash + +## Variables +RED='\033[1;31m' +YELLOW='\033[1;33m' +GREEN='\033[1;32m' +NC='\033[0m' # No colour +VERSION="$(lsb_release -cs)" + +check_sudo () { + ## Get username & check running as sudo + USER_ID="$(logname)" + if [ "$EUID" -ne 0 ]; then + echo -e "${YELLOW}Please run this using; \nsudo ./$(basename "$0")${NC}" + exit + fi +} + +up_to_date () { + echo -e "${YELLOW}Checking sources...${NC}" + sudo add-apt-repository main && \ + sudo add-apt-repository restricted && \ + sudo add-apt-repository multiverse && \ + sudo add-apt-repository universe +} + +mount_hdd_fstab () { + sudo mkdir -p /mnt/extHD + sudo chown "$USER_ID":"$USER_ID" /mnt/extHD + cat >> /etc/fstab << EOF +# ExtHD +#UUID=f3224a20-0cab-4fea-9670-45e42a9550b6 /mnt/extHD ext4 defaults 0 0 +EOF +} + +packages_to_install () { + echo -e "${YELLOW}Checking for dependencies..." + PKG_NAMES=("git" "unrar" "unzip" "curl" "mediainfo" "sqlite3" "debconf-utils") + # Add MediaInfo Repo + wget https://mediaarea.net/repo/deb/repo-mediaarea_1.0-12_all.deb && \ + sudo dpkg -i repo-mediaarea_1.0-12_all.deb + # Run the run_install function if any of the applications are missing + dpkg -s "${PKG_NAMES[@]}" >/dev/null 2>&1 || ( sudo apt install -qqq "${PKG_NAMES[@]}" -y ) +} + +check_installed () { + if sudo systemctl is-active --quiet "$SERVICE" + then + echo -e "${RED}${SERVICE} is installed, skipping${NC}" + sleep 3 + main_menu + fi +} + +startup () { + # Enable $SERVICE at boot & start + sudo systemctl enable "$SERVICE" + sudo systemctl start "$SERVICE" + if ! sudo systemctl is-active --quiet "$SERVICE" + then + echo "${RED}${SERVICE} is not running, please check the logs${NC}" + exit + fi + # get internal IP & display URL + echo -e "${GREEN}$SERVICE is running${NC}" +} + +## NZBGet +install_nzbget () { + # Variables + SERVICE="nzbget" + SYSD="/lib/systemd/system/$SERVICE.service" + # Call "check_installed" function + check_installed + # Install NZBGet + echo -e "${YELLOW}Installing NZBGet...${NC}" + sleep 3 + sudo apt -qq install -y nzbget + EXEPATH=$(which nzbget) + # Create systemd service + if [ -f "$SYSD" ]; then + echo -e "${RED}$SERVICE already exists, exiting back to menu...${NC}" + sleep 3 + main_menu + else + cat > "$SYSD" << EOF +[Unit] +Description=NZBGet +After=network.target #mnt-extHD.mount + +[Service] +User=$USER_ID +Group=$USER_ID +Type=forking +ExecStart=$EXEPATH -D +ExecStop=$EXEPATH -Q +ExecReload=$EXEPATH -O +KillMode=process +Restart=on-failure + +[Install] +WantedBy=multi-user.target +EOF + fi + # Change Control IP to allow access to WebGUI + sudo sed -i.bak 's|ControlIP=127.0.0.1|ControlIP=0.0.0.0|g' /etc/nzbget.conf + # Change permissons on nzbget.conf file + sudo chmod 666 /etc/nzbget.conf + # Call "startup" function to enable service at boot + startup +} + +## Plex Media Server +install_plex () { + sudo snap install plexmediaserver +} + +## Transmission +install_transmission () { + # Variables + SERVICE="transmission" + SYSD="/lib/systemd/system/$SERVICE-daemon.service" + # Call "check_installed" function + check_installed + # Install Transmission + echo -e "${YELLOW}Installing Transmission...${NC}" + sleep 3 + sudo apt -qq install -y transmission-daemon + EXEPATH=$(which transmission-daemon) + sudo systemctl stop transmission-daemon + # Create systemd service + if [ -f "$SYSD" ]; then + echo -e "${RED}${SERVICE} already exists, exiting back to menu...${NC}" + sleep 3 + main_menu + else + cat > "$SYSD" << EOF +[Unit] +Description=Transmission BitTorrent Daemon +After=network.target #mnt-extHD.mount + +[Service] +User=$USER_ID +Group=$USER_ID +Type=notify +ExecStart=$EXEPATH -f --log-error --allowed *.*.*.* +ExecStop=/bin/kill -s STOP $MAINPID +ExecReload=/bin/kill -s HUP $MAINPID +Restart=on-failure + +[Install] +WantedBy=multi-user.target +EOF + fi + sudo systemctl daemon-reload + # Call "startup" function to Enable NZBGet at boot + startup +} + +## Sonarr +install_sonarr () { + # Variables + SERVICE="sonarr" + SYSD="/lib/systemd/system/$SERVICE.service" + # Call "check_installed" function + check_installed + # Installing + echo -e "${YELLOW}Installing Sonarr...${NC}" + sleep 3 + + # Add Mono Repo + sudo apt -qq install -y gnupg ca-certificates + sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF + echo "deb https://download.mono-project.com/repo/ubuntu stable-$VERSION main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list + + # Add Sonarr Repo + sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 2009837CBFFD68F45BC180471F4F90DE2A9B4BF8 + echo "deb https://apt.sonarr.tv/ubuntu $VERSION main" | sudo tee /etc/apt/sources.list.d/sonarr.list + + # Install Sonarr + sudo DEBIAN_FRONTEND=noninteractive apt install $SERVICE -y + + # Change Users and Group +cat << EOF | sudo debconf-set-selections +sonarr sonarr/owning_user string $USER_ID +sonarr sonarr/owning_group string $USER_ID +sonarr sonarr/owning_umask string 0002 +sonarr sonarr/config_directory string /var/lib/sonarr +EOF + # Reconfigure Sonarr + sudo DEBIAN_FRONTEND=noninteractive dpkg-reconfigure $SERVICE + + # Create systemd service + if [ -f "$SYSD" ]; then + echo -e "${RED}${SERVICE} already exists, exiting back to menu...${NC}" + sleep 3 + main_menu + else +[Unit] +Description=Sonarr Daemon +After=network.target #mnt-extHD.mount + +[Service] +User=$USER_ID +Group=$USER_ID +UMask=0002 + +Type=simple +ExecStart=/usr/bin/mono --debug /usr/lib/sonarr/bin/Sonarr.exe -nobrowser -data=/var/lib/sonarr +TimeoutStopSec=20 +KillMode=process +Restart=on-failure + +[Install] +WantedBy=multi-user.target +EOF + fi + # reload systemctl service + sudo systemctl daemon-reload +} + +## Radarr +install_radarr () { + # Variables + SERVICE="radarr" + SYSD="/etc/systemd/system/$SERVICE.service" + # Call "check_installed" function + check_installed + # Installing + echo -e "${YELLOW}Installing Radarr...${NC}" + sleep 3 + + # Add MediaInfo Repo + wget https://mediaarea.net/repo/deb/repo-mediaarea_1.0-12_all.deb && \ + sudo dpkg -i repo-mediaarea_1.0-12_all.deb + + # Clone Radarr git repo + wget --content-disposition 'http://radarr.servarr.com/v1/update/master/updatefile?os=linux&runtime=netcore&arch=x64' + # Move to /opt and set permissions + tar -xvzf Radarr*.linux*.tar.gz + sudo mv Radarr /opt + sudo chown -R "$USER_ID":"$USER_ID" /opt/Radarr + + # Create systemd service + if [ -f "$SYSD" ]; then + echo -e "${RED}${SERVICE} already exists, exiting back to menu...${NC}" + sleep 3 + main_menu + else + cat > "$SYSD" << EOF +[Unit] +Description=Radarr +After=network.target #mnt-extHD.mount + +[Service] +User=$USER_ID +Group=$USER_ID + +Type=simple +ExecStart=/opt/Radarr/Radarr -nobrowser -data=/home/$USER_ID/.config/Radarr/ +TimeoutStopSec=20 +KillMode=process +Restart=on-failure + +[Install] +WantedBy=multi-user.target +EOF + fi + # Call "startup" function to Enable Service at boot + startup +} + +# main menu function +main_menu () { + clear + PS3="Select a number to install application: " + OPTIONS=("NZBGet" "Transmission" "Radarr" "Sonarr" "Plex" "Quit") + select OPT in "${OPTIONS[@]}" + do + case $OPT in + "NZBGet") + install_nzbget + ;; + "Transmission") + install_transmission + ;; + "Radarr") + install_radarr + ;; + "Sonarr") + install_sonarr + ;; + "Plex") + install_plex + ;; + "Quit") + break + ;; + *) + echo -e "${RED}Invalid option, please try again${NC}";; + esac + done +} + +## SCRIPT COMMANDS ## +check_sudo +up_to_date +packages_to_install +#mount_hdd_fstab +main_menu diff --git a/rename_file.py b/rename_file.py new file mode 100755 index 0000000..957a8dd --- /dev/null +++ b/rename_file.py @@ -0,0 +1,17 @@ +#!/bin/python3 + +import os + +def rename_files_in_directory(directory_path): + for root, dirs, files in os.walk(directory_path): + for file_name in files: + file_path = os.path.join(root, file_name) + directory_name = os.path.basename(root) + file_extension = os.path.splitext(file_name)[1] + new_file_name = directory_name + file_extension + new_file_path = os.path.join(root, new_file_name) + os.rename(file_path, new_file_path) + +# Example usage +directory_path = '/mnt/extHD/Downloads/nzb' +rename_files_in_directory(directory_path) diff --git a/templates.py b/templates.py new file mode 100755 index 0000000..a663dff --- /dev/null +++ b/templates.py @@ -0,0 +1,184 @@ +#!/usr/bin/env python3 + +import os +import shutil +import sys +import subprocess +import re +import importlib +import tempfile +import zipfile + +def installAndImport(package, module_name=None): + module_name = module_name if module_name else package + try: + importlib.import_module(module_name) + except ImportError: + subprocess.check_call([sys.executable, "-m", "pip", "install", package]) + importlib.import_module(module_name) + +def unzip_and_remove_macosx(directory, extraction_path): + for filename in os.listdir(directory): + if filename.endswith('.zip'): + zip_file_path = os.path.join(directory, filename) + with zipfile.ZipFile(zip_file_path, 'r') as zip_ref: + zip_ref.extractall(extraction_path) + + macosx_dir = os.path.join(extraction_path, '__MACOSX') + if os.path.exists(macosx_dir) and os.path.isdir(macosx_dir): + shutil.rmtree(macosx_dir) + + os.remove(zip_file_path) + print(f"Extracted {filename} to {extraction_path}") + +def create_branch_from_target(repo, target_branch, new_branch, GitCommandError): + try: + original_branch = repo.active_branch.name + if orginal_branch != target_branch: + repo.git.checkout(target_branch) + repo.git.pull() # Pull the latest changes from the remote repository + repo.git.checkout('-b', new_branch) + print(f"New branch '{new_branch}' created successfully from '{target_branch}'.") + except GitCommandError as e: + print(f"Error: {e}") + +def ensure_directory_exists(directory): + if not os.path.exists(directory): + os.makedirs(directory) + +def copy_files_to_templates(extraction_dir, template_dir): + # Dynamically get the first (top-level) directory name within the extraction directory + try: + top_level_dir_name = next(os.walk(extraction_dir))[1][0] + except IndexError: + print(f"No directories found in the extraction directory: {extraction_dir}") + return + + top_level_dir_path = os.path.join(extraction_dir, top_level_dir_name) + + for dirpath, _, filenames in os.walk(top_level_dir_path): + # Create a relative path from the top-level directory + relative_dir = os.path.relpath(dirpath, top_level_dir_path) + destination_dir = os.path.join(template_dir, top_level_dir_name, relative_dir) + + print(f"Source Directory: {dirpath}") + print(f"Destination Directory: {destination_dir}") + + # Create the destination directory if it does not exist + if not os.path.exists(destination_dir): + os.makedirs(destination_dir) + print(f"Created directory: {destination_dir}") + + # Copy each file in the current directory to the destination + for filename in filenames: + source_file = os.path.join(dirpath, filename) + destination_file = os.path.join(destination_dir, filename) + shutil.copy(source_file, destination_file) + print(f"Copied {filename} to {destination_dir}") + +def perform_git_operations(repo, template_dir, branch_name, original_branch, GitCommandError): + try: + repo.index.add([template_dir]) + repo.index.commit("Update templates") + + push_command = ["git", "push", "origin", branch_name] + push_result = subprocess.check_output(push_command, text=True) + + if "Everything up-to-date" in push_result: + print("Everything up-to-date. No changes to push.") + else: + print(push_result) + + # Checkout the original branch to allow removal of temp branch + repo.git.checkout(original_branch) + + # Remove the local branch + try: + repo.git.branch('-D', branch_name) + print(f"Local branch '{branch_name}' removed.") + except GitCommandError as e: + print(f"Error removing local branch: {e}") + + except subprocess.CalledProcessError as e: + print(f"Error in Git operations: {e}") + except GitCommandError as e: + print(f"Error in Git operations: {e}") + +def backup_current_state(repo, GitCommandError): + # Create a temporary branch to hold the current state + try: + repo.git.branch('temp_backup_branch') + except GitCommandError: + # Branch already exists, force update it + repo.git.branch('-D', 'temp_backup_branch') + repo.git.branch('temp_backup_branch') + +def rollback_to_backup(repo, GitCommandError): + try: + # Reset the current branch to the backup branch + repo.git.reset('--hard', 'temp_backup_branch') + print("Rollback successful.") + except GitCommandError as e: + print(f"Rollback failed: {e}") + +def main(): + installAndImport('GitPython', 'git') + from git import Repo, GitCommandError + + ##### Script Variables - EDIT THESE TO SUIT YOUR SETUP!! #### + git_repo_root = os.path.expanduser("~/code/dev") # Where your repo starts + template_dir = "~/code/dev/server/trunk/services/src/main/resources/templates" + default_branch = "release/17" + zip_dir = "~/Downloads/email-sms-zips" # Where you save the zipped templated from Slack to + #### END #### + + # Expand paths and ensure directories exist + git_repo_root = os.path.expanduser(git_repo_root) + templates_path = os.path.expanduser(template_dir) + zip_dir = os.path.expanduser(zip_dir) + ensure_directory_exists(zip_dir) + + # Create a unique temporary directory in /tmp + extraction_dir = tempfile.mkdtemp(prefix='templates_extraction_', dir='/tmp') + + try: + unzip_and_remove_macosx(zip_dir, extraction_dir) + + # Assuming the first directory name in the extraction_dir is the template name + template_name = os.listdir(extraction_dir)[0] + + # Generate a unique suffix for the branch name + unique_suffix = next(tempfile._get_candidate_names()) + + # Extract username initials from the system's username + whoami_output = subprocess.check_output(['whoami']).decode('utf-8').strip() + parts = whoami_output.split('.') + user_initials = parts[1][0] + parts[2][0] if len(parts) >= 3 else 'default' + + # Construct the full branch name + full_branch_name = f"{user_initials}/{template_name}_{unique_suffix}-no-build" + + repo = Repo(git_repo_root) + original_branch = repo.active_branch.name # Capture the original branch + create_branch_from_target(repo, default_branch, full_branch_name, GitCommandError) + copy_files_to_templates(extraction_dir, templates_path) + os.chdir(git_repo_root) + perform_git_operations(repo, templates_path, full_branch_name, original_branch, GitCommandError) + + # Clean up the temporary directory after successful completion + shutil.rmtree(extraction_dir) + print(f"Temporary files cleaned up from {extraction_dir}") + + except Exception as e: + print(f"An error occurred: {e}") + print(f"Temporary files are located at {extraction_dir} for review") + + # Rollback changes in case of an error + try: + backup_current_state(repo, GitCommandError) + rollback_to_backup(repo, GitCommandError) + except Exception as rollback_error: + print(f"Rollback failed: {rollback_error}") + +if __name__ == "__main__": + main() diff --git a/update.yml b/update.yml new file mode 100644 index 0000000..8c7389d --- /dev/null +++ b/update.yml @@ -0,0 +1,70 @@ +--- +- name: Upgrade packages + hosts: all + become: true + tasks: + - name: Update all packages to the latest version Debian + ansible.builtin.apt: + update_cache: yes + cache_valid_time: 3600 + upgrade: full + when: ansible_facts['os_family'] == "Debian" + + - name: Install Dependencies for RedHat + ansible.builtin.dnf: + update_cache: yes + name: dnf-utils + state: latest + when: ansible_facts['os_family'] == "RedHat" + + - name: Update all packages to the latest version RedHat + ansible.builtin.dnf: + update_cache: yes + name: "*" + state: latest + when: ansible_facts['os_family'] == "RedHat" + + - name: Remove unused packages and kernels Debian + ansible.builtin.apt: + autoremove: yes + when: ansible_facts['os_family'] == "Debian" + + - name: Remove unused packages and kernels RedHat + ansible.builtin.dnf: + autoremove: yes + when: ansible_facts['os_family'] == "RedHat" + + - name: Check if a reboot is required Debian + stat: + path: /var/run/reboot-required + register: reboot_required_file + when: ansible_facts['os_family'] == "Debian" + + - name: Reboot if required Debian + reboot: + msg: "Reboot initiated by Ansible for kernel updates Debian" + connect_timeout: 5 + reboot_timeout: 300 + pre_reboot_delay: 0 + post_reboot_delay: 30 + test_command: uptime + when: + - ansible_facts['os_family'] == "Debian" + - reboot_required_file.stat.exists + + - name: Check if a reboot is required RedHat + command: needs-restarting -r + register: reboot_required_file + when: ansible_facts['os_family'] == "RedHat" + + - name: Reboot if required RedHat + reboot: + msg: "Reboot initiated by Ansible for kernel updates RedHat" + connect_timeout: 5 + reboot_timeout: 300 + pre_reboot_delay: 0 + post_reboot_delay: 30 + test_command: uptime + when: + - ansible_facts['os_family'] == "RedHat" + - reboot_required_file.rc == 1 \ No newline at end of file From b6f0c14759fed417dc43d6e11232424c0ca04ee6 Mon Sep 17 00:00:00 2001 From: Chris Welsh Date: Fri, 8 Mar 2024 09:17:43 +0000 Subject: [PATCH 08/13] commit all changes --- needsrestart.py | 26 ++++++++++++++++++ templates.py | 73 ++----------------------------------------------- 2 files changed, 28 insertions(+), 71 deletions(-) create mode 100755 needsrestart.py diff --git a/needsrestart.py b/needsrestart.py new file mode 100755 index 0000000..0025743 --- /dev/null +++ b/needsrestart.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +def update_needrestart_config(): + config_file_path = "/etc/needrestart/needrestart.conf" + new_line = "$nrconf{restart} = 'a';\n" + + try: + # Read existing config contents + with open(config_file_path, "r") as file: + lines = file.readlines() + + # Find and replace the line + for i, line in enumerate(lines): + if line.startswith("#$nrconf{restart}"): + lines[i] = new_line + break + + # Write the modified content back to the file (requires root privileges) + with open(config_file_path, "w") as file: + file.writelines(lines) + + except (IOError, PermissionError) as e: + print(f"Error modifying config file: {e}") + +# Call the function to execute the change +update_needrestart_config() diff --git a/templates.py b/templates.py index a663dff..a85b1b4 100755 --- a/templates.py +++ b/templates.py @@ -7,7 +7,6 @@ import re import importlib import tempfile -import zipfile def installAndImport(package, module_name=None): module_name = module_name if module_name else package @@ -17,69 +16,10 @@ def installAndImport(package, module_name=None): subprocess.check_call([sys.executable, "-m", "pip", "install", package]) importlib.import_module(module_name) -def unzip_and_remove_macosx(directory, extraction_path): - for filename in os.listdir(directory): - if filename.endswith('.zip'): - zip_file_path = os.path.join(directory, filename) - with zipfile.ZipFile(zip_file_path, 'r') as zip_ref: - zip_ref.extractall(extraction_path) - - macosx_dir = os.path.join(extraction_path, '__MACOSX') - if os.path.exists(macosx_dir) and os.path.isdir(macosx_dir): - shutil.rmtree(macosx_dir) - - os.remove(zip_file_path) - print(f"Extracted {filename} to {extraction_path}") - -def create_branch_from_target(repo, target_branch, new_branch, GitCommandError): - try: - original_branch = repo.active_branch.name - if orginal_branch != target_branch: - repo.git.checkout(target_branch) - repo.git.pull() # Pull the latest changes from the remote repository - repo.git.checkout('-b', new_branch) - print(f"New branch '{new_branch}' created successfully from '{target_branch}'.") - except GitCommandError as e: - print(f"Error: {e}") - -def ensure_directory_exists(directory): - if not os.path.exists(directory): - os.makedirs(directory) - -def copy_files_to_templates(extraction_dir, template_dir): - # Dynamically get the first (top-level) directory name within the extraction directory - try: - top_level_dir_name = next(os.walk(extraction_dir))[1][0] - except IndexError: - print(f"No directories found in the extraction directory: {extraction_dir}") - return - - top_level_dir_path = os.path.join(extraction_dir, top_level_dir_name) - - for dirpath, _, filenames in os.walk(top_level_dir_path): - # Create a relative path from the top-level directory - relative_dir = os.path.relpath(dirpath, top_level_dir_path) - destination_dir = os.path.join(template_dir, top_level_dir_name, relative_dir) - - print(f"Source Directory: {dirpath}") - print(f"Destination Directory: {destination_dir}") - - # Create the destination directory if it does not exist - if not os.path.exists(destination_dir): - os.makedirs(destination_dir) - print(f"Created directory: {destination_dir}") - - # Copy each file in the current directory to the destination - for filename in filenames: - source_file = os.path.join(dirpath, filename) - destination_file = os.path.join(destination_dir, filename) - shutil.copy(source_file, destination_file) - print(f"Copied {filename} to {destination_dir}") - def perform_git_operations(repo, template_dir, branch_name, original_branch, GitCommandError): try: repo.index.add([template_dir]) - repo.index.commit("Update templates") + repo.index.commit("Update backups") push_command = ["git", "push", "origin", branch_name] push_result = subprocess.check_output(push_command, text=True) @@ -92,13 +32,6 @@ def perform_git_operations(repo, template_dir, branch_name, original_branch, Git # Checkout the original branch to allow removal of temp branch repo.git.checkout(original_branch) - # Remove the local branch - try: - repo.git.branch('-D', branch_name) - print(f"Local branch '{branch_name}' removed.") - except GitCommandError as e: - print(f"Error removing local branch: {e}") - except subprocess.CalledProcessError as e: print(f"Error in Git operations: {e}") except GitCommandError as e: @@ -127,9 +60,7 @@ def main(): ##### Script Variables - EDIT THESE TO SUIT YOUR SETUP!! #### git_repo_root = os.path.expanduser("~/code/dev") # Where your repo starts - template_dir = "~/code/dev/server/trunk/services/src/main/resources/templates" - default_branch = "release/17" - zip_dir = "~/Downloads/email-sms-zips" # Where you save the zipped templated from Slack to + default_branch = "main" #### END #### # Expand paths and ensure directories exist From 992bc936729619442c464fa1c467a161316f339f Mon Sep 17 00:00:00 2001 From: Chris Welsh Date: Sat, 9 Mar 2024 16:20:05 +0000 Subject: [PATCH 09/13] added dir rename and video chack --- dir.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 18 deletions(-) diff --git a/dir.py b/dir.py index 7c4d8a5..e715153 100755 --- a/dir.py +++ b/dir.py @@ -1,21 +1,74 @@ #!/usr/bin/python3 -# import required module import os -from pathlib import Path - -# Get filepath -path = input("Directory file path?: ") - -# Error handling -try: - # Iterate over directory - for filename in os.listdir(path): - print(filename) - -except FileNotFoundError: - print("Directory: {0} does not exist".format(path)) -except NotADirectoryError: - print("{0} is not a directory".format(path)) -except PermissionError: - print("You do not have permissions to change to {0}".format(path)) +import magic +import subprocess +import importlib + +def install_package(package): + """Installs a package using pip.""" + try: + subprocess.check_call([sys.executable, "-m", "pip", "install", package]) + except subprocess.CalledProcessError: + print(f"Error installing {package}") + +def import_module(module_name): + """Imports a module.""" + try: + importlib.import_module(module_name) + except ImportError: + print(f"{module_name} is not installed. Installing...") + install_package(module_name) + importlib.import_module(module_name) + +def main(): + install_and_import('magic') + import magic + + path = input("Directory file path?: ") + + # Error handling + try: + if not os.path.isdir(path): + raise ValueError("The provided path is not a directory.") + + video_count = 0 # Track the number of video files + + # Iterate over directory + for filename in os.listdir(path): + filepath = os.path.join(path, filename) + + # Use magic to determine file type + mime_type = magic.from_file(filepath, mime=True) + + if mime_type and mime_type.startswith('video/'): + video_count += 1 + + if video_count > 1: + print("Multiple video files found in the directory. Skipping renaming.") + break + + directory_name = os.path.basename(path) + new_filename = directory_name + os.path.splitext(filename)[1] + new_filepath = os.path.join(path, new_filename) + + try: + os.rename(filepath, new_filepath) + print(f"Video file '{filename}' renamed to '{new_filename}'") + except OSError as e: + print(f"Error renaming file: {e}") + + else: + print(filename) # Not a video file + + except FileNotFoundError: + print("Directory: {0} does not exist".format(path)) + except NotADirectoryError: + print("{0} is not a directory".format(path)) + except PermissionError: + print("You do not have permissions to change to {0}".format(path)) + except ValueError as e: + print(e) + +if __name__ == "__main__": + main() From cb76652d033e4b8445ca7462c9f0d36f155751ba Mon Sep 17 00:00:00 2001 From: Chris Welsh Date: Sat, 9 Mar 2024 16:29:11 +0000 Subject: [PATCH 10/13] package updates --- update.yml | 67 ++++++++++++++---------------------------------------- 1 file changed, 17 insertions(+), 50 deletions(-) diff --git a/update.yml b/update.yml index 8c7389d..f2367d5 100644 --- a/update.yml +++ b/update.yml @@ -1,70 +1,37 @@ --- -- name: Upgrade packages +- name: Upgrade packages and manage reboots hosts: all become: true - tasks: - - name: Update all packages to the latest version Debian - ansible.builtin.apt: - update_cache: yes - cache_valid_time: 3600 - upgrade: full - when: ansible_facts['os_family'] == "Debian" - - name: Install Dependencies for RedHat - ansible.builtin.dnf: - update_cache: yes - name: dnf-utils + tasks: + - name: Install common dependencies (if needed) + ansible.builtin.package: + name: + - dnf-utils # For RedHat-based systems + - apt-utils # For Debian-based systems state: latest - when: ansible_facts['os_family'] == "RedHat" - - name: Update all packages to the latest version RedHat - ansible.builtin.dnf: - update_cache: yes + - name: Update all packages + ansible.builtin.package: name: "*" state: latest - when: ansible_facts['os_family'] == "RedHat" - - - name: Remove unused packages and kernels Debian - ansible.builtin.apt: - autoremove: yes - when: ansible_facts['os_family'] == "Debian" + update_cache: yes - - name: Remove unused packages and kernels RedHat - ansible.builtin.dnf: + - name: Remove unused packages and kernels + ansible.builtin.package: autoremove: yes - when: ansible_facts['os_family'] == "RedHat" - - name: Check if a reboot is required Debian - stat: + - name: Check if a reboot is required + ansible.builtin.stat: path: /var/run/reboot-required register: reboot_required_file - when: ansible_facts['os_family'] == "Debian" - - - name: Reboot if required Debian - reboot: - msg: "Reboot initiated by Ansible for kernel updates Debian" - connect_timeout: 5 - reboot_timeout: 300 - pre_reboot_delay: 0 - post_reboot_delay: 30 - test_command: uptime - when: - - ansible_facts['os_family'] == "Debian" - - reboot_required_file.stat.exists - - - name: Check if a reboot is required RedHat - command: needs-restarting -r - register: reboot_required_file - when: ansible_facts['os_family'] == "RedHat" - - name: Reboot if required RedHat + - name: Reboot if required reboot: - msg: "Reboot initiated by Ansible for kernel updates RedHat" + msg: "Reboot initiated by Ansible for updates" connect_timeout: 5 reboot_timeout: 300 pre_reboot_delay: 0 post_reboot_delay: 30 test_command: uptime - when: - - ansible_facts['os_family'] == "RedHat" - - reboot_required_file.rc == 1 \ No newline at end of file + when: reboot_required_file.stat.exists From 618dac8ff82de9ef9b8899d66915201367feb6f4 Mon Sep 17 00:00:00 2001 From: Chris Welsh Date: Sat, 9 Mar 2024 16:41:21 +0000 Subject: [PATCH 11/13] update --- test.yml | 11 +++++++++++ update.yml | 11 +++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 test.yml diff --git a/test.yml b/test.yml new file mode 100644 index 0000000..a074c40 --- /dev/null +++ b/test.yml @@ -0,0 +1,11 @@ +- name: Test package installation on Ubuntu + hosts: localhost + connection: local + become: true + tasks: + - name: Install a sample package + ansible.builtin.package: + name: + - tree # A simple, non-essential package + state: latest + diff --git a/update.yml b/update.yml index f2367d5..6844eb9 100644 --- a/update.yml +++ b/update.yml @@ -7,9 +7,16 @@ - name: Install common dependencies (if needed) ansible.builtin.package: name: - - dnf-utils # For RedHat-based systems - - apt-utils # For Debian-based systems + - dnf-utils state: latest + when: ansible_facts['os_family'] == 'RedHat' + + - name: Install common dependencies (if needed) + ansible.builtin.package: + name: + - apt-utils + state: latest + when: ansible_facts['os_family'] == 'Debian' - name: Update all packages ansible.builtin.package: From 150d033563ece4698c6b399abb8d9d7ae0916d31 Mon Sep 17 00:00:00 2001 From: Chris Welsh Date: Sat, 9 Mar 2024 16:45:04 +0000 Subject: [PATCH 12/13] update --- update.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/update.yml b/update.yml index 6844eb9..2c0ae64 100644 --- a/update.yml +++ b/update.yml @@ -4,14 +4,14 @@ become: true tasks: - - name: Install common dependencies (if needed) + - name: Install common dependencies (if needed - RedHat based) ansible.builtin.package: name: - dnf-utils state: latest when: ansible_facts['os_family'] == 'RedHat' - - name: Install common dependencies (if needed) + - name: Install common dependencies (if needed - Debian based) ansible.builtin.package: name: - apt-utils From f08b04f906442f1c3bc0c911c47885857917c357 Mon Sep 17 00:00:00 2001 From: Chris Welsh Date: Sun, 10 Mar 2024 20:48:24 +0000 Subject: [PATCH 13/13] jimmy files --- jimmy_files.sh | 3 +++ test.yml | 11 ----------- 2 files changed, 3 insertions(+), 11 deletions(-) create mode 100755 jimmy_files.sh delete mode 100644 test.yml diff --git a/jimmy_files.sh b/jimmy_files.sh new file mode 100755 index 0000000..299d3ad --- /dev/null +++ b/jimmy_files.sh @@ -0,0 +1,3 @@ +#!/bin/bash +find /mnt/extHD/Downloads/complete/jimmy -maxdepth 2 -type f -name "*.mp4" -exec mv {} . \; +find /mnt/extHD/Downloads/complete/jimmy -maxdepth 1 -type d -exec rm -rf {} \; diff --git a/test.yml b/test.yml deleted file mode 100644 index a074c40..0000000 --- a/test.yml +++ /dev/null @@ -1,11 +0,0 @@ -- name: Test package installation on Ubuntu - hosts: localhost - connection: local - become: true - tasks: - - name: Install a sample package - ansible.builtin.package: - name: - - tree # A simple, non-essential package - state: latest -