-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermission_containers.sh
More file actions
128 lines (114 loc) · 3.69 KB
/
permission_containers.sh
File metadata and controls
128 lines (114 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
#####################################################################################
# Script Name: permission_containers.sh
# Author: ddrimus
# Repository: https://github.com/ddrimus/docker-system-manager
# Version: 1.0
# Description: Set appropriate permissions for Docker containers.
#####################################################################################
# Exit when a command fails (use "|| :" after a command to allow it to fail).
set -o errexit
# Initialize global variables
LOG_FILE=""
# Define color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Initialize logging
init_log() {
LOG_FILE="$PWD/log/permission_containers.log"
if mkdir -p "$(dirname "$LOG_FILE")"; then
log_info "Log directory created successfully."
else
log_error "Failed to create log directory."
fi
if touch "$LOG_FILE" && > "$LOG_FILE"; then
log_info "Log file created or cleared successfully."
else
log_error "Failed to create or clear the log file."
fi
}
# Logging function
log() {
local LEVEL=$1
local MESSAGE=$2
local TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$TIMESTAMP] [$LEVEL] $MESSAGE" >> "$LOG_FILE"
}
# Convenience functions for different log levels
log_info() { log INFO "$1"; }
log_warn() { log WARN "$1"; }
log_error() { log ERROR "$1"; }
# Function to run tasks with a spinning indicator
run_with_spin() {
local TASK_NAME="$1"
shift
local COMMAND="$*"
log_info "Starting: $TASK_NAME - $COMMAND"
eval "$COMMAND" &> /dev/null &
local PID=$!
local SP="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
local I=0
while kill -0 $PID 2>/dev/null; do
printf "\r[${YELLOW}%s${NC}] %s" "${SP:I++%${#SP}:1}" "$TASK_NAME"
sleep .1
done
if wait $PID 2>/dev/null; then
printf "\r[${GREEN}✓${NC}] %s\n" "$TASK_NAME"
log_info "Successfully completed: $TASK_NAME"
else
printf "\r[${RED}x${NC}] %s\n" "$TASK_NAME"
log_error "Failed to complete: $TASK_NAME"
fi
}
# Function to configure permissions for traefik container
permission_traefik() {
log_info "Function permission_traefik started"
if [[ ! "$(docker ps -a -q -f name=traefik)" ]]; then
log_info "Adding permission to traefik container (acme.json)..."
run_with_spin "Updating permission of traefik acme.json..." "sudo chmod 600 /srv/docker/containers/traefik/data/traefik/ssl/acme.json"
else
log_error "Traefik container is running. Permission change skipped."
echo -ne "Traefik container is running. Permission change skipped.\n"
fi
log_info "Function permission_traefik completed"
}
# Prompt user for confirmation before proceeding
confirm_execution() {
echo -e -n "Proceeding with the execution of this script: Are you sure? <Y/N> "
read PROMPT
if [[ $PROMPT == "y" || $PROMPT == "Y" ]]; then
log_info "User confirmed script execution"
else
log_info "User aborted script execution"
exit 0
fi
}
# Check if script is running with root privileges
check_root() {
if [ "$UID" -ne 0 ]; then
log_warn "Script not running with root privileges"
if sudo -n true 2>/dev/null; then
log_info "Elevating privileges with sudo"
exec sudo bash "$0" "$@"
else
log_warn "Sudo privileges required. Prompting user."
exec sudo bash "$0" "$@"
fi
else
log_info "Script running with root privileges"
fi
}
# Main function to execute script tasks
main() {
log_info "Script execution started"
permission_traefik
log_info "Script execution completed"
}
# Start the process
init_log
check_root
confirm_execution
main
exit 0