Compare commits

..

13 Commits

2 changed files with 127 additions and 7 deletions

View File

@@ -15,7 +15,7 @@ BRANCH="main"
function usage {
cat <<- EOF
$PROG [-l] [-b branch_name] [-h]
$PROG [-l] [-b branch_name] [-g] [-w] [-s] [-h]
Program to initialize synchronization with Hyperling's Ansible configuration.
$URL
@@ -23,7 +23,10 @@ function usage {
-l : Run the local playbook associated with this $PROG.
This is helpful for development or just saving bandwidth.
It also provides prettier colors than the plaintext from ansible-pull. ;)
-b branch_name: Download and run a specific branch. Default is $BRANCH.
-b branch_name : Download and run a specific branch. Default is $BRANCH.
-g : Enable the General config with test contents.
-w : Enable the Workstation config with test contents.
-s : Enable the Server config with test contents.
-h : Display this help text
EOF
@@ -32,10 +35,13 @@ function usage {
## Parameter Parsing ##
while getopts ":lb:h" arg; do
while getopts ":lb:gwsh" arg; do
case $arg in
l) local="Y" && echo "Running $LOCAL as the playbook." ;;
b) branch="$OPTARG" && echo "Using branch $branch instead of $BRANCH." ;;
l) create_general="Y" && echo "Creating basic '$general_config'." ;;
l) create_workstation="Y" && echo "Creating basic '$workstation_config'." ;;
l) create_server="Y" && echo "Creating basic '$server_config'." ;;
h) usage ;;
*) echo "ERROR: Parameter $OPTARG was not recognized." && usage 1 ;;
esac
@@ -47,15 +53,32 @@ if [[ ! -z $1 && $1 != "-"* ]]; then
usage 1
fi
if [[ $branch == "" ]]; then
if [[ -z "$local" && "$branch" == "" ]]; then
echo "Using default branch $BRANCH."
branch="$BRANCH"
fi
if [[ -n "$create_general" && -f "$general_config" ]]; then
echo "WARNING: General configuration already exists, will not overwrite."
ls -lh "$general"
fi
if [[ -n "$create_workstation" && -f "$workstation_config" ]]; then
echo "WARNING: Workstation configuration already exists, will not overwrite."
ls -lh "$workstation_config"
fi
if [[ -n "$create_server" && -f "$server_config" ]]; then
echo "WARNING: Server configuration already exists, will not overwrite."
ls -lh "$server_config"
fi
## Main ##
os="$(cat /etc/os-release)"
os="$os $(uname -a)"
if [[ ! -f /.dockerenv ]]; then
# If we are not in a Docker container, also check what the kernel says.
os="$os $(uname -a)"
# Docker containers use the host kernel which gives an incorrect reading.
fi
echo "Making sure all necessary packages are installed..."
if [[ `which ansible > /dev/null; echo $?` != 0 ]]; then
@@ -101,6 +124,41 @@ echo "Installed!"
#ansible-galaxy collection install community.general
#echo "Added!"
# Create basic layouts if configs do not exist and are requested.
if [[ -n "$create_general" && ! -f "$general_config" ]]; then
function print_general_contents {
cat <<- EOF
enable=true
user=test
user_desc=Test
branch=$branch
EOF
}
print_general_contents > "$general_config"
fi
if [[ -n "$create_workstation" && ! -f "$workstation_config" ]]; then
function print_workstation_contents {
cat <<- EOF
enable=true
user=test
user_desc=Test
branch=$branch
EOF
}
print_workstation_contents > "$workstation_config"
fi
if [[ -n "$create_server" && ! -f "$server_config" ]]; then
function print_server_contents {
cat <<- EOF
enable=true
user=test
user_desc=Test
branch=$branch
EOF
}
print_server_contents > "$server_config"
fi
echo "Provisioning Ansible..."
if [[ $local == "Y" ]]; then
sudo ansible-playbook $LOCAL

View File

@@ -793,16 +793,70 @@
date "+%Y%m%d"
}
function_backup: |
export BACKUP_DIR="/srv/backup"
function backup {
/usr/local/bin/backup_system.sh
}
alias backup-system="backup"
alias system-backup="backup"
alias check-backups="ll /srv/backup"
alias check-backups="ll $BACKUP_DIR"
function clean-backup {
sh -c "rm -rfv /srv/backup/*"
sh -c "rm -rfv $BACKUP_DIR/*"
}
alias clean-backups="clean-backup"
function_pull_prod_backups: |
export DOCKER="Docker"
export SYSTEM="System"
function pull_prod_backup {
if [[ -z "$1" || ("$1" != "$DOCKER" && "$1" != "$SYSTEM") ]]; then
echo "ERROR: Parameter 1 not correct, expecting '$DOCKER' or '$SYSTEM'."
return 1
fi
backup_type="$1"
echo "Looking for '*${backup_type}*.zip'"
ls -lh *${backup_type}*.zip 2>/dev/null
status="$?"
if [[ "$status" == "0" ]]; then
echo "File already downloaded, skipping."
return 0
else
echo "File still needed, downloading to local tmp/."
fi
mkdir -pv tmp
scp -P {{ prod_port }} \
{{ prod_user }}@{{ prod_host }}:"$BACKUP_DIR/*${backup_type}*.zip" \
tmp/
if [[ "$?" == "0" ]]; then
echo "Succeeded!"
mv -v tmp/*.zip ./
rmdir -v tmp
else
echo "Failed!"
mv tmp ~/TRASH/"tmp_`now`"
fi
}
function pull_prod_backups {
dir="ProductionBackups-Pulled`today`"
mkdir -pv "$dir"
cd "$dir"
pull_prod_backup "$DOCKER"
pull_prod_backup "$SYSTEM"
du -h *.zip | sort -h
status="$?"
cd ..
if [[ "$status" != 0 ]]; then
echo "Failed to find zip files, removing folder."
rmdir -v "$dir"
return 1
fi
echo "Done!"
}
alias pull-prod-backups="pull_prod_backups"
alias pull-backups="pull_prod_backups"
alias prod-backups="pull_prod_backups"
alias prod-backup="pull_prod_backups"
alias pull-prod="pull_prod_backups"
alias prod-pull="pull_prod_backups"
function_log: |
function log {
echo -e "$1"
@@ -953,6 +1007,12 @@
alias reset-prod="load-branch prod main"
alias reset-stage="load-branch stage main"
alias reset-dev="load-branch dev main"
alias_reload_bash: |
alias reload-bash="source ~/.bashrc"
alias bash-reload="reload-bash"
alias shell-reload="reload-bash"
alias reload-shell="reload-bash"
alias reload="reload-bash"
- name: General | Account Management | Users | Files | Common Variable
set_fact:
@@ -1024,11 +1084,13 @@
{{ alias_ansible_facts }}
{{ function_now }}
{{ function_backup }}
{{ function_pull_prod_backups }}
{{ function_log }}
{{ function_debian_upgrade }}
{{ function_ebook_convert }}
{{ alias_sudo }}
{{ function_load_branch }}
{{ alias_reload_bash }}
- name: General | Account Management | Users | Files | .bashrc
blockinfile: