Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build deb | service #15

Merged
merged 24 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/markdownlint/markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Markdownlint configuration
# refet to the documentation for details:
# - [Linter for markdown](https://github.com/avto-dev/markdown-lint)
# - [Rules configuration](https://github.com/DavidAnson/markdownlint/blob/main/schema/.markdownlint.yaml)
# - [github](https://github.com/markdownlint/markdownlint?tab=readme-ov-file)
# Default state for all rules
default: true

# Path to configuration file to extend
extends: null

# MD013/line-length : Line length : https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md013.md
MD013:
# Number of characters
line_length: 160
# Number of characters for headings
heading_line_length: 80
# Number of characters for code blocks
code_block_line_length: 160
# Include code blocks
code_blocks: true
# Include tables
tables: true
# Include headings
headings: true
# Strict length checking
strict: false
# Stern length checking
stern: false

# MD033/no-inline-html : Inline HTML : https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md033.md
MD033:
# Allowed elements
allowed_elements: [
details,
summary,
]

176 changes: 176 additions & 0 deletions .github/workflows/packaging/deb/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#!/bin/bash
# create a deb package from rust sources
#
############ LIST OF MANAGED VARIABLES REQUIRED FOR DEB PACKAGE ############
name=api-server
# version=x.y.z - reading from first arg $1
descriptionShort="API Server wrapping databases, executable and python scripts plugins"
descriptionExtended="API Server - service running on the socket.
Provides simple and universe access to the databases, executable and python plugins.
Wrapping databases:
SQLite
MySQL
PostgreSQL
Runs puthon script:
Python script received some json data via stdin
Python script handle some algorithms
Python script can access to the databases data via self API or directly
Python script returns some json data
Runs binaty executable:
Executable received some json data via stdin
Executable handle some algorithms
Executable can access to the databases data via self API or directly
Executable returns some json data"
changeDetails="
- TcpServer | clean threads
- Some fixes in the TcpConnection
- ApiQuery moved to the library
"
copyrightNotice="Copyright 2024 anton lobanov"
maintainer="anton lobanov <[email protected]>"
licenseName="GNU GENERAL PUBLIC LICENSE v3.0"
licenseFile="LICENSE"

############ READING VERDION FROM ARGIMENT ############
RED='\033[0;31m'
NC='\033[0m' # No Color
version=$1
if [[ "$version" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
echo "Version: $version"
else
echo -e "${RED}ERROR${NC}: Version not supplied.\nDebian package build script required proper version of your softvare in the format: x.y.z, passed as argument"
fi
############ LIST OF MANAGED VARIABLES OPTIONAL FOR DEB PACKAGE ############
# preinst, postinst, prerm and postrm scripts:
preinst="./.github/workflows/packaging/deb/preinst"
postinst="./.github/workflows/packaging/deb/postinst"
prerm="./.github/workflows/packaging/deb/prerm"
postrm="./.github/workflows/packaging/deb/postrm"
# list of assets in the format:
# <sourcePath> <installPath> <permissions>
assets=(
"./target/release/api-server /usr/bin/ 755",
"./service/api-server.service /etc/systemd/system/ 644"
"./config.yaml /home/scada/api-server/"
)
outputDir=target/
# 'any', 'all' or one of the supported architecture (e.g., 'amd64', 'arm64', 'i386', 'armhf')
# you can choose one of the provided by `dpkg-architecture -L` or leave the field blank for automatic detection
arch=
# comma separated list of the package dependecies in the following format:
# "<package_name> [(<<|>>|<=|>=|= <version>)], ..."
# e.g. "foo (>=2.34), bar"
depends=""

# check required variables
echo "Checking reqired variables ..."
missedVarMsg="non-empty value required"
echo "${!name@}=${name:?$missedVarMsg}"
echo "${!version@}=${version:?$missedVarMsg}"
echo "${!descriptionShort@}=${descriptionShort:?$missedVarMsg}"
echo "${!descriptionExtended@}=${descriptionExtended:?$missedVarMsg}"
echo "${!changeDetails@}=${changeDetails:?$missedVarMsg}"
echo "${!copyrightNotice@}=${copyrightNotice:?$missedVarMsg}"
echo "${!maintainer@}=${maintainer:?$missedVarMsg}"
echo "${!licenseName@}=${licenseName:?$missedVarMsg}"
echo "${!licenseFile@}=${licenseFile:?$missedVarMsg}"

echo "Start packaging ..."

############ INITIALIZE THE PACKAGE SOURCE STRUCTURE AND COPY RESOURCES ############

arch=${arch:=$(dpkg --print-architecture)}
debFileName="${name}_${version}_${arch}"
packageRoot=$(readlink -m "/tmp/debian/${debFileName}")

if [[ -d $packageRoot ]]; then
echo "Freeing the directory for temporary build files ..."
rm -rf $packageRoot
fi

echo "Creating ${packageRoot} directory for temporary build files ..."
mkdir -p "$packageRoot"
echo "Creating ${packageRoot}/DEBIAN directory ..."
mkdir -p "${packageRoot}/DEBIAN"

copyAsset() {
sourcePath=$1; targetDir=$2; permissions=$3
assetPath=$(readlink -m "$sourcePath")
if [[ ! -d $assetPath && ! -f $assetPath ]]; then
echo "Asset ${assetPath} not found."
exit 1
fi
installPath=$(readlink -m "${packageRoot}/${targetDir}")
mkdir -p $installPath && cp -r "$assetPath" "$installPath"
echo "Copying ${assetPath} to ${installPath} ..."
if [[ -d $assetPath ]]; then
chmod -R "$permissions" "$installPath"
elif [[ -f $assetPath ]]; then
chmod "$permissions" "${installPath}/$(basename ${assetPath})"
fi
}
for asset in "${assets[@]}"; do
read -ra assetOptions <<< $asset
copyAsset ${assetOptions[0]} ${assetOptions[1]} ${assetOptions[2]}
done
copyAsset ${preinst} "DEBIAN" "755"
copyAsset ${postinst} "DEBIAN" "755"
copyAsset ${prerm} "DEBIAN" "755"
copyAsset ${postrm} "DEBIAN" "755"

############ CREATE A DEB CONTROL FILE ############

echo "Creating ${packageRoot}/DEBIAN/control file ..."
cat > "${packageRoot}/DEBIAN/control" <<- CONTROL
Section: misc
Priority: optional
Version: $version
Maintainer: $maintainer
Package: $name
Architecture: $arch
Depends: $depends
Description: $descriptionShort
$(echo "$descriptionExtended" | sed "s/^/ /")
CONTROL

############ CREATE CHANGELOG AND COPYRIGHT FILES ############

docDir="${packageRoot}/usr/share/doc/${name}"
mkdir -p "$docDir"

echo "Generating changelog file ..."
changelogFile="${docDir}/changelog"
cat > "$changelogFile" <<- CHANGELOG
$name ($version) unstable; urgency=medium

$(echo "$changeDetails" | sed "s/^/ * /")

$(echo " -- $maintainer $(date -R)")


CHANGELOG
gzip -n --best "$changelogFile"
rm -f "$changelogFile"

echo "Generating copyright file ..."
copyrightFile="${docDir}/copyright"
cat > "$copyrightFile" <<- COPYRIGHT
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: $name
Copyright: $copyrightNotice
License: $licenseName
$(cat "$licenseFile" | sed "s/^/ /")
COPYRIGHT

############ CREATE MD5 SUM FILES ############

cd $packageRoot
md5sum $(find . -type f -printf "%P\n" | grep -v "^DEBIAN/") > DEBIAN/md5sums
cd - > /dev/null

############ BUILD A DEB PACKAGE ############
echo "Building deb package ..."
dpkg-deb --build "${packageRoot}" "$outputDir" > /dev/null || exit 1
echo "Deleting temporary created ${packageRoot} directory"
# rm -rf "${packageRoot}"
echo "Debian package created and saved in $(readlink -m "${outputDir}/${debFileName}.deb")"
26 changes: 26 additions & 0 deletions .github/workflows/packaging/deb/install-api-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
rm -rf /tmp/api-server
git clone --progress -b packaging https://github.com/a-givertzman/api-server.git /tmp/api-server
current=$PWD
cd /tmp/api-server

echo ""
echo "building release..."
cargo build --release

echo ""
echo "building deb package..."
/tmp/api-server/.github/workflows/packaging/deb/build.sh

echo ""
echo "installing api server"
sudo apt install -y /tmp/api-server/target/api-server_0.1.15_amd64.deb

echo ""
echo "enabling api-server service..."
sudo systemctl daemon-reload
sudo systemctl --now enable api-server

echo ""
echo "cleaning temp files..."
# rm -rf /tmp/api-server
19 changes: 19 additions & 0 deletions .github/workflows/packaging/deb/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
#
# Deb package postinst script for API Server
#
echo ""
systemctl daemon-reload
name="api-server"
if [[ $(systemctl list-unit-files --all -t service --full --no-legend "$name.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $name.service ]]; then
if ! systemctl is-enabled --quiet "$name.service" ; then
echo "Enabling $name service..."
systemctl enable api-server
fi
if ! systemctl is-active --quiet "$name.service" ; then
echo "Starting $name service..."
systemctl start api-server
fi
else
echo "$name service - not found"
fi
7 changes: 7 additions & 0 deletions .github/workflows/packaging/deb/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
#
# Deb package postrm script for API Server
#
echo ""
echo "Reload systemd manager configuration..."
systemctl daemon-reload
16 changes: 16 additions & 0 deletions .github/workflows/packaging/deb/preinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
#
# Deb package prinst script for API Server
#
echo ""
name="api-server"
if [[ $(systemctl list-units --all -t service --full --no-legend "$name.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $name.service ]]; then
if systemctl is-active --quiet "$name.service" ; then
echo "Stopping $name service..."
systemctl stop api-server
fi
if systemctl is-enabled --quiet "$name.service" ; then
echo "Disabling $name service..."
systemctl disable api-server
fi
fi
16 changes: 16 additions & 0 deletions .github/workflows/packaging/deb/prerm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
#
# Deb package prerem script for API Server
#
echo ""
name="api-server"
if [[ $(systemctl list-units --all -t service --full --no-legend "$name.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $name.service ]]; then
if systemctl is-active --quiet "$name.service" ; then
echo "Stopping $name service..."
systemctl stop api-server
fi
if systemctl is-enabled --quiet "$name.service" ; then
echo "Disabling $name service..."
systemctl disable api-server
fi
fi
17 changes: 17 additions & 0 deletions .github/workflows/packaging/deb/service/api-server.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=API Server
After=syslog.target network.target multi-user.target

[Service]
User=scada
Type=simple
Restart=always
RestartSec=5
# WorkingDirectory=/home/scada/api-server/
ExecStart=api-server --config /home/scada/api-server/config.yaml

StandardOutput=append:/var/log/api-server.log
StandardError=append:/var/log/api-server.log

[Install]
WantedBy=multi-user.target
27 changes: 27 additions & 0 deletions .github/workflows/packaging/deb/uninstall-api-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
echo ""
echo "disabling api-server service..."
sudo systemctl stop api-server
sudo systemctl disable api-server

echo ""
echo "uninstalling api-server..."
sudo apt purge -y api-server

echo ""
echo "removing api-server service unit file..."
sudo rm /etc/systemd/system/api-server.service

echo ""
echo "reloading systemd..."
sudo systemctl daemon-reload
sudo systemctl reset-failed

# systemctl stop [servicename]
# systemctl disable [servicename]
# rm /etc/systemd/system/[servicename]
# rm /etc/systemd/system/[servicename] # and symlinks that might be related
# rm /usr/lib/systemd/system/[servicename]
# rm /usr/lib/systemd/system/[servicename] # and symlinks that might be related
# systemctl daemon-reload
# systemctl reset-failed
Loading
Loading