From eb841b59b41618394f4eb266b1017e0401eec631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Fri, 30 Apr 2021 10:35:09 -0400 Subject: [PATCH 1/4] devolutions-gateway: simplify linux package --- package/Linux/cron.daily/devolutions-gateway | 246 ------------------- package/Linux/gateway/debian/copyright | 2 +- package/Linux/gateway/debian/postinst | 8 - package/Linux/gateway/debian/postrm | 35 --- package/Linux/gateway/template/rules | 2 - 5 files changed, 1 insertion(+), 292 deletions(-) delete mode 100755 package/Linux/cron.daily/devolutions-gateway diff --git a/package/Linux/cron.daily/devolutions-gateway b/package/Linux/cron.daily/devolutions-gateway deleted file mode 100755 index b689c2c95..000000000 --- a/package/Linux/cron.daily/devolutions-gateway +++ /dev/null @@ -1,246 +0,0 @@ -#!/bin/sh - -# setup package repository for updates - -# System-wide package configuration. -DEFAULTS_FILE="/etc/default/devolutions-gateway" - -arch=`dpkg --print-architecture` -REPOCONFIG="deb [arch=$arch] https://devolutions.bintray.com/wayk-now stable main" -REPOCONFIGREGEX="deb (\[arch=[^]]*\bamd64\b[^]]*\][[:space:]]*) https?://devolutions.bintray.com/wayk-now stable main" - -APT_GET="`which apt-get 2> /dev/null`" -APT_CONFIG="`which apt-config 2> /dev/null`" - -SOURCES_PREAMBLE="### THIS FILE IS AUTOMATICALLY CONFIGURED ### -# You may comment out this entry, but any other modifications may be lost.\n" - -# Parse apt configuration and return requested variable value. -apt_config_val() { - APTVAR="$1" - if [ -x "$APT_CONFIG" ]; then - "$APT_CONFIG" dump | sed -e "/^$APTVAR /"'!d' -e "s/^$APTVAR \"\(.*\)\".*/\1/" - fi -} - -install_key() { - APT_KEY="`which apt-key 2> /dev/null`" - if [ ! -x "$APT_KEY" ]; then - return - fi - - NEED_KEYS=0 - "$APT_KEY" export 379CE192D401AB61 2>&1 | \ - grep -q -- "-----BEGIN PGP PUBLIC KEY BLOCK-----" - if [ $? -ne 0 ]; then - NEED_KEYS=1 - fi - - if [ $NEED_KEYS -eq 1 ]; then - "$APT_KEY" adv --keyserver hkp://keyserver.ubuntu.com:80 \ - --recv-keys 379CE192D401AB61 >/dev/null 2>&1 - - "$APT_KEY" add - >/dev/null 2>&1 </dev/null) - - # Check if the correct repository configuration is in there. - REPOMATCH=$(grep -E "^[[:space:]#]*\b$REPOCONFIGREGEX\b" "$SOURCELIST" \ - 2>/dev/null) - - # Check if the correct repository is disabled. - MATCH_DISABLED=$(echo "$REPOMATCH" | grep "^[[:space:]]*#" 2>/dev/null) - - # Now figure out if we need to fix things. - BADCONFIG=1 - if [ "$REPOMATCH" ]; then - # If it's there and active, that's ideal, so nothing to do. - if [ ! "$MATCH_DISABLED" ]; then - BADCONFIG=0 - else - # If it's not active, but neither is anything else, that's fine too. - if [ ! "$ACTIVECONFIGS" ]; then - BADCONFIG=0 - fi - fi - fi - - if [ $BADCONFIG -eq 0 ]; then - return 0 - fi - - # At this point, either the correct configuration is completely missing, or - # the wrong configuration is active. In that case, just abandon the mess and - # recreate the file with the correct configuration. If there were no active - # configurations before, create the new configuration disabled. - DISABLE="" - if [ ! "$ACTIVECONFIGS" ]; then - DISABLE="#" - fi - printf "$SOURCES_PREAMBLE" > "$SOURCELIST" - printf "$DISABLE$REPOCONFIG\n" >> "$SOURCELIST" - if [ $? -eq 0 ]; then - return 0 - fi - return 2 -} - -# Add the devolutions repository to the apt sources. -# Returns: -# 0 - sources list was created -# 2 - error -create_sources_lists() { - if [ ! "$REPOCONFIG" ]; then - return 0 - fi - - find_apt_sources - - SOURCELIST="$APT_SOURCESDIR/devolutions-gateway.list" - if [ -d "$APT_SOURCESDIR" ]; then - printf "$SOURCES_PREAMBLE" > "$SOURCELIST" - printf "$REPOCONFIG\n" >> "$SOURCELIST" - if [ $? -eq 0 ]; then - return 0 - fi - fi - return 2 -} - -# Remove our custom sources list file. -# Returns: -# 0 - successfully removed, or not configured -# !0 - failed to remove -clean_sources_lists() { - if [ ! "$REPOCONFIG" ]; then - return 0 - fi - - find_apt_sources - - rm -f "$APT_SOURCESDIR/devolutions-gateway.list" -} - -# Detect if the repo config was disabled by distro upgrade and enable if necessary. -handle_distro_upgrade() { - if [ ! "$REPOCONFIG" ]; then - return 0 - fi - - find_apt_sources - SOURCELIST="$APT_SOURCESDIR/devolutions-gateway.list" - if [ -r "$SOURCELIST" ]; then - REPOLINE=$(grep -E "^[[:space:]]*#[[:space:]]*$REPOCONFIGREGEX[[:space:]]*# disabled on upgrade to .*" "$SOURCELIST") - if [ $? -eq 0 ]; then - sed -i -e "s,^[[:space:]]*#[[:space:]]*\(.*\)[[:space:]]*# disabled on upgrade to .*,\1," \ - "$SOURCELIST" - LOGGER=$(which logger 2> /dev/null) - if [ "$LOGGER" ]; then - "$LOGGER" -t "$0" "Reverted repository modification: $REPOLINE." - fi - fi - fi -} - -## MAIN ## -DEFAULTS_FILE="/etc/default/devolutions-gateway" -if [ -r "$DEFAULTS_FILE" ]; then - . "$DEFAULTS_FILE" -fi - -install_key - -if [ "$repo_add_once" = "true" ]; then - create_sources_lists - RES=$? - # Sources creation succeeded, so stop trying. - if [ $RES -ne 2 ]; then - sed -i -e 's/[[:space:]]*repo_add_once=.*/repo_add_once="false"/' "$DEFAULTS_FILE" - fi -else - update_bad_sources -fi - -if [ "$repo_reenable_on_distupgrade" = "true" ]; then - handle_distro_upgrade -fi diff --git a/package/Linux/gateway/debian/copyright b/package/Linux/gateway/debian/copyright index a7e9cb595..a2133838d 100644 --- a/package/Linux/gateway/debian/copyright +++ b/package/Linux/gateway/debian/copyright @@ -3,6 +3,6 @@ Upstream-Name: devolutions-gateway Source: https://wayk.devolutions.net Files: * -Copyright: 2020 Devolutions Inc. +Copyright: 2020-2021 Devolutions Inc. License: non-free . diff --git a/package/Linux/gateway/debian/postinst b/package/Linux/gateway/debian/postinst index 6d736d3e8..cb5d4d983 100755 --- a/package/Linux/gateway/debian/postinst +++ b/package/Linux/gateway/debian/postinst @@ -3,14 +3,6 @@ # System-wide package configuration. DEFAULTS_FILE="/etc/default/devolutions-gateway" -#if [ ! -e "$DEFAULTS_FILE" ]; then - echo 'repo_add_once="true"' > "$DEFAULTS_FILE" - echo 'repo_reenable_on_distupgrade="true"' >> "$DEFAULTS_FILE" -#fi - -# Run the cron job immediately to perform repository configuration. -nohup sh /etc/cron.daily/devolutions-gateway > /dev/null 2>&1 & - if [ ! -d /etc/devolutions-gateway ]; then /bin/mkdir /etc/devolutions-gateway /bin/chmod 655 /etc/devolutions-gateway diff --git a/package/Linux/gateway/debian/postrm b/package/Linux/gateway/debian/postrm index c6127176d..b33efce21 100755 --- a/package/Linux/gateway/debian/postrm +++ b/package/Linux/gateway/debian/postrm @@ -12,41 +12,6 @@ fi # System-wide package configuration. DEFAULTS_FILE="/etc/default/devolutions-gateway" -arch=`dpkg --print-architecture` -REPOCONFIG="deb [arch=$arch] http://https://devolutions.bintray.com/wayk stable main" -REPOCONFIGREGEX="deb (\[arch=[^]]*\bamd64\b[^]]*\][[:space:]]*) https?://devolutions.bintray.com/wayk stable main" - -# Finding the APT tools -APT_GET="$(which apt-get 2> /dev/null)" -APT_CONFIG="$(which apt-config 2> /dev/null)" - -find_apt_sources() { - eval $("$APT_CONFIG" shell APT_SOURCESDIR 'Dir::Etc::sourceparts/d') -} - -# Remove our custom sources list file. -# Returns: -# 0 - successfully removed, or not configured -# !0 - failed to remove -clean_sources_lists() { - if [ ! "$REPOCONFIG" ]; then - return 0 - fi - - find_apt_sources - - SOURCELIST="$APT_SOURCESDIR/devolutions-gateway.list" - - if [ ! -r "$SOURCELIST" ]; then - return 0 - fi - - rm -f $SOURCELIST -} - if [ -s "$DEFAULTS_FILE" ]; then rm "$DEFAULTS_FILE" || exit 1 fi - -# Remove repository added by the package. -clean_sources_lists diff --git a/package/Linux/gateway/template/rules b/package/Linux/gateway/template/rules index d0cb5996e..8fa278478 100644 --- a/package/Linux/gateway/template/rules +++ b/package/Linux/gateway/template/rules @@ -8,5 +8,3 @@ override_dh_auto_test: override_dh_auto_install: override_dh_usrlocal: install -D -m 0755 {{ dgateway_executable }} $$(pwd)/debian/devolutions-gateway/usr/bin/devolutions-gateway - mkdir -p $$(pwd)/debian/devolutions-gateway/etc/cron.daily/ - cp {{ platform_dir }}/cron.daily/devolutions-gateway $$(pwd)/debian/devolutions-gateway/etc/cron.daily/devolutions-gateway From 45ab6d7f3baafd5d4d6155ab946912e4a7584286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Fri, 30 Apr 2021 14:29:05 -0400 Subject: [PATCH 2/4] devolutions-gateway: add service register/unregister commands --- devolutions-gateway/src/config.rs | 8 ++-- devolutions-gateway/src/main.rs | 72 +++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/devolutions-gateway/src/config.rs b/devolutions-gateway/src/config.rs index 84e9f0fcf..fe0a09f51 100644 --- a/devolutions-gateway/src/config.rs +++ b/devolutions-gateway/src/config.rs @@ -38,10 +38,10 @@ const ARG_SOGAR_USERNAME: &str = "sogar-username"; const ARG_SOGAR_PASSWORD: &str = "sogar-password"; const ARG_SOGAR_IMAGE_NAME: &str = "sogar-image-name"; -const SERVICE_NAME: &str = "devolutions-gateway"; -const DISPLAY_NAME: &str = "Devolutions Gateway"; -const DESCRIPTION: &str = "Devolutions Gateway service"; -const COMPANY_NAME: &str = "Devolutions"; +pub const SERVICE_NAME: &str = "devolutions-gateway"; +pub const DISPLAY_NAME: &str = "Devolutions Gateway"; +pub const DESCRIPTION: &str = "Devolutions Gateway service"; +pub const COMPANY_NAME: &str = "Devolutions"; cfg_if! { if #[cfg(target_os = "windows")] { diff --git a/devolutions-gateway/src/main.rs b/devolutions-gateway/src/main.rs index 1d8cf0c83..538a3050a 100644 --- a/devolutions-gateway/src/main.rs +++ b/devolutions-gateway/src/main.rs @@ -1,7 +1,7 @@ use devolutions_gateway::config::Config; use devolutions_gateway::service::GatewayService; - -use ceviche::controller::{dispatch, Controller}; +use clap::{crate_name, crate_version, App, SubCommand}; +use ceviche::controller::{*, dispatch, Controller}; use ceviche::{Service, ServiceEvent}; use slog_scope::info; use std::sync::mpsc; @@ -41,28 +41,62 @@ Service!("gateway", gateway_service_main); #[tokio::main] async fn main() -> Result<(), String> { - let config = Config::init(); + let args: Vec = std::env::args().collect(); + if (args.len() > 1) && (!args[1].starts_with("-")) { + let cli_app = App::new(crate_name!()) + .author("Devolutions Inc.") + .version(concat!(crate_version!(), "\n")) + .version_short("v") + .about("Devolutions Gateway") + .subcommand(SubCommand::with_name("service") + .subcommand(SubCommand::with_name("register")) + .subcommand(SubCommand::with_name("unregister")) + ); + + match cli_app.get_matches().subcommand() { + ("service", Some(matches)) => { + let service_name = devolutions_gateway::config::SERVICE_NAME; + let display_name = devolutions_gateway::config::DISPLAY_NAME; + let description = devolutions_gateway::config::DESCRIPTION; + let mut controller = Controller::new(service_name, display_name, description); + + match matches.subcommand() { + ("register", Some(_matches)) => { + controller.create().expect("failed to register service"); + }, + ("unregister", Some(_matches)) => { + controller.delete().expect("failed to unregister service"); + }, + _ => panic!("invalid service subcommand") + } + } + _ => panic!("invalid command") + } + } else { + let config = Config::init(); - if !config.service_mode { - let mut service = GatewayService::load().expect("error loading service"); + if !config.service_mode { + let mut service = GatewayService::load().expect("error loading service"); - service.start(); + service.start(); - // future waiting for some stop signals (CTRL-C…) - let _ = build_signals_fut().await?; + // future waiting for some stop signals (CTRL-C…) + let _ = build_signals_fut().await?; - service.stop(); - } else { - let mut controller = Controller::new( - config.service_name.as_str(), - config.display_name.as_str(), - config.description.as_str(), - ); - - controller - .register(service_main_wrapper) - .map_err(|err| format!("failed to register service - {}", err))?; + service.stop(); + } else { + let mut controller = Controller::new( + config.service_name.as_str(), + config.display_name.as_str(), + config.description.as_str(), + ); + + controller + .register(service_main_wrapper) + .map_err(|err| format!("failed to register service - {}", err))?; + } } + Ok(()) } From bd9c7ed3ebc1d5f7437f22875609c9f41a725e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Fri, 30 Apr 2021 16:14:42 -0400 Subject: [PATCH 3/4] devolutions-gateway: try registering linux service --- devolutions-gateway/src/main.rs | 19 +++++++++++++++++++ package/Linux/gateway/debian/postinst | 6 ++++++ package/Linux/gateway/debian/prerm | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/devolutions-gateway/src/main.rs b/devolutions-gateway/src/main.rs index 538a3050a..0d149462b 100644 --- a/devolutions-gateway/src/main.rs +++ b/devolutions-gateway/src/main.rs @@ -5,6 +5,7 @@ use ceviche::controller::{*, dispatch, Controller}; use ceviche::{Service, ServiceEvent}; use slog_scope::info; use std::sync::mpsc; +use cfg_if::cfg_if; enum GatewayServiceEvent {} @@ -60,6 +61,24 @@ async fn main() -> Result<(), String> { let description = devolutions_gateway::config::DESCRIPTION; let mut controller = Controller::new(service_name, display_name, description); + + cfg_if! { if #[cfg(target_os = "linux")] { + controller.config = Some(r#" + [Unit] + After= + After=network-online.target + + [Service] + ExecStart= + ExecStart=/usr/bin/devolutions-gateway --service + Restart=on-failure + + [Install] + WantedBy= + WantedBy=multi-user.target + "#.to_string()); + }} + match matches.subcommand() { ("register", Some(_matches)) => { controller.create().expect("failed to register service"); diff --git a/package/Linux/gateway/debian/postinst b/package/Linux/gateway/debian/postinst index cb5d4d983..71291467d 100755 --- a/package/Linux/gateway/debian/postinst +++ b/package/Linux/gateway/debian/postinst @@ -7,3 +7,9 @@ if [ ! -d /etc/devolutions-gateway ]; then /bin/mkdir /etc/devolutions-gateway /bin/chmod 655 /etc/devolutions-gateway fi + +if [ -d /run/systemd/system ]; then + /usr/bin/devolutions-gateway service register >/dev/null + systemctl restart devolutions-gateway >/dev/null 2>&1 + systemctl daemon-reload +fi diff --git a/package/Linux/gateway/debian/prerm b/package/Linux/gateway/debian/prerm index 084d4fd1f..3d477d262 100755 --- a/package/Linux/gateway/debian/prerm +++ b/package/Linux/gateway/debian/prerm @@ -12,3 +12,8 @@ fi if [ "$action" = "upgrade" ] ; then exit 0 fi + +if [ -d /run/systemd/system ]; then + systemctl stop devolutions-gateway >/dev/null 2>&1 + /usr/bin/devolutions-gateway service unregister >/dev/null +fi From b9ad979000d027cee5d4a9edd87bb45e36e72361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Fri, 30 Apr 2021 16:43:44 -0400 Subject: [PATCH 4/4] devolutions-gateway: fix formatting --- devolutions-gateway/src/main.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/devolutions-gateway/src/main.rs b/devolutions-gateway/src/main.rs index 0d149462b..33e3749ea 100644 --- a/devolutions-gateway/src/main.rs +++ b/devolutions-gateway/src/main.rs @@ -1,11 +1,11 @@ +use ceviche::controller::{dispatch, Controller, *}; +use ceviche::{Service, ServiceEvent}; +use cfg_if::cfg_if; +use clap::{crate_name, crate_version, App, SubCommand}; use devolutions_gateway::config::Config; use devolutions_gateway::service::GatewayService; -use clap::{crate_name, crate_version, App, SubCommand}; -use ceviche::controller::{*, dispatch, Controller}; -use ceviche::{Service, ServiceEvent}; use slog_scope::info; use std::sync::mpsc; -use cfg_if::cfg_if; enum GatewayServiceEvent {} @@ -49,9 +49,10 @@ async fn main() -> Result<(), String> { .version(concat!(crate_version!(), "\n")) .version_short("v") .about("Devolutions Gateway") - .subcommand(SubCommand::with_name("service") - .subcommand(SubCommand::with_name("register")) - .subcommand(SubCommand::with_name("unregister")) + .subcommand( + SubCommand::with_name("service") + .subcommand(SubCommand::with_name("register")) + .subcommand(SubCommand::with_name("unregister")), ); match cli_app.get_matches().subcommand() { @@ -61,7 +62,6 @@ async fn main() -> Result<(), String> { let description = devolutions_gateway::config::DESCRIPTION; let mut controller = Controller::new(service_name, display_name, description); - cfg_if! { if #[cfg(target_os = "linux")] { controller.config = Some(r#" [Unit] @@ -82,14 +82,14 @@ async fn main() -> Result<(), String> { match matches.subcommand() { ("register", Some(_matches)) => { controller.create().expect("failed to register service"); - }, + } ("unregister", Some(_matches)) => { controller.delete().expect("failed to unregister service"); - }, - _ => panic!("invalid service subcommand") + } + _ => panic!("invalid service subcommand"), } } - _ => panic!("invalid command") + _ => panic!("invalid command"), } } else { let config = Config::init();