Skip to content

Commit

Permalink
Merge pull request #3007 from reubenmiller/feat-remote-access-socket-…
Browse files Browse the repository at this point in the history
…activated

feat: socket activated c8y-remote-access-plugin service
  • Loading branch information
reubenmiller authored Jul 26, 2024
2 parents c33692a + e16aba6 commit 4ea6317
Show file tree
Hide file tree
Showing 23 changed files with 301 additions and 6 deletions.
1 change: 1 addition & 0 deletions configuration/init/systemd/75-tedge.preset
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# Device management
enable c8y-firmware-plugin.service
enable c8y-remote-access-plugin.socket

# Agent
disable tedge-agent.service
Expand Down
13 changes: 13 additions & 0 deletions configuration/init/systemd/c8y-remote-access-plugin.socket
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=c8y-remote-access-plugin socket
PartOf=c8y-remote-access-plugin.service

[Socket]
ListenStream=/run/c8y-remote-access-plugin.sock
SocketMode=0660
SocketUser=tedge
SocketGroup=tedge
Accept=yes

[Install]
WantedBy=sockets.target
12 changes: 12 additions & 0 deletions configuration/init/systemd/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=c8y-remote-access-plugin Service
After=network.target c8y-remote-access-plugin.socket
Requires=c8y-remote-access-plugin.socket
CollectMode=inactive-or-failed

[Service]
ExecStart=/usr/bin/c8y-remote-access-plugin --child -
StandardInput=socket

[Install]
WantedBy=default.target
26 changes: 26 additions & 0 deletions configuration/package_manifests/nfpm.c8y-remote-access-plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ deb:
depends:
- tedge

contents:
# service definitions
- src: ./configuration/init/systemd/c8y-remote-access-plugin.socket
dst: /lib/systemd/system/c8y-remote-access-plugin.socket
file_info:
mode: 0644
packager: deb

- src: ./configuration/init/systemd/c8y-remote-access-plugin.socket
dst: /lib/systemd/system/c8y-remote-access-plugin.socket
file_info:
mode: 0644
packager: rpm

- src: ./configuration/init/systemd/[email protected]
dst: /lib/systemd/system/[email protected]
file_info:
mode: 0644
packager: deb

- src: ./configuration/init/systemd/[email protected]
dst: /lib/systemd/system/[email protected]
file_info:
mode: 0644
packager: rpm

overrides:
apk:
scripts:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/sh
set -e




### Create supported operation files
c8y-remote-access-plugin --init
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/sh
set -e

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh
set -e



### Remove supported operation files
c8y-remote-access-plugin --cleanup
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
#!/bin/sh
set -e

# Automatically added by thin-edge.io
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask c8y-remote-access-plugin.socket >/dev/null || true

# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled c8y-remote-access-plugin.socket; then
# Enables the unit on first installation, creates new
# symlinks on upgrades if the unit file has changed.
deb-systemd-helper enable c8y-remote-access-plugin.socket >/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 c8y-remote-access-plugin.socket >/dev/null || true
fi
fi
# End automatically added section
# Automatically added by thin-edge.io
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
deb-systemd-invoke start c8y-remote-access-plugin.socket >/dev/null || true
fi
fi
# End automatically added section

### Create supported operation files
c8y-remote-access-plugin --init
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
#!/bin/sh
set -e
# Automatically added by thin-edge.io
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by thin-edge.io
if [ "$1" = "remove" ]; then
if [ -x "/usr/bin/deb-systemd-helper" ]; then
deb-systemd-helper mask c8y-remote-access-plugin.socket >/dev/null || true
fi
fi

if [ "$1" = "purge" ]; then
if [ -x "/usr/bin/deb-systemd-helper" ]; then
deb-systemd-helper purge c8y-remote-access-plugin.socket >/dev/null || true
deb-systemd-helper unmask c8y-remote-access-plugin.socket >/dev/null || true
fi
fi
# End automatically added section
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/bin/sh
set -e

# Automatically added by thin-edge.io
if [ -d /run/systemd/system ] && [ "$1" = remove ]; then
deb-systemd-invoke stop c8y-remote-access-plugin.socket >/dev/null || true
fi
# End automatically added section

### Remove supported operation files
c8y-remote-access-plugin --cleanup
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
#!/bin/sh
set -e

# Automatically added by thin-edge.io
if [ $1 -eq 1 ] && [ -x "/usr/lib/systemd/systemd-update-helper" ]; then
# Initial installation
/usr/lib/systemd/systemd-update-helper install-system-units c8y-remote-access-plugin.socket || :
fi
# End automatically added section
# Automatically added by thin-edge.io
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
systemctl start c8y-remote-access-plugin.socket >/dev/null || true
fi
# End automatically added section

### Create supported operation files
c8y-remote-access-plugin --init
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
#!/bin/sh
set -e
# Automatically added by thin-edge.io
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/bin/sh
set -e

# Automatically added by thin-edge.io
if [ $1 -eq 0 ] && [ -x "/usr/lib/systemd/systemd-update-helper" ]; then
# Package removal, not upgrade
/usr/lib/systemd/systemd-update-helper remove-system-units c8y-remote-access-plugin.socket || :
fi
# End automatically added section

### Remove supported operation files
c8y-remote-access-plugin --cleanup
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh
set -e

#LINUXHELPER#

### Create supported operation files
c8y-remote-access-plugin --init
2 changes: 2 additions & 0 deletions configuration/package_scripts/c8y-remote-access-plugin/prerm
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh
set -e

#LINUXHELPER#

### Remove supported operation files
c8y-remote-access-plugin --cleanup
9 changes: 7 additions & 2 deletions configuration/package_scripts/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ def write_script(
output_file.write_text(contents, encoding="utf8")
return contents

def format_unit_name(name: str, default_suffix = ".service") -> str:
if "." not in name:
return name + default_suffix
return name


def process_package(name: str, manifest: dict, package_type: str, out_dir: Path):
services = [Service(**service) for service in manifest.get("services", [])]
Expand All @@ -113,7 +118,7 @@ def process_package(name: str, manifest: dict, package_type: str, out_dir: Path)
prerm = []
postrm = []

service_names = [(service.name or name) + ".service" for service in services]
service_names = [format_unit_name((service.name or name), ".service") for service in services]
log.info("Processing package: %s, type=%s", name, package_type)

variables = {
Expand All @@ -123,7 +128,7 @@ def process_package(name: str, manifest: dict, package_type: str, out_dir: Path)
service = None

for service in services:
service_name = (service.name or name) + ".service"
service_name = format_unit_name((service.name or name), ".service")
log.info(
"Processing service: package=%s, service=%s, type=%s",
name,
Expand Down
6 changes: 5 additions & 1 deletion configuration/package_scripts/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
],
"packages": {
"tedge": {},
"c8y-remote-access-plugin": {},
"c8y-remote-access-plugin": {
"services": [
{"name": "c8y-remote-access-plugin.socket", "enable": true, "start": true, "restart_after_upgrade": false, "stop_on_upgrade": false}
]
},
"tedge-agent": {
"services": [
// Don't stop or restart service when upgrading as the old agent
Expand Down
11 changes: 10 additions & 1 deletion plugins/c8y_remote_access_plugin/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use serde::Deserialize;
use std::io::stdin;
use std::io::BufRead;
use std::path::PathBuf;
use tedge_config::Path;
use tedge_config::TEdgeConfigLocation;
use tedge_config::DEFAULT_TEDGE_CONFIG_PATH;

use crate::csv::deserialize_csv_record;
use crate::UNIX_SOCKFILE;

#[derive(Parser, Deserialize, Debug, PartialEq, Eq)]
pub struct RemoteAccessConnect {
Expand Down Expand Up @@ -63,6 +65,7 @@ pub enum Command {
Init,
Cleanup,
SpawnChild(String),
TryConnectUnixSocket(String),
Connect(RemoteAccessConnect),
}

Expand All @@ -79,7 +82,13 @@ impl TryFrom<C8yRemoteAccessPluginOpt> for Command {
C8yRemoteAccessPluginOpt {
connect_string: Some(message),
..
} => Ok(Command::SpawnChild(message)),
} => {
if Path::new(UNIX_SOCKFILE).exists() {
Ok(Command::TryConnectUnixSocket(message))
} else {
Ok(Command::SpawnChild(message))
}
}
C8yRemoteAccessPluginOpt {
child: Some(message),
..
Expand Down
Loading

0 comments on commit 4ea6317

Please sign in to comment.