first commit

This commit is contained in:
2024-01-20 22:28:11 +00:00
commit 98cb157593
21 changed files with 513 additions and 0 deletions

8
ansible/all.yml Normal file
View File

@@ -0,0 +1,8 @@
---
- hosts:
- all
roles:
- db
- web
become: true
gather_facts: false

9
ansible/database.yml Normal file
View File

@@ -0,0 +1,9 @@
---
- name: install and configure database
hosts:
- all
roles:
- db
become: true
gather_facts: false

View File

@@ -0,0 +1,12 @@
INSERT INTO `section` (`id`, `name`) VALUES
(1, 'Flachwitze'),
(2, 'Schwarzer Humor');
INSERT INTO `joke` (`id`, `section_idfs`, `text`, `rating`, `creation_date`) VALUES
(1, 1, 'Kunde: \"Ich möchte Ihren Chef sprechen!\"\r\nSekretärin: \"Geht leider nicht, er ist nicht da!\"\r\nKunde: \"Ich hab ihn doch durchs Fenster gesehen!\"\r\nSekretärin: \"Er Sie auch!\"', 5, '2014-01-08 21:39:40'),
(2, 1, 'Der Verwaltungsrat zum CEO:\r\n\"Na, wie macht sich denn der neue Buchhalter?\"\r\nCEO: \"Toll, dieser Mann!\"\r\nVerwaltungsrat: \"Was kann er denn so besonderes?\"\r\nCEO: \"Er ist gelernter Friseur, er kann frisieren!\"', 3, '2014-01-08 21:42:41'),
(3, 1, 'Chef: \"Müller, Sie sind das beste Pferd in meinem Stall!\"\r\nMüller: \"Wirklich, Chef?\"\r\nChef: \"Ja, Sie machen den meisten Mist!\"', 5, '2014-01-08 21:43:20'),
(6, 2, 'Was steht auf dem Grabstein eines Mathematikers?\r\n\"Damit hat er nicht gerechnet.\"', 3, '2021-04-06 12:47:17');

View File

@@ -0,0 +1,26 @@
DROP TABLE IF EXISTS section;
CREATE TABLE `section` (
`id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
DROP TABLE IF EXISTS joke;
CREATE TABLE `joke` (
`id` int(11) NOT NULL,
`section_idfs` int(11) NOT NULL,
`text` text COLLATE utf8_bin NOT NULL,
`rating` int(11) NOT NULL,
`creation_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Indizes für die Tabelle `joke`
--
ALTER TABLE `joke` ADD PRIMARY KEY (`id`);
ALTER TABLE `section` ADD PRIMARY KEY (`id`);
ALTER TABLE `joke` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
ALTER TABLE `section` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

View File

@@ -0,0 +1,3 @@
---
- name: restart mariadb
service: name=mysql state=restarted

View File

@@ -0,0 +1,52 @@
---
- name: update
apt: update_cache=yes
ignore_errors: yes
- name: install mariadb server because jokesdb has localhost hard coded
apt: name=mariadb-server state=latest
notify: restart mariadb
- name: install mariadb client
apt: name=mariadb-client state=latest
- name: install python3-pymysql
apt: name=python3-pymysql state=latest
- name: create a new database with name "{{ db_name }}"
community.mysql.mysql_db:
name: "{{ db_name }}"
state: present
login_unix_socket: /run/mysqld/mysqld.sock
- name: create database user with all database privileges
community.mysql.mysql_user:
name: "{{ db_username }}"
password: "{{ db_password }}"
priv: "{{ db_name }}.*:ALL"
state: present
login_unix_socket: /run/mysqld/mysqld.sock
- name: copy database schema file
copy:
src: files/schema.sql
dest: /tmp
- name: copy database data file
copy:
src: files/data.sql
dest: /tmp
- name: import db schema
community.mysql.mysql_db:
state: import
name: "{{ db_name }}"
target: /tmp/schema.sql
login_unix_socket: /run/mysqld/mysqld.sock
- name: import db data
community.mysql.mysql_db:
state: import
name: "{{ db_name }}"
target: /tmp/data.sql
login_unix_socket: /run/mysqld/mysqld.sock

View File

@@ -0,0 +1,12 @@
INSERT INTO `section` (`id`, `name`) VALUES
(1, 'Flachwitze'),
(2, 'Schwarzer Humor');
INSERT INTO `joke` (`id`, `section_idfs`, `text`, `rating`, `creation_date`) VALUES
(1, 1, 'Kunde: \"Ich möchte Ihren Chef sprechen!\"\r\nSekretärin: \"Geht leider nicht, er ist nicht da!\"\r\nKunde: \"Ich hab ihn doch durchs Fenster gesehen!\"\r\nSekretärin: \"Er Sie auch!\"', 5, '2014-01-08 21:39:40'),
(2, 1, 'Der Verwaltungsrat zum CEO:\r\n\"Na, wie macht sich denn der neue Buchhalter?\"\r\nCEO: \"Toll, dieser Mann!\"\r\nVerwaltungsrat: \"Was kann er denn so besonderes?\"\r\nCEO: \"Er ist gelernter Friseur, er kann frisieren!\"', 3, '2014-01-08 21:42:41'),
(3, 1, 'Chef: \"Müller, Sie sind das beste Pferd in meinem Stall!\"\r\nMüller: \"Wirklich, Chef?\"\r\nChef: \"Ja, Sie machen den meisten Mist!\"', 5, '2014-01-08 21:43:20'),
(6, 2, 'Was steht auf dem Grabstein eines Mathematikers?\r\n\"Damit hat er nicht gerechnet.\"', 3, '2021-04-06 12:47:17');

View File

@@ -0,0 +1,26 @@
DROP TABLE IF EXISTS section;
CREATE TABLE `section` (
`id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
DROP TABLE IF EXISTS joke;
CREATE TABLE `joke` (
`id` int(11) NOT NULL,
`section_idfs` int(11) NOT NULL,
`text` text COLLATE utf8_bin NOT NULL,
`rating` int(11) NOT NULL,
`creation_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Indizes für die Tabelle `joke`
--
ALTER TABLE `joke` ADD PRIMARY KEY (`id`);
ALTER TABLE `section` ADD PRIMARY KEY (`id`);
ALTER TABLE `joke` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
ALTER TABLE `section` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

View File

@@ -0,0 +1,3 @@
---
- name: restart mariadb
service: name=mysql state=restarted

View File

@@ -0,0 +1,72 @@
---
- name: install jre
apt: name=openjdk-21-jre-headless state=latest
# the next steps we can skip here because we run the db-role first
# thanks to the java app wich has the db uri hard coded
#
#- name: install mariadb-client
# apt: name=mariadb-client state=latest
#
#- name: install PyMySQL
# apt: name=python3-pymysql state=latest
#
#- name: copy database schema file
# copy:
# src: files/schema.sql
# dest: /tmp
#
#- name: copy database data file
# copy:
# src: files/data.sql
# dest: /tmp
- name: add user for jokesdb
ansible.builtin.user:
name: jokesdb
comment: user to run application jokesdb
create_home: True
home: /var/jokesdb
- name: that destination path exists
file:
path: /var/jokesdb/bin
state: directory
- name: copy architecture-refcard-03-0.0.1-SNAPSHOT.jar
copy:
src: files/architecture-refcard-03-0.0.1-SNAPSHOT.jar
dest: /var/jokesdb/bin
# we run this only because we want to show that it works
- name: import db schema
community.mysql.mysql_db:
state: import
name: jokedb
login_host: "{{ db_address }}"
login_user: "{{ db_username }}"
login_password: "{{ db_password }}"
target: /tmp/schema.sql
- name: import db data
community.mysql.mysql_db:
state: import
name: "{{ db_name }}"
login_host: "{{ db_address }}"
login_user: "{{ db_username }}"
login_password: "{{ db_password }}"
target: /tmp/data.sql
- name: create default file for application
ansible.builtin.template:
src: templates/jokesdb.j2
dest: /etc/default/jokesdb
- name: create service file to start the application
ansible.builtin.template:
src: templates/jokesdb_service.j2
dest: /etc/systemd/system/jokesdb.service
- name: start jokesdb
systemd: state=started name=jokesdb daemon_reload=yes

View File

@@ -0,0 +1,5 @@
DDB_USERNAME="{{ db_username }}"
DDB_PASSWORD="{{ db_password }}"
DDB_URI="mysql://{{ db_address }}:{{ db_port }}/{{ db_name }}"
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"
JAR_FILE="/var/jokesdb/bin/architecture-refcard-03-0.0.1-SNAPSHOT.jar"

View File

@@ -0,0 +1,19 @@
[Unit]
Description=JokesDB
After=syslog.target network.target
[Service]
EnvironmentFile=-/etc/default/jokesdb
SuccessExitStatus=143
User=jokesdb
Group=jokesdb
Type=simple
WorkingDirectory=/var/jokesdb
ExecStart=/usr/bin/env ${JAVA_HOME}/bin/java -DDB_USERNAME=${DDB_USERNAME} -DDB_PASSWORD=${DDB_PASSWORD} -jar ${JAR_FILE}
ExecStop=/bin/kill -15 $MAINPID
[Install]
WantedBy=multi-user.target

8
ansible/web.yml Normal file
View File

@@ -0,0 +1,8 @@
---
- name: install and configure database server and start application
hosts:
- all
roles:
- web
become: true
gather_facts: false