From e004c60e5ba2d9402cc61e530c6dcf416f7ff5e8 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Tue, 23 Apr 2024 16:49:20 +0300 Subject: [PATCH 01/24] Build deb | service --- .github/workflows/packaging/deb/build.sh | 156 +++++++++++++++++++++++ service/api-server.service | 14 ++ 2 files changed, 170 insertions(+) create mode 100755 .github/workflows/packaging/deb/build.sh create mode 100644 service/api-server.service diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh new file mode 100755 index 00000000..0f3c0f7d --- /dev/null +++ b/.github/workflows/packaging/deb/build.sh @@ -0,0 +1,156 @@ +#!/bin/bash +# create a deb package from rust sources + +############ LIST OF MANAGED VARIABLES REQUIRED FOR DEB PACKAGE ############ +name=api-server +version=0.1.15 +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 " +licenseName="GNU GENERAL PUBLIC LICENSE v3.0" +licenseFile="LICENSE" + +############ LIST OF MANAGED VARIABLES OPTIONAL FOR DEB PACKAGE ############ +# list of assets in the format: +# +assets=( + "./target/release/api-server /usr/bin/ 755" +) +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: +# " [(<<|>>|<=|>=|= )], ..." +# 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 + +############ CREATE A DEB CONTROL FILE ############ + +echo "Creating ${packageRoot}/DEBIAN/control file ..." +cat > "${packageRoot}/DEBIAN/control" <<- CONTROL + Section: rust + 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")" \ No newline at end of file diff --git a/service/api-server.service b/service/api-server.service new file mode 100644 index 00000000..d3af9f67 --- /dev/null +++ b/service/api-server.service @@ -0,0 +1,14 @@ +[Unit] +Description=Run api-server application as Service +After=syslog.target network.target multi-user.target + +[Service] +User=scada +Type=simple +Restart=always +RestartSec=1 +WorkingDirectory=/home/scada/app/data_server/ +ExecStart=python3.10 /home/scada/app/data_server/api_server.py + +[Install] +WantedBy=multi-user.target From 0689871e1f6c2ed7c9c13ccead10e27944d65721 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Tue, 23 Apr 2024 17:37:07 +0300 Subject: [PATCH 02/24] Build deb | service --- .github/workflows/packaging/deb/build.sh | 3 ++- service/api-server.service | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index 0f3c0f7d..0c6e176a 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -35,7 +35,8 @@ licenseFile="LICENSE" # list of assets in the format: # assets=( - "./target/release/api-server /usr/bin/ 755" + "./target/release/api-server /usr/bin/ 755", + "./service/api-server.service /etc/systemd/system/" ) outputDir=target/ # 'any', 'all' or one of the supported architecture (e.g., 'amd64', 'arm64', 'i386', 'armhf') diff --git a/service/api-server.service b/service/api-server.service index d3af9f67..bebffaf2 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -7,8 +7,8 @@ User=scada Type=simple Restart=always RestartSec=1 -WorkingDirectory=/home/scada/app/data_server/ -ExecStart=python3.10 /home/scada/app/data_server/api_server.py +WorkingDirectory=/home/scada/api-server/ +ExecStart=python3.10 api-server [Install] WantedBy=multi-user.target From f2524c2edb51da59c10b075450b6e710c4758d55 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Tue, 23 Apr 2024 18:18:19 +0300 Subject: [PATCH 03/24] Build deb | service --- .github/workflows/packaging/deb/build.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index 0c6e176a..1feb3515 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -37,6 +37,7 @@ licenseFile="LICENSE" assets=( "./target/release/api-server /usr/bin/ 755", "./service/api-server.service /etc/systemd/system/" + "./config.yaml /home/scada/api-server/" ) outputDir=target/ # 'any', 'all' or one of the supported architecture (e.g., 'amd64', 'arm64', 'i386', 'armhf') From 416489459e50f4e9c331d9262d7bb1b53022312a Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 12:37:08 +0300 Subject: [PATCH 04/24] Build deb | service --- .github/workflows/packaging/deb/build.sh | 2 +- .../packaging/deb/install-api-server.sh | 16 ++++++ Cargo.lock | 55 ++++++++++++++++++- Cargo.toml | 4 +- service/api-server.service | 9 +-- src/config.rs | 12 ++-- src/core_/cli/cli.rs | 10 ++++ src/core_/cli/mod.rs | 1 + src/core_/mod.rs | 1 + src/main.rs | 12 ++-- 10 files changed, 102 insertions(+), 20 deletions(-) create mode 100755 .github/workflows/packaging/deb/install-api-server.sh create mode 100644 src/core_/cli/cli.rs create mode 100644 src/core_/cli/mod.rs diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index 1feb3515..1c02e685 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -4,7 +4,7 @@ ############ LIST OF MANAGED VARIABLES REQUIRED FOR DEB PACKAGE ############ name=api-server version=0.1.15 -descriptionShort="API Server wrapping databases, executable and python scripts plugins." +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: diff --git a/.github/workflows/packaging/deb/install-api-server.sh b/.github/workflows/packaging/deb/install-api-server.sh new file mode 100755 index 00000000..0a79cec1 --- /dev/null +++ b/.github/workflows/packaging/deb/install-api-server.sh @@ -0,0 +1,16 @@ +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 \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 62439730..fa4b91cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,6 +125,7 @@ dependencies = [ "api-tools", "bytes", "chrono", + "clap", "env_logger", "indexmap 2.2.5", "linked-hash-map", @@ -329,6 +330,46 @@ dependencies = [ "windows-targets 0.52.4", ] +[[package]] +name = "clap" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim 0.11.1", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.52", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + [[package]] name = "colorchoice" version = "1.0.0" @@ -380,7 +421,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim", + "strsim 0.10.0", "syn 2.0.52", ] @@ -598,6 +639,12 @@ dependencies = [ "hashbrown 0.14.3", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" @@ -1290,6 +1337,12 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.5.0" diff --git a/Cargo.toml b/Cargo.toml index ae8998c9..5e55687e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,4 +40,6 @@ rust_decimal = { version = "^1.32", features = [ "tokio-pg" ] } rand = "^0.8" -indexmap = {version = "^2.1", features = ["serde"] } +indexmap = { version = "^2.1", features = ["serde"] } + +clap = { version = "^4.5", features = ["derive"] } diff --git a/service/api-server.service b/service/api-server.service index bebffaf2..99ac12bb 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -1,14 +1,15 @@ [Unit] -Description=Run api-server application as Service +Description=API Server wrapping databases, executable and python scripts plugins After=syslog.target network.target multi-user.target [Service] User=scada Type=simple Restart=always -RestartSec=1 -WorkingDirectory=/home/scada/api-server/ -ExecStart=python3.10 api-server +RestartSec=5 +# WorkingDirectory=/home/scada/api-server/ +ExecStart= +ExecStart=api-server --config /home/scada/api-server/config.yaml [Install] WantedBy=multi-user.target diff --git a/src/config.rs b/src/config.rs index 058e8edc..baf45f6c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,5 @@ use log::{trace, debug}; -use std::{fs, collections::HashMap}; +use std::{collections::HashMap, fs, path::Path}; use linked_hash_map::LinkedHashMap; use yaml_rust::{YamlLoader, Yaml}; @@ -12,7 +12,7 @@ pub struct Config { pub services: HashMap, } impl Config { - pub fn new(path: &str) -> Config { + pub fn new

(path: P) -> Config where P: AsRef { match fs::read_to_string(&path) { Ok(yaml_string) => { let yaml_vec = YamlLoader::load_from_str(&yaml_string).unwrap(); @@ -21,7 +21,7 @@ impl Config { let services_vec = yaml_doc["services"] .as_vec() .expect( - format!("Config | error reading 'services' from config file {:?}", &path).as_str(), + format!("Config | error reading 'services' from config file {:?}", path.as_ref()).as_str(), ); for item in services_vec { trace!("Config | service config item: {:?}", item); @@ -33,7 +33,7 @@ impl Config { // debug!("Config | dataBaseConfigMap: {:?}", dataBaseConfigMap); let service_config = ServiceConfig::new(service_config_key, service_config_map); if services.contains_key(&service_config.name) { - panic!("Duplicated service name: \"{}\" in the config: \"{}\"", &service_config.name, path) + panic!("Duplicated service name: '{}' in the config: '{}'", &service_config.name, path.as_ref().display()) } else { // services.insert((&serviceConfig.name).clone(), serviceConfig); services.insert(service_config.name.clone(), service_config); @@ -44,7 +44,7 @@ impl Config { yaml_doc["address"] .as_str() .expect( - format!("Config | error reading 'address' from config file {:?}", &path).as_str(), + format!("Config | error reading 'address' from config file {:?}", path.as_ref()).as_str(), ) ), services, @@ -52,7 +52,7 @@ impl Config { }, Err(err) => { // warn!("File {} reading error: {:?}", path, err); - panic!("File {} reading error: {:?}", path, err) + panic!("File {} reading error: {:?}", path.as_ref().display(), err) }, } } diff --git a/src/core_/cli/cli.rs b/src/core_/cli/cli.rs new file mode 100644 index 00000000..ef1e6231 --- /dev/null +++ b/src/core_/cli/cli.rs @@ -0,0 +1,10 @@ +use clap::Parser; +/// +/// Application cli arguments +#[derive(Parser, Debug)] +#[command(version = "0.1.14", about = "API Server | Wrapping databases, executable and python scripts plugins", long_about = None)] +pub struct Cli { + /// Optional path to configuration file, if omitted 'config.yaml' from current dir will be used + #[arg(short, long)] + pub config: Option, +} \ No newline at end of file diff --git a/src/core_/cli/mod.rs b/src/core_/cli/mod.rs new file mode 100644 index 00000000..5d863fb7 --- /dev/null +++ b/src/core_/cli/mod.rs @@ -0,0 +1 @@ +pub mod cli; \ No newline at end of file diff --git a/src/core_/mod.rs b/src/core_/mod.rs index 1be1de2f..4759196a 100644 --- a/src/core_/mod.rs +++ b/src/core_/mod.rs @@ -1,3 +1,4 @@ pub mod aprox_eq; pub mod debug; // pub mod error; +pub mod cli; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0ee27667..d3a75d59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,21 +17,19 @@ mod sql_query_mysql; mod tcp_connection; use std::sync::{Arc, Mutex}; - +use clap::Parser; use log::debug; - use crate::{ - config::Config, - tcp_server::TcpServer, - core_::debug::debug_session::{DebugSession, LogLevel, Backtrace}, + config::Config, core_::{cli::cli::Cli, debug::debug_session::{Backtrace, DebugSession, LogLevel}}, tcp_server::TcpServer }; fn main() { DebugSession::init(LogLevel::Debug, Backtrace::Short); + let cli = Cli::parse(); debug!("starting api server..."); let dir = std::env::current_dir().unwrap(); - let path: &str = &format!("{}/config.yaml", dir.to_str().unwrap()); - debug!("reading config file: {}", path); + let path = dir.join(cli.config.unwrap_or("config.yaml".to_owned())); + debug!("reading config file: {}", path.to_str().unwrap()); let config = Config::new(path); let tcp_server = Arc::new(Mutex::new( TcpServer::new( From 38e5b8e214d3f8693466082dac40195d45824bf3 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 12:53:29 +0300 Subject: [PATCH 05/24] Build deb | service --- .github/workflows/packaging/deb/build.sh | 2 +- .../packaging/deb/install-api-server.sh | 6 ++++- .../packaging/deb/uninstall-api-server.sh | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100755 .github/workflows/packaging/deb/uninstall-api-server.sh diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index 1c02e685..b24900c9 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -36,7 +36,7 @@ licenseFile="LICENSE" # assets=( "./target/release/api-server /usr/bin/ 755", - "./service/api-server.service /etc/systemd/system/" + "./service/api-server.service /etc/systemd/system/ 644" "./config.yaml /home/scada/api-server/" ) outputDir=target/ diff --git a/.github/workflows/packaging/deb/install-api-server.sh b/.github/workflows/packaging/deb/install-api-server.sh index 0a79cec1..832bee3d 100755 --- a/.github/workflows/packaging/deb/install-api-server.sh +++ b/.github/workflows/packaging/deb/install-api-server.sh @@ -13,4 +13,8 @@ echo "building deb package..." echo "" echo "installing api server" -sudo apt install -y /tmp/api-server/target/api-server_0.1.15_amd64.deb \ No newline at end of file +sudo apt install -y /tmp/api-server/target/api-server_0.1.15_amd64.deb + +echo "" +echo "cleaning temp files..." +# rm -rf /tmp/api-server diff --git a/.github/workflows/packaging/deb/uninstall-api-server.sh b/.github/workflows/packaging/deb/uninstall-api-server.sh new file mode 100755 index 00000000..4d9335b8 --- /dev/null +++ b/.github/workflows/packaging/deb/uninstall-api-server.sh @@ -0,0 +1,26 @@ +echo "" +echo "disabling api-server service..." +sudo systemctl stop api-server +sudo systemctl disable api-server + +echo "" +echo "uninstalling api-server..." +sudo apt purge 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 From 10e8f756078fd6e1f3d383f97cdc626e5e9ef045 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 12:56:11 +0300 Subject: [PATCH 06/24] Build deb | service --- service/api-server.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/api-server.service b/service/api-server.service index 99ac12bb..1d7b2fef 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -1,5 +1,5 @@ [Unit] -Description=API Server wrapping databases, executable and python scripts plugins +Description=API Server After=syslog.target network.target multi-user.target [Service] From 8327a0bec80f6d8e9b052288c3c7b8d2316ab548 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 13:24:57 +0300 Subject: [PATCH 07/24] Build deb | service --- service/api-server.service | 2 ++ src/main.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/service/api-server.service b/service/api-server.service index 1d7b2fef..61687110 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -7,6 +7,8 @@ User=scada Type=simple Restart=always RestartSec=5 +StandardOutput=file:/var/log/api-server.log +StandardError=file:/var/log/api-server.log # WorkingDirectory=/home/scada/api-server/ ExecStart= ExecStart=api-server --config /home/scada/api-server/config.yaml diff --git a/src/main.rs b/src/main.rs index d3a75d59..df499784 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod sql_query_postgre; mod sql_query_mysql; mod tcp_connection; -use std::sync::{Arc, Mutex}; +use std::{path::{Path, PathBuf}, sync::{Arc, Mutex}}; use clap::Parser; use log::debug; use crate::{ @@ -27,8 +27,11 @@ fn main() { DebugSession::init(LogLevel::Debug, Backtrace::Short); let cli = Cli::parse(); debug!("starting api server..."); - let dir = std::env::current_dir().unwrap(); - let path = dir.join(cli.config.unwrap_or("config.yaml".to_owned())); + let path = cli.config.map_or_else( + || PathBuf::from("config.yaml"), // || std::env::current_dir().unwrap().join("config.yaml"), + |path| PathBuf::from(path) + ); + let path = Path::new(&path); debug!("reading config file: {}", path.to_str().unwrap()); let config = Config::new(path); let tcp_server = Arc::new(Mutex::new( From 04841213ebd6d959b54344641b6339a049c14ef5 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 13:35:05 +0300 Subject: [PATCH 08/24] Build deb | service --- .github/workflows/packaging/deb/install-api-server.sh | 5 +++++ .github/workflows/packaging/deb/uninstall-api-server.sh | 2 +- service/api-server.service | 9 +++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.github/workflows/packaging/deb/install-api-server.sh b/.github/workflows/packaging/deb/install-api-server.sh index 832bee3d..6d683a35 100755 --- a/.github/workflows/packaging/deb/install-api-server.sh +++ b/.github/workflows/packaging/deb/install-api-server.sh @@ -15,6 +15,11 @@ 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 diff --git a/.github/workflows/packaging/deb/uninstall-api-server.sh b/.github/workflows/packaging/deb/uninstall-api-server.sh index 4d9335b8..b3ba5f8c 100755 --- a/.github/workflows/packaging/deb/uninstall-api-server.sh +++ b/.github/workflows/packaging/deb/uninstall-api-server.sh @@ -5,7 +5,7 @@ sudo systemctl disable api-server echo "" echo "uninstalling api-server..." -sudo apt purge api-server +sudo apt purge -y api-server echo "" echo "removing api-server service unit file..." diff --git a/service/api-server.service b/service/api-server.service index 61687110..210ad4a2 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -7,11 +7,16 @@ User=scada Type=simple Restart=always RestartSec=5 -StandardOutput=file:/var/log/api-server.log -StandardError=file:/var/log/api-server.log # WorkingDirectory=/home/scada/api-server/ ExecStart= ExecStart=api-server --config /home/scada/api-server/config.yaml +StandardOutput=syslog +StandardError=syslog +SyslogIdentifier=bird_watching + +# StandardOutput=file:/var/log/api-server.log +# StandardError=file:/var/log/api-server.log + [Install] WantedBy=multi-user.target From 9fb85a47b74a00b94a1518b80cb56162231175a0 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 13:58:46 +0300 Subject: [PATCH 09/24] Build deb | service --- service/api-server.service | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/service/api-server.service b/service/api-server.service index 210ad4a2..0569eebd 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -9,14 +9,11 @@ Restart=always RestartSec=5 # WorkingDirectory=/home/scada/api-server/ ExecStart= +ExecStart=api-server --version ExecStart=api-server --config /home/scada/api-server/config.yaml -StandardOutput=syslog -StandardError=syslog -SyslogIdentifier=bird_watching - -# StandardOutput=file:/var/log/api-server.log -# StandardError=file:/var/log/api-server.log +StandardOutput=append:/var/log/api-server.log +StandardError=append:/var/log/api-server.log [Install] WantedBy=multi-user.target From ce71b1f4c829a9558b2a9d60aa2c2edff3909a6d Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 14:03:09 +0300 Subject: [PATCH 10/24] Build deb | service --- service/api-server.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/api-server.service b/service/api-server.service index 0569eebd..301da474 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -4,7 +4,7 @@ After=syslog.target network.target multi-user.target [Service] User=scada -Type=simple +Type=oneshot # use 'simple' to have single ExecStart Restart=always RestartSec=5 # WorkingDirectory=/home/scada/api-server/ From ced251e018d1c0fa6e65855f35cb7db4aa5b461f Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 14:17:45 +0300 Subject: [PATCH 11/24] Build deb | service --- service/api-server.service | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/service/api-server.service b/service/api-server.service index 301da474..5e6518ed 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -4,13 +4,11 @@ After=syslog.target network.target multi-user.target [Service] User=scada -Type=oneshot # use 'simple' to have single ExecStart +Type=simple Restart=always RestartSec=5 # WorkingDirectory=/home/scada/api-server/ -ExecStart= -ExecStart=api-server --version -ExecStart=api-server --config /home/scada/api-server/config.yaml +ExecStart=api-server --version && api-server --config /home/scada/api-server/config.yaml StandardOutput=append:/var/log/api-server.log StandardError=append:/var/log/api-server.log From 4a56daaa1045d0efe6ced9fb8e24b3813f5802ec Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 14:21:09 +0300 Subject: [PATCH 12/24] Build deb | service --- service/api-server.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/api-server.service b/service/api-server.service index 5e6518ed..3a1bc00a 100644 --- a/service/api-server.service +++ b/service/api-server.service @@ -8,7 +8,7 @@ Type=simple Restart=always RestartSec=5 # WorkingDirectory=/home/scada/api-server/ -ExecStart=api-server --version && api-server --config /home/scada/api-server/config.yaml +ExecStart=api-server --config /home/scada/api-server/config.yaml StandardOutput=append:/var/log/api-server.log StandardError=append:/var/log/api-server.log From 97a26279363ffde64ca13bc265853432d39cc537 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 17:07:41 +0300 Subject: [PATCH 13/24] Build deb | pre & post scripts --- .github/workflows/packaging/deb/00_postinst.sh | 9 +++++++++ .github/workflows/packaging/deb/00_postrm.sh | 4 ++++ .github/workflows/packaging/deb/00_preinst.sh | 8 ++++++++ .github/workflows/packaging/deb/00_prerm.sh | 8 ++++++++ .github/workflows/packaging/deb/build.sh | 10 ++++++++++ .github/workflows/packaging/deb/install-api-server.sh | 1 + .../workflows/packaging/deb/uninstall-api-server.sh | 1 + 7 files changed, 41 insertions(+) create mode 100644 .github/workflows/packaging/deb/00_postinst.sh create mode 100644 .github/workflows/packaging/deb/00_postrm.sh create mode 100644 .github/workflows/packaging/deb/00_preinst.sh create mode 100644 .github/workflows/packaging/deb/00_prerm.sh diff --git a/.github/workflows/packaging/deb/00_postinst.sh b/.github/workflows/packaging/deb/00_postinst.sh new file mode 100644 index 00000000..96996000 --- /dev/null +++ b/.github/workflows/packaging/deb/00_postinst.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# +# Deb package prinst script for API Server +# +echo "" +echo "Enabling api-server service..." +systemctl daemon-reload +systemctl enable api-server +systemctl start api-server diff --git a/.github/workflows/packaging/deb/00_postrm.sh b/.github/workflows/packaging/deb/00_postrm.sh new file mode 100644 index 00000000..9754d1cd --- /dev/null +++ b/.github/workflows/packaging/deb/00_postrm.sh @@ -0,0 +1,4 @@ +#!/bin/bash +echo "" +echo "Reload systemd manager configuration..." +systemctl daemon-reload diff --git a/.github/workflows/packaging/deb/00_preinst.sh b/.github/workflows/packaging/deb/00_preinst.sh new file mode 100644 index 00000000..ca75a583 --- /dev/null +++ b/.github/workflows/packaging/deb/00_preinst.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# +# Deb package prinst script for API Server +# +echo "" +echo "Disabling api-server service..." +systemctl stop api-server +systemctl disable api-server diff --git a/.github/workflows/packaging/deb/00_prerm.sh b/.github/workflows/packaging/deb/00_prerm.sh new file mode 100644 index 00000000..e7ade677 --- /dev/null +++ b/.github/workflows/packaging/deb/00_prerm.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# +# Deb package prerem script for API Server +# +echo "" +echo "Disabling api-server service..." +systemctl stop api-server +systemctl disable api-server diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index b24900c9..8d59cf22 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -32,6 +32,16 @@ licenseName="GNU GENERAL PUBLIC LICENSE v3.0" licenseFile="LICENSE" ############ LIST OF MANAGED VARIABLES OPTIONAL FOR DEB PACKAGE ############ +# list of preinst, postinst, prerm and postrm scripts: +preinst=( +) +postinst=( + "./.github/workflows/packaging/deb/00_postinst.sh", +) +prerm=( +) +postrm=( +) # list of assets in the format: # assets=( diff --git a/.github/workflows/packaging/deb/install-api-server.sh b/.github/workflows/packaging/deb/install-api-server.sh index 6d683a35..e418a9aa 100755 --- a/.github/workflows/packaging/deb/install-api-server.sh +++ b/.github/workflows/packaging/deb/install-api-server.sh @@ -1,3 +1,4 @@ +#!/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 diff --git a/.github/workflows/packaging/deb/uninstall-api-server.sh b/.github/workflows/packaging/deb/uninstall-api-server.sh index b3ba5f8c..1aaa2ae8 100755 --- a/.github/workflows/packaging/deb/uninstall-api-server.sh +++ b/.github/workflows/packaging/deb/uninstall-api-server.sh @@ -1,3 +1,4 @@ +#!/bin/bash echo "" echo "disabling api-server service..." sudo systemctl stop api-server From 237a1862eb6621738dd4e37138377bde30a24f83 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 17:31:55 +0300 Subject: [PATCH 14/24] Build deb | pre & post scripts --- .github/workflows/packaging/deb/build.sh | 21 +++++++++---------- .../deb/{00_postinst.sh => postinst} | 2 +- .../packaging/deb/{00_postrm.sh => postrm} | 3 +++ .../packaging/deb/{00_preinst.sh => preinst} | 0 .../packaging/deb/{00_prerm.sh => prerm} | 0 5 files changed, 14 insertions(+), 12 deletions(-) rename .github/workflows/packaging/deb/{00_postinst.sh => postinst} (75%) mode change 100644 => 100755 rename .github/workflows/packaging/deb/{00_postrm.sh => postrm} (65%) mode change 100644 => 100755 rename .github/workflows/packaging/deb/{00_preinst.sh => preinst} (100%) mode change 100644 => 100755 rename .github/workflows/packaging/deb/{00_prerm.sh => prerm} (100%) mode change 100644 => 100755 diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index 8d59cf22..a60f1487 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -32,16 +32,11 @@ licenseName="GNU GENERAL PUBLIC LICENSE v3.0" licenseFile="LICENSE" ############ LIST OF MANAGED VARIABLES OPTIONAL FOR DEB PACKAGE ############ -# list of preinst, postinst, prerm and postrm scripts: -preinst=( -) -postinst=( - "./.github/workflows/packaging/deb/00_postinst.sh", -) -prerm=( -) -postrm=( -) +# 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: # assets=( @@ -109,6 +104,10 @@ for asset in "${assets[@]}"; do read -ra assetOptions <<< $asset copyAsset ${assetOptions[0]} ${assetOptions[1]} ${assetOptions[2]} done +copyAsset ${preinst} "" "755" +copyAsset ${postinst} "" "755" +copyAsset ${prerm} "" "755" +copyAsset ${postrm} "" "755" ############ CREATE A DEB CONTROL FILE ############ @@ -164,5 +163,5 @@ cd - > /dev/null echo "Building deb package ..." dpkg-deb --build "${packageRoot}" "$outputDir" > /dev/null || exit 1 echo "Deleting temporary created ${packageRoot} directory" -rm -rf "${packageRoot}" +# rm -rf "${packageRoot}" echo "Debian package created and saved in $(readlink -m "${outputDir}/${debFileName}.deb")" \ No newline at end of file diff --git a/.github/workflows/packaging/deb/00_postinst.sh b/.github/workflows/packaging/deb/postinst old mode 100644 new mode 100755 similarity index 75% rename from .github/workflows/packaging/deb/00_postinst.sh rename to .github/workflows/packaging/deb/postinst index 96996000..0a263862 --- a/.github/workflows/packaging/deb/00_postinst.sh +++ b/.github/workflows/packaging/deb/postinst @@ -1,6 +1,6 @@ #!/bin/bash # -# Deb package prinst script for API Server +# Deb package postinst script for API Server # echo "" echo "Enabling api-server service..." diff --git a/.github/workflows/packaging/deb/00_postrm.sh b/.github/workflows/packaging/deb/postrm old mode 100644 new mode 100755 similarity index 65% rename from .github/workflows/packaging/deb/00_postrm.sh rename to .github/workflows/packaging/deb/postrm index 9754d1cd..c6fefcad --- a/.github/workflows/packaging/deb/00_postrm.sh +++ b/.github/workflows/packaging/deb/postrm @@ -1,4 +1,7 @@ #!/bin/bash +# +# Deb package postrm script for API Server +# echo "" echo "Reload systemd manager configuration..." systemctl daemon-reload diff --git a/.github/workflows/packaging/deb/00_preinst.sh b/.github/workflows/packaging/deb/preinst old mode 100644 new mode 100755 similarity index 100% rename from .github/workflows/packaging/deb/00_preinst.sh rename to .github/workflows/packaging/deb/preinst diff --git a/.github/workflows/packaging/deb/00_prerm.sh b/.github/workflows/packaging/deb/prerm old mode 100644 new mode 100755 similarity index 100% rename from .github/workflows/packaging/deb/00_prerm.sh rename to .github/workflows/packaging/deb/prerm From 5b1f69982bb3c788a329a92c10576fe1e11bce69 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 17:59:08 +0300 Subject: [PATCH 15/24] Build deb | pre & post scripts --- .github/workflows/packaging/deb/build.sh | 8 ++++---- .github/workflows/packaging/deb/preinst | 14 +++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index a60f1487..3512d499 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -104,10 +104,10 @@ for asset in "${assets[@]}"; do read -ra assetOptions <<< $asset copyAsset ${assetOptions[0]} ${assetOptions[1]} ${assetOptions[2]} done -copyAsset ${preinst} "" "755" -copyAsset ${postinst} "" "755" -copyAsset ${prerm} "" "755" -copyAsset ${postrm} "" "755" +copyAsset ${preinst} "DEBIAN" "755" +copyAsset ${postinst} "DEBIAN" "755" +copyAsset ${prerm} "DEBIAN" "755" +copyAsset ${postrm} "DEBIAN" "755" ############ CREATE A DEB CONTROL FILE ############ diff --git a/.github/workflows/packaging/deb/preinst b/.github/workflows/packaging/deb/preinst index ca75a583..b6815e05 100755 --- a/.github/workflows/packaging/deb/preinst +++ b/.github/workflows/packaging/deb/preinst @@ -3,6 +3,14 @@ # Deb package prinst script for API Server # echo "" -echo "Disabling api-server service..." -systemctl stop api-server -systemctl disable api-server +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 stop api-server + fi +fi From 9ac3f957207e2431caed5088333dd9ddd15b9df8 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 23:26:07 +0300 Subject: [PATCH 16/24] Build deb | pre & post scripts --- .github/workflows/packaging/deb/postinst | 12 ++++++++++-- .github/workflows/packaging/deb/preinst | 4 ++-- .github/workflows/packaging/deb/prerm | 14 +++++++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.github/workflows/packaging/deb/postinst b/.github/workflows/packaging/deb/postinst index 0a263862..6f3eced4 100755 --- a/.github/workflows/packaging/deb/postinst +++ b/.github/workflows/packaging/deb/postinst @@ -5,5 +5,13 @@ echo "" echo "Enabling api-server service..." systemctl daemon-reload -systemctl enable api-server -systemctl start 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-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 +fi diff --git a/.github/workflows/packaging/deb/preinst b/.github/workflows/packaging/deb/preinst index b6815e05..e6622dbb 100755 --- a/.github/workflows/packaging/deb/preinst +++ b/.github/workflows/packaging/deb/preinst @@ -11,6 +11,6 @@ if [[ $(systemctl list-units --all -t service --full --no-legend "$name.service" fi if systemctl is-enabled --quiet "$name.service" ; then echo "Disabling $name service..." - systemctl stop api-server - fi + systemctl disable api-server + fi fi diff --git a/.github/workflows/packaging/deb/prerm b/.github/workflows/packaging/deb/prerm index e7ade677..fa5f9961 100755 --- a/.github/workflows/packaging/deb/prerm +++ b/.github/workflows/packaging/deb/prerm @@ -3,6 +3,14 @@ # Deb package prerem script for API Server # echo "" -echo "Disabling api-server service..." -systemctl stop api-server -systemctl disable api-server +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 From 73bc458316b4a3ad576dcde91d5660edf4a568f8 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 23:46:43 +0300 Subject: [PATCH 17/24] Build deb | pre & post scripts --- .github/workflows/packaging/deb/postinst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/packaging/deb/postinst b/.github/workflows/packaging/deb/postinst index 6f3eced4..10a68be7 100755 --- a/.github/workflows/packaging/deb/postinst +++ b/.github/workflows/packaging/deb/postinst @@ -3,9 +3,9 @@ # Deb package postinst script for API Server # echo "" -echo "Enabling api-server service..." systemctl daemon-reload -if [[ $(systemctl list-units --all -t service --full --no-legend "$name.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $name.service ]]; then +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 @@ -14,4 +14,6 @@ if [[ $(systemctl list-units --all -t service --full --no-legend "$name.service" echo "Starting $name service..." systemctl start api-server fi +else + echo "$name service - not found" fi From 5d5bdc182792a2c4fc7f4c7f521f1e26722f44c3 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Wed, 24 Apr 2024 23:55:34 +0300 Subject: [PATCH 18/24] Build deb | pre & post scripts --- .github/workflows/packaging/deb/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index 3512d499..e48495dd 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -113,7 +113,7 @@ copyAsset ${postrm} "DEBIAN" "755" echo "Creating ${packageRoot}/DEBIAN/control file ..." cat > "${packageRoot}/DEBIAN/control" <<- CONTROL - Section: rust + Section: misc Priority: optional Version: $version Maintainer: $maintainer From ae0e81077ead06fd21568cf4cdd563a188d289aa Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Thu, 25 Apr 2024 00:21:37 +0300 Subject: [PATCH 19/24] Build deb | pre & post scripts --- .github/workflows/packaging/deb/build.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index e48495dd..c88cd595 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -1,9 +1,9 @@ #!/bin/bash # create a deb package from rust sources - +# ############ LIST OF MANAGED VARIABLES REQUIRED FOR DEB PACKAGE ############ name=api-server -version=0.1.15 +# version=0.1.15 - reading late 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. @@ -31,6 +31,15 @@ maintainer="anton lobanov " 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" From 60bfce5e0a97f75560e1d9c75467b69e711c189d Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Thu, 25 Apr 2024 00:22:02 +0300 Subject: [PATCH 20/24] Build deb | pre & post scripts --- .github/workflows/packaging/deb/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index c88cd595..2019ed6f 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -3,7 +3,7 @@ # ############ LIST OF MANAGED VARIABLES REQUIRED FOR DEB PACKAGE ############ name=api-server -# version=0.1.15 - reading late from first arg $1 +# version=x.y.z - reading late 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. From 70cbe6b07a69751b9b0c69b03b86f175186ea3f2 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Thu, 25 Apr 2024 12:35:12 +0300 Subject: [PATCH 21/24] Build deb | github action --- .github/workflows/markdownlint.yaml | 38 ++++++++++++ .github/workflows/packaging/deb/build.sh | 2 +- .../packaging/deb/service}/api-server.service | 0 .github/workflows/release_deb_package.yaml | 59 +++++++++++++++++++ .github/workflows/test.yaml | 30 +++++----- 5 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/markdownlint.yaml rename {service => .github/workflows/packaging/deb/service}/api-server.service (100%) create mode 100644 .github/workflows/release_deb_package.yaml diff --git a/.github/workflows/markdownlint.yaml b/.github/workflows/markdownlint.yaml new file mode 100644 index 00000000..01da110c --- /dev/null +++ b/.github/workflows/markdownlint.yaml @@ -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, + ] + \ No newline at end of file diff --git a/.github/workflows/packaging/deb/build.sh b/.github/workflows/packaging/deb/build.sh index 2019ed6f..f63a7f1d 100755 --- a/.github/workflows/packaging/deb/build.sh +++ b/.github/workflows/packaging/deb/build.sh @@ -3,7 +3,7 @@ # ############ LIST OF MANAGED VARIABLES REQUIRED FOR DEB PACKAGE ############ name=api-server -# version=x.y.z - reading late from first arg $1 +# 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. diff --git a/service/api-server.service b/.github/workflows/packaging/deb/service/api-server.service similarity index 100% rename from service/api-server.service rename to .github/workflows/packaging/deb/service/api-server.service diff --git a/.github/workflows/release_deb_package.yaml b/.github/workflows/release_deb_package.yaml new file mode 100644 index 00000000..ac318a2f --- /dev/null +++ b/.github/workflows/release_deb_package.yaml @@ -0,0 +1,59 @@ +name: Create release with beb pacage + +on: + workflow_dispatch: + +env: + TERM: dumb + CARGO_TERM_COLOR: always + API_SERVER_ADDR: '0.0.0.0' + API_SERVER_PORT: 8080 + POSTGRES_PASSWORD: postgres + PYTHON_VERSION: 'python3.10' + +jobs: + build_and_test: + name: Creating new release with beb pacage + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v2 + + - name: Update version number in the Cargo.toml + run: | + old_version_var=$(cat ./Cargo.toml | sed -rn 's/^version[\t ]*=[\t ]*\"([0-9]+\.[0-9]+\.[0-9]+)\"\s*$/\1/gp') + echo "old version: $old_version_var" + echo "new build: ${{ github.run_number }}" + sed -i -r "s/^(version[\t ]*=[\t ]*\"[0-9]+\.[0-9]+\.)[0-9]+\"\s*$/\1${{ github.run_number }}\"/g" ./Cargo.toml + new_version_var=$(cat ./Cargo.toml | sed -rn 's/^version[\t ]*=[\t ]*\"([0-9]+\.[0-9]+\.[0-9]+)\"\s*$/\1/gp') + echo "new version: $new_version_var" + echo "NEW_VERSION=$new_version_var" >> $GITHUB_ENV + echo "new version: ${{ env.NEW_VERSION }}" + + - name: Build release + run: cargo build --release + + - name: Commit new version ${{ env.NEW_VERSION }} + run: | + echo "on repositiry: ${{ github.repository }}" + git config --global user.name 'anton.lobanov' + git config --global user.email 'lobanov.anton@gmail.com' + git remote set-url origin https://a-givertzman:${{ secrets.RELEASES_TOKEN }}@github.com/${{ github.repository }} + git commit -am "Automated version update to: ${{ env.NEW_VERSION }}" + + - name: Push to protected branch "master" + uses: CasperWA/push-protected@v2 + with: + token: ${{ secrets.RELEASES_TOKEN }} + branch: master + unprotect_reviews: true + + - name: Zip artifact for deployment + run: zip ./target/release/release.zip -r ./target/release/* ./config.yaml ./extensions/* + + - name: Publish release + uses: ncipollo/release-action@v1 + with: + artifacts: "./target/release/release.zip" + tag: internal_v${{ env.NEW_VERSION }} + token: ${{ secrets.RELEASES_TOKEN }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 2ca4b423..a53d0553 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -16,14 +16,6 @@ jobs: build_and_test: name: Testing runs-on: ubuntu-latest - # strategy: - # matrix: - # toolchain: - # - stable - # - beta - # - nightly - - # Service containers to run with `container-job` services: # Label used to access the service container @@ -55,15 +47,25 @@ jobs: toolchain: stable override: true - # - name: Static analisis - # run: | - # cargo check - # # - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} - # # - run: cargo build --release + - name: Static analysis | Markdown lint tool + uses: docker://avtodev/markdown-lint:v1 + with: + config: '.github/workflows/markdownlint.yaml' + args: "**/*.md" + - name: Static analysis | cargo check + run: | + cargo check + - name: Static analysis | cargo clippy + run: | + cargo clippy + # cargo fmt --check + - name: Static analysis | cargo fmt + run: | + echo "escaped for now because of to much of errors, will be turned on later" + # cargo fmt --check - name: Unit tests run: cargo test -- --show-output - - name: Integration tests run: | From b3e4ce16aed7ea19c6f2ac384bbc19f5c93b947d Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Thu, 25 Apr 2024 12:37:51 +0300 Subject: [PATCH 22/24] Build deb | github action --- .github/workflows/{ => markdownlint}/markdownlint.yaml | 0 .github/workflows/test.yaml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{ => markdownlint}/markdownlint.yaml (100%) diff --git a/.github/workflows/markdownlint.yaml b/.github/workflows/markdownlint/markdownlint.yaml similarity index 100% rename from .github/workflows/markdownlint.yaml rename to .github/workflows/markdownlint/markdownlint.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index a53d0553..041b085d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -50,7 +50,7 @@ jobs: - name: Static analysis | Markdown lint tool uses: docker://avtodev/markdown-lint:v1 with: - config: '.github/workflows/markdownlint.yaml' + config: '.github/workflows/markdownlint/markdownlint.yaml' args: "**/*.md" - name: Static analysis | cargo check run: | From 77ff74bef0d4c168c41d5d9306dba8820e9a8634 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Thu, 25 Apr 2024 12:38:51 +0300 Subject: [PATCH 23/24] Build deb | github action --- .github/{workflows => }/markdownlint/markdownlint.yaml | 0 .github/workflows/test.yaml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename .github/{workflows => }/markdownlint/markdownlint.yaml (100%) diff --git a/.github/workflows/markdownlint/markdownlint.yaml b/.github/markdownlint/markdownlint.yaml similarity index 100% rename from .github/workflows/markdownlint/markdownlint.yaml rename to .github/markdownlint/markdownlint.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 041b085d..b2f1a439 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -50,7 +50,7 @@ jobs: - name: Static analysis | Markdown lint tool uses: docker://avtodev/markdown-lint:v1 with: - config: '.github/workflows/markdownlint/markdownlint.yaml' + config: '.github/markdownlint/markdownlint.yaml' args: "**/*.md" - name: Static analysis | cargo check run: | From 043a8c6d8c61194292d5bc5f701e0aba3249995c Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Thu, 25 Apr 2024 12:47:42 +0300 Subject: [PATCH 24/24] Build deb | github action --- .github/workflows/release_deb_package.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release_deb_package.yaml b/.github/workflows/release_deb_package.yaml index ac318a2f..81f97efd 100644 --- a/.github/workflows/release_deb_package.yaml +++ b/.github/workflows/release_deb_package.yaml @@ -1,7 +1,8 @@ name: Create release with beb pacage on: - workflow_dispatch: + pull_request: + # workflow_dispatch: env: TERM: dumb