From e39aeb20e78d11804d8e01d96bde81260c7e69f8 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sat, 3 Dec 2022 15:28:29 -0600 Subject: [PATCH] Save Battery Life (#32) * Added parameters (also made the script more of a program). * Add additional options for unplugged devices. * Allow disabling of cron jobs and lowering of telegraf frequency through new config options. * Fix cron package name. * Fix ssh service name for Ubuntu. * Add output to the options so user knows they were switched. * Prevent some tasks when device is mobile. * Enhance and add to comments. * Add function to check flatpak app disk usage. * Add repair into flatpak commands to help clean disk space of unused apps. * Remove "Done!" output from flatpak-usage. * Go ahead and add function for purging Flatpak apps. * Few more comment changes. --- facts/general/package.yml | 4 + facts/general/service.yml | 8 +- local.yml | 7 +- setup.sh | 118 +++++++++++++----- tasks/general/acct_mgmt/provision_config.yml | 22 ++++ tasks/general/acct_mgmt/users.yml | 20 +++ tasks/general/cron/ansible.yml | 4 +- tasks/general/software/packages.yml | 8 +- tasks/general/software/telegraf.yml | 4 +- tasks/workstation/linux/cron/ansible.yml | 4 +- tasks/workstation/linux/software/flatpaks.yml | 11 +- 11 files changed, 162 insertions(+), 48 deletions(-) diff --git a/facts/general/package.yml b/facts/general/package.yml index 57cbb85..4a34aab 100644 --- a/facts/general/package.yml +++ b/facts/general/package.yml @@ -16,6 +16,7 @@ tar: tar microcode_amd: amd64-microcode microcode_intel: intel-microcode + cron: cron when: ansible_pkg_mgr == "apt" - name: General | Facts | Package | pacman @@ -26,6 +27,7 @@ tar: tar microcode_amd: linux-firmware microcode_intel: intel-ucode + cron: cronie when: ansible_pkg_mgr == "pacman" - name: General | Facts | Package | FreeBSD @@ -47,6 +49,7 @@ tar: tar microcode_amd: microcode_ctl microcode_intel: microcode_ctl + cron: cronie when: ansible_pkg_mgr == "dnf" @@ -140,6 +143,7 @@ update_flatpak: | echo "*** Flatpak ***" && sudo flatpak uninstall --unused {{ update_accept_var }} && + sudo flatpak repair && sudo flatpak update {{ update_accept_var }} && when: flatpak_exec is defined and flatpak_exec.failed is defined and not flatpak_exec.failed diff --git a/facts/general/service.yml b/facts/general/service.yml index a32d7ee..5f64d7d 100644 --- a/facts/general/service.yml +++ b/facts/general/service.yml @@ -15,8 +15,14 @@ acpi: acpid acpi_pattern: acpid -- name: General | Facts | Service | Linux +- name: General | Facts | Service | Arch Linux set_fact: crond: cronie crond_pattern: cronie when: ansible_distribution == "Archlinux" + +- name: General | Facts | Service | Ubuntu Linux + set_fact: + sshd: ssh + sshd_pattern: ssh + when: ansible_distribution == "Ubuntu" diff --git a/local.yml b/local.yml index c94e060..a47f081 100644 --- a/local.yml +++ b/local.yml @@ -66,10 +66,10 @@ # Software Tasks # - include: tasks/workstation/linux/software/flatpaks.yml - when: ansible_system == "Linux" and flatpak_distro + when: ansible_system == "Linux" and flatpak_distro and not mobile - include: tasks/workstation/linux/software/brave.yml - when: ansible_pkg_mgr in ("apt", "dnf") + when: ansible_pkg_mgr in ("apt", "dnf") and not mobile - include: tasks/workstation/freebsd/software/packages.yml when: ansible_system == "FreeBSD" @@ -79,9 +79,10 @@ # Configuration Tasks # - include: tasks/workstation/shared/settings/gnome.yml + when: not mobile - include: tasks/workstation/linux/cron/ansible.yml - when: ansible_system == "Linux" + when: ansible_system == "Linux" and not mobile - include: tasks/workstation/shared/settings/nfs.yml diff --git a/setup.sh b/setup.sh index 61ce123..df547df 100755 --- a/setup.sh +++ b/setup.sh @@ -1,43 +1,91 @@ #!/bin/bash # Script to initialize a system into Ansible collection. -branch="main" -if [[ $1 != "" ]]; then - branch="$1" +## Global Variables ## + +PROG=`basename $0` +LOCAL=`dirname $0`/local.yml +URL="https://github.com/Hyperling/ansible" +BRANCH="main" + +## Functions ## + +# Accepts 1 parameter, it is used as the exit status. +function usage { + cat <<- EOF + $PROG [-l] [-b branch_name] [-h] + Program to initialize synchronization with Hyperling's Ansible configuration. + $URL + + Parameters: + -l : Run the local version associated with this setup.sh. + -b branch_name: Download and run a specific branch. Default is $BRANCH. + -h : Display this help text + EOF + exit $1 +} + +## Parameter Parsing ## + +while getopts ":lb:h" arg; do + case $arg in + l) + echo "Running $LOCAL as the playbook." + local="Y" + ;; + b) + echo -n "Using branch " + branch="$OPTARG" + echo "$branch instead of $BRANCH." + ;; + h) + usage + ;; + *) + echo "ERROR: A parameter was not recognized. Please check your command and try again." + usage 1 + ;; + esac +done + +if [[ $branch == "" ]]; then + branch="$BRANCH" fi +## Main ## + os="$(cat /etc/os-release)" os="$os $(uname -a)" echo "Making sure all necessary packages are installed..." if [[ `which ansible > /dev/null; echo $?` != 0 ]]; then - if [[ $os == *Debian* || $os == *Ubuntu* || $os == *"Pop!_OS"* || $os == *Mint* || $os == *Parrot* ]]; then - sudo apt update - sudo apt install -y ansible git <<< N - sudo mkdir -p /etc/ansible - sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' - elif [[ $os == *FreeBSD* ]]; then - sudo pkg install -y py38-ansible git - sudo mkdir -p /usr/local/etc/ansible - sudo sh -c 'echo "localhost ansible_connection=local" > /usr/local/etc/ansible/hosts' - elif [[ $os == *Arch* || $os == *Manjaro* || $os == *Artix* ]]; then - sudo pacman -Sy --noconfirm ansible git - sudo mkdir -p /etc/ansible - sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' - elif [[ $os == *Darwin* ]]; then - bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - echo "TESTING - EXIT!" - exit 0 - brew install ansible git - elif [[ $os == *Fedora* ]]; then - sudo dnf install -y ansible git python3-libselinux - sudo mkdir -p /etc/ansible - sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' - else - echo -e "ERROR: OS not detected." - echo -e "$os" - exit 1 - fi + if [[ $os == *Debian* || $os == *Ubuntu* || $os == *"Pop!_OS"* || $os == *Mint* || $os == *Parrot* ]]; then + sudo apt update + sudo apt install -y ansible git <<< N + sudo mkdir -p /etc/ansible + sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' + elif [[ $os == *FreeBSD* ]]; then + sudo pkg install -y py38-ansible git + sudo mkdir -p /usr/local/etc/ansible + sudo sh -c 'echo "localhost ansible_connection=local" > /usr/local/etc/ansible/hosts' + elif [[ $os == *Arch* || $os == *Manjaro* || $os == *Artix* ]]; then + sudo pacman -Sy --noconfirm ansible git + sudo mkdir -p /etc/ansible + sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' + elif [[ $os == *Darwin* ]]; then + bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" + echo "TESTING - EXIT!" + exit 0 + brew install ansible git + elif [[ $os == *Fedora* ]]; then + sudo dnf install -y ansible git python3-libselinux + sudo mkdir -p /etc/ansible + sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' + else + echo -e "ERROR: OS not detected." + echo -e "$os" + exit 1 + fi fi echo "Installed!" @@ -45,9 +93,13 @@ echo "Installed!" #ansible-galaxy collection install community.general #echo "Added!" -echo "Running ansible-pull..." -sudo ansible-pull -U https://github.com/Hyperling/ansible.git --checkout $branch -echo "Pulled!" +echo "Provisioning Ansible..." +if [[ $local == "Y" ]]; then + sudo ansible-playbook $LOCAL +else + sudo ansible-pull -U $URL.git --checkout $branch +fi +echo "Provisioned!" echo "Mounting all drives..." mount -a diff --git a/tasks/general/acct_mgmt/provision_config.yml b/tasks/general/acct_mgmt/provision_config.yml index d95d9af..38648d1 100644 --- a/tasks/general/acct_mgmt/provision_config.yml +++ b/tasks/general/acct_mgmt/provision_config.yml @@ -41,6 +41,13 @@ ; ; pentesting : Set to true to install tools such as metasploit (nmap is already provided for reporting) ; + ; no_telem : Set to true to avoid setting up telemetry services. + ; Disables GitHub updates to this project. + ; Disables all telegraf pings. + ; + ; battery : Set to true to attempt to save battery life. + ; Slows down the rate of services such as telegraf and cron. + ; [global] marker: '; {mark} MANAGED BY ANSIBLE | Generic Config' state: present @@ -54,6 +61,8 @@ user_desc: "{{ lookup('ini', 'user_desc file={{gen_file}} default=Hyperling') }}" branch: "{{ lookup('ini', 'branch file={{gen_file}} default=main') }}" pentesting: "{{ lookup('ini', 'pentesting file={{gen_file}} default=false') }}" + no_telem: "{{ lookup('ini', 'no_telem file={{gen_file}} default=false') }}" + battery: "{{ lookup('ini', 'battery file={{gen_file}} default=false') }}" - name: General | Account Management | Provisioning Configuration | General | List set_fact: @@ -64,6 +73,8 @@ - { 'user_desc': "{{ user_desc }}" } - { 'branch': "{{ branch }}" } - { 'pentesting': "{{ pentesting }}" } + - { 'no_telem': "{{ no_telem }}" } + - { 'battery': "{{ battery }}" } ## Workstation ## @@ -91,6 +102,10 @@ ; bsd_gpu : Set to [] to install GPU driver ; Example: amdgpu ; + ; mobile : Set to true if not using an amd64 processor. + ; Not used yet but expecting it to be helpful for Pinephone. + ; May eventually avoid things like Flatpak if they don't play well. + ; [global] marker: '; {mark} MANAGED BY ANSIBLE | Workstation Config' state: present @@ -106,6 +121,7 @@ rdp: "{{ lookup('ini', 'rdp file={{wrk_file}} default=false') }}" vnc: "{{ lookup('ini', 'vnc file={{wrk_file}} default=false') }}" bsd_gpu: "{{ lookup('ini', 'bsd_gpu file={{wrk_file}} default=false') }}" + mobile: "{{ lookup('ini', 'mobile file={{wrk_file}} default=false') }}" - name: General | Account Management | Provisioning Configuration | Workstation | List set_fact: @@ -118,6 +134,7 @@ - { 'rdp': "{{ rdp }}" } - { 'vnc': "{{ vnc }}" } - { 'bsd_gpu': "{{ bsd_gpu }}" } + - { 'mobile': "{{ mobile }}" } # No longer mining, this is now considered deprecated. ### Miner ## @@ -223,6 +240,10 @@ ; ; certbot : Set to true to add cron job for `certbot renew`. ; + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ; + ; Never got these fully working or did not understand how to use them. + ; ; hugo : Set to true to install HUGO static website generator. ; ; gitlab : ee - Installs Enterprise Edition Free Tier. Basically CE with an easier upgrade path for Paid Features. @@ -247,6 +268,7 @@ ; git_sep : Separator for git variables above. ; Example: # ; + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; [global] marker: '; {mark} MANAGED BY ANSIBLE | Server Config' state: present diff --git a/tasks/general/acct_mgmt/users.yml b/tasks/general/acct_mgmt/users.yml index c66728c..105f442 100644 --- a/tasks/general/acct_mgmt/users.yml +++ b/tasks/general/acct_mgmt/users.yml @@ -259,6 +259,24 @@ check-trash --clean sudo df -h } + function_flatpak_usage: | + function flatpak-usage() { + flatpak list --columns=application | while read app; do + size=`flatpak info -s $app 2>/dev/null` + if [[ ! -z $size ]]; then + mb=$(( size / (1000*1000) )) + echo "${mb} MB, $size Bytes, $app" + fi + done | sort -n + } + function_flatpak_purge: | + function flatpak-purge() { + flatpak remove --all --delete-data && + flatpak repair && + echo "Finished purging all Flatpak apps. Executable may still need uninstalled." && + return + echo "ERROR: Something went wrong while removing Flatpak apps!" + } - name: General | Account Management | Users | Files | Common Variable set_fact: @@ -282,6 +300,8 @@ {{ edit_config }} {{ function_check_trash }} {{ function_clean }} + {{ function_flatpak_usage }} + {{ function_flatpak_purge }} - name: General | Account Management | Users | Files | .bashrc blockinfile: diff --git a/tasks/general/cron/ansible.yml b/tasks/general/cron/ansible.yml index a44d1cf..dfe28cc 100644 --- a/tasks/general/cron/ansible.yml +++ b/tasks/general/cron/ansible.yml @@ -8,7 +8,7 @@ minute: "*/30" job: "sudo {{ ansible_pull_exec.stdout }} -o -U {{ repo_local }} --checkout {{ branch }}" state: present - disabled: no + disabled: "{{ 'yes' if no_telem else 'no' }}" - name: General | Cron | Ansible | Create Forced Weekly Subscriber Job cron: @@ -17,4 +17,4 @@ special_time: weekly job: "{{ user_root.home }}/bin/scm.sh" state: present - disabled: no + disabled: "{{ 'yes' if no_telem else 'no' }}" diff --git a/tasks/general/software/packages.yml b/tasks/general/software/packages.yml index aea571e..925932c 100644 --- a/tasks/general/software/packages.yml +++ b/tasks/general/software/packages.yml @@ -61,11 +61,11 @@ when: branch == "dev" ignore_errors: yes -- name: General | Software | Services | Install CROND (Looking at you, Fedora) +- name: General | Software | Services | Install CROND (Looking at you, Fedora+Mobian) package: - name: cronie + name: "{{ cron }}" state: present - when: ansible_pkg_mgr == "dnf" + when: ansible_pkg_mgr == "dnf" or ansible_distribution == "Mobian" - name: General | Software | Services | Install killall (Looking at you, Debian) package: @@ -77,7 +77,7 @@ package: name: - which - - cronie + - "{{ cron }}" - vi state: present when: ansible_distribution == "Archlinux" diff --git a/tasks/general/software/telegraf.yml b/tasks/general/software/telegraf.yml index 10f6eb1..04162f5 100644 --- a/tasks/general/software/telegraf.yml +++ b/tasks/general/software/telegraf.yml @@ -85,7 +85,7 @@ # user = "$USER" [agent] - interval = "5s" + interval = "{{ '300s' if battery else '5s' }}" metric_batch_size = 1000 metric_buffer_limit = 10000 collection_jitter = "0s" @@ -168,7 +168,7 @@ job: "{{ item.command }}" special_time: "{{ item.freq }}" state: present - disabled: no + disabled: "{{ 'yes' if no_telem else 'no' }}" loop: - { "name": "Telegraf Reboot Job" , "freq": "reboot", "command": "{{ telegraf_cmd }}"} - { "name": "Telegraf Keep-Alive Job", "freq": "hourly", "command": "{{ telegraf_watcher }}"} diff --git a/tasks/workstation/linux/cron/ansible.yml b/tasks/workstation/linux/cron/ansible.yml index e1c1511..2abb922 100644 --- a/tasks/workstation/linux/cron/ansible.yml +++ b/tasks/workstation/linux/cron/ansible.yml @@ -8,7 +8,7 @@ special_time: hourly job: "sudo flatpak update --noninteractive" state: present - disabled: no + disabled: "{{ 'yes' if battery else 'no' }}" when: flatpak_distro - name: General | Cron | Ansible | Linux | Update User Flatpaks @@ -18,5 +18,5 @@ special_time: hourly job: "sudo -u {{ user }} flatpak update --noninteractive" state: present - disabled: no + disabled: "{{ 'yes' if battery else 'no' }}" when: flatpak_distro diff --git a/tasks/workstation/linux/software/flatpaks.yml b/tasks/workstation/linux/software/flatpaks.yml index f1c3ce3..f632b0c 100644 --- a/tasks/workstation/linux/software/flatpaks.yml +++ b/tasks/workstation/linux/software/flatpaks.yml @@ -1,5 +1,14 @@ --- -# Use flatpaks because they're consistent! :) +# Use flatpaks because they're consistent versions across different distros! :) + +## To see how much space is taken up try the .rc function flatpak-usage ## + +# If you'd like to remove flatpaks to save space: +## Note: This functionality now exists in .rc function flatpak-purge ## +# $ flatpak remove --all --delete-data +# $ flatpak repair +# Then remove, autoremove, and purge flatpak executable with package manager. +# Source: https://softhints.com/how-to-completely-remove-flatpak-linux-mint/ ## Flatpak Pre-reqs ##