Add the photos role
This commit is contained in:
parent
24c4d845ae
commit
786bb5cbdc
10 changed files with 169 additions and 0 deletions
5
fotos.yaml
Normal file
5
fotos.yaml
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
- hosts: fotos
|
||||
roles:
|
||||
- common
|
||||
- photos
|
|
@ -11,3 +11,4 @@ topic bitlair/wifi/+/online out
|
|||
topic bitlair/climate/# out
|
||||
topic bitlair/humidity/+ out
|
||||
topic bitlair/lasercutter/+ out
|
||||
topic bitlair/photos out
|
||||
|
|
3
roles/photos/defaults/main.yaml
Normal file
3
roles/photos/defaults/main.yaml
Normal file
|
@ -0,0 +1,3 @@
|
|||
photos_mqtt_host: mqtt.bitlair.nl
|
||||
photos_mqtt_topic: bitlair/photos
|
||||
photos_path: /opt/wip
|
12
roles/photos/handlers/main.yaml
Normal file
12
roles/photos/handlers/main.yaml
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
- name: restart photo-gallery
|
||||
systemd:
|
||||
name: photo-gallery
|
||||
state: restarted
|
||||
daemon_reload: true
|
||||
|
||||
- name: restart photos2mqtt
|
||||
systemd:
|
||||
name: photos2mqtt
|
||||
state: restarted
|
||||
daemon_reload: true
|
6
roles/photos/tasks/main.yaml
Normal file
6
roles/photos/tasks/main.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
- tags: photos_gallery
|
||||
import_tasks: photo-gallery.yaml
|
||||
|
||||
- tags: photos_mqtt
|
||||
import_tasks: photos2mqtt.yaml
|
24
roles/photos/tasks/photo-gallery.yaml
Normal file
24
roles/photos/tasks/photo-gallery.yaml
Normal file
|
@ -0,0 +1,24 @@
|
|||
---
|
||||
- name: Clone source
|
||||
git:
|
||||
repo: https://github.com/bitlair/photo-gallery.git
|
||||
version: master
|
||||
dest: /opt/photo-gallery
|
||||
accept_hostkey: yes
|
||||
notify: restart photo-gallery
|
||||
|
||||
- name: Install photo-gallery service file
|
||||
template:
|
||||
src: photo-gallery.service
|
||||
dest: /etc/systemd/system/photo-gallery.service
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0644
|
||||
notify: restart photo-gallery
|
||||
|
||||
- name: Start photo-gallery
|
||||
systemd:
|
||||
name: photo-gallery
|
||||
state: started
|
||||
enabled: yes
|
||||
daemon_reload: true
|
34
roles/photos/tasks/photos2mqtt.yaml
Normal file
34
roles/photos/tasks/photos2mqtt.yaml
Normal file
|
@ -0,0 +1,34 @@
|
|||
---
|
||||
- name: Install dependencies
|
||||
apt:
|
||||
name:
|
||||
- make
|
||||
- liblinux-inotify2-perl
|
||||
|
||||
- name: Install mqtt-simple
|
||||
command: cpan Net::MQTT::Simple
|
||||
|
||||
- name: Install photos2mqtt
|
||||
template:
|
||||
src: photos2mqtt.pl
|
||||
dest: /opt/photos2mqtt.pl
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0755
|
||||
notify: restart photos2mqtt
|
||||
|
||||
- name: Install photos2mqtt service file
|
||||
template:
|
||||
src: photos2mqtt.service
|
||||
dest: /etc/systemd/system/photos2mqtt.service
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0644
|
||||
notify: restart photos2mqtt
|
||||
|
||||
- name: Start photos2mqtt
|
||||
systemd:
|
||||
name: photos2mqtt
|
||||
state: started
|
||||
enabled: yes
|
||||
daemon_reload: true
|
13
roles/photos/templates/photo-gallery.service
Normal file
13
roles/photos/templates/photo-gallery.service
Normal file
|
@ -0,0 +1,13 @@
|
|||
# Managed by Ansible
|
||||
|
||||
[Unit]
|
||||
Description=Gallery service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/bin/node /opt/photo-gallery/server.js
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
55
roles/photos/templates/photos2mqtt.pl
Normal file
55
roles/photos/templates/photos2mqtt.pl
Normal file
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/perl -w
|
||||
|
||||
# Managed by Ansible
|
||||
|
||||
use strict;
|
||||
use Net::MQTT::Simple;
|
||||
use Linux::Inotify2;
|
||||
use POSIX qw(strftime);
|
||||
use Time::HiRes qw(sleep time);
|
||||
|
||||
my $path = "{{ photos_path }}";
|
||||
my $mqtt = Net::MQTT::Simple->new('{{ photos_mqtt_host }}');
|
||||
|
||||
my $inotify = new Linux::Inotify2 or die $!;
|
||||
$inotify->blocking(0);
|
||||
|
||||
sub today {
|
||||
return strftime "%Y%m%d", localtime;
|
||||
}
|
||||
|
||||
sub watch_daydir {
|
||||
my ($dn) = @_;
|
||||
|
||||
print "Watching $dn\n";
|
||||
|
||||
$inotify->watch($dn, IN_CREATE, sub {
|
||||
my $event = shift;
|
||||
$inotify->watch($event->fullname, IN_CLOSE_WRITE, sub {
|
||||
my $event = shift;
|
||||
my $fn = $event->fullname;
|
||||
unless ($fn =~ m[^\.|/\.]) { # skip hidden files
|
||||
print "New file written: $fn\n";
|
||||
$mqtt->retain("{{ photos_mqtt_topic }}", $fn =~ s[^$path/][]r);
|
||||
}
|
||||
$event->w->cancel;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
print "Watching $path\n";
|
||||
$inotify->watch($path, IN_CREATE, sub {
|
||||
my $event = shift;
|
||||
my $dn = $event->fullname;
|
||||
-d $dn or next;
|
||||
|
||||
watch_daydir($dn);
|
||||
});
|
||||
|
||||
watch_daydir("$path/" . today());
|
||||
|
||||
while (1) {
|
||||
$mqtt->tick(.05);
|
||||
$inotify->poll;
|
||||
sleep .1;
|
||||
}
|
16
roles/photos/templates/photos2mqtt.service
Normal file
16
roles/photos/templates/photos2mqtt.service
Normal file
|
@ -0,0 +1,16 @@
|
|||
# Managed by Ansible
|
||||
|
||||
[Unit]
|
||||
Description=Photos to MQTT
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
Restart=on-failure
|
||||
RestartSec=10s
|
||||
ExecStart=/usr/bin/perl /opt/photos2mqtt.pl
|
||||
User=wip
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
Loading…
Add table
Reference in a new issue