diff --git a/scripts/fan.py b/scripts/fan.py old mode 100644 new mode 100755 index 8c35890..f6656d7 --- a/scripts/fan.py +++ b/scripts/fan.py @@ -1,43 +1,47 @@ #!/usr/bin/python3 ''' Description: A simple script to control 3pin PWM fan in raspberry pi 4 -Reference: https://github.com/geekworm-com/x-c1/blob/main/fan.py ''' import pigpio import time import logging -logging.basicConfig(format='%(levelname)s:%(asctime)s %(message)s', level=logging.DEBUG) +# Pin number for the fan +FAN_PIN = 18 + +# PWM frequency and duty cycle range +PWM_FREQ = 25000 +PWM_RANGE = 100 -servo = 18 +# Temperature thresholds and corresponding duty cycles +TEMP_THRESHOLDS = [(75, 100), (70, 80), (60, 70), (48, 50), (30, 40)] + +logging.basicConfig(format='%(levelname)s:%(asctime)s %(message)s', level=logging.DEBUG) pwm = pigpio.pi() -pwm.set_mode(servo, pigpio.OUTPUT) -pwm.set_PWM_frequency(servo, 25000) -pwm.set_PWM_range(servo, 100) -while (True): +pwm.set_mode(FAN_PIN, pigpio.OUTPUT) +pwm.set_PWM_frequency(FAN_PIN, PWM_FREQ) +pwm.set_PWM_range(FAN_PIN, PWM_RANGE) + +prev_dc = None +while True: # Get CPU temp with open("/sys/class/thermal/thermal_zone0/temp") as f: - temp = float(f.read()) / 1000.00 - temp = float('%.2f' % temp) - - if (temp > 75): - logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 100%") - pwm.set_PWM_dutycycle(servo, 100) - elif (temp > 70): - logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 80%") - pwm.set_PWM_dutycycle(servo, 80) - elif (temp > 60): - logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 70%") - pwm.set_PWM_dutycycle(servo, 70) - elif (temp > 50): - logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 50%") - pwm.set_PWM_dutycycle(servo, 50) - elif (temp > 30): - logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 40%") - pwm.set_PWM_dutycycle(servo, 40) - elif (temp < 30): - logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 0%") - pwm.set_PWM_dutycycle(servo, 0) - - time.sleep(120) + temp = float(f.read()) / 1000.00 + temp = round(temp, 2) + + # Determine duty cycle based on temperature + duty_cycle = 0 + for threshold, dc in TEMP_THRESHOLDS: + if temp >= threshold: + duty_cycle = dc + break + + # Set fan speed if duty cycle has changed + if duty_cycle != prev_dc: + pwm.set_PWM_dutycycle(FAN_PIN, duty_cycle) + logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to {duty_cycle}%") + prev_dc = duty_cycle + + # Wait for 2 minutes before checking temperature again + time.sleep(60) diff --git a/services/databases/docker-stack.yml b/services/databases/docker-stack.yml index edd8860..4d639f3 100644 --- a/services/databases/docker-stack.yml +++ b/services/databases/docker-stack.yml @@ -3,7 +3,7 @@ version: "3.7" networks: traefik_public: external: true - net: + internal: driver: overlay attachable: true ipam: @@ -46,7 +46,7 @@ services: volumes: - /media/disk2/postgres:/var/lib/postgresql/data:rw networks: - - net + - internal secrets: - postgres_db - postgres_password @@ -77,6 +77,6 @@ services: volumes: - redis:/data networks: - - net + - internal secrets: - redis_host_password \ No newline at end of file diff --git a/services/jellyfin/docker-stack.yml b/services/jellyfin/docker-stack.yml index 963141d..fa22c16 100644 --- a/services/jellyfin/docker-stack.yml +++ b/services/jellyfin/docker-stack.yml @@ -1,4 +1,4 @@ -version: "3.0" +version: "3.2" volumes: config: @@ -28,6 +28,15 @@ services: hostname: jellyfin env_file: - .env_jellyfin + ports: + - target: 7359 + published: 7359 + protocol: udp + mode: host + - target: 1900 + published: 1900 + protocol: udp + mode: host volumes: - /media/disk2:/disk2:ro - /media/disk2/jellyfin/config:/config diff --git a/services/nextcloud/.env_nextcloud b/services/nextcloud/.env_nextcloud index 50eb1ae..603022d 100644 --- a/services/nextcloud/.env_nextcloud +++ b/services/nextcloud/.env_nextcloud @@ -1,4 +1,4 @@ -POSTGRES_HOST=postgres +POSTGRES_HOST=databases_postgres POSTGRES_DB_FILE=/run/secrets/postgres_db POSTGRES_USER_FILE=/run/secrets/postgres_user POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password @@ -13,5 +13,5 @@ NEXTCLOUD_TRUSTED_DOMAINS=192.168.0.120 OVERWRITECLIURL=https://192.168.0.120/nextcloud OVERWRITEHOST=192.168.0.120 # ----- END HARDCODED NEXTCLOUD IP ------ -REDIS_HOST=redis +REDIS_HOST=databases_redis REDIS_HOST_PASSWORD_FILE=/run/secrets/redis_host_password \ No newline at end of file diff --git a/services/nextcloud/.env_postgres b/services/nextcloud/.env_postgres index 0badf41..2d00c8f 100644 --- a/services/nextcloud/.env_postgres +++ b/services/nextcloud/.env_postgres @@ -1,4 +1,4 @@ -POSTGRES_HOST=postgres +POSTGRES_HOST=databases_postgres POSTGRES_DB_FILE=/run/secrets/postgres_db POSTGRES_USER_FILE=/run/secrets/postgres_user POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password \ No newline at end of file diff --git a/services/nextcloud/docker-stack.yml b/services/nextcloud/docker-stack.yml index f3d3f2f..e8446fa 100644 --- a/services/nextcloud/docker-stack.yml +++ b/services/nextcloud/docker-stack.yml @@ -3,22 +3,22 @@ version: "3.7" networks: traefik_public: external: true - databases_net: + databases_internal: external: true secrets: nextcloud_admin_password: - file: ../databases/secrets/nextcloud_admin_password.txt + file: secrets/nextcloud_admin_password.txt nextcloud_admin_user: - file: ../databases/secrets/nextcloud_admin_user.txt + file: secrets/nextcloud_admin_user.txt postgres_db: - file: ../databases/secrets/postgres_db.txt + file: secrets/postgres_db.txt postgres_password: - file: ../databases/secrets/postgres_password.txt + file: secrets/postgres_password.txt postgres_user: - file: ../databases/secrets/postgres_user.txt + file: secrets/postgres_user.txt redis_host_password: - file: ../databases/secrets/redis_host_password.txt + file: secrets/redis_host_password.txt services: nextcloud: @@ -42,11 +42,8 @@ services: hostname: nextcloud env_file: - .env_nextcloud - depends_on: - - postgres - - redis networks: - - databases_net + - databases_internal - traefik_public volumes: - /media/disk2/nextcloud:/var/www/html diff --git a/services/traefik/docker-stack.yml b/services/traefik/docker-stack.yml index 12a23f2..16cad10 100644 --- a/services/traefik/docker-stack.yml +++ b/services/traefik/docker-stack.yml @@ -23,6 +23,7 @@ services: - "traefik.http.routers.api.tls=true" - "traefik.http.routers.api.service=api@internal" - "traefik.http.services.dummy.loadbalancer.server.port=9999" + hostname: traefikv2 env_file: - .env_traefik ports: diff --git a/tasks/deploy-services.yml b/tasks/deploy-services.yml index 6e8d782..b53edb9 100644 --- a/tasks/deploy-services.yml +++ b/tasks/deploy-services.yml @@ -15,19 +15,19 @@ - name: Synchronize services directories synchronize: - src: "services/{{ item }}" + src: "services/" dest: "{{ services_base_dir_location }}" delete: false recursive: true perms: false - with_items: "{{ services }}" - name: Create traefik_public network docker_stack: state: present name: traefik compose: - - "{{ [services_base_dir_location, 'traefikv2', 'traefik.yml'] | path_join }}" + - "{{ [services_base_dir_location, 'traefik', 'traefik.yml'] | path_join }}" + when: "'traefik' in services" - name: Deploy service stacks docker_stack: diff --git a/tasks/prepare-pi.yml b/tasks/prepare-pi.yml index ad0724c..2e68124 100644 --- a/tasks/prepare-pi.yml +++ b/tasks/prepare-pi.yml @@ -48,17 +48,6 @@ - bluetooth - hciuart - # - name: Disable bluetooth in /boot/firmware/config.txt - # ini_file: - # path: /boot/firmware/config.txt - # section: all - # option: dtoverlay - # value: disable-bt - # mode: '0744' - # backup: yes - # become: yes - # when: disable_bluetooth|bool == true - - name: Disable HDMI Output shell: /usr/bin/tvservice -o become: yes @@ -76,17 +65,7 @@ - name: Reboot Pi after upgrade reboot: post_reboot_delay: 10 - when: reboot_after_pi_preparation|bool == true - become: yes - -- name: Create mount directories - file: - path: "{{ item.value }}" - state: directory - owner: "{{ run_user }}" - group: "{{ run_user }}" - mode: "0644" - loop: "{{ lookup('dict', disk_mount_dirs) }}" + when: reboot_after_pi_preparation|bool == true and ansible_connection != 'local' become: yes - name: Configure disk mounts in /etc/fstab @@ -99,3 +78,25 @@ boot: no loop: "{{ lookup('dict', disk_mount_dirs) }}" become: yes + +- name: Change mount directories ownership + file: + path: "{{ item.value }}" + owner: "{{ run_user }}" + group: "{{ run_user }}" + mode: "0644" + loop: "{{ lookup('dict', disk_mount_dirs) }}" + become: yes + +- name: Add mount disks in /etc/rc.local + copy: + content: | + #!/bin/sh -e + mount /dev/sdb1 /media/disk2 + exit 0 + dest: /etc/rc.local + force: false + group: root + owner: root + mode: 0755 + become: yes diff --git a/tasks/ufw.yml b/tasks/ufw.yml index 741fb04..995f644 100644 --- a/tasks/ufw.yml +++ b/tasks/ufw.yml @@ -10,6 +10,7 @@ - 53 - 7359 - 1900 + - 2377 allow_udp_ports: - 53 - 67 diff --git a/vars.yml b/vars.yml index 0807751..8174667 100644 --- a/vars.yml +++ b/vars.yml @@ -11,8 +11,8 @@ services: - portainer - traefik -# Service's docker stack files copies in this location. It should not end with slash -services_base_dir_location: /opt/services +# Service's docker stack files copies in this location. It should end with slash +services_base_dir_location: /opt/services/ pkgs: - "neofetch"