Compare commits

..

8 Commits

9 changed files with 184 additions and 151 deletions

View File

@ -58,7 +58,7 @@ system_maintenance_timeout_cleanup_services: "15min"
system_maintenance_timeout_backup_services: "1h"
system_maintenance_timeout_heal_docker: "30min"
system_maintenance_timeout_update_docker: "5min"
system_maintenance_timeout_defroster: "2min"
system_maintenance_timeout_freezer_action: "2min"
## Services
@ -107,9 +107,11 @@ redirect_domain_mappings:
- { source: "bbb.{{top_domain}}", target: "meet.{{top_domain}}" }
- { source: "short.{{top_domain}}", target: "s.{{top_domain}}" }
- { source: "mastodon.{{top_domain}}", target: "microblog.{{top_domain}}" }
- { source: "peertube.{{top_domain}}", target: "videos.{{top_domain}}" }
- { source: "peertube.{{top_domain}}", target: "video.{{top_domain}}" }
- { source: "videos.{{top_domain}}", target: "video.{{top_domain}}" }
- { source: "funkwhale.{{top_domain}}", target: "music.{{top_domain}}" }
- { source: "pixelfed.{{top_domain}}", target: "pictures.{{top_domain}}" }
- { source: "pixelfed.{{top_domain}}", target: "picture.{{top_domain}}" }
- { source: "pictures.{{top_domain}}", target: "picture.{{top_domain}}" }
- { source: "matrix.{{top_domain}}", target: "chat.{{top_domain}}" }
## Docker Applications
@ -130,9 +132,9 @@ domain_matomo: "matomo.{{top_domain}}"
domain_matrix: "chat.{{top_domain}}"
domain_mediawiki: "wiki.{{top_domain}}"
domain_nextcloud: "cloud.{{top_domain}}"
domain_pixelfed: "pictures.{{top_domain}}"
domain_pixelfed: "picture.{{top_domain}}"
domains_pixelfed: []
domain_peertube: "videos.{{top_domain}}"
domain_peertube: "video.{{top_domain}}"
domains_peertube: []
domain_roulette: "roulette.{{top_domain}}"
domain_attendize: "tickets.{{top_domain}}"

View File

@ -38,5 +38,8 @@ This role was created by [Kevin Veen-Birkenbach](https://github.com/kevinveenbir
## Chat Conversation
To see how this role was developed, you can refer to the [Chat Conversation](https://chat.openai.com/share/41c47fdb-a92d-466d-9e92-5a894fe6bec3) that produced this software.
To see how this role was developed, you can refer to the following ChatGPT Conversation that produced this software:
- https://chat.openai.com/share/1f1dde28-3fb2-4df7-be89-4f7ffe67e5c7
- https://chat.openai.com/share/41c47fdb-a92d-466d-9e92-5a894fe6bec3
- https://chat.openai.com/share/e45d186a-f2e1-4c96-9390-36269e274193

View File

@ -0,0 +1,139 @@
import sys
import datetime
import subprocess
def hex_to_rgb(hex_color):
""" Converts a hexadecimal color string to an RGB tuple.
Args:
hex_color (str): Hexadecimal color string.
Returns:
tuple: Tuple representing the RGB color.
"""
if len(hex_color) != 6:
raise ValueError("Hex color must be 6 characters long.")
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
def calculate_color(start_color, end_color, ratio):
""" Calculates the interpolated color between two colors.
Args:
start_color (str): Start color in hex format.
end_color (str): End color in hex format.
ratio (float): Interpolation ratio (0 to 1).
Returns:
str: Interpolated color in hex format.
"""
start_rgb = hex_to_rgb(start_color)
end_rgb = hex_to_rgb(end_color)
current_rgb = [round(start + (end - start) * ratio) for start, end in zip(start_rgb, end_rgb)]
return ''.join(f"{value:02x}" for value in current_rgb)
def get_current_period(current_time, color_times):
"""
Determines the current time period and returns the corresponding colors.
Args:
current_time (datetime.time): The current time.
color_times (dict): A dictionary linking time periods (as a string in 'HH:MM' format)
with pairs of colors.
Returns:
tuple: A tuple of the start and end color (as hex codes) for the current period.
"""
sorted_periods = sorted(color_times.items())
for i, (period_start_str, colors) in enumerate(sorted_periods):
period_start_time = datetime.datetime.strptime(period_start_str, "%H:%M").time()
if current_time <= period_start_time:
return sorted_periods[i - 1 if i > 0 else -1][1]
return sorted_periods[0][1]
def calculate_transition_ratio(current_time, start_time, end_time):
"""
Calculates the transition ratio for a color transition.
Args:
current_time (datetime.time): The current time.
start_time (datetime.time): The start time of the current color period.
end_time (datetime.time): The end time of the current color period.
Returns:
float: The transition ratio between 0 and 1.
"""
# Use the current date for timestamp calculation
today = datetime.datetime.now().date()
# Calculate timestamps for the start and end of the current period
start_timestamp = datetime.datetime.combine(today, start_time).timestamp()
end_timestamp = datetime.datetime.combine(today, end_time).timestamp()
# If start and end times are the same, return 0 to indicate no transition
if start_timestamp == end_timestamp:
return 0
# Calculate the current timestamp
current_timestamp = datetime.datetime.combine(today, current_time).timestamp()
# Calculate the transition ratio
transition_duration = end_timestamp - start_timestamp
time_since_start = current_timestamp - start_timestamp
# Ratio as the proportion of time elapsed in the total transition duration
return time_since_start / transition_duration
def change_keyboard_color(color, device_id):
""" Changes the keyboard color using an external command.
Args:
color (str): Color in hex format.
device_id (str): Vendor and product ID for the keyboard.
"""
command = ["msi-perkeyrgb", "--model", "GS65", "-s", color, "--id", device_id]
try:
subprocess.run(command, check=True)
print(f"Keyboard color changed to #{color}.")
except subprocess.CalledProcessError as e:
print(f"Error setting keyboard color: {e}")
sys.exit(1)
def main(vendor_and_product_id):
color_times = {
"22:00": ("990000", "990000"), # Night
"21:00": ("0000ff", "990000"), # Evening Blue to Night
"20:00": ("fdbe51", "0000ff"), # Evening Golden to Blue
"19:30": ("ff4500", "fdbe51"), # Sunset to Golden
"12:00": ("ffffff", "ff4500"), # Noon to Sunset
"07:30": ("ffd700", "ffffff"), # Sunrise to Noon
"07:00": ("f9c80e", "ffd700"), # Morning Golden to Sunrise
"06:00": ("ff7f00", "f9c80e"), # Dawn to Golden
"05:00": ("5bc0eb", "ff7f00"), # Morning Blue to Dawn
"04:00": ("990000", "5bc0eb"), # Night to Blue
}
current_time = datetime.datetime.now().time()
start_color, end_color = get_current_period(current_time, color_times)
sorted_times = sorted(color_times.keys())
for time_str in sorted_times + [sorted_times[0]]:
next_start_time_obj = datetime.datetime.strptime(time_str, "%H:%M").time()
if current_time < next_start_time_obj:
break
current_period_start_time_str = [time for time, colors in color_times.items() if colors == (start_color, end_color)][0]
current_period_start_time_obj = datetime.datetime.strptime(current_period_start_time_str, "%H:%M").time()
transition_ratio = calculate_transition_ratio(current_time, current_period_start_time_obj, next_start_time_obj)
current_color = calculate_color(start_color, end_color, transition_ratio)
change_keyboard_color(current_color, vendor_and_product_id)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Error: Vendor and product ID is missing.")
print("Usage: python keyboard_color.py <vendor_and_product_id>")
sys.exit(1)
vendor_and_product_id = sys.argv[1]
main(vendor_and_product_id)

View File

@ -1,139 +0,0 @@
#!/bin/bash
set -euo pipefail
# Check if the vendor and product ID is provided
if [ -z "${1-}" ]; then
echo "Error: Vendor and product ID is missing."
echo "Usage: /opt/keyboard_color.sh <vendor_and_product_id>"
exit 1
fi
# Extract the vendor and product ID from the command-line argument
vendor_and_product_id=$1
# Define the color transition parameters
morning_blue_color="5bc0eb" # Morning Blue Color
dawn_color="ff7f00" # Dawn Color
morning_golden_hour_color="f9c80e" # Morning Golden Hour Color
sunrise_color="ffd700" # Sunrise Color
noon_color="ffffff" # Noon Color
twilight_color="ff6f61" # Twilight Color
evening_golden_hour_color="fdbe51" # Evening Golden Hour Color
evening_blue_color="0000ff" # Evening Blue Color
sunset_color="ff4500" # Sunset Color
night_color="990000" # Night Color
# Function to calculate the color based on the transition ratio
calculate_color() {
local color_start=$1
local color_end=$2
local transition_ratio=$3
local start_r=$((16#${color_start:0:2}))
local start_g=$((16#${color_start:2:2}))
local start_b=$((16#${color_start:4:2}))
local end_r=$((16#${color_end:0:2}))
local end_g=$((16#${color_end:2:2}))
local end_b=$((16#${color_end:4:2}))
local current_r=$(awk "BEGIN { r = ${start_r} + (${end_r} - ${start_r}) * ${transition_ratio}; printf(\"%.0f\", r) }")
local current_g=$(awk "BEGIN { g = ${start_g} + (${end_g} - ${start_g}) * ${transition_ratio}; printf(\"%.0f\", g) }")
local current_b=$(awk "BEGIN { b = ${start_b} + (${end_b} - ${start_b}) * ${transition_ratio}; printf(\"%.0f\", b) }")
local calculated_color=$(printf "%02x%02x%02x" "$current_r" "$current_g" "$current_b")
if [ "$calculated_color" = "ffff100" ]; then
calculated_color="ffffff"
fi
echo "$calculated_color"
}
# Get the current time in HH:MM format
current_time=$(date +%H:%M)
# Calculate the transition ratio based on the current time
if [[ "$current_time" > "22:00" || "$current_time" < "04:00" ]]; then
# Night (22:00 to 04:00)
color_start="$night_color"
color_end="$night_color"
color_start_time="22:00"
color_end_time="04:00"
elif [[ "$current_time" > "21:00" ]]; then
# Evening Blue Hour to Night (21:00 to 22:00)
color_start="$evening_blue_color"
color_end="$night_color"
color_start_time="21:00"
color_end_time="22:00"
elif [[ "$current_time" > "20:00" ]]; then
# Evening Golden Hour to Evening Blue Hour (20:00 to 21:00)
color_start="$evening_golden_hour_color"
color_end="$evening_blue_color"
color_start_time="20:00"
color_end_time="21:00"
elif [[ "$current_time" > "19:30" ]]; then
# Sunset to Evening Golden Hour (19:30 to 20:00)
color_start="$sunset_color"
color_end="$evening_golden_hour_color"
color_start_time="19:30"
color_end_time="20:00"
elif [[ "$current_time" > "12:00" ]]; then
# Noon to Sunset (12:00 to 19:30)
color_start="$noon_color"
color_end="$sunset_color"
color_start_time="12:00"
color_end_time="19:30"
elif [[ "$current_time" > "07:30" ]]; then
# Sunrise to Noon (07:30 to 12:00)
color_start="$sunrise_color"
color_end="$noon_color"
color_start_time="07:30"
color_end_time="12:00"
elif [[ "$current_time" > "07:00" ]]; then
# Morning Golden Hour to Sunrise (07:00 to 07:30)
color_start="$morning_golden_hour_color"
color_end="$sunrise_color"
color_start_time="07:00"
color_end_time="07:30"
elif [[ "$current_time" > "06:00" ]]; then
# Dawn to Morning Golden Hour (06:00 to 07:00)
color_start="$dawn_color"
color_end="$morning_golden_hour_color"
color_start_time="06:00"
color_end_time="07:00"
elif [[ "$current_time" > "05:00" ]]; then
# Morning Blue Hour to Dawn (05:00 to 06:00)
color_start="$morning_blue_color"
color_end="$dawn_color"
color_start_time="05:00"
color_end_time="06:00"
elif [[ "$current_time" > "04:00" ]]; then
# Night to Morning Blue Hour (22:00 to 04:00)
color_start="$night_color"
color_end="$morning_blue_color"
color_start_time="04:00"
color_end_time="05:00"
fi
# Get the current date in YYYY-MM-DD format
current_date=$(date +%Y-%m-%d)
# Calculate the start and end timestamps based on the current date and time
start_timestamp=$(date -d "${current_date} ${color_start_time}" +%s)
end_timestamp=$(date -d "${current_date} ${color_end_time}" +%s)
# Get the current timestamp
current_timestamp=$(date +%s)
# Calculate the transition ratio
transition_ratio=$(awk "BEGIN { ratio = ($current_timestamp - $start_timestamp) / ($end_timestamp - $start_timestamp); print ratio }")
# Calculate the current color based on the transition ratio
current_color=$(calculate_color "$color_start" "$color_end" "$transition_ratio")
echo "Changing keyboard color to #$current_color."
# Set the color using msi-perkeyrgb
msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id"

View File

@ -1,8 +1,8 @@
---
- name: Copy keyboard_color.sh script
copy:
src: keyboard_color.sh
dest: /opt/keyboard_color.sh
src: keyboard_color.py
dest: /opt/keyboard_color.py
mode: 0755
tags:
- keyboard-color

View File

@ -4,4 +4,4 @@ OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot
ExecStart=/opt/keyboard_color.sh {{ vendor_and_product_id }}
ExecStart=/bin/python /opt/keyboard_color.py {{ vendor_and_product_id }}

View File

@ -0,0 +1,29 @@
#!/bin/bash
# Pfad zum Keyboard-Color-Skript
KEYBOARD_COLOR_SCRIPT="$1"
DEVICE_ID="$2"
# NTP deaktivieren
timedatectl set-ntp false
# Startzeit speichern
START_TIME=$(date +%s)
# 2 Minuten (120 Sekunden) für den gesamten Tag simulieren
for i in {0..60}; do
# Berechnung der aktuellen simulierten Zeit
CURRENT_TIME=$((START_TIME + (( 86400 / 60 ) * i )))
# Systemzeit auf die simulierte Zeit setzen
date +%s -s "@$CURRENT_TIME"
# Keyboard-Color-Skript ausführen
python $KEYBOARD_COLOR_SCRIPT $DEVICE_ID
# 2 Sekunden Pause
sleep 2
done
# NTP wieder aktivieren
timedatectl set-ntp true

View File

@ -179,7 +179,7 @@ if __name__ == "__main__":
parser.add_argument('action', choices=['freeze', 'defrost'], help='Action to perform: freeze or defrost services.')
parser.add_argument('services', nargs='+', help='List of services to apply the action to.')
parser.add_argument('--ignore', nargs='*', help='List of services to ignore in the action.', default=[])
parser.add_argument('--timeout', help='Timeout for freezing services (e.g., 1h, 30min, 45s).', default='1h')
parser.add_argument('--timeout', help='Timeout for freezer actions (e.g., 1h, 30min, 45s).', default='1min')
args = parser.parse_args()
services = args.services
ignored_services = args.ignore if args.ignore else []

View File

@ -4,5 +4,4 @@ OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot
ExecStartPre=/bin/sh -c '/usr/bin/python {{ path_system_maintenance_service_freezer_script }} {{item}} {{ system_maintenance_services | join(' ') }} --timeout "{{system_maintenance_timeout_defroster}}"'
ExecStart=/bin/sh -c '/usr/bin/python {{ path_system_maintenance_service_freezer_script }} {{item}} {{ system_maintenance_services | join(' ') }}'
ExecStart=/bin/sh -c '/usr/bin/python {{ path_system_maintenance_service_freezer_script }} {{item}} {{ system_maintenance_services | join(' ') }} --timeout "{{system_maintenance_timeout_freezer_action}}"'