Skip to content

Commit 57940e1

Browse files
author
thigg
committed
added ansible role to install the gatherer
1 parent 2545a23 commit 57940e1

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

gatherer_role/Readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
You can install the gatherer to a linux host with ansible with this role.
2+
3+
After running this role, write your stations seperated by `;` , one at each line into `/opt/fahrpreis_gatherer/stations.csv`
4+
The gatherer runs as a systemd timer every 3 hours and writes the results to the configured sqlite file

gatherer_role/defaults/main.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
db_file: /mnt/fahrpreise/fahrpreise.sqlite3

gatherer_role/tasks/main.yaml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
- name: Install Node.js 22.x
3+
block:
4+
- name: Import the NodeSource Node.js repository signing key
5+
ansible.builtin.apt_key:
6+
url: https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key
7+
state: present
8+
9+
- name: Add the NodeSource Node.js
10+
ansible.builtin.apt_repository:
11+
repo: "deb https://deb.nodesource.com/node_21.x nodistro main"
12+
state: present
13+
update_cache: yes
14+
15+
- name: Install Node.js
16+
ansible.builtin.apt:
17+
name:
18+
- nodejs
19+
- npm
20+
state: present
21+
update_cache: yes
22+
23+
24+
- name: Create a user for the Node.js application
25+
ansible.builtin.user:
26+
name: fahrpreis_gatherer
27+
system: yes
28+
home: "/opt/fahrpreis_gatherer"
29+
30+
- name: Ensure the application directory exists
31+
ansible.builtin.file:
32+
path: "/opt/fahrpreis_gatherer"
33+
state: directory
34+
owner: fahrpreis_gatherer
35+
group: fahrpreis_gatherer
36+
37+
- name: Copy the Node.js application to the server
38+
ansible.builtin.copy:
39+
src: "{{ role_path }}/../gatherer_hafas/"
40+
dest: "/opt/fahrpreis_gatherer"
41+
owner: fahrpreis_gatherer
42+
group: fahrpreis_gatherer
43+
mode: '0755'
44+
- name: Install Node.js application dependencies with npm
45+
ansible.builtin.command:
46+
cmd: npm install
47+
chdir: /opt/fahrpreis_gatherer
48+
become: yes
49+
become_user: fahrpreis_gatherer
50+
51+
- name: Deploy the run script
52+
ansible.builtin.template:
53+
src: "run.sh.j2"
54+
dest: "/opt/fahrpreis_gatherer/run.sh"
55+
mode: 'o=rx'
56+
57+
- name: Deploy the systemd service template
58+
ansible.builtin.template:
59+
src: "fahrpreis_gatherer.service.j2"
60+
dest: "/etc/systemd/system/fahrpreis_gatherer.service"
61+
62+
- name: Deploy the systemd timer template
63+
ansible.builtin.template:
64+
src: "fahrpreis_gatherer.timer.j2"
65+
dest: "/etc/systemd/system/fahrpreis_gatherer.timer"
66+
67+
- name: Enable and start the timer
68+
ansible.builtin.systemd:
69+
name: fahrpreis_gatherer.timer
70+
enabled: yes
71+
state: started
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Unit]
2+
Description=collect fahrpreise
3+
4+
[Service]
5+
ExecStart=/opt/fahrpreis_gatherer/run.sh
6+
User=fahrpreis_gatherer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=schedule fahrpreis_gatherer
3+
4+
[Timer]
5+
OnCalendar=*-*-* *:00/3:00
6+
Persistent=true
7+
8+
[Install]
9+
WantedBy=timers.target

gatherer_role/templates/run.sh.j2

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
while IFS=';' read -r from to || [ -n "$from" ]; do
3+
echo "connection between $from and $to"
4+
/usr/bin/node /opt/fahrpreis_gatherer/fetchBestPrices.mjs "$from" "$to" 60 {{ db_file }}
5+
done < "/opt/fahrpreis_gatherer/stations.csv"
6+

0 commit comments

Comments
 (0)