diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4176d25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +package-lock.json +yarn.lock +debian/changelog +node_modules \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2ff5fe3 --- /dev/null +++ b/README.md @@ -0,0 +1,92 @@ +# SendMail + +> Ce projet m'a permis de savoir comment on package un service sous debian manuellement + +Le système de template de service permet de rendre chaque projet indépendant + +## Prérequies + +Si vous utilisez gmail il faut autoriser l'utilisation de votre login et mot de passe + +cliquez [ici]( https://www.google.com/accounts/DisplayUnlockCaptcha) pour forcer + +## Dependences pour le packaging + +Télécharger le dpkg_changelog [ici](http://deb.regdrasil.com/) en __root__ + +```Shell +echo "deb http://deb.regdrasil.com/apt/debian regdrasil main" > /etc/apt/sources.list.d/regdrasil.com.list + +wget -O - http://deb.regdrasil.com/apt/debian/regdrasil.gpg.key | apt-key add - + +# Installer les MAJ +apt update +``` + +```Shell +apt-get install dh-make dpkg-changelog git +``` + +## Dossier Debian + +Voici le contenu du dossier debian + +```Config +changelog : journal des modifications +compat: +control: métadonnées sur le paquet (dépendances) +dh_install ( *.dirs, *.docs, *.manpages...) +install : +rules : +``` + +## Builder + +- faire un premier commit +- Modifier la version dans `debian/control` +- `ctrl+shift+p` pour creer un tag avec __create:tag__ +- tapez la version exemple __1.0.0__ arrivez au message même chose +- Un nouveau commit avec pour message uniquement le numéro version +- `dpkg-changelog` +- `dpkg-buildpackage -us -uc -b` pour builder un .deb ( /!\ le build n'est pas construit dans le répertoire courant, mais un niveau au dessus ) +- `dpkg-buildpackage -Tclean` pour effacer les traces de build +- `dpkg -i` suivi du .deb créé dans le repertoire parent du projet + +## Execute service + +Initialiser le projet + +``` +# Ce rendre dans sendmailer +/etc/sendmailer + +# Creer un dossier +mkdir projet1 + +# Creer votre fichier `.env` avec l'exemple dans /usr/share/doc/sendmailer +``` + +`.env` + +```Env +MAIL_SENDER=monadresse@email.com +PASS_SENDER=monmotdepasse +MAIL_RECEIVER=desitnataire@outlook.com +``` + +Lancer le service ( recommandé pour une crontab ) + +``` +/bin/systemctl start sendmailer@projet1 +``` + +Regarder le service + +``` +# +## Rappel journalctl : -t pour definir un tag, et -u pour les services comme ici +# + +# rajouté pour l'argument -f pour visualiser en direct +journalctl -u sendmailer@projet1 +``` \ No newline at end of file diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..f11c82a --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +9 \ No newline at end of file diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..b3fb86a --- /dev/null +++ b/debian/control @@ -0,0 +1,11 @@ +Source: sendmailer +Build-Depends: debhelper (>= 9), npm +Section: base +Priority: optional +Maintainer: Kevin grondin +Standards-Version: 1.1.2 + +Package: sendmailer +Architecture: all +Depends: nodejs +Description: Send dog by email without node_modules \ No newline at end of file diff --git a/debian/dirs b/debian/dirs new file mode 100644 index 0000000..28df130 --- /dev/null +++ b/debian/dirs @@ -0,0 +1 @@ +etc/sendmailer \ No newline at end of file diff --git a/debian/docs b/debian/docs new file mode 100644 index 0000000..3c7dfca --- /dev/null +++ b/debian/docs @@ -0,0 +1,2 @@ +README.md +exemple.env \ No newline at end of file diff --git a/debian/install b/debian/install new file mode 100644 index 0000000..1633fd5 --- /dev/null +++ b/debian/install @@ -0,0 +1,3 @@ +sendmailer.js usr/share/sendmailer +package.json usr/share/sendmailer +node_modules usr/share/sendmailer \ No newline at end of file diff --git a/debian/rules b/debian/rules new file mode 100644 index 0000000..ebdbda5 --- /dev/null +++ b/debian/rules @@ -0,0 +1,11 @@ +#!/usr/bin/make -f + +DPKG_EXPORT_BUILDFLAGS = 1 +include /usr/share/dpkg/default.mk + + +%: + dh $@ --with systemd + +override_dh_auto_build: + npm run get:install diff --git a/debian/sendmailer.service b/debian/sendmailer.service new file mode 100644 index 0000000..d1c95d4 --- /dev/null +++ b/debian/sendmailer.service @@ -0,0 +1,13 @@ +[Unit] +Description=sendmailer parent service +After=network.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/bin/true +ExecReload=/bin/true +WorkingDirectory=/etc/sendmailer + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/debian/sendmailer@.service b/debian/sendmailer@.service new file mode 100644 index 0000000..1e22df5 --- /dev/null +++ b/debian/sendmailer@.service @@ -0,0 +1,14 @@ +[Unit] +Description=sendmailer %I +After=syslog.target +After=network.target + +[Service] +Type=simple +Restart=no +WorkingDirectory=/etc/sendmailer/%i +ExecStart=/usr/bin/node /usr/share/sendmailer/sendmailer.js +RuntimeMaxSec=240 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/exemple.env b/exemple.env new file mode 100644 index 0000000..fd57e2d --- /dev/null +++ b/exemple.env @@ -0,0 +1,3 @@ +MAIL_SENDER=monadresse@email.com +PASS_SENDER=monmotdepasse +MAIL_RECEIVER=desitnataire@outlook.com \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..22e1be7 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "sendmail", + "version": "1.0.0", + "description": "Journée du chien", + "main": "sendmailer.js", + "scripts": { + "get:install": "npm install" + }, + "keywords": [], + "author": "kevin grondin", + "license": "ISC", + "dependencies": { + "axios": "^0.21.0", + "dotenv": "^8.2.0", + "nodemailer": "^6.4.17", + "nodemailer-smtp-transport": "^2.7.4" + } +} diff --git a/sendmailer.js b/sendmailer.js new file mode 100644 index 0000000..0cf77fa --- /dev/null +++ b/sendmailer.js @@ -0,0 +1,36 @@ +const smtpTransport = require('nodemailer-smtp-transport'); +const nodemailer = require('nodemailer'); +const axios = require("axios"); +const config = require('dotenv').config(); + +(async () => { + const transporter = nodemailer.createTransport(smtpTransport({ + service: 'gmail', + host: 'smtp.gmail.com', + auth: { + user: process.env.MAIL_SENDER, + pass: process.env.PASS_SENDER + } + })); + + const subject = "Journée du chien" + let { data } = await axios.get("https://api.thedogapi.com/v1/images/search") + let {url} = data[0] + + const message = `Le chien du jour 😁
` + + const mailOptions = { + from: process.env.MAIL_SENDER, + to: process.env.MAIL_RECEIVER, + subject: subject, + html: message + }; + + transporter.sendMail(mailOptions, function(error, info){ + if (error) { + console.error(error); + } else { + console.info('Email sent: ' + info.response); + } + }); +})(); \ No newline at end of file