Commit 48e7aa17 authored by Thomas Bella's avatar Thomas Bella

Add config file and debian package

parent 7479e5c2
Pipeline #1062 passed with stages
in 1 minute and 57 seconds
......@@ -3,6 +3,7 @@ image: thomas2500/golang-helper
stages:
- test
- build
- pack
- deploy
# Test formatting, suspicious constructs and predefined tests. Creates code coverage statistics and saves a HTML coverage report to coverage/
......@@ -30,6 +31,52 @@ build:
paths:
- build/
pack-amd64-bionic:
image: ubuntu:bionic
stage: pack
script:
- apt-get -yqq update
- apt-get -yqq install dpkg git
# Determine new version and set it
- export PKGNAME=$(grep "Package:" source/DEBIAN/control | cut -d " " -f2)
- export MAINVERSION=$(grep "Version:" source/DEBIAN/control | cut -d " " -f2)
- export DATE=$(date +%Y%m%d%H%M)
- export SHORTVERS=$(echo $CI_COMMIT_SHA | cut -c 1-8)
- export NEWVERSION="${MAINVERSION}-${DATE}+${SHORTVERS}"
- sed -i -e "/^Version/c\Version\x3a ${NEWVERSION}" source/DEBIAN/control
- sed -i -e "/^Installed-Size/c\Installed-Size\x3a $(du -s --exclude=DEBIAN source/ | cut -f 1)" source/DEBIAN/control
# Generate changelog
- export LASTLOG=$(git log -1 --pretty=%B | tr -d '\n')
- echo "${PKGNAME} (${NEWVERSION}) stable; urgency=low" > source/DEBIAN/changelog
- echo "" >> source/DEBIAN/changelog
- echo " * Automatically generated with GitLab CI - ${LASTLOG}" >> source/DEBIAN/changelog
- echo "" >> source/DEBIAN/changelog
- echo " -- Thomas Bella <thomas+deb@bella.network> $(date '+%a, %d %b %Y %H:%M:%S %z')" >> source/DEBIAN/changelog
- cat source/DEBIAN/changelog
# Move files
- cp build/utelebot-linux-amd64 source/usr/bin/utelebot
- rm source/usr/bin/.gitkeep
# Set permissions
- chmod 755 source/DEBIAN -R
- chmod +x source/DEBIAN/postinst source/DEBIAN/postrm source/DEBIAN/prerm
- chmod +x source/usr/bin/utelebot
- chmod 600 source/etc/utelebot/config.json
- chmod 644 source/lib/systemd/system/utelebot.service
# Create checksums
- cd source/
- find . -type f ! -regex '.*.hg.*' ! -regex '.*?debian-binary.*' ! -regex '.*?DEBIAN.*' -printf '%P ' | xargs md5sum > DEBIAN/md5sums
- cd ..
# Create package
- dpkg-deb -z9 -Zxz -b source/ "${PKGNAME}_${NEWVERSION}_all.deb"
- mkdir -p build/
- mv *.deb build/
dependencies:
- build
artifacts:
expire_in: 1 week
paths:
- "build/*.deb"
# Deploy GitLab Pages (everything in public/ will be made available)
pages:
stage: deploy
......@@ -49,14 +96,18 @@ pages:
only:
- master
.deploy:
deploy:
image: thomas2500/utility-image
stage: deploy
dependencies:
- pack-amd64-bionic
script:
# Prepare SSH for upload
- mkdir -p ~/.ssh
- echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519
- chmod 700 ~/.ssh/id_ed25519
- echo -e "Host *\n\tStrictHostKeyChecking no\n\tIdentityFile ~/.ssh/id_ed25519\n\tVerifyHostKeyDNS yes\n\n" > ~/.ssh/config
# Upload builds
#- rsync -vuzrP --delete -e 'ssh -i ~/.ssh/id_ed25519' $CI_PROJECT_DIR/build/* release@10.20.23.252:/var/vhosts/bella.ml/release
# Upload build and install it
- rsync -vuzrP --delete -e 'ssh -i ~/.ssh/id_ed25519' $CI_PROJECT_DIR/build/*.deb root@10.20.16.85:/root/pkginst
- ssh -i ~/.ssh/id_ed25519 root@10.20.16.85 dpkg -i /root/pkginst/*
- ssh -i ~/.ssh/id_ed25519 root@10.20.16.85 rm /root/pkginst/*
package main
type mainConfig struct {
TelegramBotAPIKey string
WebhookTarget string
Listen string
}
......@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
......@@ -15,7 +16,26 @@ var (
)
func main() {
bot, err := tgbotapi.NewBotAPI("551335021:AAF2qBhv55pFmd6hYv2u12HEIfDNXyTkqnk")
// Read configuration from config file
var config mainConfig
// Load stats from disk to memory
configFile, err := ioutil.ReadFile(`/etc/utelebot/config.json`)
if err != nil {
log.Println(err)
return
}
if err := json.Unmarshal(configFile, &config); err != nil {
log.Println(err)
return
}
// Check if config is valid
if config.WebhookTarget == "https://example.com/" {
log.Fatal("Please configure this bot in /etc/utelebot/config.json")
}
// Set API key
bot, err := tgbotapi.NewBotAPI(config.TelegramBotAPIKey)
if err != nil {
log.Fatal(err)
}
......@@ -23,7 +43,7 @@ func main() {
log.Printf("Authorized on account %s", bot.Self.UserName)
// Set webhook target on program start
_, err = bot.SetWebhook(tgbotapi.NewWebhook("https://tgbot.bella.ml/utelebot/"))
_, err = bot.SetWebhook(tgbotapi.NewWebhook(config.WebhookTarget))
if err != nil {
log.Fatal(err)
}
......@@ -40,7 +60,7 @@ func main() {
// Start http server and listen for incoming data
updates := bot.ListenForWebhook("/")
go func() {
err := http.ListenAndServe("0.0.0.0:5505", nil)
err := http.ListenAndServe(config.Listen, nil)
if err != nil {
log.Fatal(err)
}
......
......@@ -85,14 +85,13 @@ func init() {
}
// Load stats from disk to memory
statsFile, err := ioutil.ReadFile(`stats.json`)
statsFile, err := ioutil.ReadFile(`/var/lib/utelebot/stats.json`)
if err != nil {
fmt.Println(err)
return
}
if err := json.Unmarshal(statsFile, &MessageData.Chat); err != nil {
fmt.Println(err)
return
} else {
if err := json.Unmarshal(statsFile, &MessageData.Chat); err != nil {
fmt.Println(err)
}
}
// Save data on program exit
......@@ -115,7 +114,7 @@ func init() {
// WriteData writes the current data to stats.json
func WriteData() {
stats, _ := json.Marshal(MessageData.GetAll())
err := ioutil.WriteFile(`stats.json`, stats, 0644)
err := ioutil.WriteFile(`/var/lib/utelebot/stats.json`, stats, 0644)
if err != nil {
fmt.Println(err)
}
......
utelebot
----------------------------
This is example text
-- Thomas Bella <thomas+deb@bella.network> Wed Apr 11 16:22:00 CEST 2018
utelebot (1.0) stable; urgency=low
* Initial Release.
-- Thomas Bella <thomas+deb@bella.network> Sat Jan 12 02:47:00 CEST 2018
/etc/utelebot/config.json
/var/lib/utelebot/stats.json
Package: utelebot
Version: 1.0
Section: base
Priority: optional
Architecture: all
Depends: coreutils, systemd
Maintainer: Thomas Bella <thomas+deb@bella.network>
Installed-Size: 0
Homepage: https://git.bella.network/Thomas2500/utelebot
Description: uTeleBot
Telegram Bot
#!/bin/sh
# postinst script
# Bei Software, die für das konkrete System konfiguriert werden muss, ist es typischerweise ein solches Skript, das (nach dem Transfer der Dateien auf die Festplatte) die Konfiguration des Pakets foo abschließt.
# Oft erfragen »postinst«-Skripte Eingaben vom Benutzer und/oder warnen ihn, dass er das Paket bei Bedarf neu konfigurieren muss, wenn er die Standardwerte akzeptiert. Viele »postinst«-Skripte führen nach der Installation/Aktualisierung die für das Starten bzw. Neustarten der Dienste nötigen Befehle aus.
#
if [ "$1" = "triggered" ]; then
invoke-rc.d utelebot.service restart
fi
deb-systemd-helper unmask utelebot.service >/dev/null || true
if deb-systemd-helper --quiet was-enabled utelebot.service; then
# Enables the unit on first installation, creates new
# symlinks on upgrades if the unit file has changed.
deb-systemd-helper enable utelebot.service >/dev/null || true
else
# Update the statefile to add new symlinks (if any), which need to be
# cleaned up on purge. Also remove old symlinks.
deb-systemd-helper update-state utelebot.service >/dev/null || true
fi
if deb-systemd-helper --quiet is-enabled utelebot.service >/dev/null; then
systemctl restart utelebot.service >/dev/null || true
fi
exit 0
#!/bin/sh
# postrm script
# Typischerweise modifiziert dieses Skript Links oder andere Dateien, die zu foo gehören und/oder entfernt Dateien, die vom Paket erzeugt wurden. (Siehe auch Was ist ein virtuelles Paket?, Abschnitt 7.8.)
set -e
# _manupdate() {
# mandb -f /usr/share/man/man1/them-sslutils.1.gz
#}
# summary of how this script can be called:
# * <postrm> `remove'
# * <postrm> `purge'
# * <old-postrm> `upgrade' <new-version>
# * <new-postrm> `failed-upgrade' <old-version>
# * <new-postrm> `abort-install'
# * <new-postrm> `abort-install' <old-version>
# * <new-postrm> `abort-upgrade' <old-version>
# * <disappearer's-postrm> `disappear' <overwriter>
# <overwriter-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
# Reload systemd
systemctl daemon-reload || :
if [ "$1" = "remove" ] ; then
rm -f /lib/systemd/system/utelebot.service
fi
if [ "$1" = "purge" ] ; then
rm -rf /etc/bemo/agent.yaml
# Only remove folder if it is empty
if [ -z "$(ls -A /etc/bemo)" ]; then
rm -rf /etc/bemo
fi
rm -rf /var/lib/bemo
fi
exit 0
#!/bin/sh
# postrm script
# Dieses Skript stoppt üblicherweise alle Dienste, die mit einem Paket verknüpft sind.
# Es wird ausgeführt, bevor die Dateien des Paketes gelöscht werden.
# see: dh_installdeb(1)
set -e
if [ "$1" = "remove" ] || [ "$1" = "deconfigure" ] ; then
systemctl stop utelebot.service
systemctl disable utelebot.service
fi
exit 0
{
"TelegramBotAPIKey": "551335021:AAF2qBhv55pFm...2HEIfDNXyTkqnk",
"WebhookTarget": "https://example.com/",
"Listen": "0.0.0.0:5505"
}
[Unit]
Description=uTeleBot
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/utelebot
Restart=always
RestartSec=10s
[Install]
WantedBy=multi-user.target
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment