initial commit

This commit is contained in:
2025-08-19 11:40:26 -07:00
commit c34b325cf0
18 changed files with 1803 additions and 0 deletions

42
tasks/main.yaml Normal file
View File

@ -0,0 +1,42 @@
---
# - name: Video Capture - Configure Owncast
# include_tasks: owncast.yaml
- name: Video Capture - Check arch if needed
when: refresh_special | bool
block:
- name: Video Capture - Check CPU Arch
shell: "dpkg --print-architecture"
register: cpu_architecture_output
- name: Video Capture - Set cpu_architecture variable
set_fact:
cpu_architecture: "{{ cpu_architecture_output.stdout_lines[0] }}"
- name: Video Capture - Install Packages
when: not refresh_special | bool
apt:
name:
- "{{ streamer_packages_items }}"
state: present
loop: "{{ streamer_packages }}"
loop_control:
loop_var: streamer_packages_items
- name: Video Capture - SD Card Handler
when: mount_sd | bool
include_tasks: sd_handler.yaml
- name: Video Capture - Configure MediaMTX
include_tasks: mediamtx.yaml
- name: Video Capture - Configure Streaming
include_tasks: streamer.yaml
#- name: Video Capture - Configure service control
# include_tasks: service_control.yaml
...

82
tasks/mediamtx.yaml Normal file
View File

@ -0,0 +1,82 @@
---
# mediamtx might automatically make video files
# newest release URL:
# curl -sL https://api.github.com/repos/bluenviron/mediamtx/releases/latest | \
# grep browser_download_url | grep linux_amd64 | cut -d\" -f 4
- name: MediaMTX - stop mediamtx_service if running
systemd:
name: mediamtx_service.service
state: stopped
ignore_errors: yes
# Create service Folders
- name: MediaMTX - create mediamtx_working_folder folder
file:
path: "{{ mediamtx_working_folder }}"
state: directory
mode: '0755'
owner: root
group: root
- name: MediaMTX - check for arm
when: '"arm" in cpu_architecture'
set_fact:
mediamtx_version: "arm64"
- name: MediaMTX - get current release URL
shell: |
curl -sL https://api.github.com/repos/bluenviron/mediamtx/releases/latest | \
grep browser_download_url | grep linux_{{ mediamtx_version }} | cut -d\" -f 4
register: mediamtx_latest_url
- debug:
msg: "URL To Download: {{ mediamtx_latest_url.stdout_lines[0] }}"
- name: MediaMTX - get current release archive
shell: "curl -s -o {{ mediamtx_working_folder }}/mediamtx.tar.gz -L {{ mediamtx_latest_url.stdout_lines[0] }}"
- name: MediaMTX - extract archive
unarchive:
# src: "/var/jenkins_home/ansible-files/programs/mediamtx_v1.14.0_linux_amd64.tar.gz"
src: "{{ mediamtx_working_folder }}/mediamtx.tar.gz"
dest: "{{ mediamtx_working_folder }}"
mode: '0755'
remote_src: yes
- name: MediaMTX - update configs
lineinfile:
path: "{{ mediamtx_working_folder }}/mediamtx.yml"
search_string: "{{ mediamtx_configs_item.search_string }}"
line: "{{ mediamtx_configs_item.line }}"
loop: "{{ mediamtx_configs }}"
loop_control:
loop_var: mediamtx_configs_item
# - name: MediaMTX - config - enable recording
# lineinfile:
# path: "{{ mediamtx_working_folder }}/mediamtx.yml"
# search_string: 'record'
# line: ' record: yes'
#
- name: MediaMTX - create service file
template:
src: mediamtx_service.service.j2
dest: "/etc/systemd/system/mediamtx_service.service"
mode: 0644
# daemon reload
- name: MediaMTX - daemon reload
systemd:
daemon_reload: yes
# Enable and start
- name: MediaMTX - enable and start mediamtx_service
systemd:
name: mediamtx_service.service
state: started
enabled: yes
...

102
tasks/sd_handler.yaml Normal file
View File

@ -0,0 +1,102 @@
---
# when ran, this will look for an SD card
# optionally format it
# map it to working_storage
# can only run on arm64
# '"arm64" in cpu_architecture'
- name: Video Capture - Check for SD
shell: "df"
register: df_check_output
- name: Video Capture - Set sd_unmounted
when: working_storage in df_check_output.stdout
set_fact:
sd_unmounted: false
- name: Video Capture - Set arm_arch
when: '"arm64" in cpu_architecture'
set_fact:
arm_arch: true
- name: SD Handler - Checks have passed
when: sd_unmounted and arm_arch | bool
block:
- name: SD Handler - format SD card
when: format_sd | bool
block:
- name: dummy task
debug:
msg: "nothing to see here, move along"
- name: SD Handler - find SD card
block:
- name: SD Handler - get boot device
shell: "blkid | grep '\"boot\"' | cut -d: -f 1 | cut -d/ -f 3 | cut -dp -f 1"
register: boot_device
- name: SD Handler - get sd card device
shell: "lsblk -o NAME,SIZE --nodeps | grep -v -e loop -e {{ boot_device.stdout_lines[0] }} -e NAME | cut -d ' ' -f 1"
register: sd_card_device
- name: SD Handler - get sd card uuid
shell: "blkid | grep {{ sd_card_device.stdout_lines[0] }} | awk '{for (i=1; i<=NF; i++) print $i}' | grep UUID | grep -v PART | cut -d '\"' -f 2"
register: sd_card_uuid_output
- name: SD Handler - set uuid variable
set_fact:
sd_card_uuid: "{{ sd_card_uuid_output.stdout_lines[0] }}"
- debug:
msg: "UUID Found: {{ sd_card_uuid }}"
- name: SD Handler - mount sd card
block:
- name: SD Handler - check folder
file:
path: "{{ working_storage }}"
state: directory
mode: '0755'
owner: root
group: root
- name: SD Handler - Generate fstab entry
set_fact:
fstab_line_sd: "UUID={{ sd_card_uuid }} {{ working_storage }} ext4 errors=remount-ro 0 1"
- name: SD Handler - add fstab entry
lineinfile:
path: "/etc/fstab"
search_string: "{{ sd_card_uuid }}"
line: "{{ fstab_line_sd }}"
- debug:
msg: |
fstab entry:
{{ fstab_line_sd }}
- name: SD Handler - daemon reload
systemd:
daemon_reload: yes
- name: SD Handler - Mount it
shell: mount -a
- name: SD Handler - validate this
block:
- name: SD Handler - check for new mount point
shell: "df -h | grep -e Size -e {{ working_storage }}"
register: sd_test_output
- debug:
msg: "{{ sd_test_output.stdout_lines }}"
...

139
tasks/service_control.yaml Normal file
View File

@ -0,0 +1,139 @@
---
###############################################
# lifted directly from my carputer playbook
###############################################
###############################################
# This part sets up python serice control api
###############################################
- name: service control api
block:
# Stop service
- name: video_capture - service_control api - stop service api
systemd:
name: service_control.service
state: stopped
ignore_errors: yes
# Create API Folder
- name: video_capture - service_control api - create api folder
file:
path: "{{ service_control_folder }}"
state: directory
mode: '0755'
# Copy API Code
- name: video_capture - service_control api - copy api code
template:
src: app-service.py.j2
dest: "{{ service_control_folder }}/app.py"
mode: 0644
# Create service_control api service
- name: video_capture - service_control api - create requirement file
copy:
dest: "{{ service_control_folder }}/requirements.txt"
content: |
Flask==2.1.0
pytz
requests
lxml
Werkzeug==2.0
mode: 0644
# build venv
- name: video_capture - service_control api - build venv
pip:
virtualenv: "{{ service_control_folder }}/venv"
requirements: "{{ service_control_folder }}/requirements.txt"
virtualenv_command: python3 -m venv
state: present
# Create service_control api service
- name: video_capture - service_control api - create service file
# vars:
template:
src: service_control.service.j2
dest: /etc/systemd/system/service_control.service
mode: 0644
# daemon reload
- name: video_capture - service_control api - daemon reload
systemd:
daemon_reload: yes
# Enable and start
- name: video_capture - service_control api - enable and start service api
systemd:
name: service_control.service
state: started
enabled: yes
###############################################
# This part sets up serice control website
###############################################
- name: service control web interface
block:
- name: set docker folder variable
set_fact:
service_control_web_folder: "{{ service_control_folder }}/web"
# Create docker Folder
- name: service_control_website - create service_control_web_folder folder
file:
path: "{{ service_control_web_folder }}"
state: directory
mode: '0755'
owner: root
group: root
- name: service_control_website - copy files for docker container
copy:
src: "service_control_api/website/"
dest: "{{ service_control_web_folder }}/html"
mode: 0755
owner: root
group: root
# - name: service_control_website - template index.php
# template:
# src: index-service_control.php.j2
# dest: "{{ service_control_web_folder }}/html/index.php"
# mode: 0644
###############################################
# Start service_control_website
###############################################
# https://unix.stackexchange.com/questions/265704/start-stop-a-systemd-service-at-specific-times
# i can create several conflicting services with various timeouts
- name: start service_control_website
block:
- name: set container variables
set_fact:
container_name: "service_control_website"
container_http_port: "8081"
- name: service_control_website - template config
template:
src: docker-compose-php.yaml.j2
dest: "{{ service_control_web_folder }}/docker-compose.yaml"
mode: 0644
- name: "service_control_website - Start container at 0.0.0.0:{{ container_http_port }}"
shell: "docker-compose -f {{ service_control_web_folder }}/docker-compose.yaml up -d"
register: docker_output
- debug: |
msg="{{ docker_output.stdout_lines }}"
msg="{{ docker_output.stderr_lines }}"
...

72
tasks/streamer.yaml Normal file
View File

@ -0,0 +1,72 @@
---
# video stream with ustreamer
# audio stream probably just a device
# looks like ffmpeg can do it
# Create service Folder
- name: video_capture - streamer - create streaming_working_folder folder
file:
path: "{{ streaming_working_folder }}"
state: directory
mode: '0755'
owner: root
group: root
# this service shouldn't stay running
- name: video_capture - streamer - stop stream_service if running
systemd:
name: stream_service.service
state: stopped
ignore_errors: yes
# gonna try to automatically find the audio info
# the card is MS201
- name: video_capture - get card ID
shell: "arecord -l | grep {{ capture_device_ID_string }} | cut -d: -f1 | rev | cut -b 1"
register: sound_ID_0
- name: video_capture - get device ID
shell: "arecord -l | grep {{ capture_device_ID_string }} | cut -d: -f2 | rev | cut -b 1"
register: sound_ID_1
- name: video_capture - set audio_device
set_fact:
audio_device: "hw:{{ sound_ID_0.stdout_lines[0] }},{{ sound_ID_1.stdout_lines[0] }}"
# same with video, the lsusb ID is 534d:0021
- name: video_capture - get video device
shell: "v4l2-ctl --list-devices -z {{ lsusb_device_ID }}| grep video | head -n 1"
register: video_ID_0
- name: video_capture - set video_device
set_fact:
video_device: "{{ video_ID_0.stdout_lines[0] }}"
- name: video_capture - show results
debug:
msg: |
Audio Device: {{ audio_device }}
Video Device: {{ video_device }}
- name: video_capture - streamer - copy service script
template:
src: stream_service.sh.j2
dest: "{{ streaming_working_folder }}/stream_service.sh"
mode: 0755
- name: video_capture - streamer - create service file
template:
src: stream_service.service.j2
dest: /etc/systemd/system/stream_service.service
mode: 0644
# daemon reload
- name: video_capture - streamer - daemon reload
systemd:
daemon_reload: yes
...