diff options
author | Rina Fujino <rina.fujino@cumulocity.com> | 2021-02-09 12:45:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-09 12:45:31 +0100 |
commit | 813d7f9423f42fa959351117f44d95c9b2cef4ff (patch) | |
tree | 54aa515d8e2157d55b0a57cdee446a277dd203c6 /configuration/init | |
parent | ad9d4938cf3bec78695be3b7d3ab3ab39bcb8875 (diff) |
[CIT-182] Change the directory layout (#32)
[CIT-182] Change the directory layout and rename some directories
- The `mqtt_client` crate move to common directory
- The `tedge-mapper` crate rename to `c8y_mapper` and move to mapper/cumulocity directory
- The `c8y_json_translarot` crate rename to `c8y_translator_lib` and move to mapper/cumulocity directory
- The `script` directory rename to `ci`
Diffstat (limited to 'configuration/init')
-rw-r--r-- | configuration/init/README.md | 88 | ||||
-rw-r--r-- | configuration/init/initd/tedge | 130 | ||||
-rw-r--r-- | configuration/init/systemd/tedge-mapper.service | 18 | ||||
-rw-r--r-- | configuration/init/systemd/tedge.service | 34 |
4 files changed, 270 insertions, 0 deletions
diff --git a/configuration/init/README.md b/configuration/init/README.md new file mode 100644 index 00000000..45ab6ab3 --- /dev/null +++ b/configuration/init/README.md @@ -0,0 +1,88 @@ +# Sample init scripts templates + +There are a few samples provided, for systemd and for initd based systems. + +The files provide simple daemon configuration templates for init scripts along the start, stop, restart and status commands. + +## systemd Example + +This is provided in the thin-edge.service file which contains basic configuration of the binary startup behaviour. + +Most Linux distributions use systemd as a system and service manager, however in case of Yocto (as well as buildroot) and other custom distributions this may not be a case. + +The systemctl is the main command in systemd, used to control services. + +### Sample Service + +Create systemd service file in /etc/systemd/system/ (copy the template): + +```bash + sudo cp systemd/thin-edge.service /etc/systemd/system/thin-edge.service + sudo chmod 664 /etc/systemd/system/thin-edge.service +``` + +Once the service file is added, you need to reload systemd daemon: + +```bash + sudo systemctl daemon-reload +``` + +Now you should be able to start, stop, restart and check the service status + +```bash + sudo systemctl start thin-edge + sudo systemctl stop thin-edge + sudo systemctl restart thin-edge + systemctl status thin-edge +``` + +All daemons can be configured to start on boot: + +```bash + sudo systemctl enable thin-edge +``` + +Logs are available via: + +```bash + journalctl -u thin-edge +``` + +## initd Example + +### Getting started + +Copy thin-edge to /etc/init.d/thin-edge +```bash + cp /initd/tedge /etc/init.d/tedge +``` + +### Script usage + +Start the app. + +```bash + /etc/init.d/tedge start +``` + +Stop the app. + +```bash + /etc/init.d/tedge stop +``` + +Restart the app. + +```bash + /etc/init.d/tedge restart +``` + +Print current daemon status. + +```bash + /etc/init.d/tedge status +``` + +## Logging + +By default, standard output goes to `/var/log/tedge.log` and error output to `/var/log/tedge.err`. You can change where the logs will be written by changing `stdout_log` and `stderr_log`. diff --git a/configuration/init/initd/tedge b/configuration/init/initd/tedge new file mode 100644 index 00000000..30734c74 --- /dev/null +++ b/configuration/init/initd/tedge @@ -0,0 +1,130 @@ +#!/bin/sh + +### BEGIN INIT INFO +# Provides: +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Start thin-edge daeemon at boot time +# Description: Enable thin-edge services. +### END INIT INFO + +# Provide working directory of your service. +dir="/usr/bin" + +# Command to start the process, including arguments if required. +cmd="/usr/bin/tedge" + +# User to execute the command (if left empty will execute as root) +user="tedge" + +name=$(basename $0) +pid_file="/var/run/$name.pid" + +# Default location for logging assumed to be /var/log. +stdout_log="/var/log/$name.log" +stderr_log="/var/log/$name.err" + +get_pid() { + cat "$pid_file" +} + +is_running() { + [ -f "$pid_file" ] && ps -p "$(get_pid)" > /dev/null 2>&1 +} + +start() { + if is_running; then + echo "$name is already running" + else + echo "Starting $name" + cd "$dir" || return + if [ -z "$user" ]; then + sudo $cmd >> "$stdout_log" 2>> "$stderr_log" & + else + sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" & + fi + echo $! > "$pid_file" + if ! is_running; then + echo "Unable to start thin-edge, see $stdout_log and $stderr_log" + exit 1 + fi + fi +} + +stop() { + if is_running; then + echo -n "Stopping $name.." + kill "$(get_pid)" + for i in 1 2 3 4 5 6 7 8 9 10 + do + if ! is_running; then + break + fi + + echo -n "." + sleep 1 + done + echo + + if is_running; then + echo "Not stopped; may still be shutting down or shutdown may have failed" + exit 1 + else + echo "Stopped" + if [ -f "$pid_file" ]; then + rm "$pid_file" + fi + fi + else + echo "Not running" + fi +} + +restart() { + stop + if is_running; then + echo "Unable to stop, will not attempt to start" + exit 1 + fi + start +} + +status() { + if is_running; then + echo "Running" + else + echo "Stopped" + exit 1 + fi +} + + +# Add options for our script, by default we provide: start, stop, status, restart +case "$1" in + + # start subcommand, starts the script if not already started. + start) + start + ;; + + # stop the service, it will try to stop the service for up to 10 seconds or otherwise exit with code 1 + stop) + stop + ;; + + restart) + restart + ;; + + status) + status + ;; + + *) + echo "Usage: $0 {start|stop|restart|status}" + exit 1 +esac + +exit 0 diff --git a/configuration/init/systemd/tedge-mapper.service b/configuration/init/systemd/tedge-mapper.service new file mode 100644 index 00000000..5e299f99 --- /dev/null +++ b/configuration/init/systemd/tedge-mapper.service @@ -0,0 +1,18 @@ +# location: /etc/systemd/system/ +[Unit] +Description = tedge-mapper converts open json measurements to connectivity provider format. +After = syslog.target network.target + +[Service] +ExecStart = /usr/bin/tedge-mapper +KillSignal=SIGINT +KillMode=process + +User=tedge +ExecReload=kill -TERM `cat /var/run/tedge.pid` +Restart=on-failure +RestartPreventExitStatus=255 +PIDFile = /var/run/tedge-mapper.pid + +[Install] +WantedBy=multi-user.target diff --git a/configuration/init/systemd/tedge.service b/configuration/init/systemd/tedge.service new file mode 100644 index 00000000..a925d134 --- /dev/null +++ b/configuration/init/systemd/tedge.service @@ -0,0 +1,34 @@ +# location: /etc/systemd/system/ +[Unit] +Description = Template service to run thin-edge + +# We need netowrk and syslog to be up before we can start our daemon. +After = syslog.target network.target + +[Service] +# make sure the shell script is executable +ExecStart = /usr/bin/tedge +KillSignal=SIGINT +KillMode=process + +# Provide use unless execution by root required. +User=tedge + +# This is some quite nice feature of systemd but it will require us to implement signal handling in thin-edge binary +ExecReload=kill -TERM `cat /var/run/tedge.pid` + +# In case if it gets stopped, restart it immediately +Restart=on-failure +RestartPreventExitStatus=255 + +# With notify Type, service manager will be notified when the starting up has finished +Type = notify +# Since Type is notify, notify only service updates sent from the main process of the service +NotifyAccess=main + +# systemd gets to read the PID of daemon's main process see ExecStop and ExecReload +PIDFile = /var/run/tedge.pid + +[Install] +# multi-user.target corresponds to run level 3 roughly meaning wanted by system start +WantedBy=multi-user.target |